google-ai-edge/litert-torch

GitHub: google-ai-edge/litert-torch

LiteRT Torch 是一个将 PyTorch 模型转换为 .tflite 格式的 Python 库,使模型能够在移动和 IoT 设备上端侧运行。

Stars: 1070 | Forks: 173

# LiteRT Torch LiteRT Torch 是一个 Python 库,支持将 PyTorch 模型转换为 .tflite 格式,随后可以使用 [LiteRT](https://ai.google.dev/edge/litert) 运行。 这使得 Android、iOS 和 IoT 应用程序能够完全在设备端运行模型。 LiteRT Torch 提供了广泛的 CPU 覆盖,并初步支持 GPU 和 NPU。 LiteRT Torch 致力于与 PyTorch 紧密集成,基于 torch.export() 构建, 并对 Core ATen 算子提供良好的覆盖率。 要开始将 PyTorch 模型转换为 LiteRT,请参阅 [PyTorch 转换器](#pytorch-converter) 部分了解更多详情。针对 大型语言模型(LLM)和基于 Transformer 的模型的具体情况,[Generative API](#generative-api) 支持模型编写和量化,从而实现更好的设备端性能。 尽管属于同一个 PyPi 包,但 PyTorch 转换器是 Beta 版本, 而 Generative API 是 Alpha 版本。请参阅[发布 说明](https://github.com/google-ai-edge/litert-torch/releases/) 获取更多 信息。 ## 安装说明 ### 要求和依赖 * Python 版本:`>=3.10` 且 `<3.14` (**强烈推荐使用 Python 3.11。** *注意:Python 3.14 目前在 `torchao` 的类型标注方面存在兼容性问题。*) * 操作系统:Linux * PyTorch:[![torch](https://img.shields.io/badge/torch->=2.4.0-blue)](https://pypi.org/project/torch/) * TensorFlow:[![tf-nightly](https://img.shields.io/badge/tf--nightly-latest-blue)](https://pypi.org/project/tf-nightly/) ### Python 虚拟环境 设置一个 Python virtualenv(我们强烈推荐使用 Python 3.11): ``` python3.11 -m venv --prompt litert-torch venv source venv/bin/activate ``` 可以通过以下方式安装最新的稳定版本(我们在此包含了 `torchvision` 以运行下方的快速入门示例,以及用于 CLI 性能基准测试工具的 `ai-edge-litert`): ``` pip install litert-torch torchvision ai-edge-litert ``` 或者,可以通过以下方式安装每日构建(nightly)版本: ``` pip install --pre litert-torch-nightly torchvision ai-edge-litert litert-cli-nightly ``` * 版本化发布的列表可以在[此处](https://github.com/google-ai-edge/litert-torch/releases)查看。 * 完整的 PyPi 发布列表(包括每日构建)可以在[此处](https://pypi.org/project/litert-torch/#history)查看。 ## PyTorch 转换器 以下是将 PyTorch 模型转换为 .tflite flatbuffer 所需的步骤: ``` import torch import torchvision import litert_torch # 使用带有预训练权重的 resnet18。 resnet18 = torchvision.models.resnet18(torchvision.models.ResNet18_Weights.IMAGENET1K_V1) with torch.no_grad(): sample_inputs = (torch.randn(1, 3, 224, 224),) # Convert and serialize PyTorch model to a .tflite flatbuffer. Note that we # are setting the model to evaluation mode prior to conversion. edge_model = litert_torch.convert(resnet18.eval(), sample_inputs) edge_model.export("resnet18.tflite") ``` ### 下一步:运行模型 导出后,您可以使用 **LiteRT 编译模型 API** 在设备端运行此模型,以进行原生的 C++ 或 Java 部署,从而确保在 CPU、GPU 和 NPU 硬件加速器上实现最佳性能。 [入门](docs/pytorch_converter/getting_started.ipynb) Jupyter notebook 提供了对转换过程的初步演练,并且可以在 Google Colab 中进行试用。 有关 PyTorch 转换器的更多技术细节请参见[此处](docs/pytorch_converter/README.md)。 ## Generative API LiteRT Torch Generative API 是一个原生的 Torch 库,用于编写 经过移动端优化的 PyTorch Transformer 模型,这些模型可以转换为 LiteRT-LM 模型, 从而允许用户在边缘设备上轻松部署大型语言模型(LLM)。用户可以通过 [LiteRT-LM](https://github.com/google-ai-edge/LiteRT-LM) 运行转换后的模型。 *提示:在使用 Generative API 时,您可以使用 `litert-lm-builder` CLI 工具(通过每日构建包安装)将转换后的 `.tflite` 模型与 tokenizer 打包成可随时部署的 `.litertlm` 容器。* 更详细的文档可以在[此处](litert_torch/generative)找到。 Generative API 目前支持 CPU 和 GPU,并计划支持 NPU。 未来的一个方向是与 PyTorch 社区合作, 确保常用的 Transformer 抽象能够无需重新编写代码即可得到直接支持。 ## 构建状态 构建类型 | 状态 | ----------- | --------------| Generative API (Linux) | [![](https://static.pigsec.cn/wp-content/uploads/repos/cas/08/0848432c3cc190b2f19a7aa03b523504c32f463d65943442f65d27526e486fa6.svg)](https://github.com/google-ai-edge/litert-torch/actions/workflows/nightly_generative_api.yml) | 模型覆盖率 (Linux) | [![](https://github.com/google-ai-edge/litert-torch/actions/workflows/nightly_model_coverage.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/litert-torch/actions/workflows/nightly_model_coverage.yml) | 单元测试 (Linux) | [![](https://github.com/google-ai-edge/litert-torch/actions/workflows/nightly_unittests.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/litert-torch/actions/workflows/nightly_unittests.yml) | 每日构建发布 | [![](https://github.com/google-ai-edge/litert-torch/actions/workflows/nightly_release.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/litert-torch/actions/workflows/nightly_release.yml) | # 贡献 请参阅我们的[贡献文档](CONTRIBUTING.md)。 # 获取帮助 如有任何问题,请[创建 GitHub issue](https://github.com/google-ai-edge/litert-torch/issues/new/choose)。
标签:LiteRT, PyTorch, 人工智能, 凭据扫描, 模型转换, 用户模式Hook绕过, 移动开发, 端侧计算, 边缘计算, 逆向工具