Transformers 文档
LiteRT
加入 Hugging Face 社区
并获取增强的文档体验
开始使用
LiteRT
LiteRT(之前称为 TensorFlow Lite)是一个为设备上机器学习设计的高性能运行时。
Optimum 库将模型导出到 LiteRT,用于多种架构。
导出到 LiteRT 的好处包括以下几点。
- 低延迟、注重隐私、无需互联网连接,并减小模型尺寸和功耗,适用于设备上机器学习。
- 广泛的平台、模型框架和语言支持。
- 针对 GPU 和 Apple Silicon 的硬件加速。
使用 Optimum CLI 将 Transformers 模型导出到 LiteRT。
运行以下命令安装 Optimum 和 LiteRT 的 exporters 模块。
pip install optimum[exporters-tf]
有关所有可用参数,请参阅使用 optimum.exporters.tflite 将模型导出到 TFLite 指南,或使用以下命令。
optimum-cli export tflite --help
设置 --model
参数以从 Hub 导出模型。
optimum-cli export tflite --model google-bert/bert-base-uncased --sequence_length 128 bert_tflite/
您应该看到日志指示进度,并显示生成的 model.tflite
的保存位置。
Validating TFLite model...
-[✓] TFLite model output names match reference model (logits)
- Validating TFLite Model output "logits":
-[✓] (1, 128, 30522) matches (1, 128, 30522)
-[x] values not close enough, max diff: 5.817413330078125e-05 (atol: 1e-05)
The TensorFlow Lite export succeeded with the warning: The maximum absolute difference between the output of the reference model and the TFLite exported model is not within the set tolerance 1e-05:
- logits: max diff = 5.817413330078125e-05.
The exported model was saved at: bert_tflite
对于本地模型,请确保模型权重和 tokenizer 文件保存在同一目录中,例如 local_path
。将目录传递给 --model
参数,并使用 --task
指示模型可以执行的任务。如果未提供 --task
,则使用没有特定于任务的头的模型架构。
optimum-cli export tflite --model local_path --task question-answering google-bert/bert-base-uncased --sequence_length 128 bert_tflite/