Repository navigation
Preserve FP32 Qwen expert partials through tensor collectives - #346
Closed
jonathan308 wants to merge 4 commits into
Closed
jonathan308 wants to merge 4 commits into
jonathan308 wants to merge 4 commits into
Conversation
This was referenced Oct 23, 2026
javiech1
pushed a commit
to javiech1/mlx-swift-lm
that referenced
this pull request
Oct 24, 2026
…Layr-Labs#346) Gemma3TextModel.prepare previously returned the whole prompt to the TokenIterator, which then ran the 262k-vocabulary lm_head over every prompt position just to produce the first token. Instead, prefill all but the last token through the inner model (which has no lm_head), updating only the KV cache, and hand just the final token to the iterator. The chunk size honours an explicit GenerateParameters.prefillStepSize and otherwise defaults to 128, tuned for this path on Apple Silicon for the best asyncEval CPU/GPU pipelining. To make that default reachable, prefillStepSize becomes optional (nil = let the model choose); other models keep their 512 default via `windowSize ?? 512`. Measured on translategemma-4b-it-4bit (Apple Silicon, 577-token prompt, greedy): prompt prefill 177 -> 463 tok/s (2.6x), 3253 -> 1246 ms. Greedy output is byte-identical to before, including prompts longer than the 1024 sliding window where the rotating KV cache rotates. Co-authored-by: David Koski <46639364+davidkoski@users.noreply.github.com>
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.
Channel-sharded Qwen experts currently round their local output before the cluster sums the routed/shared block. This loses precision before router weighting. Add an affine expert-down wrapper that hands FP32 partials to an injected collective, then rounds once before the existing SwitchGLU restores route order and applies weights.
The wrapper retains the packed shard and rejects replicated biases, unsupported quantization and subclasses with an unknown forward contract. Generic routing and projection implementations remain unchanged. The provider integration separately handles shared-expert reduction and budgets the retained FP32 constant casts.
Depends on #345. Keep as draft pending full-model two-Mac qualification and resolution of the SDK stack's existing CI failures.
Before
flowchart LR A["Local expert projection"] --> B["BF16 round and router weights"] B --> C["Combine shared expert"] --> D["Cluster sums rounded blocks"] classDef compute fill:#dbeafe,stroke:#1d4ed8,color:#172554 classDef loss fill:#fee2e2,stroke:#b91c1c,color:#450a0a class A compute class B,C,D lossAfter
flowchart LR A["Packed expert shard"] --> B["FP32 down projection"] B --> C["Injected collective sum"] --> D["Round once"] D --> E["Existing route order and weighting"] classDef compute fill:#dbeafe,stroke:#1d4ed8,color:#172554 classDef retained fill:#dcfce7,stroke:#15803d,color:#052e16 class A,B compute class C,D,E retainedValidation: the actual-source standalone Swift Testing CPU runner passed all three new kernel tests: unrounded partials with an early-rounding negative control, actual sorted/unsorted SwitchGLU dispatch before weighting, and replicated-bias refusal. MLXLMCommon module and optimized-object compilation passed;
swift-format lint --stricton both new Swift files andscripts/check-unit-test-files.shpassed. Independent implementation review found no remaining arithmetic defect. This is focused component evidence, not full-model numerical or performance acceptance.