executorch icon indicating copy to clipboard operation
executorch copied to clipboard

Add support for Gemma 3 270M

Open msluszniak opened this issue 1 year ago • 17 comments

🚀 The feature, motivation and pitch

As in the title, users of both ExecuTorch and React Native ExecuTorch will heavily benefit from such addition to the set of available models, look here: https://github.com/software-mansion/react-native-executorch/discussions/566.

Alternatives

Support for any powerful yet very small model like Gemma 3 270M.

Additional context

No response

RFC (Optional)

No response

cc @mergennachin @iseeyuan @lucylq @helunwencser @tarun292 @kimishpatel @jackzhxng

msluszniak avatar Oct 09 '25 14:10 msluszniak

cc @jackzhxng

kirklandsign avatar Oct 09 '25 17:10 kirklandsign

Hi @msluszniak, you can export Gemma3 270M with Optimum ExecuTorch

jackzhxng avatar Oct 09 '25 19:10 jackzhxng

Thank you for the information :))

msluszniak avatar Oct 09 '25 19:10 msluszniak

I'm reopening this issue as I cannot export it via optimum-executorch. After logging into HF CLI, installing optimum-executorch, and running the following command:

optimum-cli export executorch \
    --model "google/gemma-3-270m" \
    --task "text-generation" \
    --recipe "xnnpack" \
    --use_custom_sdpa \
    --use_custom_kv_cache \
    --qlinear 8da4w \
    --qembedding 8w \
    --output_dir="gemma3_270"

I get the following logs:

WARNING:torchao.kernel.intmm:Warning: Detected no triton, on systems without Triton certain kernels will not work
W1017 13:17:49.043000 91476 site-packages/torch/distributed/elastic/multiprocessing/redirects.py:29] NOTE: Redirects are currently not supported in Windows or MacOs.
/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/executorch/exir/dialects/edge/_ops.py:9: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
  import pkg_resources
