Skip to content

Guide: Exporting Keras models to LiteRT with PyTorch backend#2373

Merged
hertschuh merged 9 commits into
keras-team:masterfrom
pctablet505:litert-export-guide
Jul 21, 2026
Merged

Guide: Exporting Keras models to LiteRT with PyTorch backend#2373
hertschuh merged 9 commits into
keras-team:masterfrom
pctablet505:litert-export-guide

Conversation

@pctablet505

Copy link
Copy Markdown
Contributor

This PR adds a comprehensive guide for exporting Keras models to LiteRT using the PyTorch backend.

Key changes from the previous approach (TF backend)

  • PyTorch backend is now the recommended path — it does not generate flex ops, which are unsupported by the new LiteRT Android runtime.
  • Uses ai_edge_litert.interpreter.Interpretertf.lite.Interpreter is deprecated and scheduled for deletion.
  • Quantization via ai-edge-quantizer — Post-export quantization is recommended over passing optimizations to the exporter, which can fail on the PyTorch backend for some models.
  • Reference model: Gemma 3 270M — A lightweight LLM that is fast to export and small enough to run on edge devices.

Verified

  • make ✅
  • serve ✅
  • rendering ✅

Notes

- Rewrite the guide to use the PyTorch backend as the recommended path
- Document why TF backend is not recommended (flex ops, deprecated interpreter)
- Show ai-edge-quantizer as the recommended quantization approach
- Include ai_edge_litert.interpreter.Interpreter for inference
- Add backend comparison table and troubleshooting section
- Reference Gemma 3 270M as the practical LLM example

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new guide on exporting Keras models to LiteRT using the PyTorch backend, adding the Python script, Jupyter Notebook, and Markdown versions of the guide, and registering it in the master guides list. The review feedback suggests removing redundant model.compile() calls since the model is only used for inference and export, and simplifying the Gemma 3 preprocessing code by passing a single string directly to the preprocessor instead of a training-like dictionary.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread guides/litert_export.py Outdated
Comment thread guides/litert_export.py Outdated
Comment thread guides/ipynb/litert_export.ipynb Outdated
Comment thread guides/ipynb/litert_export.ipynb Outdated
Comment thread guides/md/litert_export.md Outdated
Comment thread guides/md/litert_export.md Outdated
- Add Android dependencies section (com.google.ai.edge.litert, tensorflow-lite, select-tf-ops)
- Fix Gemma3 example to use empty responses for inference export
- Reorganize: features before quantization (subclass models, multi-input, dict inputs, resizing, signature runner, custom input signature)
- Add TensorFlow backend alternative section with Float16 example
- Follow keras.io guide style: clear explanations before code, runnable examples
- Add master branch installation instructions for keras and keras-hub
- Document dynamic shapes behavior and recommend runtime resizing
- Clarify that InputSpec(None) becomes static at export time
- Fix Gemma3 example to use generate_preprocess() for inference
- Update Android dependency versions to latest (2.1.5, 2.16.1)
- Add runtime input resizing to backend comparison table
- Update best practices and troubleshooting sections
@pctablet505
pctablet505 marked this pull request as ready for review July 6, 2026 05:53
@pctablet505
pctablet505 requested a review from hertschuh as a code owner July 6, 2026 05:53
@pctablet505
pctablet505 force-pushed the litert-export-guide branch from eee5846 to d77844d Compare July 6, 2026 15:54
@pctablet505

Copy link
Copy Markdown
Contributor Author

@laxmareddyp May you please review the PR, it is ready for it.
Thanks

Comment thread guides/litert_export.py Outdated
Comment on lines +44 to +46
pip install -q git+https://github.com/keras-team/keras.git
pip install -q git+https://github.com/keras-team/keras-hub.git
pip install -q git+https://github.com/google-ai-edge/litert-torch.git

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that keras and keras-hub were released, this should work and is much cleaner (and predictable):

pip install -qU keras
pip install -qU keras-hub
pip install -q litert-torch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed keras and keras-hub to latest,
kept litert-torch as installation from github, as our fix for input name mismatch is not in release.

@pctablet505
pctablet505 force-pushed the litert-export-guide branch from edfaeba to 947b0c6 Compare July 21, 2026 09:47

@hertschuh hertschuh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this guide!

@hertschuh
hertschuh merged commit 4bfb5a1 into keras-team:master Jul 21, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants