Optimum 文件

快速入門

您正在檢視的是需要從原始碼安裝。如果您想使用常規的 pip install,請檢視最新的穩定版本 (v1.27.0)。
Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

快速入門

本快速入門指南旨在幫助那些準備好深入程式碼並檢視如何將 🤗 Optimum 整合到其模型訓練和推理工作流程中的開發者。

加速推理

OpenVINO

要載入模型並使用 OpenVINO Runtime 執行推理,您只需將 AutoModelForXxx 類替換為相應的 OVModelForXxx 類。如果您想載入 PyTorch 檢查點,請設定 export=True 將您的模型轉換為 OpenVINO IR (中間表示)。

- from transformers import AutoModelForSequenceClassification
+ from optimum.intel.openvino import OVModelForSequenceClassification
  from transformers import AutoTokenizer, pipeline

  # Download a tokenizer and model from the Hub and convert to OpenVINO format
  tokenizer = AutoTokenizer.from_pretrained(model_id)
  model_id = "distilbert-base-uncased-finetuned-sst-2-english"
- model = AutoModelForSequenceClassification.from_pretrained(model_id)
+ model = OVModelForSequenceClassification.from_pretrained(model_id, export=True)

  # Run inference!
  classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
  results = classifier("He's a dreadful magician.")

您可以在文件示例中找到更多例子。

ONNX Runtime

為了使用 ONNX Runtime 加速推理,🤗 Optimum 使用配置物件來定義圖最佳化和量化的引數。然後,這些物件被用於例項化專用的最佳化器量化器

在應用量化或最佳化之前,我們首先需要載入我們的模型。要載入模型並使用 ONNX Runtime 執行推理,您只需將標準的 Transformers AutoModelForXxx 類替換為相應的 ORTModelForXxx 類。如果您想從 PyTorch 檢查點載入,請設定 export=True 將您的模型匯出為 ONNX 格式。

>>> from optimum.onnxruntime import ORTModelForSequenceClassification
>>> from transformers import AutoTokenizer

>>> model_checkpoint = "distilbert-base-uncased-finetuned-sst-2-english"
>>> save_directory = "tmp/onnx/"

>>> # Load a model from transformers and export it to ONNX
>>> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
>>> ort_model = ORTModelForSequenceClassification.from_pretrained(model_checkpoint, export=True)

>>> # Save the ONNX model and tokenizer
>>> ort_model.save_pretrained(save_directory)
>>> tokenizer.save_pretrained(save_directory)

現在讓我們看看如何使用 ONNX Runtime 應用動態量化

>>> from optimum.onnxruntime.configuration import AutoQuantizationConfig
>>> from optimum.onnxruntime import ORTQuantizer

>>> # Define the quantization methodology
>>> qconfig = AutoQuantizationConfig.arm64(is_static=False, per_channel=False)
>>> quantizer = ORTQuantizer.from_pretrained(ort_model)

>>> # Apply dynamic quantization on the model
>>> quantizer.quantize(save_dir=save_directory, quantization_config=qconfig)

在此示例中,我們量化了來自 Hugging Face Hub 的一個模型,同樣地,我們也可以透過提供包含模型權重的目錄路徑來量化本地託管的模型。應用 quantize() 方法的結果是一個 model_quantized.onnx 檔案,可用於執行推理。以下是如何載入 ONNX Runtime 模型並用它生成預測的示例

>>> from optimum.onnxruntime import ORTModelForSequenceClassification
>>> from transformers import pipeline, AutoTokenizer

>>> model = ORTModelForSequenceClassification.from_pretrained(save_directory, file_name="model_quantized.onnx")
>>> tokenizer = AutoTokenizer.from_pretrained(save_directory)
>>> classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
>>> results = classifier("I love burritos!")

您可以在文件示例中找到更多例子。

加速訓練

Habana

為了在 Habana 的 Gaudi 處理器上訓練 transformers,🤗 Optimum 提供了一個 GaudiTrainer,它與 🤗 Transformers 的 Trainer 非常相似。這裡是一個簡單的例子

- from transformers import Trainer, TrainingArguments
+ from optimum.habana import GaudiTrainer, GaudiTrainingArguments

  # Download a pretrained model from the Hub
  model = AutoModelForXxx.from_pretrained("bert-base-uncased")

  # Define the training arguments
- training_args = TrainingArguments(
+ training_args = GaudiTrainingArguments(
      output_dir="path/to/save/folder/",
+     use_habana=True,
+     use_lazy_mode=True,
+     gaudi_config_name="Habana/bert-base-uncased",
      ...
  )

  # Initialize the trainer
- trainer = Trainer(
+ trainer = GaudiTrainer(
      model=model,
      args=training_args,
      train_dataset=train_dataset,
      ...
  )

  # Use Habana Gaudi processor for training!
  trainer.train()

您可以在文件示例中找到更多例子。

ONNX Runtime

要使用 ONNX Runtime 的加速功能訓練 transformers,🤗 Optimum 提供了一個 ORTTrainer,它與 🤗 Transformers 的 Trainer 非常相似。這裡是一個簡單的例子

- from transformers import Trainer, TrainingArguments
+ from optimum.onnxruntime import ORTTrainer, ORTTrainingArguments

  # Download a pretrained model from the Hub
  model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")

  # Define the training arguments
- training_args = TrainingArguments(
+ training_args = ORTTrainingArguments(
      output_dir="path/to/save/folder/",
      optim="adamw_ort_fused",
      ...
  )

  # Create a ONNX Runtime Trainer
- trainer = Trainer(
+ trainer = ORTTrainer(
      model=model,
      args=training_args,
      train_dataset=train_dataset,
+     feature="text-classification", # The model type to export to ONNX
      ...
  )

  # Use ONNX Runtime for training!
  trainer.train()

您可以在文件示例中找到更多例子。

開箱即用的 ONNX 匯出

Optimum 庫為 Transformers 和 Diffusers 模型提供了開箱即用的 ONNX 匯出功能!

將模型匯出到 ONNX 非常簡單

optimum-cli export onnx --model gpt2 gpt2_onnx/

檢視幫助以獲取更多選項

optimum-cli export onnx --help

請檢視文件瞭解更多資訊。

torch.fx 整合

Optimum 與 torch.fx 整合,提供了一行程式碼即可實現的多種圖變換。我們的目標是透過 torch.fx 更好地支援量化,包括量化感知訓練 (QAT) 和訓練後量化 (PTQ)。

請檢視文件參考瞭解更多!

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.