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
... )