Skip to content
Back to search

converting-pytorch-to-litert

SkillWorks with Claude CodeGitHub

Converts PyTorch models (e.g. ResNet, timm, HuggingFace transformers) directly to LiteRT (.tflite) flatbuffer format. Use when converting PyTorch models to TFLite, setting up export environments, or troubleshooting torch-to-litert conversion bugs. Don't use for ONNX exports or converting existing TensorFlow models.

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked yesterday

Check it deeper on your machine

It has changed since its scan on 17 Nov 2026. Its scripts were only partly scanned.

As a Claude Code plugin, install it and then ask Claude Code:

/plugin marketplace add keithmackay/mackayi/plugin install scanskillsafety@mackayiDo a deep scan of https://github.com/google-ai-edge/litert-torch with scanskillsafety

Or from a terminal, clone it and run it:

git clone https://github.com/keithmackay/scanskillsafety-skill ~/.claude/skills/scanskillsafetynode ~/.claude/skills/scanskillsafety/cli.cjs --deep 'https://github.com/google-ai-edge/litert-torch'

It checks the exact commit: every file, its packages against OSV (api.osv.dev), how it compares with this page's scan (findsafeskills.com receives the repository's address), and, if you agree, Claude reading every file with your own Claude subscription. It never runs the code.

Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/google-ai-edge--litert-torch/badge.svg)](/listings/google-ai-edge--litert-torch)

HTML

<a href="/listings/google-ai-edge--litert-torch"><img src="/api/listings/google-ai-edge--litert-torch/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

Requirements and Dependencies

* Python versions: `>=3.10` (**Python 3.11 is highly recommended.**) * Operating system: 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 Virtual Env

Set up a Python virtualenv (we strongly recommend Python 3.11):

python3.11 -m venv --prompt litert-torch venv
source venv/bin/activate

The latest stable release can be installed with (we include `torchvision` here to run the quickstart example below, and `ai-edge-litert` for CLI benchmarking tools):

pip install litert-torch torchvision ai-edge-litert

Alternately, the nightly version can be installed with:

pip install --pre litert-torch-nightly torchvision ai-edge-litert litert-cli-nightly

