Transformers 文档

导出为TFLite

导出为TFLite

TensorFlow Lite 是一个轻量级框架,用于在资源受限的设备上部署机器学习模型,例如手机、嵌入式系统和物联网(IoT)设备。TFLite 旨在优化并高效地运行这些设备上的模型,这些设备的计算能力、内存和功耗都有限。TensorFlow Lite 模型以一种特殊的高效便携格式表示,由 .tflite 文件扩展名标识。

🤗 Optimum 提供了通过 exporters.tflite 模块将 🤗 Transformers 模型导出为 TFLite 的功能。 有关支持的模型架构列表,请参阅 🤗 Optimum 文档

要将模型导出为TFLite,请安装所需的依赖项:

pip install optimum[exporters-tf]

要查看所有可用的参数,请参考🤗 Optimum 文档,或在命令行中查看帮助:

optimum-cli export tflite --help

要从🤗 Hub导出模型的检查点,例如google-bert/bert-base-uncased,请运行以下命令:

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

上面的例子展示了如何从🤗 Hub导出检查点。当导出本地模型时,首先确保你将模型的权重和分词器文件保存在同一个目录中(local_path)。使用CLI时,将local_path传递给model参数,而不是🤗 Hub上的检查点名称。

< > Update on GitHub