Add Gemma 4 text-decoder export to CoreML#19253
Open
john-rocky wants to merge 1 commit intopytorch:mainfrom
Open
Add Gemma 4 text-decoder export to CoreML#19253john-rocky wants to merge 1 commit intopytorch:mainfrom
john-rocky wants to merge 1 commit intopytorch:mainfrom
Conversation
The Gemma 4 text decoder shipped with examples/models/gemma4 already
implements hybrid sliding/full attention, partial RoPE, per-layer
head_dim, MQA, and YOCO KV sharing in plain PyTorch. That
implementation lowers cleanly through torch.export and
CoreMLPartitioner — every node in the resulting edge program is a
single executorch_call_delegate and a getitem. This script wires up
the small amount of glue needed for an on-device-friendly default:
* compile_specs targeting iOS18+ so the YOCO KV caches can be taken
over as stateful tensors.
* fp16 by default (the ANE requires fp16).
* compute_unit=CPU_AND_NE so the runtime is free to keep ops on the
ANE.
* Optional --random_weights mode for smoke-testing the export
without a HuggingFace checkpoint, plus --config_json /
--sliding_window / --sliding_window_pattern overrides.
Audio and vision encoders are intentionally out of scope here — the
existing ATen pipeline in examples/models/gemma4 is more appropriate
for those.
### Test plan
`test.py` builds a 10-layer synthetic Gemma 4 (4 sliding + 1 full
× 2) and runs the full export pipeline, asserting the resulting .pte
exists.
$ python -m pytest examples/apple/coreml/gemma4/test.py -v
test.py::TestGemma4CoreMLExport::test_eager_forward_runs PASSED
test.py::TestGemma4CoreMLExport::test_full_export_pipeline_lowers_to_coreml PASSED
============================== 2 passed in 15.32s ==============================
I also ran the export by hand against the synthetic config and
confirmed the lowered edge program contains only `executorch_call_delegate`
and `getitem` at the top level.
Authored with Claude.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19253
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Gemma 4 text decoder shipped with
examples/models/gemma4/text_decoder/already implements hybrid sliding/full attention, partial RoPE,
per-layer
head_dim(256 for sliding / 512 for full), MQA, and YOCOKV sharing in plain PyTorch.
I checked, and that implementation lowers cleanly through
torch.exportandCoreMLPartitionertoday — for the synthetic10-layer Gemma 4 used in the new test, the lowered edge program
contains exactly
executorch_call_delegateandgetitemat the toplevel (1186 MIL ops fully delegated). No portable fallbacks, no
unsupported ops.
So the missing piece is not new modeling code — it is the small amount
of glue that turns "exportable in principle" into "exportable from one
shell command". This PR adds that glue:
examples/apple/coreml/gemma4/export_gemma4_text_decoder_coreml.py,with sensible CoreML defaults:
iOS18+deployment target so theYOCO KV caches can be taken over as stateful tensors,
compute_unit=CPU_AND_NE, fp16 by default (the ANE requires fp16).--random_weightsmode for smoke-testing the export pipelinewithout a HuggingFace checkpoint, plus
--config_json,--sliding_window,--sliding_window_patternoverrides.readme.mddocumenting the flags and the "everything delegates"property.
BUCKtarget so the script is buildable in fbcode the same waythe existing CoreML llama scripts are.
The audio and vision encoders are intentionally out of scope — the
existing ATen pipeline in
examples/models/gemma4is more appropriatefor those.
Test plan
examples/apple/coreml/gemma4/test.pybuilds a 10-layer syntheticGemma 4 (4 sliding + 1 full × 2) — same hybrid pattern as Gemma 4 E2B,
just at smaller dimensions — and runs the full export pipeline,
asserting the resulting
.pteis non-empty.I also ran the export by hand and confirmed the resulting edge program
is fully delegated.
Relationship to other open PRs
--sliding_window/--sliding_window_patternfor the static-LLM Llama path. Gemma 4's text decoder uses a
different attention implementation (per-layer
head_dim, partialRoPE, etc.) that already understands those concepts via
Gemma4Config,so this PR doesn't depend on those — it just plumbs the equivalent
overrides through to
Gemma4Configdirectly.coreml_compute_plan.py, which is the natural next stepfor tuning a Gemma 4 export: run it against the produced
.ptetosee which ops the runtime would dispatch to the ANE vs the CPU.
Authored with Claude.