Skip to content

GLM-5.3-Flash: end the last expert's sorted pairs at the pair count - #864

Merged
ashhart merged 1 commit into
ashhart:mainfrom
harrisonfriia:fix/glm-rows-last-expert
Oct 16, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
harrisonfriia:fix/glm-rows-last-expert

Conversation

@harrisonfriia

Copy link
Copy Markdown
Contributor

What this changes

glm_rows_sorted skipped the last expert's pairs on prompt chunks. It read that expert's end from offsets[e + 1], a word the route's sort never writes (prefill/sort.metal:44). It now ends the last expert at the pair count, as the affine_mm gathers do, and the offsets buffer drops the unwritten word.

Only layers whose routed experts aren't all 4/64 or all 2/64 use this kernel, so the 4-bit checkpoint never hits the bug. Checkpoints with 5- or 6-bit routed experts do. On the checkpoint below, main returns empty replies from 12K tokens up.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision: base c7137bb3, candidate 09b9df6f. M3 Ultra (80-core GPU, 256 GB), macOS 27.0.1, Xcode 27.0, Zig 0.17.0, no MLX or PyTorch. ddalcu/GLM-5.3-Flash-MLX-Serve-iQ-MLX-5.1bpw at 2b1c4539, with 5- and 6-bit routed experts in layers 3 to 26. Served with GLM_LOAD_LIMIT_GB=215, --parallel 3, --temperature 0.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false:
    • bench_concurrent.py --levels 3 --temperatures 0 --alone --serial, base and candidate alike: 9 of 9 concurrent replies equal their request alone, and 1 of 1 alone reply equals "draft": false.
    • bench_cache.py on 8K-, 12K- and 20K-token prompts: on the candidate, both repeats and all 3 concurrent copies have the first reply's token_sha.
    • A resumed prompt equals a fresh one: a 12,064-token chat turn sent cold, then again resuming 12,036 tokens from the prompt cache, gives the same reply on the candidate.
  • Decode speed before and after, from tools/bench_openai.py: n/a, decode doesn't use this kernel.
  • Prompt speed before and after: the cold first request's ttft_s from tools/bench_cache.py --require-cold. At 8K tokens, 37.1 s on base and 37.2 s on the candidate. At 12K and 20K tokens, base streams no text, so it has no ttft_s; the candidate's are 57.7 s and 97.1 s.
  • Tests run, with pass, fail and skip counts:
    • Served chat requests at temperature 0: on a 22,048-token prompt, a 13,768-token prompt and a follow-up that resumed 13,766 tokens from cache, base returned empty content all three times. The candidate answered all three about the prompt's code.
    • tf-glm-rows-check now lays out the offsets the way the route writes them. It passes on the candidate (8,909,088 bit-equal pairs) and fails with BitsDiffer on main's kernel.
    • zig build test test-golden -Dcpu=apple_m1, base and candidate alike: 164/164 steps, 1,981 of 1,989 tests passed, 8 skipped. Goldens 309/319, 0 unexpected.
    • lean_check.py reports 527 problems, the same as base.
  • Not run, and why: M1, M2, M4 and M5. The change has no chip-specific code.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
  • No precision traded for speed. If the bits change, the description says which sums change and why. Only the last expert's pairs change. They're now computed.
  • Prompt processing is no slower than the last release.
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

…count

glm_rows_sorted read each expert's end as offsets[e + 1], but the route's sort writes one offset per expert, so the
last expert's run ended at the buffer's spare word, which nothing writes, and its pairs were skipped. Prompt chunks
take this kernel unless a layer's routed gate, up and down are all 4-bit or all 2-bit at group 64, so long prompts on
a checkpoint with 5- or 6-bit routed experts went wrong.

The kernel ends the last expert at the pair count, as the affine_mm gathers do, and the offsets buffer drops its spare
word. tf-glm-rows-check lays out the offsets the way the route writes them, so it fails on the old kernel and passes
on this one.
@ashhart

ashhart commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Thanks, @harrisonfriia. Ready at 09b9df6. zig/kernels/metal/glm_rows.metal:173 now ends the final expert at the actual pair count, and both prompt.zig:271-272 callers pass M*topk. The smaller offsets buffer matches what the sorter writes. The other inspected expert-offset consumers already guard the last expert or use a producer-written sentinel, so this fix is complete for that pattern. The updated comparison no longer supplies an artificial sentinel. GPU checks were not rerun here.

@ashhart
ashhart merged commit 4635041 into ashhart:main Oct 16, 2026
4 checks passed
ashhart added a commit that referenced this pull request Oct 16, 2026
…lm_rows_check.zig)

Layout only, by zig fmt after closing commas.
ashhart added a commit that referenced this pull request Oct 16, 2026
@ashhart

ashhart commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Thank you, @harrisonfriia. Finding that the last expert's end was a word the sort never writes, and that only checkpoints with 5- or 6-bit routed experts reach that kernel, explains the empty replies past 12,000 tokens, and your receipt shows it from both sides.

Your commit ships in TensorFold 1.0.12 as it is, merged in 463504135, so this pull request shows as merged. One commit of ours, aea9ae105, wraps six lines at 120 columns, with no statement changed.

tf-glm-rows-check passes on an M3 Ultra and an M5 Ultra (1,328,072 values within the fp32-sum bound, 8,909,088 bit-equal pairs), and the build before your fix fails it with BitsDiffer. GLM-5.3-Flash's 4-bit checkpoint, which never took that path, gave the same tokens as before.

Posted by Ash's AI Assistant Synthena

BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
….zig, glm_rows_check.zig)

Layout only, by zig fmt after closing commas.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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.

2 participants