Optimum 文档

推理

您正在查看的是需要从源码安装。如果您想通过 pip 进行常规安装,请查看最新稳定版本(v1.27.0)。
Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

推理

Optimum Intel 可以用于从 Hub 加载优化模型并创建管道,以便在各种 Intel 处理器上使用 OpenVINO Runtime 运行推理(查看支持设备的完整列表)

加载

Transformers 模型

模型导出后,您可以将 `AutoModelForXxx` 类替换为相应的 `OVModelForXxx` 来加载它。

- from transformers import AutoModelForCausalLM
+ from optimum.intel import OVModelForCausalLM
  from transformers import AutoTokenizer, pipeline

  model_id = "helenai/gpt2-ov"
- model = AutoModelForCausalLM.from_pretrained(model_id)
  # here the model was already exported so no need to set export=True
+ model = OVModelForCausalLM.from_pretrained(model_id)
  tokenizer = AutoTokenizer.from_pretrained(model_id)
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
  results = pipe("He's a dreadful magician and")

如下表所示,每个任务都与一个能够自动加载模型的类相关联。

自动类 任务
OVModelForSequenceClassification 文本分类
OVModelForTokenClassification 词元分类
OVModelForQuestionAnswering 问题回答
OVModelForAudioClassification 音频分类
OVModelForImageClassification image-classification
OVModelForFeatureExtraction feature-extraction
OVModelForMaskedLM fill-mask
OVModelForImageClassification image-classification
OVModelForAudioClassification 音频分类
OVModelForCausalLM text-generation-with-past
OVModelForSeq2SeqLM text2text-generation-with-past
OVModelForSpeechSeq2Seq 自动语音识别
OVModelForVision2Seq image-to-text
OVModelForTextToSpeechSeq2Seq text-to-audio

Diffusers 模型

请确保您已安装 🤗 Diffusers。要安装 `diffusers`:

pip install diffusers
- from diffusers import StableDiffusionPipeline
+ from optimum.intel import OVStableDiffusionPipeline

  model_id = "echarlaix/stable-diffusion-v1-5-openvino"
- pipeline = StableDiffusionPipeline.from_pretrained(model_id)
+ pipeline = OVStableDiffusionPipeline.from_pretrained(model_id)
  prompt = "sailing ship in storm by Rembrandt"
  images = pipeline(prompt).images

如下表所示,每个任务都与一个能够自动加载模型的类相关联。

自动类 任务
OVStableDiffusionPipeline text-to-image
OVStableDiffusionImg2ImgPipeline image-to-image
OVStableDiffusionInpaintPipeline 图像修复
OVStableDiffusionXLPipeline text-to-image
OVStableDiffusionXLImg2ImgPipeline image-to-image
OVLatentConsistencyModelPipeline text-to-image
OVLTXPipeline text-to-video
OVPipelineForText2Video text-to-video

有关参数的更多信息和不同任务的示例,请参阅参考文档

编译

默认情况下,模型在实例化 `OVModel` 时进行编译。当模型被重新塑形或放置到另一个设备时,模型将需要重新编译,这将在第一次推理之前默认发生(从而增加第一次推理的延迟)。为了避免不必要的编译,您可以通过设置 `compile=False` 来禁用第一次编译。

from optimum.intel import OVModelForQuestionAnswering

model_id = "distilbert/distilbert-base-cased-distilled-squad"
# Load the model and disable the model compilation
model = OVModelForQuestionAnswering.from_pretrained(model_id, compile=False)

要在 Intel 集成或独立 GPU 上运行推理,请使用 `to("gpu")`。在 GPU 上,模型默认以 FP16 精度运行。(请参阅 OpenVINO 文档,了解有关安装 GPU 推理驱动程序的更多信息)。

model.to("gpu")

模型可以被编译

model.compile()

静态形状

默认情况下,支持动态形状,允许对各种形状的输入进行推理。为了加快推理速度,可以通过使用 .reshape() 提供所需的输入形状来启用静态形状。

# Fix the batch size to 1 and the sequence length to 40
batch_size, seq_len = 1, 40
model.reshape(batch_size, seq_len)

使用 `reshape()` 方法固定形状后,无法使用不同形状的输入进行推理。


from transformers import AutoTokenizer
from optimum.intel import OVModelForQuestionAnswering

model_id = "distilbert/distilbert-base-cased-distilled-squad"
model = OVModelForQuestionAnswering.from_pretrained(model_id, compile=False)
tokenizer = AutoTokenizer.from_pretrained(model_id)
batch_size, seq_len = 1, 40
model.reshape(batch_size, seq_len)
# Compile the model before the first inference
model.compile()

question = "Which name is also used to describe the Amazon rainforest ?"
context = "The Amazon rainforest, also known as Amazonia or the Amazon Jungle"
tokens = tokenizer(question, context, max_length=seq_len, padding="max_length", return_tensors="np")

outputs = model(**tokens)

对于处理图像的模型,您还可以在重塑模型时指定 `height` 和 `width`

batch_size, num_images, height, width = 1, 1, 512, 512
pipeline.reshape(batch_size=batch_size, height=height, width=width, num_images_per_prompt=num_images)
images = pipeline(prompt, height=height, width=width, num_images_per_prompt=num_images).images

配置

`ov_config` 参数允许提供自定义 OpenVINO 配置值。例如,这可用于在默认使用 FP16 或 BF16 推理精度的设备上启用全精度推理。

ov_config = {"INFERENCE_PRECISION_HINT": "f32"}
model = OVModelForSequenceClassification.from_pretrained(model_id, ov_config=ov_config)

Optimum Intel 利用 OpenVINO 的模型缓存来加速 GPU 上的模型编译。默认情况下,将在 Hugging Face Hub 缓存中模型的目录中创建一个 `model_cache` 目录。要覆盖此设置,请使用 ov_config 参数并将 `CACHE_DIR` 设置为不同的值。要在 GPU 上禁用模型缓存,请将 `CACHE_DIR` 设置为空字符串。

ov_config = {"CACHE_DIR": ""}
model = OVModelForSequenceClassification.from_pretrained(model_id, device="gpu", ov_config=ov_config)

权重量化

您还可以在加载模型时对线性、卷积和嵌入层应用 fp16、8 位或 4 位权重压缩,以减少内存占用和推理延迟。

有关量化参数的更多信息,请查看文档

如果未指定,当参数超过 10 亿的模型导出为 OpenVINO 格式(`export=True`)时,`load_in_8bit` 将默认设置为 `True`。您可以通过设置 `load_in_8bit=False` 来禁用它。

也可以使用 `OVQuantizer` 对权重和激活进行量化。

< > 在 GitHub 上更新