Skip to content

Preserve FP32 Qwen expert partials through tensor collectives - #346

Closed
jonathan308 wants to merge 4 commits into
fix/cluster-gemma-prefill-reductionsfrom
fix/cluster-qwen-expert-reduction
Closed

jonathan308 wants to merge 4 commits into
fix/cluster-gemma-prefill-reductionsfrom
fix/cluster-qwen-expert-reduction

Conversation

@jonathan308

Copy link
Copy Markdown

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 loss
Loading

After

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 retained
Loading

Validation: 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 --strict on both new Swift files and scripts/check-unit-test-files.sh passed. Independent implementation review found no remaining arithmetic defect. This is focused component evidence, not full-model numerical or performance acceptance.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant