Skip to content

qwen3_5_moe: the experts' prompt sort as one threadgroup (stacked on #1125) - #1799

Merged
ashhart merged 2 commits into
ashhart:mainfrom
BobClawblaw:ornith-route-count
Nov 4, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
BobClawblaw:ornith-route-count

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

Stacked on #1125: this branch is #1125's head (22f1026) plus one commit, 3b77807. Please review that commit only; I will rebase it onto main once #1125 lands.

Closes #1798.

What changes

q27_route_sort sorts each prompt chunk's routed slots by expert for the tensor units' gathers. It ran on one GPU thread, which walked the chunk's slots twice (count, then place). It now runs as one threadgroup with a thread an expert (config.zig admits at most 256 experts):

  • the threads count the slots together in threadgroup memory (atomic_int counts[256]);
  • each expert's thread takes its first slot from the counts before it, and thread 0 writes the end;
  • each expert's thread writes its own slots in ascending order, so the sort stays stable.

The kernel's name, buffers and outputs (OFFS, ORDER, the sorted weights) are unchanged, so the gathers and everything after them see the same bits. Only the launch changes, from one thread to one threadgroup of the experts rounded up to 32. The dense 27B does not route and never runs this kernel. Four files: moe.metal, moe_gpu.zig (the launch), the contract record (256 threads, 1,024 bytes of threadgroup memory) and the contract test's launch.

Checks (M5 Ultra, the publisher's ornith-ai/Ornith-1.5-122B-MLX-4bit @ 06a57aba)

Exactness, tf-qwen27-run at the served 1,024-row chunks against #1125's head:

  • prompt state (prompt_sha256) and 48 greedy tokens byte-identical at 2,048, 8,192 and 32,768 prompt tokens;
  • --compare-chunk 13 --seams 1024: prompt_state_byte_equal, decode_state_byte_equal and seams_byte_equal all true.

Prompt speed, tf-qwen27-run, two runs of each build, alternating:

prompt #1125's head this PR
8,192 tokens 2,316 / 2,326 tok/s 2,680 / 2,632 tok/s
32,768 tokens 2,128 / 2,118 tok/s 2,443 / 2,448 tok/s

Served, the recipe harness's prefill plan (one cold prompt a size, thinking off), against the same plan on #1125's rebase 078e224:

prompt 078e224 this PR
2,231 tokens 2,424 tok/s 2,963 tok/s
8,770 tokens 2,478 tok/s 2,929 tok/s
35,019 tokens 2,157 tok/s 2,204 tok/s
140,013 tokens 1,322 tok/s 1,413 tok/s

Each served row is a single prompt, so read the long rows loosely: attention takes a larger share there. The harness's quick plan on the served build matches #1125's results (structured 18/18, sampling 9/9, turns, URLs and the soak, draft and concurrency suites pass; tools 4/5 with the same named-tool-with-thinking case as #1125). The vision suite gives 4/6 and 16/16, as on #1125. Decode is unchanged (one stream 132.7 / 191.6 / 160.6 / 193.8 tok/s for prose, code, structured and lists).

Kernel contract, tf-kernel-contract --only qwen27:

SUMMARY 8 kernels: 8 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems

CI set: zig fmt --check, test-kernel-contract, test-golden, test-server-security and test-pull-security pass. Lean: 424 problems, the same 424 as #1125's head. zig build test has one failure, contract_images_test ("every registered CUDA launch and control selects its own image"), which fails the same way on #1125's head without this commit.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw: this is byte-identical to the version we reviewed (the one-threadgroup sort, byte-equal to the serial one). It lands right after #1125.

@BobClawblaw

Copy link
Copy Markdown
Contributor Author

@ashhart, restacked on #1125's new head (53c9074): head 9c9a5f8. The sort commit's patch is line-for-line what you reviewed. CI checks pass, lean has 0 new problems, and Ornith's live replies (greedy, 1/2/4 streams, with drafts) are identical to #1125's head and to the 2 Nov run. #1125 is being rebased once more over today's main (a CHANGELOG conflict only); this follows it.

@BobClawblaw
BobClawblaw force-pushed the ornith-route-count branch 3 times, most recently from 2906c5b to bd524f1 Compare November 3, 2026 14:33
@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @BobClawblaw. We checked the restack: the sort commit is line for line the one we approved, and everything else under it is #1125's. Nothing more is needed here. It lands right after #1125, and if #1125's final rebase moves it again, a plain restack is all it needs.

@ashhart
ashhart force-pushed the ornith-route-count branch from bd524f1 to 6d6e033 Compare November 4, 2026 12:27
ashhart added a commit to BobClawblaw/TensorFold that referenced this pull request Nov 4, 2026
@ashhart
ashhart force-pushed the ornith-route-count branch from 6d6e033 to 385595f Compare November 4, 2026 13:42
BobClawblaw and others added 2 commits November 4, 2026 15:29
q27_route_sort ran on a single GPU thread, walking every routed slot three times for each prompt chunk. It now runs as
one threadgroup with a thread an expert: the threads count the slots together in threadgroup memory, each expert's
thread sums the counts before it for its first slot and writes its own slots in ascending order. The outputs (OFFS,
ORDER, the sorted weights) and the kernel's interface are unchanged, so the gathers' bits are too.

Ornith-1.5-122B (the publisher's 4-bit) on an M5 Ultra, tf-qwen27-run at the served 1,024-row chunks, two runs each:
8,192-token prompt 2,316 / 2,326 -> 2,680 / 2,632 tokens/s, 32,768-token prompt 2,128 / 2,118 -> 2,443 / 2,448.
Prompt state and 48 decoded tokens byte-identical to the previous build at 2k, 8k and 32k; --compare-chunk 13 with
--seams 1024 byte-equal. The dense model does not route and never runs this kernel.
@ashhart
ashhart force-pushed the ornith-route-count branch from 385595f to 4600e4c Compare November 4, 2026 15:29
@ashhart
ashhart merged commit b331731 into ashhart:main Nov 4, 2026
1 check passed
@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thank you @BobClawblaw. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

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.

qwen3_5_moe: the experts' prompt sort runs on one GPU thread

2 participants