Skip to content

MoE route tap: optional TF_ROUTE_TAP trace of global top-k - #1886

Open
Cosmic-Construct wants to merge 18 commits into
ashhart:mainfrom
Cosmic-Construct:route-tap
Open

Cosmic-Construct wants to merge 18 commits into
ashhart:mainfrom
Cosmic-Construct:route-tap

Conversation

@Cosmic-Construct

Copy link
Copy Markdown

Closes the request in #1758, following the conditions in the maintainer comment there.

MoE route tap (TF_ROUTE_TAP)

Off-by-default switch for inspecting or monitoring MoE routing. Unset, the engine is unchanged (same tokens, same speed). Set to a file path, rank 0 of a tapped family writes a versioned TFR1 trace of each layer's global top-k (step, layer, ids, weights).

Families

Tapped (named, and only these): glm5_next, glm_moe_dsa, qwen3_5_moe, qwen4_exp, nemotron_h, nemotron_h_omni, minimax_m3_vl, step5_vl, lfm2_moe.

Every other model_type is refused by name while TF_ROUTE_TAP is set, including deepseek_v41 (not tapped).

Copies

  • CUDA: async D2H of the global top-k after the pick, before localisation.
  • HIP: the same, before tf_moe_localize or tf_nemo_localize.
  • Metal: blit each layer's picks (after the concurrent-encoder barrier) into a shared-storage ring in the same command buffer. Each submit records its reserved count, and the host publishes only that buffer's slots at its own wait, so in-flight buffers are never read early. The writer copies before releasing a slot; an all-zero slot is dropped, not written.

The host ring is independent of the file writer. A full ring drops the newest record. The header top_k field is reserved and stays 0; per-record n is rows × k.

Evidence

Host tests: ring wraparound, header/record round-trip, tapped names, unset no-op (zig/src/core/route_tap.zig). tools/route_tap_bench.py decode fails on any all-zero record.

GPU run, Metal only: Apple M4 Max (64 GB class), nemotron_h (Nemotron 3.5 Lightning 30B-A3B, MLX 4-bit), zig build native -Dcpu=apple_m1, 256 tokens, temp 0, 3 reps, medians.

  1. Token equality, tap on vs off: equal.
  2. Decode tok/s, variable unset vs main, alternated (a/b):
prompt main a / b tap unset a / b
fibonacci-raw 218.8 / 217.6 217.4 / 217.2
gpu-chat-no-think 203.5 / 203.9 201.6 / 202.3
  1. On-cost, tap on vs unset (aggregate tok/s), 0 failures, all outputs equal:
prompt 1 stream 8 streams
code 217.1 vs 217.2 353.1 vs 350.1
chat 204.9 vs 201.4 306.6 vs 303.7
  1. Trace: 25,448 records over steps 0–1017 decode cleanly, 0 all-zero records.

Not run: CUDA and HIP paths have not been run on GPU hardware yet; GPU_RUNBOOK in docs/ has the checklist. Happy to add those receipts when available.

Docs

