Skip to content

Reduce unused vocabulary projections during Qwen prefill (~8% speedup) - #614

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:qwen_prefill_voc
Oct 7, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:qwen_prefill_voc

Conversation

@aleroot

@aleroot aleroot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Proposed changes

Qwen currently computes output scores for prompt positions that normal generation discards. This change reduces that work, making prompt processing faster and reducing peak memory usage.

Applies to supported quantized Qwen3 and Qwen3.5 models, including Qwen3.5’s vision-language path. It keeps the same calculation order for the final scores and preserves full outputs where needed, including speculative decoding.

On my Apple M2 with 16GB RAM, using 512-token text prompts and alternating baseline/optimized runs:

Model Before → after Lower prefill latency
Qwen3-4B-4bit 3.45 → 3.17 s 8.1%
Qwen3.5-9B-4bit, text model 5.42 → 4.84 s 10.7%
Qwen3.5-9B-4bit, vision-language model with text input 5.40 → 4.82 s 10.7%

Peak memory decreased by approximately 130, 185, and 212 MiB respectively. Gains depend on prompt length and prefill chunking.

Exact comparisons passed for final scores, cache contents, and generated tokens on both cached checkpoints.

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

Copy link
Copy Markdown
Member

Needs a rebase -- project() looks like the same code as projectLogits() from #672.

Comment on lines -918 to +919
model(previous[text: .newAxis], cache: cache.isEmpty ? nil : cache, state: state)
model.nextTokenLogits(
previous[text: .newAxis], cache: cache.isEmpty ? nil : cache, state: state)

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.

Nice!

@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.

Just needs a rebase to pick up the changes from #672, otherwise I think ready to merge.

Keep aligned final projection tiles for Qwen3 and Qwen3.5 generation,
preserving full-output paths and cache updates. Add exact parity,
fallback, and local checkpoint tests.
@aleroot

aleroot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Just needs a rebase to pick up the changes from #672, otherwise I think ready to merge.

Done, thanks.

@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 great, thanks!

jeethu added a commit to numen-tech/mlx-swift-lm that referenced this pull request Oct 12, 2026
Merge upstream main (65 commits, through ml-explore#662); re-thread the Qwen3.5 seam around ml-explore#614 (gemma4-qat#283)
slamfinger pushed a commit to slamfinger/mlx-swift-lm that referenced this pull request Oct 14, 2026
)

Keep aligned final projection tiles for Qwen3 and Qwen3.5 generation,
preserving full-output paths and cache updates. Add exact parity,
fallback, and local checkpoint tests.
@aleroot aleroot mentioned this pull request Nov 10, 2026
4 of 5 tasks
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