* The list of versioned releases can be seen [here](https://github.com/google-ai-edge/litert-torch/releases). * The full list of PyPi releases (including nightly builds) can be seen [here](https://pypi.org/project/litert-torch/#history).

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

SKILL.md · Manifest · 27 lines
  1. ---
  2. name: converting-pytorch-to-litert
  3. description: >-
  4. Converts PyTorch models (e.g. ResNet, timm, HuggingFace transformers) directly to
  5. LiteRT (.tflite) flatbuffer format. Use when converting PyTorch models to TFLite,
  6. setting up export environments, or troubleshooting torch-to-litert conversion bugs.
  7. Don't use for ONNX exports or converting existing TensorFlow models.
  8. ---
  9. # PyTorch to LiteRT Conversion Guide
  10. **ATTENTION ALL AGENTS:** If you are tasked with converting a PyTorch model (e.g., ResNet or similar architectures) to a `.tflite` flatbuffer format in this repository, **you must strictly follow these rules based on recent project findings:**
  11. ## 1. Direct Conversion Only (No ONNX)
  12. Bypass ONNX entirely. Do not attempt to export the model to ONNX or use `onnx2tf`. Convert directly from PyTorch to LiteRT.
  13. ## 2. Use `litert-torch`
  14. Use the `litert-torch` library for the direct PyTorch to TFLite conversion. Note that `ai-edge-torch` is deprecated and has been officially renamed to `litert-torch`.
  15. ## 3. Strict Python Environment Rules
  16. - **Do NOT use Python 3.14**. It currently has compatibility issues with typing in `torchao` (`AttributeError: 'typing.Union' object has no attribute '__module__'`).
  17. - **Use Python 3.11** (e.g., `python3.11`) as it is highly recommended for stability and avoids the aforementioned type hint bugs.
  18. ## 4. Dependencies
  19. Before running any conversion script, ensure the following are installed in your `python3.11` environment:
  20. ```bash
  21. python3.11 -m pip install torch transformers timm litert-torch ai-edge-litert litert-cli-nightly
README.md · README · 114 lines
  1. # LiteRT Torch
  2. LiteRT Torch is a python library that supports converting PyTorch models into a
  3. .tflite format, which can then be run with [LiteRT](https://ai.google.dev/edge/litert).
  4. This enables applications for Android, iOS and IOT that can run models
  5. completely on-device. LiteRT Torch offers broad CPU coverage, with initial GPU
  6. and NPU support. LiteRT Torch seeks to closely integrate with PyTorch,
  7. building on top of torch.export() and providing good coverage of Core ATen
  8. operators.
  9. To get started converting PyTorch models to LiteRT, see additional details in
  10. the [PyTorch converter](#pytorch-converter) section. For the particular case of
  11. Large Language Models (LLMs) and transformer-based models, the [Generative
  12. API](#generative-api) supports model authoring and quantization to enable
  13. improved on device performance.
  14. Although part of the same PyPi package, the PyTorch converter is a Beta release,
  15. while the Generative API is an Alpha release. Please see the [release
  16. notes](https://github.com/google-ai-edge/litert-torch/releases/) for additional
  17. information.
  18. ## Installation
  19. ### Requirements and Dependencies
  20. * Python versions: `>=3.10` (**Python 3.11 is highly recommended.**)
  21. * Operating system: Linux
  22. * PyTorch: [![torch](https://img.shields.io/badge/torch->=2.4.0-blue)](https://pypi.org/project/torch/)
  23. * TensorFlow: [![tf-nightly](https://img.shields.io/badge/tf--nightly-latest-blue)](https://pypi.org/project/tf-nightly/)
  24. <!-- requirement badges are updated by ci/update_nightly_versions.py -->
  25. ### Python Virtual Env
  26. Set up a Python virtualenv (we strongly recommend Python 3.11):
  27. ```bash
  28. python3.11 -m venv --prompt litert-torch venv
  29. source venv/bin/activate
  30. ```
  31. The latest stable release can be installed with (we include `torchvision` here to run the quickstart example below, and `ai-edge-litert` for CLI benchmarking tools):
  32. ```bash
  33. pip install litert-torch torchvision ai-edge-litert
  34. ```
  35. Alternately, the nightly version can be installed with:
  36. ```bash
  37. pip install --pre litert-torch-nightly torchvision ai-edge-litert litert-cli-nightly
  38. ```
  39. * The list of versioned releases can be seen [here](https://github.com/google-ai-edge/litert-torch/releases).
  40. * The full list of PyPi releases (including nightly builds) can be seen [here](https://pypi.org/project/litert-torch/#history).
  41. ## PyTorch Converter
  42. Here are the steps needed to convert a PyTorch model to a .tflite flatbuffer:
  43. ```python
  44. import torch
  45. import torchvision
  46. import litert_torch
  47. # Use resnet18 with pre-trained weights.
  48. resnet18 = torchvision.models.resnet18(torchvision.models.ResNet18_Weights.IMAGENET1K_V1)
  49. with torch.no_grad():
  50. sample_inputs = (torch.randn(1, 3, 224, 224),)
  51. # Convert and serialize PyTorch model to a .tflite flatbuffer. Note that we
  52. # are setting the model to evaluation mode prior to conversion.
  53. edge_model = litert_torch.convert(resnet18.eval(), sample_inputs)
  54. edge_model.export("resnet18.tflite")
  55. ```
  56. ### Next Steps: Running the Model
  57. Once exported, you can run this model on-device using the **LiteRT compiled model API** for native C++ or Java deployments, ensuring maximum performance across CPU, GPU, and NPU hardware accelerators.
  58. The [getting started](docs/pytorch_converter/getting_started.ipynb) Jupyter
  59. notebook gives an initial walkthrough of the conversion process and can be tried
  60. out with Google Colab.
  61. Additional technical details of the PyTorch Converter are [here](docs/pytorch_converter/README.md).
  62. ## Generative API
  63. The LiteRT Torch Generative API is a Torch native library for authoring
  64. mobile-optimized PyTorch Transformer models, which can be converted to LiteRT-LM models,
  65. allowing users to easily deploy Large Language Models (LLMs) on edge
  66. devices. Users can run the converted models via [LiteRT-LM](https://github.com/google-ai-edge/LiteRT-LM).
  67. *Tip: When working with the Generative API, you can package your converted `.tflite` model alongside a tokenizer into a deployment-ready `.litertlm` container using the `litert-lm-builder` CLI tool (installed via the nightly package).*
  68. More detailed documentation can be found [here](litert_torch/generative).
  69. The Generative API currently supports CPU and GPU, with planned support for NPU.
  70. A further future direction is to collaborate with the PyTorch community to
  71. ensure that frequently used transformer abstractions can be directly supported
  72. without reauthoring.
  73. ## Build Status
  74. Build Type | Status |
  75. ----------- | --------------|
  76. Generative API (Linux) | [![](https://github.com/google-ai-edge/litert-torch/actions/workflows/nightly_generative_api.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/litert-torch/actions/workflows/nightly_generative_api.yml) |
  77. Model Coverage (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) |
  78. Unit tests (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) |
  79. Nightly Release | [![](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) |
  80. # Contributing
  81. See our [contribution documentation](CONTRIBUTING.md).
  82. # Getting Help
  83. Please [create a GitHub issue](https://github.com/google-ai-edge/litert-torch/issues/new/choose) with any questions.

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.