docs/route-tap.md. TF_ROUTE_TAP is listed in capabilities --json.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks @Cosmic-Construct, this is a careful design, and it follows #1758's conditions closely. We read it through. With TF_ROUTE_TAP unset nothing changes on any path: no extra copy, sync, allocation or graph node. That's the part we care about most. With the tap on, a few things need fixing before it lands:

  1. CUDA and HIP graph capture. The per-layer device-to-host copies run in host code that the decode graph capture also runs (cuda/graph.zig beginCapture). Copying into a pageable ring inside a capture invalidates it. A pinned ring wouldn't fix it alone either: a replayed graph writes the same captured slot every step, with no host bookkeeping. While the tap is live, please run tapped families without graphs (or refuse graphs by name, and say so in docs/route-tap.md). Please also use a pinned ring (cuMemHostAlloc / hipHostMalloc), so the copies don't wait twice per layer.
  2. Failed copies. reserve writes the step and layer before the copy, and the copy's error is swallowed. After the ring's first lap, a failed copy writes the previous record's ids under the new step and layer, and it looks valid. Please clear the slot or count it as a drop, and report the drops at stop.
  3. Publishing. publish runs after any stream's synchronize and marks every reserved slot readable. Please publish per step, after an event recorded behind that step's copies.
  4. Backends with no copy. XPU nemotron_h, Metal Flash Next and Metal Qwen3.5 MoE are admitted but never copy, so they write an empty trace without an error. Please have admit refuse those family and backend pairs by name.
  5. Metal barrier. Nemotron adds a barrier before the copy in a concurrent encoder. GLM, GLM-DSA, Step5, MiniMax and LFM2 don't, so the copy can read the previous layer's picks. Please add the same barrier, and show tap-on replies equal to tap-off for each of them.
  6. XPU build. build/xpu.zig seems to give tokenizer.zig two modules, one for XPU's core and the standalone one for the server. Please make one and pass it to both, as build/cuda.zig now does.

It also conflicts with main now (the CHANGELOG and a few one-line hook-ups). A rebase onto current main is fine whenever you push the fixes. You can leave the CHANGELOG to us; we add the line at landing. Once it's in, we'll run it on a GB10.

@Cosmic-Construct

Copy link
Copy Markdown
Author

Thanks for the review. I've rebased the branch onto current main (9bb51cd) and it's five commits now. The fourth (MoE route tap: graphs off while live, pinned ring, named refusals) covers your six points, in your order. The fifth (MoE route tap: free the ring on every Metal family close) fixes a leak I found while testing, described below.

  1. CUDA/HIP graph capture and the pinned ring. While TF_ROUTE_TAP is live, the tapped CUDA/HIP families run without graphs, and the copies go into a cuMemHostAlloc/hipHostMalloc ring. With the variable unset, graph use doesn't change.
  2. Failed copies. A failed copy clears its slot, counts as a drop, and the drop count is logged when the tap stops.
  3. Publish. Each step is published after an event that waits on that step's copies.
  4. Refusals. admit refuses XPU nemotron_h, Metal Flash Next (qwen4_exp) and Metal Qwen3.5 MoE (qwen3_5_moe) by name, and docs/route-tap.md lists them.
  5. Metal barrier. GLM, GLM-DSA, Step5, MiniMax and LFM2 now barrier before the blit. tools/run_metal_proofs.py checks that tap-on equals tap-off for each one.
  6. XPU tokenizer. It's one shared module now.

I've left CHANGELOG to you.

Hardware checks (Apple M4 Max, 64 GB; zig build native -Dcpu=apple_m1, Zig 0.17.0; clean build):

  • LFM2 (LiquidAI/LFM2.5-24B-A2B-MLX-4bit), run_metal_proofs.py run --family lfm2_moe:
    • Tap-on equals tap-off: 64 tokens, same reply sha c79d8207ed3e.
    • The trace has 6,308 records over 166 steps × 38 layers, top-4, with 0 all-zero records.
    • Decode was 191.2 tok/s tap-off and 185.2 tok/s tap-on (one request each).
    • The first LFM2 run showed a leak. On shutdown, the tap-on server's SafeAllocator reported 4 leaked blocks (984 B, 1 KiB, 2 × 2 MiB). The cause was that the LFM2, GLM-DSA and MiniMax Metal host close paths never called route_tap.stop(). Commit 5 adds that call and frees the ring on every Metal family close. After the fix, the LFM2 rerun is clean: the leak checker reports nothing, and the replies and trace are unchanged.
  • Nemotron 3.5 Lightning 30B-A3B MLX 4-bit (rerun on all five commits, to check for regressions):
    • Tap-on equals tap-off, and the token SHAs match.
    • The trace has 25,448 records over 1,018 steps × 50 layers, with 0 all-zero records.
    • Tap cost, unset vs on, in aggregate tok/s, is within noise:
      • code, 1 stream: 217.8 vs 218.4
      • code, 8 streams: 354.6 vs 351.4
      • chat, 1 stream: 202.9 vs 202.9
      • chat, 8 streams: 302.3 vs 300.8
    • None of the server logs reports a leak.
  • Not run: GLM-5.3-Flash, GLM-DSA, Step-5 and MiniMax-M3.1. Their checkpoints aren't on this Mac, and their recipes target 256 GB Macs or two-Mac setups. Commit 5 also covers the GLM-DSA and MiniMax close paths, but neither has been run here. I only built Metal here, so the CUDA, HIP and XPU changes haven't been built or run on this host.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks @Cosmic-Construct. That's all six, in order, plus a leak you found yourself. We're now running what your Mac couldn't, on a GB10 (CUDA, Nemotron 3.5 Lightning 30B-A3B): main against this head with TF_ROUTE_TAP unset (prompt and stream sweeps), and with it set (tokens must match main's, and it measures the tap's cost with graphs off). We'll post the numbers here, and CI covers the CUDA, HIP and XPU builds. We'll write the changelog line.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

