Optimum 文件
使用 Furiosa NPU 實現最佳推理
加入 Hugging Face 社群
並獲得增強的文件體驗
開始使用
使用 Furiosa NPU 實現最佳推理
Optimum Furiosa 是一個用於使用 Furiosa NPU 構建和執行推理的實用程式包。Optimum 可用於從 Hugging Face Hub 載入最佳化模型,並建立管道以執行加速推理,而無需重寫您的 API。
從 Transformers 切換到 Optimum Furiosa
optimum.furiosa.FuriosaAIModelForXXX
模型類與 Hugging Face 模型 API 相容。這意味著您只需將 AutoModelForXXX
類替換為 optimum.furiosa
中相應的 FuriosaAIModelForXXX
類即可。
您無需調整程式碼即可使其與 FuriosaAIModelForXXX
類配合使用
由於您想要使用的模型可能尚未轉換為 ONNX 格式,因此 FuriosaAIModel
包含一種將普通的 Hugging Face 模型轉換為 ONNX 模型的方法。只需將 export=True
傳遞給 from_pretrained
方法,您的模型將在執行時載入並轉換為 ONNX 格式
載入和推理普通的 Transformers 模型
import requests
from PIL import Image
- from transformers import AutoModelForImageClassification
+ from optimum.furiosa import FuriosaAIModelForImageClassification
from transformers import AutoFeatureExtractor, pipeline
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
model_id = "microsoft/resnet-50"
- model = AutoModelForImageClassification.from_pretrained(model_id)
+ model = FuriosaAIModelForImageClassification.from_pretrained(model_id, export=True, input_shape_dict={"pixel_values": [1, 3, 224, 224]}, output_shape_dict={"logits": [1, 1000]},)
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
cls_pipe = pipeline("image-classification", model=model, feature_extractor=feature_extractor)
outputs = cls_pipe(image)
將編譯後的模型推送到 Hugging Face Hub
與普通的 PreTrainedModel 一樣,也可以將您的 FurisoaAIModelForXXX
推送到 Hugging Face 模型中心
>>> from optimum.furiosa import FuriosaAIModelForImageClassification
>>> # Load the model from the hub
>>> model = FuriosaAIModelForImageClassification.from_pretrained(
... "microsoft/resnet-50", export=True, input_shape_dict={"pixel_values": [1, 3, 224, 224]}, output_shape_dict={"logits": [1, 1000]},
... )
>>> # Save the converted model
>>> model.save_pretrained("a_local_path_for_compiled_model")
# Push the compiled model to HF Hub
>>> model.push_to_hub(
... "a_local_path_for_compiled_model", repository_id="my-furiosa-repo", use_auth_token=True
... )