Optimum 文档

推理

您正在查看 main 版本,该版本需要从源代码安装。如果您想要常规 pip 安装,请查看最新的稳定版本 (v1.24.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 token-classification
OVModelForQuestionAnswering 问答
OVModelForAudioClassification 音频分类
OVModelForImageClassification 图像分类
OVModelForFeatureExtraction 特征提取
OVModelForMaskedLM 填充掩码
OVModelForImageClassification 图像分类
OVModelForAudioClassification 音频分类
OVModelForCausalLM 带历史记录的文本生成
OVModelForSeq2SeqLM 带历史记录的文本到文本生成
OVModelForSpeechSeq2Seq 自动语音识别
OVModelForVision2Seq 图像到文本

Diffusers 模型

确保您已安装 🤗 Diffusers。要安装 diffusers

pip install optimum[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 文本到图像
OVStableDiffusionImg2ImgPipeline 图像到图像
OVStableDiffusionInpaintPipeline 图像修复
OVStableDiffusionXLPipeline 文本到图像
OVStableDiffusionXLImg2ImgPipeline 图像到图像
OVLatentConsistencyModelPipeline 文本到图像

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

编译

默认情况下,模型将在实例化 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 精度运行。(有关 GPU 推理的驱动程序安装,请参阅 OpenVINO 文档)。

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)

对于处理图像的模型,您还可以指定重塑模型时的 heightwidth

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 上的模型编译。默认情况下,model_cache 目录在 Hugging Face Hub 缓存中的模型目录中创建。要覆盖此设置,请使用 ov_config 参数并将 CACHE_DIR 设置为不同的值。要在 GPU 上禁用模型缓存,请将 CACHE_DIR 设置为空字符串。

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

权重 量化

您还可以在加载模型时在 Linear、Convolutional 和 Embedding 层上应用 fp16、8 位或 4 位权重压缩,以减少内存占用和推理延迟。

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

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

也可以使用 OVQuantizer 对权重和激活都应用量化。

< > 在 GitHub 上更新