@Cosmic-Construct, our runs on hardware you don't have are in. Two things need fixing before this lands.

1. With TF_ROUTE_TAP unset, LFM2 on Metal is slower. We ran LiquidAI/LFM2.5-24B-A2B-MLX-4bit on an M5 Ultra, main (9bb51cd63) against this head, as two jobs in opposite orders (base-new-new-base, then new-base-base-new). The tokens are identical. The stream sweeps lose in both jobs, aggregate tok/s:

cell main this PR change
chat, T 0, 1 stream 240.4 221.6 −7.8%
chat, T 0, 2 streams 425.2 382.6 −10.0%
code, T 0, 2 streams 474.4 427.1 −10.0%
code, T 0, 4 streams 663.5 631.0 −4.9%
code, T 1, 1 stream 263.4 243.6 −7.5%
code, T 1, 4 streams 515.7 484.5 −6.1%

(These are from the second job. In the first, the PR's runs sat at 219-220 at one stream and three of main's four runs at 239-241.)

The tap-off path should cost nothing. Our guess is the barrier before the blit you added for LFM2 (and GLM, GLM-DSA, Step5 and MiniMax) runs whether or not the tap is live. Could you make it conditional on a live tap?

2. With the tap set, CUDA Nemotron doesn't load. This was Nemotron 3.5 Lightning 30B-A3B MLX 4-bit on a GB10, TF_ROUTE_TAP=<file>. The server refuses with the native CUDA engine cannot load … with its own glue kernels (GraphNotPermitted), then reports leaks on the way out: 1,024 B, 1,256 B and two 2 MiB blocks. That's the same signature as the Metal close-path leak you fixed. So on CUDA:

  • the graphs-off path isn't accepted by Nemotron's glue kernels;
  • a failed open doesn't call route_tap.stop(), so the ring isn't freed.

Our tap-unset CUDA A/B is still queued. We'll post it when it's in.

@Cosmic-Construct

Copy link
Copy Markdown
Author

Thanks for both reports. The fixes are on this head (86726bb7d, sixth commit on 9bb51cd); the Mac numbers below were measured on the same change before it was committed. CHANGELOG is still yours.

1. Tap-off Metal regression. The blit barrier was already behind live() at the family sites, but afterWait / afterLand / reservedCount still ran after every wait, CUDA/HIP land still ran after every stream synchronize, and afterPick / afterSelect were still called. Those are all gated on live() now. The barrier itself lives only inside the blit, which returns before any encoder work when the tap is off, so the tap-off command-buffer path should be the same work as main.

held() now takes a typed api.Opened{ ... } in the Metal native open (native/metal.zig) and the DeepSeek CUDA entry (deepseek_v41/serve_engine.zig) (the untyped .{ ... } literal did not compile on Metal: inferred anonymous struct, not !?api.Opened).

M4 Max tap-unset A/B of main 9bb51cd63 vs this head is within ~2% on all six cells, tokens equal:

