Add Gemma 4 text-decoder export to CoreML#19253
Add Gemma 4 text-decoder export to CoreML#19253john-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.
|
|
Thanks @john-rocky, really appreciate the CoreML batch. Linking the related PRs in this stack so reviewers can see the full picture: #19245, #19246, #19247, #19248, #19249, #19250, #19251, #19252. @metascroy you're already on this one. Would you mind taking a sweep across the stack, or should we pull in another CoreML reviewer? |
|
Thanks @shoumikhin! Quick orientation for whoever does the sweep:
All nine have unit tests I ran on macOS 26 / Python 3.10 / coremltools 9.0; the test plan section in each PR body has the local pytest output. Happy to split, squash, retitle, or |
|
@pytorchbot label "release notes: apple" |
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.