Optimum 文件
推理
並獲得增強的文件體驗
開始使用
推理
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 上更新