cell main v6
chat T0 1 stream 187.9 185.0
chat T0 2 streams 309.7 309.1
code T0 2 streams 335.9 336.2
code T0 4 streams 430.8 429.4
code T1 1 stream 182.3 181.3
code T1 4 streams 335.6 334.3

v5 also matched main on this M4 Max, so the M5 Ultra 5–10% regression does not reproduce here. Could you rerun main vs this head on the M5 Ultra?

2. CUDA Nemotron GraphNotPermitted, and the leak on the way out. The real fix, not a named refusal. Engine.init already skipped capture while the tap was live, but cuda_native.zig still captured the MTP head and the sampled/greedy windows afterwards, and beginCapture raised GraphNotPermitted. Those captures are skipped when the tap is live; Head.launch and verifyMasked already fall back to eager glue when no graph was captured. beginCapture still refuses a live tap if something else tries to start one.

Every failed native open after admit now stops (CUDA, HIP, XPU, Metal, and the DeepSeek CUDA entry), so the pinned ring and records are freed. A testing-allocator test covers a failed open with the tap set.

Tap-on on this M4 Max: LFM2 13,604 records, 0 all-zero; Nemotron 3,095 records, 0 all-zero; tokens equal; no leaks. Decode counts from the TFR1 records list (it was looking for n_records and printing null).

