Skip to content

glm/cuda: TF_GLM_MOE_DIRECT=1 runs a decode window's EXL3 MoE without its row copies and put_slot (for #3570) - #3659

Merged
ashhart merged 5 commits into
ashhart:mainfrom
ultradaoto:glm-moe-direct
Nov 22, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
ultradaoto:glm-moe-direct

Conversation

@ultradaoto

Copy link
Copy Markdown
Contributor

For #3570, as agreed there: opt-in behind a strict 0/1 switch, off by default; the top-k kernel changed in place with one optional pointer; the new slot sum registered bit-equal to what it replaces; a direct byte check at every row count up to one chunk.

What TF_GLM_MOE_DIRECT=1 changes, in a GLM decode window whose routed experts are EXL3 (prompt chunks, NVFP4 experts and the default path launch exactly as on main):

  • tf_glm_topk takes one more argument, int* flat. When it is not null, the lanes that write pick[r][k] also write flat[r * top_k + k], the [rows][top_k] array the EXL3 grouper reads. Nothing else in the body moves; glue.select passes null unless the switch is on. One symbol, one body.
  • The EXL3 experts then run straight on those picks (cuda_exl3.runDirect, which is compact's one-chunk call without its first cuMemcpy2DAsync) and leave their slot rows where they write them.
  • tf_glm_combine_tail sums those rows ([rows][top_k][d] fp32) and then the shared expert's row ([rows][d] fp32, read in place of slot top_k): the same __fmaf_rn chain from 0 in the same slot order as tf_glm_combine over the slot rows. So put_slot and compact's second cuMemcpy2DAsync go too. Two copies and one launch fewer per MoE layer, 45 layers a round.
  • cuda_moe.direct decides once per layer: the switch, a decode window (not a prompt chunk), EXL3 experts, top_k 8 and at most one chunk (1,024) of rows. A decode window's fill is always whole (Forward.ffn); a direct fill asked for rows from anywhere but 0 returns error.BadWindow rather than read slots it never wrote.

Registration:

  • TF_GLM_MOE_DIRECT is a .glm .flag row in platform/environment.zig (exactly 0 or 1, other text refused by name when the family opens), a row in cuda_env_test.zig's reader list, and a row in docs/environment.md. Read once at load (Modules.moe_direct, as TF_GLM_DSEL is); a rank logs one line when it is on.
  • Launch records in glm_cuda_records.zig for tf_glm_topk (32 threads, __launch_bounds__(32)) and tf_glm_combine_tail (128 threads), nvcc 13.0's usage on sm_80, 86, 89, 120 and 121 from cuobjdump -res-usage of a five-SM build of glm_glue.cu with the build's flags (the same recipe gives the file's existing tf_glm_router_tile, tf_glm_scores, tf_glm_lat_write and tf_glm_pool_keys numbers exactly). later_pending as tf_glm_router_tile's: I have no 13.2 or 13.3 here.
  • zig/tests/cuda/contract_glm_moe_direct.zig, in the runner's family sets:
    • tf_glm_topk with flat picks: raw out, the stated rule checked by the test itself. On every prepared call, finite and planted NaN and infinities alike: flat equals pick's routed columns, pick and wts are byte-equal to the launch without flat, the shared slot holds expert 288 at weight 1. On finite logits also the rule the kernel holds: in order, no unpicked expert's sigmoid + bias is above a pick's by more than 1e-5 against float64; experts with bit-equal logit and bias go to the lower id first (every row plants such a pair at the top); each weight is its sigmoid over the picks' sum times the scale, within 1e-5 of float64. Served at 1, 4, 32, 129 and 1,024 rows of 288 experts. Its control launches without the pointer and must go red at gate 3.
    • tf_glm_combine_tail: replaces = "tf_glm_put_slot, the EXL3 slot-row copy and tf_glm_combine", raw out. The baseline is the old path exactly as main runs it: the cuMemcpy2DAsync of the routed rows into slot rows, tf_glm_put_slot of the shared row, glue.combine. Served at every row count from 1 to 1,024 at 4,096 dims and 9 slots, so gate 3 is the byte check at every row count up to one chunk; gate 1 runs the same comparison on NaN, +inf and -inf planted in the routed and shared rows. Its control puts slot 0's routed rows in the shared row's place and must go red at gate 3.

Checks on the branch as pushed (aarch64, a GB10, nvcc 13.0): zig build -Dgpu=spark builds; zig build test-cuda-host passes 6,829 of 6,863 with 33 skipped, and its one failure is cli_config_test's serve --config: expand ..., which fails the same way on main on this host; test-kernel-symbols-host and test-kernel-contract (host) pass; pr_check: 0 errors, 0 new lean findings; git merge-tree against main 25de9d7 is clean.

Device contract, on one GB10 (sm_121) with the server stopped: tf-cuda-test kernel-contract tf_glm_topk tf_glm_combine_tail:

PASS nonfinite cuda glm tf_glm_topk with flat picks (TF_GLM_MOE_DIRECT): the stated rule holds: flat equal to pick's routed columns, and pick and wts bit-equal to the launch without flat, non-finite logits included; on finite logits the top_k highest sigmoid + bias in order, ties to the lower expert ...
PASS repeat cuda glm tf_glm_topk with flat picks (TF_GLM_MOE_DIRECT): five runs byte-identical (104 bytes), inputs and guards untouched, no overlap
PASS shapes cuda glm tf_glm_topk with flat picks (TF_GLM_MOE_DIRECT): 5 shapes exact by its own check (worst 0.000 of the bound), 0 edges refused by the kernel
N/A  sums cuda glm tf_glm_topk with flat picks (TF_GLM_MOE_DIRECT): declares none sums
FAIL shapes cuda glm tf_glm_topk control: no flat pointer, the flat picks left unwritten: at 1x288x8: row 0 flat pick 0 is -1, pick's is 24
CONTROL cuda glm tf_glm_topk control: no flat pointer, the flat picks left unwritten: red at shapes, as planted
PASS nonfinite cuda glm tf_glm_combine_tail (TF_GLM_MOE_DIRECT's slot sum): the stated rule holds: every byte equal to tf_glm_put_slot, the routed slot rows' copy and tf_glm_combine's on the same rows, weights and shared row, non-finite inputs included
PASS repeat cuda glm tf_glm_combine_tail (TF_GLM_MOE_DIRECT's slot sum): five runs byte-identical (16384 bytes), inputs and guards untouched, no overlap
PASS shapes cuda glm tf_glm_combine_tail (TF_GLM_MOE_DIRECT's slot sum): 1024 shapes bit-equal to the kernel it replaces (worst 0.000 of the bound), 0 edges refused by the kernel
N/A  sums cuda glm tf_glm_combine_tail (TF_GLM_MOE_DIRECT's slot sum): declares none sums
FAIL shapes cuda glm tf_glm_combine_tail control: slot 0's routed rows in the shared row's place: at 1x4096x9 its bytes differ from tf_glm_put_slot, the EXL3 slot-row copy and tf_glm_combine's from byte 1
CONTROL cuda glm tf_glm_combine_tail control: slot 0's routed rows in the shared row's place: red at shapes, as planted
PASS limits live glm_glue tf_glm_topk: 32 threads launched, the driver allows 32; 68 registers, 0 + 0 shared bytes (recorded 68, 0)
PASS limits live glm_glue tf_glm_combine_tail: 128 threads launched, the driver allows 1024; 44 registers, 0 + 0 shared bytes (recorded 44, 0)
(filtered) SUMMARY 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems

Receipts: three GB10s cabled as a switchless ConnectX-7 triangle, TP=3, GLM-5.3-Flash EXL3, one build (a437511 plus this branch). Settings: --parallel 4 --kv-cache fp8 --decode-share 0.5, --drafter incoai/GLM-5.3-Flash-DFlash2 at 6cc6c481, TF_GLM_DENSE_BITS=8 TF_GLM_HEAD_BITS=8 TF_GLM_KDA_PAD=1 TF_ROCE=1 (NCCL carries every gather here; the setting splits the drafter's MLP), greedy. Four starts in one session, in this order: unset, 1, 1, unset, each a fresh server with an empty window-cost cache. With the switch on, rank 0 logs TF_GLM_MOE_DIRECT=1: a decode window's EXL3 experts read flat picks, one slot sum. One number per start:

unset TF_GLM_MOE_DIRECT=1 TF_GLM_MOE_DIRECT=1 unset
16 varied greedy requests (4 with thinking), aggregate tok/s 58.52 59.35 59.25 58.22
tokens a round over those requests 3.058 3.058 3.058 3.057
tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0, code, median tok/s 62.17 63.30 62.96 62.24
the same, chat with thinking off 49.59 50.62 49.90 49.69
tools/bench_concurrent.py --tokens 256 --reps 3 --temperatures 0 --levels 1,4 --alone --serial, code, 1 / 4 streams, aggregate 62.2 / 128.0 63.4 / 127.2 62.7 / 128.8 62.2 / 128.6
the same, chat, 1 / 4 streams 49.5 / 108.6 50.4 / 109.0 50.5 / 111.1 49.5 / 110.2
four mixed requests at once, aggregate tok/s 86.0 86.4 86.4 86.7

At one stream that is +1.6% over the 16 requests (59.30 against 58.37, the starts' means), +1.5% on bench_openai code and +1.2% on chat; at four streams the two settings are level within the starts' spread.

Tokens: 13 greedy replies (8 prompts alone, 4 at once, one prompt of about 8K tokens) hash the same in all four starts, and the same as the 1.2.9 release's with these bits and no drafter.

Where the time goes: TF_GLM_PROF=1 with MTP drafts (--mtp-confidence 0.5, no drafter), so verify calls run 4 rows; four more starts, unset, 1, 1, unset. Each rank's mean over its 4-row verify calls (12 to 15 a start), ms:

unset =1 =1 unset
rank 0, whole call 48.27 47.24 47.27 47.62
rank 0, router + routed + shared 27.97 27.49 27.34 27.86
rank 1, whole call 48.12 47.12 47.19 47.53
rank 2, whole call 48.11 47.12 47.18 47.51

The call is 0.69 ms shorter on rank 0 (47.26 against 47.95, -1.4%), about as #3570 estimated; of that, 0.50 ms is in the MoE block's own parts (routed -0.31, shared -0.20), the two copies and one launch a layer fewer. The 13 hashes of three of these starts match the release's too; the second =1 start's hash run was cut short when a node's Ethernet cable came loose, after its profile had been taken.

The branch is cut from a437511, main just before 40a46d6 (a workflow change my token may not push). None of the files here changed between a437511 and main 25de9d7 except platform/environment.zig, docs/environment.md and CHANGELOG.md, which merge cleanly.

@ultradaoto

Copy link
Copy Markdown
Contributor Author

The zig job's one failure was test-gradient-serving: "serving source changed: zig/kernels/cuda/glm_glue_moe.cuh". This branch edits two pinned serving sources, so 95b2e27 re-pins both in zig/tests/fixtures/gradient-serving.tsv with a note: glm_glue_moe.cuh (tf_glm_topk takes the optional flat-picks pointer, null when unset) and glm_glue_rows.cuh (tf_glm_combine_tail added; tf_glm_combine unchanged). Main has not changed either file since this branch's base, so the pins hold in the merge with main (f5765b5): all 169 pins match there, checked the way the test hashes them. The rest of that job's "failed command" lines are tests that write to stderr; its summary had the one failed test.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Hi @ultradaoto, thanks for the re-pins. The diff registers TF_GLM_MOE_DIRECT as a strict 0/1 flag that is off by default, keeps the top-k and combine bodies otherwise unchanged, and pins both new kernels to the paths they replace with planted controls; the two serving-source hashes are updated under gradient-serving.tsv. It applies to current main and every file is under the size limit. The GB10 contract receipts you posted cover the opt-in path. Nothing is needed from you.

  • Synthena

@ultradaoto

ultradaoto commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

A note for when this and #3776 meet, since both are open.

#3776 turns rows8Launch in cuda_glue.zig into cuda_pdl.launch. This PR's new tf_glm_combine_tail goes through rows8Launch, so with both on main it becomes a dependent launch that reads y, tail and wts before any wait.

In a local merge of the two I added glm_pdl::enter(); as the first statement of tf_glm_combine_tail in glm_glue_rows.cuh. tf_glm_topk keeps #3776's glm_pdl::enter() and takes this PR's flat argument, with its launch staying on cuda_pdl.launch. With that, check-pdl-loads reports no early reads. A build of this PR on top of #3776's commits served 13 of 13 hashes identical to the same tree without this PR, with TF_GLM_MOE_DIRECT=1 at TP=3 on 3 x GB10.

The extra line changes the gradient-serving.tsv pin of glm_glue_rows.cuh, so whichever PR lands second needs the line and the re-pin.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @ultradaoto. Nothing further is needed from you on this PR; the opt-in flag, pins and GB10 receipts are accepted.

Noted for landing: if #3776 lands first, tf_glm_combine_tail needs its glm_pdl::enter() line and a fresh pin of glm_glue_rows.cuh. This PR can land first without that change. CI on this head fails some serving-hash pins against current main; refreshing them is mechanical and we will do it ourselves before landing.

  • Synthena

ashhart pushed a commit to ultradaoto/TensorFold that referenced this pull request Nov 19, 2026
ashhart pushed a commit to ultradaoto/TensorFold that referenced this pull request Nov 19, 2026
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @ultradaoto. As promised, we rebased this onto current main ourselves and refreshed the serving-hash pins for the files it changes, in one commit of ours; your commits are as they were. It is in the landing queue now; nothing is needed from you.

  • Synthena

@AIFlowML

AIFlowML commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

I checked 22218bec with Zig 0.17.0 on M5 Max. zig build test-glm-exl3-prompt-host test-exl3-prompt-host compile-glm-native-open test-gradient-serving -Dcpu=apple_m1 -j5 --summary all passed all 13 tests and compiled the actual Linux GLM open path.

The trial merge against 124052c97 has one conflict in cuda_exl3.zig, where this PR inserts runDirect and directOut next to main's compact-helper comment. Keeping both helpers and the updated comment gave the same 13 passing tests in the scratch tree, including the serving pin. A second build of test-environment-host test-kernel-symbols-host test-kernel-contract passed all 292 host tests there. This checks the merge and build wiring; the device parity and timing data remain the posted GB10 runs.

Rechecked 27de1aa21 after adding the #3776 prologue. The trial merge into 37fa51d23 was clean. The same focused targets passed all 13 host tests and all 16 build steps, including the updated serving pins and Linux GLM open-path compile. I also verified that tf_glm_combine_tail now calls glm_pdl::enter() before its lane and bounds logic. This run did not repeat the GB10 device tests.

Igor Lessio - AIFLOW LABS

ashhart pushed a commit to ultradaoto/TensorFold that referenced this pull request Nov 21, 2026
ashhart pushed a commit to ultradaoto/TensorFold that referenced this pull request Nov 21, 2026
--
2.43.0
--
2.43.0
--
2.54.0 (Apple Git-157)
--
2.43.0
@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thanks @ultradaoto. Nothing more is needed from you on this one. TF_GLM_MOE_DIRECT stays opt-in, so the default decode path is unchanged.

Main has moved since your push, with edits next to TF_GLM_DSCORES and idol_decoder. We'll rebase it ourselves, keeping your commits as they are. Then on a GB10 we'll run check-pdl-loads, kernel-contract-limits (checking the register counts for tf_glm_topk and tf_glm_combine_tail with glm_pdl::enter() in place), and the kernel contract for both kernels, and post the results here.

  • Synthena

ashhart pushed a commit to ultradaoto/TensorFold that referenced this pull request Nov 21, 2026
@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the #4460 CI train at d5a4be76e, which carries #2239 and this PR, with #4296 at bb21321d7 applied. That combination merges cleanly and passed all 46 tests on M5 Max with Zig 0.17.0. I reran it against the newer main 1eed88bb0, keeping main's changelog in the local trial. The only conflict was in the changelog, and the second run again passed all 22 steps and all 46 tests, including the serving pins.

zig build test-qwen3-asr-host test-chunked-write test-glm-exl3-prompt-host \
  test-exl3-prompt-host test-gradient-serving compile-glm-native-open \
  -Dcpu=apple_m1 -j4 --summary all

The Linux ARM64 GLM open path compiled. The planned GB10 PDL, launch-limit and kernel checks remain device work.

Igor Lessio - AIFLOW LABS

@joshuaswarren joshuaswarren left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read at head ef86ad134 (lab-side review, joshuaswarren's fleet).

The diff matches the #3570 spec exactly, and the kernel is bit-faithful:

  • tf_glm_combine_tail is tf_glm_combine with the last slot's load pointed at tail + r * d — same slot loop order, same __fmaf_rn chain, same wts[r * slots + k] indexing including the tail's weight index. The sums are unchanged by construction.
  • direct()'s gate is tight: opt-in env only, !prompt, l.exl3 != null, top_k == exl3.top_k, rows <= chunk_rows — decode-window EXL3 only, prompts and NVFP4 untouched as specced.
  • fill's error.BadWindow on lo != 0 under direct is the right defensive check for the whole-window invariant.
  • runSlots on flat picks reuses the same grouped kernels compact() uses — the two memcpys are removed without touching expert math.
  • TF_GLM_MOE_DIRECT registered .glm/.flag (default off — opt-in first, the conservative answer to your question 1), contract records + the 375-line contract test carry the bit-equal evidence.

LGTM from this lane. CI all green at head. We can also run the SM120 single-GPU served A/B on our RTX box (TF_GLM_MOE_DIRECT=0/1 on one binary) and post it as a second receipt if a maintainer wants non-GB10 coverage.

@AIFlowML

Copy link
Copy Markdown
Contributor

The current ef86ad13 head trial-merges cleanly into d21617008 by itself. On M5 Max with Zig 0.17.0, all 46 host tests passed and the native GLM open path compiled. The same head trial-merges into 7d243251, whose delta from the tested tree is the lane-composition test and documentation.

zig build test-glm-exl3-prompt-host test-exl3-prompt-host test-environment-host \
  test-gradient-serving compile-glm-native-open \
  -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j2 --summary all

This verifies the standalone host integration, including strict environment handling and EXL3 prompt behavior. The GB10 launch limits and TP3 decode comparison remain in the device receipts.

Igor Lessio - AIFLOW LABS

ashhart pushed a commit to ultradaoto/TensorFold that referenced this pull request Nov 22, 2026
@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the updated head aefdc245cca70864355a7710f8e916eb3eb9268a on Zig 0.17.0. It trial-merges cleanly into main 15163ef5f, and #4296 at bb21321d also merges cleanly on top. The published head and the combined trial each passed all 14 focused tests with this command and compiled both the ARM64 Linux native CUDA server and the GLM open probe.

zig build test-glm-exl3-prompt-host test-exl3-prompt-host test-gradient-serving compile-glm-native-open check-native-cuda-host -Dcpu=apple_m1 -Doptimize=safe -j3 --summary all

The newest commit changes CUDA contract teardown order. That teardown still needs execution on CUDA.

Igor Lessio - AIFLOW LABS

@ashhart
ashhart merged commit 0729e04 into ashhart:main Nov 22, 2026
2 of 12 checks passed
ashhart pushed a commit that referenced this pull request Nov 22, 2026
@ashhart

ashhart commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Thank you @ultradaoto. 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 added a commit that referenced this pull request Nov 22, 2026
…fill

#3659 (TF_GLM_MOE_DIRECT) sums a direct decode window with combineTail inside
fill; this PR split fill into shared and combine. shared now stops after the
shared expert on a direct window (no slot put), and fill does the combineTail
there, so the direct path runs the same launches in the same order as on main.
Paired prompt chunks never take the direct path.
ashhart added a commit that referenced this pull request Nov 22, 2026
…fill

#3659 (TF_GLM_MOE_DIRECT) sums a direct decode window with combineTail inside
fill; this PR split fill into shared and combine. shared now stops after the
shared expert on a direct window (no slot put), and fill does the combineTail
there, so the direct path runs the same launches in the same order as on main.
Paired prompt chunks never take the direct path.
ashhart added a commit that referenced this pull request Nov 22, 2026
…fill

#3659 (TF_GLM_MOE_DIRECT) sums a direct decode window with combineTail inside
fill; this PR split fill into shared and combine. shared now stops after the
shared expert on a direct window (no slot put), and fill does the combineTail
there, so the direct path runs the same launches in the same order as on main.
Paired prompt chunks never take the direct path.
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.

4 participants