config.json: 100%|█████████████████████████████████████████████████████████████████████████████████████████████| 1.35k/1.35k [00:00<00:00, 2.00MB/s]
`torch_dtype` is deprecated! Use `dtype` instead!
model.safetensors: 100%|█████████████████████████████████████████████████████████████████████████████████████████| 536M/536M [00:35<00:00, 15.0MB/s]
`torch_dtype` is deprecated! Use `dtype` instead!
WARNING:coremltools:scikit-learn version 1.7.2 is not supported. Minimum required version: 0.17. Maximum required version: 1.5.1. Disabling scikit-learn conversion API.
WARNING:coremltools:Torch version 2.8.0 has not been tested with coremltools. You may run into unexpected errors. Torch 2.5.0 is the most recent version that has been tested.
Traceback (most recent call last):
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/optimum/executorch/attentions/custom_sdpa.py", line 86, in get_custom_sdpa_for_ring_kv_cache
    from executorch.examples.models.llama.source_transformation.custom_kv_cache import (
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/executorch/examples/models/llama/__init__.py", line 7, in <module>
    from .model import Llama2Model
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/executorch/examples/models/llama/model.py", line 22, in <module>
    from executorch.extension.llm.export.config.llm_config import LlmConfig
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/executorch/extension/llm/export/__init__.py", line 7, in <module>
    from .builder import LLMEdgeManager
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/executorch/extension/llm/export/builder.py", line 37, in <module>
    from pytorch_tokenizers import get_tokenizer
ModuleNotFoundError: No module named 'pytorch_tokenizers'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/miniconda3/envs/optimum-executorch/bin/optimum-cli", line 7, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/optimum/commands/optimum_cli.py", line 208, in main
    service.run()
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/optimum/commands/export/executorch.py", line 232, in run
    main_export(
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/optimum/exporters/executorch/__main__.py", line 145, in main_export
    return export_to_executorch(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/optimum/exporters/executorch/convert.py", line 80, in export_to_executorch
    executorch_progs = recipe_func(model, **kwargs)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/optimum/exporters/executorch/recipes/xnnpack.py", line 109, in export_to_executorch_with_xnnpack
    exported_progs = model.export()
                     ^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/optimum/exporters/executorch/integrations.py", line 484, in export
    self._register_custom_attention(exportable_module)
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/optimum/exporters/executorch/integrations.py", line 462, in _register_custom_attention
    _custom_sdpa_for_ring_kv_cache = get_custom_sdpa_for_ring_kv_cache(exportable_module)
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/optimum-executorch/lib/python3.11/site-packages/optimum/executorch/attentions/custom_sdpa.py", line 90, in get_custom_sdpa_for_ring_kv_cache
    raise ImportError(f"CustomRingKVCache not available in version {version.__version__} of ExecuTorch.")
ImportError: CustomRingKVCache not available in version 0.7.0 of ExecuTorch.

msluszniak avatar Oct 17 '25 11:10 msluszniak

@msluszniak how did you install Optimum?

jackzhxng avatar Oct 17 '25 14:10 jackzhxng

I installed it from source, as it is described in README.md of Optimum ExecuTorch

msluszniak avatar Oct 17 '25 14:10 msluszniak

Ah, yes sorry ExecuTorch is planning on releasing 1.0 soon, the pip install may be broken until we bump it since this repo is moving pretty fast. Can you install with step 3 instead? python install_dev.py.

jackzhxng avatar Oct 17 '25 14:10 jackzhxng

Sure thing, I'll return with an update ;)

msluszniak avatar Oct 17 '25 14:10 msluszniak

FYI, now export was successful, only testing of the exported model to go ;)

msluszniak avatar Oct 17 '25 14:10 msluszniak

I encountered the following problem while testing the exported model (specifically when loading the forward method - other methods seem to work just fine):

Skipping import of cpp extensions due to incompatible torch version 2.9.0 for torchao version 0.14.0         Please see GitHub issue #2919 for more info
W1020 14:24:18.596000 55360 .venv/lib/python3.12/site-packages/torch/distributed/elastic/multiprocessing/redirects.py:29] NOTE: Redirects are currently not supported in Windows or MacOs.
[program.cpp:154] InternalConsistency verification requested but not available
[operator_registry.cpp:256] kernel 'quantized_decomposed::embedding_byte.out' not found.
[operator_registry.cpp:257] dtype: 1 | dim order: [
[operator_registry.cpp:257] 0,
[operator_registry.cpp:257] 1,
[operator_registry.cpp:257] ]
[operator_registry.cpp:257] dtype: 6 | dim order: [
[operator_registry.cpp:257] 0,
[operator_registry.cpp:257] 1,
[operator_registry.cpp:257] ]
[operator_registry.cpp:257] dtype: 6 | dim order: [
[operator_registry.cpp:257] 0,
[operator_registry.cpp:257] 1,
[operator_registry.cpp:257] ]
[operator_registry.cpp:257] dtype: 4 | dim order: [
[operator_registry.cpp:257] 0,
[operator_registry.cpp:257] 1,
[operator_registry.cpp:257] ]
[operator_registry.cpp:257] dtype: 6 | dim order: [
[operator_registry.cpp:257] 0,
[operator_registry.cpp:257] 1,
[operator_registry.cpp:257] 2,
[operator_registry.cpp:257] ]
[operator_registry.cpp:257] dtype: 6 | dim order: [
[operator_registry.cpp:257] 0,
[operator_registry.cpp:257] 1,
[operator_registry.cpp:257] 2,
[operator_registry.cpp:257] ]
[method.cpp:759] Missing operator: [10] quantized_decomposed::embedding_byte.out
[operator_registry.cpp:256] kernel 'llama::update_cache_with_indices.out' not found.

[...]

[method.cpp:1024] There are 55 instructions don't have corresponding operator registered. See logs for details
Traceback (most recent call last):
  File "/Users/igorswat/Documents/Projekty/gemma-export/test.py", line 16, in <module>
    method = program.load_method("forward")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/igorswat/Documents/Projekty/gemma-export/.venv/lib/python3.12/site-packages/executorch/runtime/__init__.py", line 168, in load_method
    method = Method(self._program.load_method(name))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Failed to load method forward, error: 0x:14

The full error log is available here.

The test was conducted using the model exported with:

optimum-cli export executorch \
--model "google/gemma-3-270m" \
    --task "text-generation" \
    --recipe "xnnpack" \
    --use_custom_sdpa \
    --use_custom_kv_cache \
    --qlinear 8da4w \
    --qembedding 8w \
    --output_dir="gemma3_270"
    --max_seq_len 2048

IgorSwat avatar Oct 20 '25 12:10 IgorSwat

@IgorSwat this looks like you may not have installed ExecuTorch correctly, what do your dependencies look like / how did you install?

jackzhxng avatar Oct 20 '25 15:10 jackzhxng

I installed ExecuTorch directly through pip package manager, as recommended here.

IgorSwat avatar Oct 21 '25 08:10 IgorSwat

@IgorSwat use python install_dev.py please

jackzhxng avatar Oct 21 '25 13:10 jackzhxng

@jackzhxng do you mean the script from optimum-executorch? In ExecuTorch, there is no such file as install_dev.py

msluszniak avatar Oct 21 '25 14:10 msluszniak

Installing ExecuTorch through optimum-executorch and install_dev.py fixed the issue.

The Gemma 3 270M .pte model also seems to be working and generating proper responses without errors. Here is a piece of code I used to test the model:

from optimum.executorch import ExecuTorchModelForCausalLM
from transformers import AutoTokenizer
from pathlib import Path

tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-270m")
model_path = Path("./models/gemma/")
model = ExecuTorchModelForCausalLM.from_pretrained(model_path)

text = "Steel is an alloy of iron and carbon that demonstrates the"
initial_text = text
inputs = tokenizer(text, return_tensors="pt")
input_ids = inputs["input_ids"]

for i in range(20):
  inputs = tokenizer(text, return_tensors="pt")
  input_ids = inputs["input_ids"]
  out = model.generate(input_ids[0].tolist())
  result_decoded = tokenizer.decode(out[0], skip_special_tokens=True)

  print("Output:", result_decoded)

  text += result_decoded

print("Initial sentence:", initial_text)
print("Final sentence:", text)

IgorSwat avatar Oct 21 '25 15:10 IgorSwat

Ok, I'm closing since everything looks good :)), thank you Jack for your support

msluszniak avatar Oct 21 '25 15:10 msluszniak

No worries, apologies for the confusion with the install. We are going to bump Optimum to ExecuTorch 1.0 asap and the pip install for Optimum should work after that

jackzhxng avatar Oct 21 '25 16:10 jackzhxng