Host checks here (logs not attached): zig test zig/src/core/route_tap.zig -lc (11/11, including the new failed-open test). zig build test-cuda-host compiled the CUDA native/open paths (4325 tests passed; two unrelated C compile steps failed on this VM's clang/libstdc++ warning). CUDA Nemotron load with TF_ROUTE_TAP set is still only confirmable on a GB10.

I've left CHANGELOG to you.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

@Cosmic-Construct, the CUDA tap-off run is in: Nemotron 3.5 Lightning 30B-A3B MLX 4-bit on a GB10, main against this head with TF_ROUTE_TAP unset, prompt and sweep cells. It's GREEN: tokens identical in every row, and no cell slower. So on CUDA the unset path costs nothing. The two items from our last comment still stand: the LFM2 Metal sweeps slower with the tap unset, and CUDA Nemotron refusing to load with the tap set.

@ashhart

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Thanks @Cosmic-Construct. An independent review on current main (d38bba3):
Nothing was built or run here.

Verdict: CHANGES (four). Unset, the tap is main: every hot-path site is one global load and a skipped
branch. But the PR doesn't apply on current main, and the trace it writes is wrong in three ways when it's
on:

  • GLM on Metal loses almost every decode record;
  • layer numbers shift after a drop;
  • records can't be decoded into rows.

Applying it

  • Commits 2, 3 and 5 apply cleanly. Commits 1, 4 and 6 fail 7 hunks in 5 files, all build wiring or one
    moved call site:
    • zig/build/cuda.zig and zig/build/cuda_runtime.zig: main now builds the runtime through runtimeFor.
    • zig/build/xpu.zig (three hunks): main's XPU core already adds gpu_memory.
    • zig/src/hip/moe.zig: main moved expertParts' tail.
    • zig/src/native/xpu.zig: the openQwen27 return.
  • Lean: with the other hunks applied, 427 against main's 424. The three new problems:
    • core/route_tap.zig is 623 lines, past the 600-line split;
    • its two-line //! header counts as a multi-line comment;
    • families/nemotron/backend.zig grows to 603 lines.
  • Author: @Cosmic-Construct's noreply address on all six commits.

What holds

Unset is main.

  • live() is g_session != null. g_session is set only by start, which returns at once unless
    TF_ROUTE_TAP is non-empty, rank is 0 and the family is tapped.
  • Every hot-path site is guarded by live():
    • CUDA: moe.zig, moe_glue.zig and route_rows.zig after the pick, glm/cuda_moe.zig and
      minimax_m3/cuda_forward.zig, and Stream.synchronize;
    • HIP: moe.zig, moe_nemotron.zig and Stream.synchronize;
    • Metal: every blit and blitPicks, and each family's wait.
  • With the tap off, there's no copy, event, barrier, launch, allocation or thread. getenv is read only at
    admit and start, at open.
  • Refactors that run with the tap off:
    • moe_glue.select's if (warp) return launch(...) becomes an if/else with try. That's the same
      launches and the same error.
    • HIP Nemotron's rows == 1 and whole branch is split so the tap can sit between select and localise. The
      unset path runs the same calls in the same order; I checked line by line against main.
    • GLM Metal's return moe_route.select(...) becomes a call then return. select returns void.
    • The Flight structs gain tap_at: u32 = 0. It's written only when live.
  • Graphs: graph capture checks live() and keeps today's capture when it's false. That covers CUDA's
    beginCapture and graph cache, HIP's beginCapture, Qwen 3.5 CUDA and HIP, Flash Next CUDA and HIP, and
    Nemotron CUDA, MTP and HIP.
  • Open and close: held(&guard, try open(...)) returns the same value or error as before. The guard's
    stop is a no-op when nothing started.
  • The refusal is only while set. With TF_ROUTE_TAP set, an untapped family is refused by name at open,
    deepseek_v41 included. So are three pairs that never copy: XPU nemotron_h, Metal qwen4_exp and Metal
    qwen3_5_moe.
  • Flash Next (facts only): qwen4_exp is tapped on CUDA and HIP. When live, cuda_round_graph.run runs
    the pass eagerly instead of capturing, and HIP sets graphed = false. Metal is refused.
  • Kimi: no Kimi file changes.

Where it writes.

  • Only rank 0 opens the file, so TP ranks don't collide. Every rank already picks the same global top-k.
  • The path is the operator's variable: absolute, or relative to the working directory. createFile truncates
    an existing file, follows a symlink and uses the default mode under the umask. There's no format string or
    shell in the path's handling.
  • Format: a 16-byte header (TFR1, version 1, flags 1, top_k 0), then one record per tapped call. Each
    record is step u32, layer u16 and n u16, then n i32 ids and n f32 weights: 8 + 8n bytes.
    • At one decode row and k = 8 (+1 shared slot where the site includes it), that's 80 bytes a layer.
    • A token costs 80 bytes per MoE layer; GLM-5.3-Flash's 42 MoE layers make about 3.4 KB per token.
  • Memory when live: a 128-slot ring of 4,096 ids and weights.
    • CUDA and HIP adopt a 4 MiB pinned pair in place of the 4 MiB allocated one.
    • Metal adds a 4 MiB shared buffer and one compiled copy kernel.
    • A full ring drops the newest record and counts it, and stop logs the count.

Posted evidence (Metal only). An M4 Max running nemotron_h:

  • tap on against off: equal tokens;
  • unset against main: within 1% at 1 stream;
  • 25,448 records with no all-zero ones.

CUDA and HIP haven't run on GPU.

Changes

1. Rebase on current main and prove one core per binary.

  • The 7 failed hunks are mechanical. But they carry the one load-bearing part of the build change. CUDA's
    runtime module and every family must import the same core module, or:
    • Zig 0.17 refuses route_tap.zig in two modules;
    • or, wired another way, the runtime's live() reads a second g_session that is never set, and CUDA
      traces come out empty.
  • Ask: rebase on d38bba3, then post builds for the CUDA -Dgpu product, HIP, XPU and Metal, plus a
    host test. The host test should call route_tap.start through the engine's core and assert that
    cuda.route_tap_copy sees live().

2. GLM on Metal: plain decode rounds never publish.

  • glm/plain_rounds.zig is the default single-stream decode with no drafts. It commits each round before
    waiting on the last, and waits in waitRound, which calls neither afterWait nor afterLand. Only
    Engine.finishBy publishes.
  • So decode records stay reserved. After 128 of them (about 3 tokens at GLM-5.3-Flash's 42 MoE layers),
    reserve drops every later layer until the next finishBy.
  • afterWait also publishes every slot reserved so far, not just the waited buffer's. In GLM's ahead-committed
    paths (slots.zig's launch and its ring windows), that can include a buffer still in flight:
    • on the first lap, its slot is still zero and is dropped as blank;
    • on later laps, it still holds the previous lap's ids, which are written under the new header.
  • Nemotron and MiniMax already record tap_at per committed buffer and publish at its own wait. The docs
    say every Metal family does.
  • Ask: record the reserved count when each buffer is committed, in every tapped Metal family, and publish
    exactly that at its wait, waitRound and Slots.land included. Post the tap-on GLM record count:
    generated tokens × MoE layers, with zero drops.

3. Layer numbers shift after a drop.

  • No family calls setLayer. A record's "layer" is the count of tapped calls since the last publish.
  • reserve returns null on a full ring before it advances that count. So after one drop, every later record
    in the step carries the previous layer's number.
  • "Step" counts publishes. On CUDA and HIP, land runs at every Stream.synchronize, so a mid-pass
    synchronize starts a new step, and its layers restart at 0.
  • Ask: pass the model's layer index from each call site, or at least advance the count when a record is
    dropped. Document what a step is (one publish: a batched pass or prompt chunk, not a token). Add a host test
    that a drop leaves later layer numbers right.

4. Records can't be split into rows, and long ones are cut silently.

  • n is rows × slots, but the record carries neither, and the header's top_k is 0.
  • The slot count differs by site for the same family:
    • CUDA moe.zig and GLM CUDA copy top_k + 1 (the shared slot);
    • moe_glue copies topk + shared_slots;
    • MiniMax CUDA and every Metal site copy rows × topk.
  • n is capped at 4,096 with no flag. A 2,048-row prompt chunk at k = 8 keeps its first 455 rows (or 512,
    depending on the site) and says nothing.
  • The format is new, so this is cheap now.
  • Ask: add rows u16, slots u8 and a truncated flag to each record (version 1 isn't released), with the
    shared slot either always or never included. Add the fields to route_tap_bench.py decode and its tests.

Small points (not blocking)

  1. Write failures are silent. writeStreamingAll ... catch {} drops records on a full disk without
    counting them. Count them with the drops.
  2. A failed event goes unwaited. If Event.init fails, land publishes without waiting, so the writer
    can read a copy still landing. Refuse the tap at pin instead.
  3. A failed pin is retried per layer. A failed HostBuffer.alloc leaves the ring pageable, so the copies
    go synchronous, and pin retries on every layer. Try once.
  4. GPU on-cost is unmeasured. Graphs are off while live on CUDA and HIP, so the posted Metal on-cost says
    nothing about those backends. The docs should say tap-on CUDA and HIP decode runs without graphs.
  5. The Metal proofs aren't posted. docs/route-tap.md lists Metal proofs for GLM, GLM-DSA, Step 5,
    MiniMax and LFM2. Only Nemotron's is posted.
  6. The tests can't fail on main. They all test the new module. The "unset is a no-op" test can't fail, so
    unset-equals-main rests on the device A/B below.

Runs needed

  1. Nemotron 3.5 Lightning 30B-A3B NVFP4 on a GB10, CUDA:
    • unset against main: greedy and seeded token_sha256 and decode tok/s, with graphs on, at 1 and 8 streams;
    • tap on: equal tokens and a non-empty trace with zero drops.
  2. On a gfx1201, HIP: the same with Qwen 3.5 MoE, plus Nemotron, at TP=2, to show only rank 0 writes.
  3. GLM-5.3-Flash 4-bit on an Apple Silicon Mac, Metal, after change 2:
    • tap on against off: equal tokens over 256 tokens;
    • record count = tokens × MoE layers, with zero drops.

@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Hi @Cosmic-Construct, checking in on the route tap. With the tap unset it matches main, which is the part we cared about most, and thanks again for chasing down the close-path leak yourself. Our 3 Nov review lists what's left:

  • a rebase onto main, with builds showing one core module per binary;
  • GLM on Metal publishing each buffer's records at its own wait;
  • layer numbers that stay right after a drop;
  • records that decode into rows.

route_tap.zig and Nemotron's backend.zig also need to come back under 600 lines. Once that's pushed, we'll rerun the LFM2 tap-unset comparison on an M5 Ultra that you asked for. If you'd like us to take any part of it, just say so.

ashhart added a commit to Cosmic-Construct/TensorFold that referenced this pull request Nov 23, 2026
… ring storage

Keep the session and its tests in route_tap.zig; move unchanged wire encoding and ring storage into a bounded module. Preserve main’s existing Nemotron prompt-flight extraction, which already keeps backend.zig below the inclusive600-line cap.
ashhart added a commit to Cosmic-Construct/TensorFold that referenced this pull request Nov 23, 2026
…pped ordinal asks

Capture each Metal command buffer reservation bound and publish at its own wait, including GLM plain rounds, launched slots, ring windows and the extracted Nemotron prompt-flight owner. Tap GLM chunked routing too, and let the existing GLM device runner open a session for its record-count gate.

Keep original row dimensions and whole routed rows, flag truncation, and omit shared slots consistently in strided CUDA/HIP captures. Advance ordinals on refused reservations. Add real engine/CUDA session identity and pending-buffer host regressions, decoder cases and updated wire-format documentation. Fit host-only XPU bindings to the same core; retain the separate file-split commit.
ashhart added a commit to Cosmic-Construct/TensorFold that referenced this pull request Nov 23, 2026
Read the path through the shared platform environment and register its path rule. Validate before opening the writer so malformed control characters report an open error; unset capture stays inactive.
ashhart added a commit to Cosmic-Construct/TensorFold that referenced this pull request Nov 23, 2026
Use single-line module docstrings and point at the route-tap device gates. Tool behavior and every Zig source stay unchanged.
ashhart added a commit to Cosmic-Construct/TensorFold that referenced this pull request Nov 23, 2026
Add a valid trace path to the existing all-rules table. The first environment run correctly refused the missing fixture entry; keep that receipt and rerun the named target with the coverage row supplied.
ashhart added a commit to Cosmic-Construct/TensorFold that referenced this pull request Nov 23, 2026
Cosmic-Construct and others added 18 commits November 23, 2026 19:18
Unset, the tap is inert. Set to a path, rank 0 of a tapped family writes
TFR1 records (step, layer, ids, weights) from a host ring after the
existing step wait. CUDA and HIP copy before localisation; Metal blits
each layer in the same command buffer. DeepSeek V4.1 and every other
untapped family refuse the switch by name.
Record fields stay step, layer, ids and weights. The sample is a format
fixture. The bench receipt names only host RAM class and GPU model.
nemotron_h on HIP rewrites picks in tf_nemo_localize, not tf_moe_localize.
Tap after select on both whole-layer and sharded paths so rank 0 records
global ids. Untapped families, including deepseek_v41, still refuse by name.
While the tap is live, tapped CUDA/HIP families run without graphs and
copy into a cuMemHostAlloc/hipHostMalloc ring. A failed copy clears its
slot and counts as a drop (logged at stop). Publish is per step, after
an event behind that step's copies. admit refuses XPU nemotron_h, Metal
Flash Next and Metal Qwen3.5 MoE by name. Metal GLM, GLM-DSA, Step5,
MiniMax and LFM2 barrier before the blit. XPU tokenizer is one shared
module.
admit starts the GPA ring before family hosts open. LFM2, GLM-DSA and
MiniMax each have their own Host.close and never called stop(), so the
session and both 2 MiB slabs leaked at shutdown. Nemotron, GLM and
Step5 already stopped. stop() also runs the Metal detach callback.
A testing-allocator test covers start/stop.
… open

The blit barrier, afterWait/afterLand, reservedCount and CUDA/HIP land
run only while the tap is live, so tap-off records the same work as main.
CUDA Nemotron skips MTP and window capture when the tap is live; glue
kernels run eagerly instead of GraphNotPermitted. Failed native opens
call stop so the ring is freed. A testing-allocator test covers that path.
held() takes api.Opened at the Metal Nemotron and DeepSeek sites so the
return type is !?api.Opened. Decode counts TFR1 records from the records
list.
… ring storage

Keep the session and its tests in route_tap.zig; move unchanged wire encoding and ring storage into a bounded module. Preserve main’s existing Nemotron prompt-flight extraction, which already keeps backend.zig below the inclusive600-line cap.
…pped ordinal asks

Capture each Metal command buffer reservation bound and publish at its own wait, including GLM plain rounds, launched slots, ring windows and the extracted Nemotron prompt-flight owner. Tap GLM chunked routing too, and let the existing GLM device runner open a session for its record-count gate.

Keep original row dimensions and whole routed rows, flag truncation, and omit shared slots consistently in strided CUDA/HIP captures. Advance ordinals on refused reservations. Add real engine/CUDA session identity and pending-buffer host regressions, decoder cases and updated wire-format documentation. Fit host-only XPU bindings to the same core; retain the separate file-split commit.
Read the path through the shared platform environment and register its path rule. Validate before opening the writer so malformed control characters report an open error; unset capture stays inactive.
Use single-line module docstrings and point at the route-tap device gates. Tool behavior and every Zig source stay unchanged.
Add a valid trace path to the existing all-rules table. The first environment run correctly refused the missing fixture entry; keep that receipt and rerun the named target with the coverage row supplied.
Follow main 5ee83f2 (GLM DFlash2 prompt integration) and 4b997f8 (current Metal native layout). Remove only two and four blank lines from the merged GLM prompt and native Metal modules. Retain all main branches, readers, assertions, scheduling and incoming optional route-tap lifetime hooks; source tokens are unchanged.
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @Cosmic-Construct. The latest tree still has four route-tap correctness issues:

  1. CUDA/HIP close can drain freed buffers. core/route_tap.zig:169-181 clears g_session and invokes the backend release callback before publishing, stopping, and joining the writer. The callbacks in cuda/route_tap_copy.zig:28-44 and hip/route_tap_copy.zig:29-45 call publishTo after the session is gone, then free the adopted pinned slices. The writer subsequently pops records through those dangling slices. Please synchronize and publish outstanding copies while the session exists, stop and join the writer, and only then free the pinned buffers. Add a close regression with an outstanding copy whose writer drain is delayed.

  2. Metal LFM2 copies bf16 weights as fp32. families/lfm2_moe/state.zig:197 allocates two bytes per route weight, but core/route_tap_metal.zig:6-10,72-88 copies one uint per weight into fp32 storage. With TF_ROUTE_TAP, pairs of bf16 values become bogus fp32 values and the final copy reads beyond the source buffer. Please convert bf16 to fp32 with the correct element width (or provide an fp32 staging buffer) before publishing; a raw 32-bit copy is not valid here.

  3. CUDA LFM2 is admitted but emits no layer records. families/lfm2_moe/cuda_forward.zig:127-135 routes through its private Kernels.routeRows, whose implementation at cuda_kernels.zig:128-136 has no route-tap call. CUDA lfm2_moe is not refused by neverCopies, so the trace can contain only its header. Please copy its picks and fp32 weights after routing and before plan.route localizes the ids, or explicitly refuse this backend/family before creating the file.

  4. Captured Triton Nemotron bypasses the copy hook. families/nemotron/cuda_glue.zig:190-198 returns directly through t.route; only the own-glue branch reaches moe_glue.topk, whose copy hook is at cuda/moe_glue.zig:81-82. Please give the captured route the same pre-localization copy semantics as the own-glue route, with a captured-Triton dispatch regression.

  • Synthena

This branch has not been deployed

No deployments
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.

3 participants