Optimum 文档
运行推理
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
运行推理
本节介绍如何在 Intel Gaudi 加速器上运行仅推理工作负载。
一个有效的快速入门方法是查看 Optimum for Intel Gaudi [此处] 提供的推理示例。
您还可以浏览 Optimum for Intel Gaudi 存储库中的示例。虽然 examples 文件夹包含训练和推理示例,但特定于推理的内容为在 Intel Gaudi 加速器上优化和运行工作负载提供了有价值的指导。
有关如何加速推理的更多高级信息,请查看本指南。
使用 GaudiTrainer
您可以在下面找到一个模板,用于使用 GaudiTrainer
实例执行推理,我们希望计算给定数据集的准确率
import evaluate
metric = evaluate.load("accuracy")
# You can define your custom compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a
# predictions and label_ids field) and has to return a dictionary string to float.
def my_compute_metrics(p):
return metric.compute(predictions=np.argmax(p.predictions, axis=1), references=p.label_ids)
# Trainer initialization
trainer = GaudiTrainer(
model=my_model,
gaudi_config=my_gaudi_config,
args=my_args,
train_dataset=None,
eval_dataset=eval_dataset,
compute_metrics=my_compute_metrics,
tokenizer=my_tokenizer,
data_collator=my_data_collator,
)
# Run inference
metrics = trainer.evaluate()
变量 my_args
应包含一些特定于推理的参数,您可以在此处查看可以为推理设置的有趣参数。
在我们的示例中
我们所有的示例都包含有关在给定数据集上使用给定模型运行推理的说明。每个示例的推理都是相同的:使用 --do_eval
和 --per_device_eval_batch_size
运行示例脚本,而不使用 --do_train
。一个简单的模板如下
python path_to_the_example_script \ --model_name_or_path my_model_name \ --gaudi_config_name my_gaudi_config_name \ --dataset_name my_dataset_name \ --do_eval \ --per_device_eval_batch_size my_batch_size \ --output_dir path_to_my_output_dir \ --use_habana \ --use_lazy_mode \ --use_hpu_graphs_for_inference