Skip to content

Reuse Gemma4 softcap fusion for text inference - #675

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/gemma-text-softcap-fusion
Oct 9, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/gemma-text-softcap-fusion

Conversation

@aleroot

@aleroot aleroot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Reuse the existing Gemma4 softcap fusion in Gemma4TextModel, including the gemma4_unified text wrapper.

  • Preserve native FP16/BF16 precision through a scalar-cap overload.
  • Keep CPU execution eager to preserve rounding and avoid CPU/GPU trace reuse.
  • Add regression coverage for runtime caps, strides, all finite FP16/BF16 values, special values, gradients, CPU/GPU switching, and quantized prefill/decode with tied and untied output heads.

Performance

Isolated softcap measurements on Apple M2, Release build, BF16 logits and a 262,144-token vocabulary:

Vocabulary rows Original Fused Speedup
1 173 µs 154 µs 1.12×
16 752 µs 414 µs 1.82×
128 4.69 ms 2.01 ms 2.34×

Medians from 10 alternating baseline/fused rounds after warmup, with evaluation barriers. Outputs matched bit-for-bit.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure:

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change looks good, thank you!

@davidkoski
davidkoski merged commit 34a3fd2 into ml-explore:main Oct 9, 2026
4 checks passed
slamfinger pushed a commit to slamfinger/mlx-swift-lm that referenced this pull request Oct 14, 2026
davidtai added a commit to Layr-Labs/mlx-swift-lm that referenced this pull request Oct 23, 2026
…ogits

David's rulings, 2026-10-23.

Tool calls:
- `ToolCallPolicy.returnsFailedCallsAsText` defaults to true. The fork's frame
  state machine returns a failed, incomplete or undeclared frame as visible
  text, as on fork main before the merge. This restores the fork behavior of
  the generate path, ChatSession and the server container engine.
- Upstream's recovery state machine needs an explicit false. The separate
  `ToolCallProcessor` init parameter is removed, so the policy is the one
  switch. The FoundationModels router passes false (its rejection events
  come only from the recovery machine).
- Upstream tool tests that test the recovery machine pass false explicitly.
  Fork tests use the default and are unchanged in substance.
- QwenXMLPayloadScanner finds tag ends by Unicode scalar, so a value that
  starts with a combining mark no longer hides the `>` (the fork parser did
  this before the merge; Qwen35OpaqueFramingTests).

Gemma4Text keeps float32 softcap logits over upstream 34a3fd2 (ml-explore#675): the
sampler receives full-precision logits (fork 8a212b4, vMLX decode port).
Gemma4TextTests expects the float32 result; its structure is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants