Repository navigation
gemma4: run the audio encoder correctly and fast on the Apple Neural Engine - #734
Merged
Merged
Conversation
The audio attention masked-fill constant (-1e9) overflows to -inf in the FLOAT16 NPU graph: fully-masked rows softmax to NaN and partial rows diverge on the Neural Engine. Clamp it to a finite fp16-safe -1e4 before export (soft-capped logits stay within +/-50, so masking is unchanged), and route the gemma4 audio encoder to CPU_AND_NE via the bundle manifest. Signed-off-by: Noah Cylich <noahcylich@gmail.com>
ncylich
marked this pull request as draft
June 16, 2026 22:54
jakmro
marked this pull request as ready for review
June 22, 2026 13:57
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 audio encoder was quarantined to CPU+GPU on Apple devices because its CoreML graph produced wrong output on the Neural Engine. Root cause: the audio attention masked-fill constant is
attention_invalid_logits_value = -1e9, which overflows to -inf in the FLOAT16 NPU graph (fp16 max is 65504). Fully-masked padded rows then softmax to NaN, and partial rows diverge on the ANE.This routes the gemma4 audio encoder to the ANE and makes it numerically correct there:
attention_invalid_logits_valueto a finite, fp16-safe-1e4before export. Logits are soft-capped to ±50, so masked positions stillexp()to exactly 0 — masking is unchanged on every backend — but no value ever overflows fp16. (Same finite-mask pattern Parakeet already uses.)npu_audio_compute_units = "CPU_AND_NE"in the bundle manifest forgemma4(as already done forparakeet_tdt).Validation (Mac M4 Pro, int8 audio encoder, real
test.wav)Correctness on valid output tokens vs PyTorch fp32 ground truth:
Before the fix the ANE diverged and CPU-only returned NaN; both are resolved.
Speed (median, fully warm):
The finite mask also lets the encoder compile cleanly onto the ANE — the old
-infgraph silently partition-fell-back and was never truly resident.Notes
cactus transpile … --npuof the gemma4 bundle.Signed-off-by: Noah Cylich noahcylich@gmail.com