Skip to content

feat(qwen4_exp): apply an n-gram table's weight_scale at lookup instead of refusing it (#142) - #148

Closed
gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:fix/ngram-table-scale
Closed

gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:fix/ngram-table-scale

Conversation

@gilby

@gilby gilby commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #142.

Problem

oMLX's oQ quantizations of Qwen3.8 Flash Next store each n-gram table scaled up, with one factor kept in ngram_embedding.weight_scale. For example, Jundot/Qwen3.8-Flash-Next-oQ4e-mtp uses 0.000199. sanitize() refuses any scale other than 1, so these checkpoints don't load:

language_model.model.layers.1.ple.ple_embedding.ngram_embedding.weight_scale: an n-gram table scale other than 1 is not supported

Change

  • sanitize(weights, table_scales) collects each table's scale by embedding path. Called without the dict, it still refuses a scale other than 1, as before.
  • NGramEmbedding.table_scale and PleTables.scale carry the scale.
  • embed.ple_lookup (the fused decode and prefill), plus NGramEmbedding's host and resident fallbacks, multiply the looked-up bf16 rows by it once: an fp32 multiply and a single bf16 rounding (embed.scaled_rows). Each row gets the same function of its own values on every path and at every row count, so drafted == undrafted and prompt chunking are unaffected. A scale of 1 is the identity, so MLX conversions are untouched.
  • New tests/test_qwen4_exp_table_scale.py covering the sanitize collection, the refusal without the dict, and rounding and row-count invariance.

Checks

  • Tests: the new file plus the existing qwen4_exp / Flash Next / n-gram / PLE tests on an M5 Ultra: 224 passed, 5 skipped.
  • Jundot/Qwen3.8-Flash-Next-oQ4e-mtp, MTP drafts, --parallel auto:
loads 23×19, code, tool call 1 stream N=4 prefill 6.9K MTP accepted concurrent == serial
main, any Mac ❌ refused
this branch, M5 Ultra ✅ 101.1 GiB ✅ ✅ ✅ 122–186 tok/s 223–249 3,049 tok/s 53–64% ✅
this branch, M3 Ultra ✅ 98.5 GiB ✅ ✅ ✅ 83–111 121–126 1,148 64–70% ✅

The startup log names the applied factor: n-gram tables scaled by 0.000199318 at lookup (1 tables). Without it, the rows would be about 5,000× too large and the replies would be garbage.

🤖 Generated with Claude Code

…ad of refusing it (ashhart#142)

oMLX's oQ checkpoints of Qwen3.8 Flash Next store each n-gram table's rows scaled up with one factor in
`ngram_embedding.weight_scale` (e.g. Jundot/Qwen3.8-Flash-Next-oQ4e-mtp: 0.000199). 0.5.0 refused any scale
other than 1, so those checkpoints did not load (ashhart#142).

- sanitize() collects each table's scale by embedding path (still refuses a scale other than 1 when called
  without the dict, as before).
- NGramEmbedding.table_scale and PleTables.scale carry it; embed.ple_lookup and NGramEmbedding's host and resident
  paths multiply the looked-up bf16 rows by it once (fp32 multiply, one bf16 rounding). The same function of each
  row on every path and at every row count, so drafted == undrafted and prompt chunking are unaffected; scale 1
  is the identity.
- tests/test_qwen4_exp_table_scale.py.

Jundot/Qwen3.8-Flash-Next-oQ4e-mtp (MTP drafts) on an M5 Ultra: loads (101.1 GiB resident); 437, runnable code and
a structured tool call; 122-186 tok/s single stream, N=4 223-249, 3,049 tok/s prefill at 6.9K; MTP accepted
53-64%; concurrent replies equal to serial ones.
ashhart added a commit that referenced this pull request Sep 30, 2026
… again; smaller cli, server and weights modules; docs

- CUDA applies an n-gram table's scale after row dequantization (#148)
- The draft head takes image rows only when there are any, so DeepSeek-V4's head drafts again
- cli.py, server/app.py and qwen4_exp/cuda/weights.py split below 600 lines, no behavior change
- Docs: every serve flag, the prompt cache default, the live line, context_length_exceeded, the vision install
@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby. This landed in 0.6.0, out tonight, as you wrote it: your commit is in with your name on it. On an M3 Ultra, Jundot's oQ4e loaded in 59 s and gave 9/9 drafted replies equal to serial, and 9/9 concurrent replies equal to serial at --parallel 4. A scale-1 checkpoint's token SHAs stayed identical. We added the same scale on the CUDA side, so both backends read these tables. Your #147 is in 0.6.0 too, and #149 and #154 are first in line for 0.6.1.

@ashhart ashhart closed this Sep 30, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…on Macs again; smaller cli, server and weights modules; docs

- CUDA applies an n-gram table's scale after row dequantization (ashhart#148)
- The draft head takes image rows only when there are any, so DeepSeek-V4's head drafts again
- cli.py, server/app.py and qwen4_exp/cuda/weights.py split below 600 lines, no behavior change
- Docs: every serve flag, the prompt cache default, the live line, context_length_exceeded, the vision install
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.

Flash Next: Jundot/Qwen3.8-Flash-Next-oQ4e-mtp fails to load (n-gram table scale other than 1)

2 participants