GLM-5.3-Flash EXL3 on 2x DGX Spark with TensorFold
Python
Patch 0108: faster decode at three Sparks - a decode window's latent attention chunk pass as a CUDA kernel with the Triton kernel's exact instruction sequence
BadAd84538e4fceec15
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 54a3f69..4bcc6da 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -3,6 +3,19 @@Every change to this recipe, newest first. Each release names the image it serves: `scripts/prepare.sh` pulls`ghcr.io/miaai-lab/glm-5.3-flash-exl3-2x-dgx-sparks-tensorfold` by the digest pinned in `scripts/config.sh`.+## Unreleased++- **A decode window's latent attention in CUDA at three Sparks** (patch `0108-glm-seg-chunks-cuda`, by+ [BadAd84](https://github.com/BadAd84)): `seg_chunks.cu` computes every chunk partial of `_seg_chunks` with the Triton kernel's own+ instruction sequence (patch 0106's chains, in `_seg_chunks`' kWidth-4 dot layout and p-sum tree; key+ tiles past a chunk's last key, which Triton runs fully masked, are skipped): the same bits under Triton 3.7, and+ Triton's `_merge` reads them as before. FP8 arenas with 17-24 heads a rank (22 / 21 at TP=3); 32 heads+ keep Triton. One GB10, the whole call at 4 streams x 8 rows at 596k: 242.5 -> 94.8 us a call (x11 layers a round). Live on three Sparks:+ real code / prose rounds 46.90 / 37.40 -> 46.35 / 37.00 ms. Built when the engine starts; under another+ Triton release (`sparse.TRITON_PTX`), or when the build fails, the Triton kernel serves (logged once).+ `TF_GLM_SEG_CHUNKS_CUDA=0`: Triton. Applies after `0104`, `0105` and `0106`. GPU check:+ `tools/seg_chunks_check.py`.+## v1.10 (2026-10-08): the effort line at the tail (opt-in), a kept-state share, and a checked second railImage: `v0.6.0-a1897d591f70` (`sha256:bc34d7d63f978cf601f42863b284bc95a567c50c10e9adb0866a635be568bf5f`), 96 patches (v1.9.1's plus `0096`, `0097`). Tested live on twodiff --git a/CREDITS.md b/CREDITS.mdindex 521996e..8b2b0d7 100644--- a/CREDITS.md+++ b/CREDITS.md@@ -139,6 +139,7 @@ checkpoint's ShapleyMcg attribution and the drafter's license).[kindling spark-os](https://github.com/kindlingai/kindling-spark-os) by Matt Mastracci (Kindling AI). Nothing ofdispram ships here: the ranks import the host's client (`python/dispram.py`, GPL-3.0 with a bundling exception)mounted read-only at run time and talk to `dispramd` (AGPL-3.0) over its socket.+- `0108-glm-seg-chunks-cuda` (a decode window's latent attention chunk pass in CUDA): by [BadAd84](https://github.com/BadAd84).- `0090-glm-expert-launch-order` (`TF_GLM_EXL3_DEC_ORDER`: the launch order of the expert decode kernel's blocks): byLukasz Raczylo ([lukaszraczylo](https://github.com/lukaszraczylo)), in[PR #95](https://github.com/MiaAI-Lab/GLM-5.3-Flash-EXL3-2x-DGX-Sparks-TensorFold/pull/95).diff --git a/README.md b/README.mdindex 2bace9c..513b84e 100644--- a/README.md+++ b/README.md@@ -522,6 +522,7 @@ sets a value wins: the environment, then `scripts/local.sh`, then `.env`, then t| `TF_ROCE_WAIT_S` | `300` | seconds a RoCE all-gather waits for the other Spark before it fails (1 to 3600; the patch's own default is 20, which a late peer on long prompts outlasted, issue #54); until the engine is built, each gather first meets the peer in an NCCL barrier, so a long first start (kernels compiling) does not run into it || `TF_GLM_MULTI_WINDOW` | `32`, `64` past 4 requests | with `PARALLEL` above 1, the rows of every request's verify window together in a round (16 to 64 in steps of 8): with more requests at once each one's drafts get fewer of them (8 requests' code: 141.2 / 160.3 / 167.0 tok/s at 32 / 48 / 64 rows); past 32 rows `TF_ROCE_MAX_KB` rises with it (64: 1024) unless you set it. Exact at any size || `TF_GLM_MULTI_LONE` | `0` | `1`: with `PARALLEL` above 1, a request decoding alone runs on the one-stream graphs (+0.6-0.9%), but its move to the pool's first rows evicts other conversations' kept prompts there (issues #12, #13); `0`: on the batched ones |+| `TF_GLM_SEG_CHUNKS_CUDA` | `1` | patch 0108: a decode window's latent attention chunk pass from `seg_chunks.cu` (the same bits as the Triton kernel under Triton 3.7, whose PTX it reproduces) on FP8 arenas with 17-24 heads a rank (three Sparks; two keep Triton), built when the engine starts; `0`: the Triton kernel, which also serves (logged once) under another Triton release or when the build fails || `TF_GLM_CACHE_ENTRIES` | `32` | kept prompt states at most (TensorFold's default is 8); past this count a conversation's earlier (superseded) states go first, then the least recently used, however much of the pool is free (patch `0063`, @Alexbob0); shared-prefix states (a system block) go by recency only, so a new run of an agent still finds its system prompt (patch `0077`, issue #75, @meleesciony). An agent request keeps 1 to 3 (issue #17); each costs ~45 MiB at start, reserved out of the pool's `KV_POOL_GIB` budget (the pool shrinks by it, the estimate does not change), flat in the context length. Symptom of too few: `/health` `kept_prompts` stuck at the count while `pool_free_tokens` is large (issue #84, @jdecker76); see `TF_GLM_CACHE_SHARE_PCT` || `TF_GLM_CACHE_SHARE_PCT` | `0` (off) | make the kept-state count at least this share of the `KV_POOL_GIB` budget (patch `0097`, issue #84 by @jdecker76): on two Sparks at the defaults each state reserves ~91 MiB of a ~7.5 GiB budget, so 32 states already take ~38%: `50` gives ~42 states, the ceiling ~63 (each costs pool tokens), so the pool's tokens, not 32 finished one-shot conversations, decide when a live conversation goes. Any count is cut to three quarters of the budget (logged at start): past that the reservation would exceed the estimate. Set the same on every Spark || `TF_GLM_KEPT_BYTES_GIB` | `0` (no cap) | a byte budget for the kept states together (patch `0089`, by Thomas Wade, [ThomasWadeZ](https://github.com/ThomasWadeZ), PR #65): the device memory they own outside the pool (recurrent state, DFlash2 window copies: hundreds of MB each with long contexts); past it states are dropped in the entry cap's order (not written to the spill tier) and freed blocks go back to the driver. The author runs `4`; `kept_bytes` / `kept_bytes_cap` in `/health` |@@ -635,6 +636,7 @@ applied with `patch -p0` in filename order); `start.sh` rebuilds or re-pulls the| Decode, memory | `0090-glm-expert-launch-order` | the decode expert kernel's blocks launched so that the eight 128-column blocks of one k slice run together and read contiguous memory (`TF_GLM_EXL3_DEC_ORDER=1` or `2`; default `0`, the old order), by Lukasz Raczylo ([lukaszraczylo](https://github.com/lukaszraczylo)) | the author's run: one request +2.2% tok/s (4 at once noisy); the same bits for every value; not yet measured here || Decode, indexer | `0043-glm-visible-pools` | a decode row's token scoring and split selection bounded to the pools it can see (TensorFold v0.6.0 bounds its one-program selection, PR #140) | the same tokens || Decode, indexer | `0085-glm-select-split-loop` | the split selection's last pass sums the histograms of the chunks before its own, 16 at a time (`SPLIT_BLK`), instead of loading the row's whole chunk table masked to them: that table is the selection scratch's, sized for the pool (512 chunks a row at `PARALLEL=4` on three Sparks), not the row's context (by [BadAd84](https://github.com/BadAd84)) | one request at 226k: the selection 5.79 -> 0.28 ms a layer (11 a round); the same tokens |+| Decode, attention | `0108-glm-seg-chunks-cuda` | a decode window's latent attention chunk pass as a CUDA kernel (`seg_chunks.cu`) with the Triton kernel's own instruction sequence (patch 0106's chains in `_seg_chunks`' kWidth-4 layout, read under Triton 3.7, `sparse.TRITON_PTX`), for FP8 arenas and 17-24 heads a rank (TP=3); `_merge` unchanged; built at engine start (`TF_GLM_SEG_CHUNKS_CUDA`); under another Triton release, or when the build fails, the Triton kernel serves (by [BadAd84](https://github.com/BadAd84)) | TP=3: 4 x 8 rows at 596k 242.5 -> 94.8 us a call (one GB10); real code / prose rounds -1.1 / -0.9%; the same bits (Triton 3.7) || Link | `0006-cuda-roce-allgather`, `0052-cuda-roce-startup` | the small all-gathers as one-shot RDMA writes over RoCE (`COMM=roce`; up to 512 KiB, `TF_ROCE_MAX_KB`: code at 4 streams +1.4%); until the engine is built, each eager gather first waits for the peer in an NCCL barrier, and the RoCE kernel builds during setup (`TF_ROCE_WAIT_S`, 300 s here; errors print the proxy's counters); an idle rank 1 waiting on a socket is TensorFold v0.6.0's (#132) | 11 us a 16 KiB gather against NCCL's 45, decode +6%; an idle server holds no GPU or CPU core; a first start whose ranks drift apart while building kernels no longer fails || Drafts | `0007-glm-copy-drafts`, `0032-glm-code-copy-drafts`, `0018-glm-noise-policies`, `0021-glm-dflash-policy-env`, `0025-glm-dflash2-ring` | copy (prompt-lookup) drafts ahead of DFlash2's (`COPY`, `COPY_CODE`); DFlash2 stop rules aware of the sampling noise (`DRAFT_POLICY`); kept prompt states' DFlash2 window with shared prefixes (the ring itself and `TF_GLM_MTP` are TensorFold v0.6.0's; the recipe sets `TF_GLM_MTP=auto`, so the MTP head is not loaded when DFlash2 drafts) | `COPY`: quote / edit replies 80.3 -> 84.4 tok/s; `COPY_CODE`: code 55.4 -> 56.2, edit 120 -> 125; `DRAFT_POLICY` over `fc5:0.3`: prose 47.2 -> 50.6 tok/s, code 51.7 -> 55.5; memory for the window || Drafts, tooling | `0011-glm-draft-sim` | records of DFlash2's drafts for an offline simulator of stop rules (`TF_GLM_DRAFT_DUMP`, off) | how the stop rules were tuned |@@ -711,6 +713,7 @@ with [sparkDash](https://github.com/MiaAI-Lab/sparkDash) ([Performance](#perform| `tools/needle.py [label] [size]` | hides a passphrase in a ~195k-token prompt (the prompt comes out at ~0.8 x `size` tokens) and checks the model returns it || `tools/toolcheck.py` | makes a tool call with an array parameter and checks it comes back as a JSON array || `tools/display_kv_check.py [--gpu]` | patch 0072's checks, run in the image (`docker run ... --entrypoint python`, see the file): the setting, the span's mapping and unwinding through a fake driver, the carved planes and the pool's copies; with `--gpu`, the real span on a Spark whose display reservation is free |+| `tools/seg_chunks_check.py [--time]` | patch 0108's checks: `seg_attention` with the CUDA chunk pass against the Triton one, every output and chunk partial bit for bit (22 and 21 heads, dense and sparse rows, every e4m3 code, CUDA graphs), the routing (16 / 32 heads and bf16 stay on Triton), the build at engine start, and the Triton kernel serving when the build fails or Triton is not 3.7; run in the image on one GPU (`docker run --gpus all ... --entrypoint python`, see the file); `--time` prints the old and new paths' times || `tools/pool_pressure.py [size]` | on a fresh server with nothing else sending requests: two conversations of ~`size` tokens (default 30000) take a turn each, others fill the pool, then each takes another turn; exit 1 when a turn resumes less than 90% of its previous prompt, 2 when the kept-prompt cap stops the fill first (a smaller pool runs it faster: `CONTEXT=131072 KV_POOL_GIB=0.5`) || `tools/pool_room_check.py` | patch 0074's checks, run in the image (`docker run ... --entrypoint python`, see the file): the pool's room-making on a CPU arena, what it evicts and moves, and rank 1 replaying the same ops || `tools/test_queued_cancellation.py --source-root DIR` | patch 0073's checks on the CPU, against TensorFold's source with the patches applied (no GPU, no server): requests that wait while every slot is busy and whose client leaves are dropped at once, in queue order; `--expect-stock` before 0073 shows the old wait |diff --git a/patches/0108-glm-seg-chunks-cuda.patch b/patches/0108-glm-seg-chunks-cuda.patchnew file mode 100644index 0000000..8976772--- /dev/null+++ b/patches/0108-glm-seg-chunks-cuda.patch@@ -0,0 +1,706 @@+# The decode windows' latent attention chunk pass (latent._seg_chunks, in every DSA layer of a decode round) as a CUDA+# kernel (seg_chunks.cu) that computes every chunk partial (o, m, l) with the Triton kernel's own instruction sequence,+# read from its GPU IR / PTX: _tile's chains as patch 0106 reproduces them for _sparse_onepass, in _seg_chunks' kWidth-4+# dot layout, and its p-sum tree over the permuted keys; tiles past a chunk's last key (Triton runs and masks them) are+# skipped, a provable no-op. Triton's _merge reads the partials as before. Every output and every chunk partial is the+# Triton kernel's bit for bit. FP8 arenas and 17-24 heads a rank only (TP=3's 22 / 21); TP=2's 32 heads keep Triton.+# Built / loaded by decode.Engine's constructor, and like patch 0105's not used (logged once; the Triton kernel serves)+# under another Triton release than sparse.TRITON_PTX (3.7) or when the build fails. TF_GLM_SEG_CHUNKS_CUDA=0: the+# Triton kernel. Applies after patches 0104 (seg_head_block(R, H)), 0105 (sparse.TRITON_PTX) and 0106 (decode.py's+# start-up loads). tools/seg_chunks_check.py.++diff --git tensorfold/families/glm5_next/cuda/decode.py tensorfold/families/glm5_next/cuda/decode.py+--- tensorfold/families/glm5_next/cuda/decode.py++++ tensorfold/families/glm5_next/cuda/decode.py+@@ -203,6 +203,8 @@ class Engine:+ if sparse.prompt_scores_cuda():+ sparse._scores_ext()+ from . import latent++ if latent.seg_chunks_cuda(): # patch 0108: the decode windows' latent attention, likewise++ latent._seg_chunks_ext()++ if latent.onepass_cuda(): # patch 0106: the prompt chunks' sparse attention, likewise+ latent._onepass_ext()+diff --git tensorfold/families/glm5_next/cuda/latent.py tensorfold/families/glm5_next/cuda/latent.py+--- tensorfold/families/glm5_next/cuda/latent.py++++ tensorfold/families/glm5_next/cuda/latent.py+@@ -865,6 +865,46 @@ def seg_head_block(R: int, H: int) -> int:+ return HB_WIDE if R >= (SEG_FEW_WIDE_ROWS if HB < H <= SEG_FEW_HEADS else SEG_WIDE_ROWS) else HB++++def seg_chunks_cuda() -> bool:++ """TF_GLM_SEG_CHUNKS_CUDA (default on): seg_attention's chunk pass on an FP8 arena with 17-24 heads a++ rank (TP=3's 22 / 21) runs seg_chunks.cu, which computes every chunk partial with _seg_chunks' own instruction++ sequence (its PTX: _tile's mma chains and their K order, the online softmax's roundings, ex2.approx, the p-sum's++ reduction tree), so the same bits, with the keys gathered and converted by producer warps and the work items++ (row, chunk) run as one tile stream a CTA (SEG_CHUNKS_CTAS); _merge reads the partials as before. 0: the Triton++ kernel. Off as well when the extension is not used (another Triton release than sparse.TRITON_PTX, or a failed++ build or load: ``_seg_chunks_ext``). tools/seg_chunks_check.py."""++ return __import__("os").environ.get("TF_GLM_SEG_CHUNKS_CUDA", "1") != "0" and _SEG_CHUNKS_FAILED is None++++++SEG_CHUNKS_CTAS = 0 # patch 0108: CTAs of the CUDA chunk pass (0: one a work item, R x chunks); speed only++_SEG_CHUNKS_EXT = None++_SEG_CHUNKS_FAILED = None # why seg_chunks.cu is not used (then _seg_chunks serves)++++++def _seg_chunks_ext():++ """The extension, built / loaded once; None when it is not used (as patch 0105's ``sparse._scores_ext``)."""++ global _SEG_CHUNKS_EXT, _SEG_CHUNKS_FAILED++ if _SEG_CHUNKS_EXT is None and _SEG_CHUNKS_FAILED is None:++ from pathlib import Path++++ from tensorfold.cuda.build import load++++ from . import sparse++++ try:++ if not sparse.triton_matches_ptx():++ raise RuntimeError(f"Triton {triton.__version__}: the kernel reproduces Triton "++ f"{sparse.TRITON_PTX}'s PTX")++ _SEG_CHUNKS_EXT = load(name="tensorfold_glm_segchunks_v1",++ sources=[str(Path(__file__).parent / "seg_chunks.cu")],++ extra_cuda_cflags=["-O3"], verbose=False)++ except Exception as exc:++ _SEG_CHUNKS_FAILED = f"{type(exc).__name__}: {(str(exc).strip().splitlines() or [''])[-1][:200]}"++ print(f"[tensorfold] seg_chunks.cu is not used ({_SEG_CHUNKS_FAILED}); decode windows' latent attention "++ "runs the Triton kernel", flush=True)++ return _SEG_CHUNKS_EXT+++++ def seg_attention(qa: torch.Tensor, cache: torch.Tensor, rows, tokens: torch.Tensor | None,+ counts: torch.Tensor | None, s: LatentScratch, *, scale: float, out: torch.Tensor,+ hb: int | None = None) -> torch.Tensor:+@@ -886,9 +926,17 @@ def seg_attention(qa: torch.Tensor, cache: torch.Tensor, rows, tokens: torch.Ten+ if hb not in (HB, HB_WIDE):+ raise ValueError(f"segmented attention: {hb} heads a tile, not {HB} or {HB_WIDE}")+ vals, scl, rs, fp8 = _cache_parts(cache, LW)+- _seg_chunks[(R, triton.cdiv(H, hb), nch)](qa, vals, scl, rows.pos, rows.base, rows.sparse, tokens, counts,+- s.po[:n * LW], s.pm[:n], s.pl[:n], R, W=W, H=H, LW=LW, CH=CHUNK,+- SCALE=scale, HBT=hb, KTT=KT, RS=rs, FP8=fp8, num_warps=8,+- num_stages=chunk_stages())++ ext = (_seg_chunks_ext() if fp8 and LW == 512 and 16 < H <= 24 and tokens.dtype == torch.int32++ and counts.dtype == torch.int32 and qa.is_contiguous() and tokens.is_contiguous() and seg_chunks_cuda()++ else None)++ if ext is not None:++ # patch 0108: the same partials (either head tile: 16- and 32-head tiles give the same bits)++ ext.seg_chunks(qa, cache, rs, rows.pos, rows.base, rows.sparse, tokens.view(-1, W), counts, s.po, s.pm, s.pl,++ nch, scale, SEG_CHUNKS_CTAS) # (the dummy list as [1, 1])++ else:++ _seg_chunks[(R, triton.cdiv(H, hb), nch)](qa, vals, scl, rows.pos, rows.base, rows.sparse, tokens, counts,++ s.po[:n * LW], s.pm[:n], s.pl[:n], R, W=W, H=H, LW=LW, CH=CHUNK,++ SCALE=scale, HBT=hb, KTT=KT, RS=rs, FP8=fp8, num_warps=8,++ num_stages=chunk_stages())+ _merge[(R, H)](s.po, s.pm, s.pl, out, s.dummy, R, H=H, LW=LW, NCH=nch, SPARSE=False, num_warps=4)+ return out+diff --git tensorfold/families/glm5_next/cuda/seg_chunks.cu tensorfold/families/glm5_next/cuda/seg_chunks.cu+new file mode 100644+--- /dev/null++++ tensorfold/families/glm5_next/cuda/seg_chunks.cu+@@ -0,0 +1,606 @@++// Patch 0108: the decode windows' latent attention chunk pass (latent._seg_chunks as seg_attention launches it by++// default: an FP8 latent arena, 512-wide rows, TP=3's 22 / 21 heads in 16- or 32-head tiles, 32-key tiles, 512-key++// chunks, 8 warps, TF_GLM_LATENT_STAGES' pipeline) as a CUDA kernel that computes every chunk partial (o, m, l) with++// the Triton kernel's own instruction sequence, so every bit is the same; _merge (Triton) then reads them as before.++// tools/seg_chunks_check.py.++//++// _seg_chunks runs _tile, the function _sparse_onepass runs, and its GPU IR has _sparse_onepass' layouts and counts++// (Triton 3.7 on a GB10: mma warpsPerCTA [1, 8], instrShape [16, 8]; a 32-head tile's loop body 96++// mma.sync, 12 ex2.approx, 20 shfl, 18 max.f32, 2 fma.rn.f32, 100 mul.f32, 14 add.f32 in both; a 16-head tile the++// same chains over one m16 row block, and the head_block contract: 16- and 32-head tiles give the same bits).++// So a chunk partial is patch 0106's chain (sparse_onepass.cu) over the chunk's keys from m = -inf, l = 0,++// o = 0, stored before the division:++// - the tile's 32 cache rows: e4m3 codes -> bf16 (cvt.rn.f16x2.e4m3x2, then cvt.bf16.f16 each), keys past the++// chunk's last zeros, their scales 1;++// - scores[heads, keys] = mma.sync m16n8k16 bf16 -> fp32 of q (heads past H zero) and the keys, each output one++// chain over the 32 k16 steps in ascending order from +0;++// - per element (dot * ks[key]) * SCALE, keys past the chunk's last -> -inf;++// - tile_m = max over the keys, active = tile_m != -inf, next_m = active ? max(m, tile_m) : m, alpha = active ?++// (m == -inf ? 0 : ex2.approx((m - next_m) * log2e)) : 1;++// - p = (key valid and active) ? ex2.approx((score - next_m) * log2e) : 0;++// - o = the mma chain over the two 16-key steps (in order) of bf16(p * ks) and the keys, from o * alpha;++// - l = fma(l, alpha, sum_k p), the sum in the layout's tree: a lane's keys 2 u, 2 u + 1, then the lanes ^ 2, then++// ^ 1 over a warp's 8 keys, then the four 8-key partials (s0 + s2) + (s1 + s3);++// - PO / PM / PL [chunk, row, head] = o, m, l for heads below H; a chunk past the row's keys (Triton's c * CH >= n)++// o 0, m -inf, l 0.++// Triton's tiles past the chunk's keys (its loop always runs CH / KT tiles) are skipped: every key of such a tile is++// masked, so tile_m = -inf, alpha = 1, p = 0, and o * 1 + 0 . keys, l * 1 + 0 change no bit of what the chunk's last++// key tile left (finite o and l are never -0 after an mma / fma from +0; a NaN stays the canonical NaN it already is).++// The layout is sparse_onepass.cu's (patch 0106): 3 QK warps (q^T in registers, 8 heads each), 5 PV warps a tile++// behind, 4 producer warps gathering and converting two tiles ahead into three bf16 key-tile buffers; here a CTA takes++// work items (row, chunk) i = blockIdx.x, + gridDim.x, ... in Triton's chunk-major order and runs all their key tiles++// as one stream, the buffers, barriers and the producers' prefetch carrying on across items.++#include <ATen/cuda/CUDAContext.h>++#include <c10/cuda/CUDAGuard.h>++#include <cuda_bf16.h>++#include <stdint.h>++#include <torch/extension.h>++++namespace {++++constexpr int HQ = 24, LW = 512, KT = 32, NB = 3; // heads kept, latent width, keys a tile, key-tile buffers++constexpr int CHUNK = 512; // keys a chunk (latent.CHUNK): a work item's partial++constexpr int NV = 5, NQ = 3, NPW = 4; // PV, QK, producer warps (warp roles: see the kernel)++// PV warp v's share of the 32 latent m16 tiles (6 mma a tile each). A warp's scheduler is warp & 3; the QK warps++// (64 mma a tile, latency-bound score chains: ncu put 15% of their time in math-pipe stalls) sit on schedulers 0-2, so++// PV 0-2 take 6 tiles there and PV 3 / 4 (warps 3 and 7, both on scheduler 3) 7 each: 100 / 100 / 100 / 84 mma a++// tile a scheduler, where four PV warps of 8 tiles gave 112 / 112 / 112 / 48++__device__ __forceinline__ constexpr int pv_tiles(int v) { return v < 3 ? 6 : 7; }++constexpr int PV_MAX = 7; // the most tiles a PV warp takes++constexpr int NT = (NV + NQ + NPW) * 32;++constexpr int CH = LW * 2 / 16; // 16-byte chunks of a bf16 latent row (64)++constexpr int BAR_FULL = 1, BAR_EMPTY = 4, BAR_PREADY = 7, BAR_PFREE = 8;++constexpr int N_FULL = (NPW + NQ) * 32, N_EMPTY = (NV + NPW) * 32, N_QV = (NQ + NV) * 32;++++__device__ __forceinline__ void mma16816(float (&d)[4], const uint32_t (&a)[4], uint32_t b0, uint32_t b1) {++ asm volatile("mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32 {%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, "++ "{%0,%1,%2,%3};\n"++ : "+f"(d[0]), "+f"(d[1]), "+f"(d[2]), "+f"(d[3])++ : "r"(a[0]), "r"(a[1]), "r"(a[2]), "r"(a[3]), "r"(b0), "r"(b1));++}++++__device__ __forceinline__ void ldsm_x4(uint32_t (&r)[4], unsigned s) {++ asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];\n"++ : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3])++ : "r"(s));++}++++__device__ __forceinline__ void ldsm_x4_t(uint32_t (&r)[4], unsigned s) {++ asm volatile("ldmatrix.sync.aligned.m8n8.x4.trans.shared.b16 {%0,%1,%2,%3}, [%4];\n"++ : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3])++ : "r"(s));++}++++__device__ __forceinline__ void bar_sync(int id, int n) { asm volatile("bar.sync %0, %1;" ::"r"(id), "r"(n) : "memory"); }++__device__ __forceinline__ void bar_arrive(int id, int n) {++ asm volatile("bar.arrive %0, %1;" ::"r"(id), "r"(n) : "memory");++}++++__device__ __forceinline__ float ex2(float x) {++ float y;++ asm("ex2.approx.f32 %0, %1;" : "=f"(y) : "f"(x));++ return y;++}++++__device__ __forceinline__ uint16_t bf16(float x) {++ uint16_t r;++ asm("cvt.rn.bf16.f32 %0, %1;" : "=h"(r) : "f"(x));++ return r;++}++++// Two e4m3 codes (low byte first) -> bf16x2 (low first) by Triton's own instruction sequence (its PTX: the codes to++// f16x2, then cvt.bf16.f16 each): used only for a tile holding a NaN code, whose NaN bits this keeps.++__device__ __forceinline__ uint32_t e4m3x2_bf16x2_tri(uint32_t two) {++ uint32_t r;++ const unsigned short v = (unsigned short)two;++ asm("{\n .reg .b32 a;\n .reg .f16 a<2>;\n .reg .b16 b<2>;\n cvt.rn.f16x2.e4m3x2 a, %1;\n mov.b32 {a0, a1}, a;\n"++ " cvt.bf16.f16 b0, a0;\n cvt.bf16.f16 b1, a1;\n mov.b32 %0, {b0, b1};\n}" : "=r"(r) : "h"(v));++ return r;++}++++// Four e4m3 codes -> two bf16x2 (codes 0, 1 and 2, 3) on the integer and bf16 pipes, exactly for every code but NaN:++// a code's sign-extended 16 bits, shifted left 4 and masked, are the bf16 with its sign, the 4 exponent bits and++// the 3 mantissa bits in place and 2^-120 of the value (a subnormal code a subnormal bf16), then times 2^120 (exact).++__device__ __forceinline__ void e4m3x4_bf16(uint32_t w, uint32_t& lo, uint32_t& hi) {++ uint32_t a, b;++ asm("prmt.b32 %0, %1, %2, %3;" : "=r"(a) : "r"(w), "r"(0u), "r"(0x9180u)); // [sext b1 | sext b0]++ asm("prmt.b32 %0, %1, %2, %3;" : "=r"(b) : "r"(w), "r"(0u), "r"(0xB3A2u)); // [sext b3 | sext b2]++ a = (a << 4) & 0x87F087F0u;++ b = (b << 4) & 0x87F087F0u;++ asm("mul.rn.bf16x2 %0, %1, %2;" : "=r"(lo) : "r"(a), "r"(0x7B807B80u)); // 2^120++ asm("mul.rn.bf16x2 %0, %1, %2;" : "=r"(hi) : "r"(b), "r"(0x7B807B80u));++}++++// a key tile's 16-byte chunk c of key r: c ^ ((r ^ (c >> 3)) & 7). Eight consecutive keys of one chunk (an ldmatrix's++// 8 rows, QK and PV) still fall in eight bank groups; a producer lane stores chunks 2 l and 2 l + 1 of one key, and++// with the (c >> 3) term lanes 0-3 and 4-7 of every quarter-warp land in different bank groups (a plain c ^ (r & 7):++// 2-way conflicts, 37 M of a 113k chunk's 243 M shared-memory wavefronts, ncu)++__device__ __forceinline__ int swz(int r, int c) { return c ^ ((r ^ (c >> 3)) & 7); }++__device__ __forceinline__ int pswz(int r, int c) { return c ^ ((r >> 1) & 3); }++// Triton's kWidth = 4 dot operands (_seg_chunks' GPU IR; _sparse_onepass' are kWidth 2): k =++// 32 b + 16 u + 4 t + 2 h + e is step 2 b + h, mma slot 8 u + 2 t + e (patch 0105's reading of that layout), in both++// dots, so a step's 16 products are not a contiguous k16 group. The key tiles hold, in each 32-wide block, at place++// 16 h + 8 u + 2 t + e the value k = 16 u + 4 t + 2 h + e - latent columns so along a row (the QK dot's k), keys so++// down the rows (the P.V dot's k) - and a plain k16 step of that layout is Triton's step.++__device__ __forceinline__ int kw4(int p) { // place -> k++ return (p & ~31) | ((p & 8) << 1) | ((p & 6) << 1) | ((p >> 3) & 2) | (p & 1);++}++__device__ __forceinline__ int kw4_place(int k) { // k -> place++ return (k & ~31) | ((k & 2) << 3) | ((k >> 1) & 8) | ((k >> 1) & 6) | (k & 1);++}++++constexpr float LOG2E = 1.44269502162933349609375f; // 0f3FB8AA3B++++struct Smem {++ uint4 kt[NB][KT * CH]; // 96 KB: key tiles, bf16 [key][64 chunks], swizzled++ uint16_t ps[HQ * KT]; // P [head][key] bf16, 4 chunks a row, chunk-swizzled++ float ks[NB][KT];++ float alpha[HQ];++};++++struct Raw {++ uint4 v[8];++ float s[8];++};++++// The window: R rows, each a dense row (keys base + 0 .. base + pos) or a sparse one (keys base + its first cnt++// selected tokens, list order); work item i = chunk i / R of row i % R (Triton's program order, chunk-major), its keys++// CHUNK c .. c + CHUNK - 1 of the row's list. A CTA takes items blockIdx.x, + gridDim.x, ... and runs their key tiles++// as one stream (the tile buffers, barriers and the producers' prefetch run on across items).++struct Win {++ const int32_t* __restrict__ POS;++ const int32_t* __restrict__ BASE;++ const int32_t* __restrict__ SPR;++ const int32_t* __restrict__ TOK;++ const int32_t* __restrict__ CNT;++ int W, R, items, step;++};++++struct Cur { // an item and one of its tiles++ int i, t, nt, r, c, nk, b;++ bool sp;++};++++__device__ __forceinline__ void cur_item(const Win& w, Cur& u, int i) {++ u.i = i;++ u.t = 0;++ if (i >= w.items) {++ u.nt = 0;++ return;++ }++ u.r = i % w.R;++ u.c = i / w.R;++ const int sp = w.SPR[u.r], pos = w.POS[u.r], b = w.BASE[u.r];++ u.sp = sp != 0;++ const int cnt = u.sp ? w.CNT[u.r] : 0; // Triton's masked load (CNT: 1 entry without sparse rows)++ const int n = u.sp ? cnt : pos + 1; // selected tokens, or keys 0 .. pos++ u.nk = min(n - u.c * CHUNK, CHUNK); // <= 0: an empty chunk++ u.nt = u.nk > 0 ? (u.nk + KT - 1) / KT : 0;++ u.b = b;++}++++// the CTA's first tile / the next one (past the last: u.i >= items)++__device__ __forceinline__ void cur_skip(const Win& w, Cur& u) {++ while (u.i < w.items && u.nt == 0) cur_item(w, u, u.i + w.step);++}++__device__ __forceinline__ void cur_next(const Win& w, Cur& u) {++ if (u.i >= w.items) return;++ if (++u.t < u.nt) return;++ cur_item(w, u, u.i + w.step);++ cur_skip(w, u);++}++++// the tile's keys as row tokens (-1 past the chunk's last) and their base: the token loads stay in flight until++// raw_load adds the base (an add here stalled the producer on each load: ncu, 27% of the kernel's samples)++__device__ __forceinline__ void ids_load(const Win& w, const Cur& u, int pw, int (&id)[8], int& base) {++ base = u.b;++#pragma unroll++ for (int i = 0; i < 8; ++i) {++ const int k = u.t * KT + pw + 4 * i; // the key within the item's chunk++ const int x = u.c * CHUNK + k; // its place in the row's list++ id[i] = u.i < w.items && k < u.nk ? (u.sp ? w.TOK[(size_t)u.r * w.W + x] : x) : -1;++ }++}++++__device__ __forceinline__ void raw_load(const uint8_t* __restrict__ LC, int RS, const int (&id)[8], int base,++ int lane, Raw& r) {++#pragma unroll++ for (int i = 0; i < 8; ++i) {++ const uint8_t* src = LC + (size_t)(id[i] < 0 ? 0 : base + id[i]) * RS;++ r.v[i] = id[i] < 0 ? make_uint4(0, 0, 0, 0) : *reinterpret_cast<const uint4*>(src + 16 * lane);++ r.s[i] = id[i] < 0 || lane ? 1.0f : *reinterpret_cast<const float*>(src + LW);++ }++}++++__device__ __forceinline__ void convert(const Raw& r, uint4* kt, float* ks, int pw, int lane) {++ uint32_t nan = 0; // bit 7 of a byte: some code of it was 0x7F / 0xFF++#pragma unroll++ for (int i = 0; i < 8; ++i) {++ const uint32_t in[4] = {r.v[i].x, r.v[i].y, r.v[i].z, r.v[i].w};++#pragma unroll++ for (int q = 0; q < 4; ++q) nan |= (in[q] & 0x7F7F7F7Fu) + 0x01010101u;++ }++ if (__any_sync(0xffffffffu, nan & 0x80808080u)) {++#pragma unroll++ for (int i = 0; i < 8; ++i) {++ const int key = kw4_place(pw + 4 * i); // the key's row++ const uint32_t in[4] = {r.v[i].x, r.v[i].y, r.v[i].z, r.v[i].w};++ uint32_t w[8];++#pragma unroll++ for (int m = 0; m < 8; ++m) w[m] = e4m3x2_bf16x2_tri(in[m >> 1] >> (16 * (m & 1)));++ // lane l's latent columns 16 l .. 16 l + 15 (b = l / 2, u = l % 2): words 2 t + h to chunk 4 b + 2 h + u++ kt[key * CH + swz(key, 2 * lane - (lane & 1))] = make_uint4(w[0], w[2], w[4], w[6]);++ kt[key * CH + swz(key, 2 * lane - (lane & 1) + 2)] = make_uint4(w[1], w[3], w[5], w[7]);++ if (lane == 0) ks[key] = r.s[i];++ }++ return;++ }++#pragma unroll++ for (int i = 0; i < 8; ++i) {++ const int key = kw4_place(pw + 4 * i);++ const uint32_t in[4] = {r.v[i].x, r.v[i].y, r.v[i].z, r.v[i].w};++ uint32_t w[8];++#pragma unroll++ for (int q = 0; q < 4; ++q) e4m3x4_bf16(in[q], w[2 * q], w[2 * q + 1]);++ kt[key * CH + swz(key, 2 * lane - (lane & 1))] = make_uint4(w[0], w[2], w[4], w[6]);++ kt[key * CH + swz(key, 2 * lane - (lane & 1) + 2)] = make_uint4(w[1], w[3], w[5], w[7]);++ if (lane == 0) ks[key] = r.s[i];++ }++}++++__global__ void __launch_bounds__(NT, 1) seg_chunks_kernel(const __nv_bfloat16* __restrict__ QA,++ const uint8_t* __restrict__ LC, int RS, Win w,++ float* __restrict__ PO, float* __restrict__ PM,++ float* __restrict__ PL, int H, float scale) {++ extern __shared__ __align__(16) uint8_t smem_raw[];++ Smem& S = *reinterpret_cast<Smem*>(smem_raw);++ const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31, g = lane >> 2, t4 = lane & 3;++ int ntot = 0; // the CTA's key tiles, all its items++ {++ Cur u;++ for (int i = blockIdx.x; i < w.items; i += w.step) {++ cur_item(w, u, i);++ ntot += u.nt;++ }++ }++++ // warps 0-3 PV 0-3, 4-6 QK, 7 PV 4, 8-11 producers++ if (warp >= 8) {++ // ---------------------------------------------------- producers: keys pw + 4 i, 16 B a lane, two tiles ahead++ const int pw = warp - 8;++ Raw A, B;++ int ia[8], ib[8], ba, bb;++ Cur uc; // the tile ia's ids are for (two ahead of A's)++ cur_item(w, uc, blockIdx.x);++ cur_skip(w, uc);++ Cur ud = uc; // ib's++ cur_next(w, ud);++ ids_load(w, uc, pw, ia, ba);++ ids_load(w, ud, pw, ib, bb);++ raw_load(LC, RS, ia, ba, lane, A);++ raw_load(LC, RS, ib, bb, lane, B);++ cur_next(w, uc);++ cur_next(w, uc);++ cur_next(w, ud);++ cur_next(w, ud);++ ids_load(w, uc, pw, ia, ba);++ ids_load(w, ud, pw, ib, bb);++ int b = 0;++ for (int t = 0; t < ntot; t += 2) {++ if (t >= NB) bar_sync(BAR_EMPTY + b, N_EMPTY);++ convert(A, S.kt[b], S.ks[b], pw, lane);++ bar_arrive(BAR_FULL + b, N_FULL);++ b = b + 1 == NB ? 0 : b + 1;++ if (t + 2 < ntot) {++ raw_load(LC, RS, ia, ba, lane, A);++ cur_next(w, uc);++ cur_next(w, uc);++ ids_load(w, uc, pw, ia, ba);++ }++ if (t + 1 >= ntot) break;++ if (t + 1 >= NB) bar_sync(BAR_EMPTY + b, N_EMPTY);++ convert(B, S.kt[b], S.ks[b], pw, lane);++ bar_arrive(BAR_FULL + b, N_FULL);++ b = b + 1 == NB ? 0 : b + 1;++ if (t + 3 < ntot) {++ raw_load(LC, RS, ib, bb, lane, B);++ cur_next(w, ud);++ cur_next(w, ud);++ ids_load(w, ud, pw, ib, bb);++ }++ }++ return;++ }++++ if (warp >= 4 && warp < 7) {++ // ------------------------------------------------- QK warps: heads 8 j .. 8 j + 7 (an n8 tile), all 32 keys++ const int j = warp - 4;++ const int hq = 8 * j + g; // this lane's B column (head)++ const int ar = lane & 15, ac = lane >> 4;++ uint32_t qf[32][2]; // q^T B fragments: step s, k = 16 s + 2 t (+ 8)++ // QK(t + 1)'s 32 steps are issued between softmax(t)'s stages, so the in-order warp has the score chains'++ // mma in flight while it waits on the softmax's shuffles and ex2 (and the other way round)++ uint32_t a[3][2][4];++ unsigned ktn;++ float acc[2][4], accn[2][4];++#define LOADA(slot, s) \++ { \++ ldsm_x4(a[slot][0], ktn + 16 * (ar * CH + swz(ar, 2 * (s) + ac))); \++ ldsm_x4(a[slot][1], ktn + 16 * ((16 + ar) * CH + swz(16 + ar, 2 * (s) + ac))); \++ }++#define QKSTEP(s) \++ { \++ if ((s) + 2 < 32) LOADA(((s) + 2) % 3, (s) + 2) \++ mma16816(accn[0], a[(s) % 3][0], qf[s][0], qf[s][1]); \++ mma16816(accn[1], a[(s) % 3][1], qf[s][0], qf[s][1]); \++ }++#define QKBEGIN() \++ { \++ for (int i_ = 0; i_ < 2; ++i_) \++ for (int e_ = 0; e_ < 4; ++e_) accn[i_][e_] = 0.0f; \++ LOADA(0, 0) \++ LOADA(1, 1) \++ }++ int b = 0, T = 0; // the tile buffer, the CTA's tile count so far++ Cur u;++ for (int i = blockIdx.x; i < w.items; i += w.step) {++ cur_item(w, u, i);++ const int pb = (u.c * w.R + u.r) * H; // the item's partials++ if (u.nt == 0) { // an empty chunk: m -inf, l 0 (Triton's stores)++ if (g == 0)++#pragma unroll++ for (int e = 0; e < 2; ++e)++ if (8 * j + 2 * t4 + e < H) {++ PM[pb + 8 * j + 2 * t4 + e] = -INFINITY;++ PL[pb + 8 * j + 2 * t4 + e] = 0.0f;++ }++ continue;++ }++ const int ntile = u.nt, nk = u.nk;++#pragma unroll++ for (int s = 0; s < 32; ++s) { // step s = 2 b + h: k 32 b + 4 t + 2 h (+ 1), + 16++ const __nv_bfloat16* q =++ QA + ((size_t)u.r * H + (hq < H ? hq : 0)) * LW + 32 * (s >> 1) + 4 * t4 + 2 * (s & 1);++ qf[s][0] = hq < H ? *reinterpret_cast<const uint32_t*>(q) : 0u;++ qf[s][1] = hq < H ? *reinterpret_cast<const uint32_t*>(q + 16) : 0u;++ }++ float m[2] = {-INFINITY, -INFINITY}, l[2] = {0.0f, 0.0f}; // heads 8 j + 2 t, + 1++ bar_sync(BAR_FULL + b, N_FULL);++ ktn = (unsigned)__cvta_generic_to_shared(S.kt[b]);++ QKBEGIN()++#pragma unroll++ for (int s = 0; s < 32; ++s) QKSTEP(s)++#pragma unroll++ for (int i2 = 0; i2 < 2; ++i2)++#pragma unroll++ for (int e = 0; e < 4; ++e) acc[i2][e] = accn[i2][e];++ for (int t = 0; t < ntile; ++t, ++T) {++ const int t0 = t * KT, bn = b + 1 == NB ? 0 : b + 1;++ if (t + 1 < ntile) bar_sync(BAR_FULL + bn, N_FULL);++ // the item's last tile's "next" scores run on its own buffer and are dropped (no branch below)++ ktn = (unsigned)__cvta_generic_to_shared(S.kt[t + 1 < ntile ? bn : b]);++ QKBEGIN()++#pragma unroll++ for (int s = 0; s < 8; ++s) QKSTEP(s)++ // acc[i][e]: row q = 16 i + g (e < 2) or 16 i + 8 + g (e >= 2) of the tile, key kw4(q), head++ // 8 j + 2 t + (e & 1)++ float sc[2][4], kk[2][2];++ bool val[2][2];++#pragma unroll++ for (int i2 = 0; i2 < 2; ++i2)++#pragma unroll++ for (int hi = 0; hi < 2; ++hi) {++ const int key = 16 * i2 + 8 * hi + g;++ kk[i2][hi] = S.ks[b][key];++ val[i2][hi] = t0 + kw4(key) < nk;++#pragma unroll++ for (int e = 0; e < 2; ++e)++ sc[i2][2 * hi + e] =++ val[i2][hi] ? __fmul_rn(__fmul_rn(acc[i2][2 * hi + e], kk[i2][hi]), scale) : -INFINITY;++ }++ float nm[2], alpha[2];++ bool act[2];++#pragma unroll++ for (int e = 0; e < 2; ++e) {++ float x = fmaxf(fmaxf(sc[0][e], sc[0][2 + e]), fmaxf(sc[1][e], sc[1][2 + e]));++ x = fmaxf(x, __shfl_xor_sync(0xffffffffu, x, 4));++ x = fmaxf(x, __shfl_xor_sync(0xffffffffu, x, 8));++ x = fmaxf(x, __shfl_xor_sync(0xffffffffu, x, 16));++ act[e] = x != -INFINITY;++ nm[e] = act[e] ? fmaxf(m[e], x) : m[e];++ const float a_ = m[e] == -INFINITY ? 0.0f : ex2(__fmul_rn(__fsub_rn(m[e], nm[e]), LOG2E));++ alpha[e] = act[e] ? a_ : 1.0f;++ }++#pragma unroll++ for (int s = 8; s < 16; ++s) QKSTEP(s)++ float p[2][4];++#pragma unroll++ for (int i2 = 0; i2 < 2; ++i2)++#pragma unroll++ for (int x = 0; x < 4; ++x) {++ const int e = x & 1;++ p[i2][x] = val[i2][x >> 1] && act[e] ? ex2(__fmul_rn(__fsub_rn(sc[i2][x], nm[e]), LOG2E)) : 0.0f;++ }++#pragma unroll++ for (int s = 16; s < 24; ++s) QKSTEP(s)++ // the p sums in Triton's tree over the keys k = kw4(q) of this lane's rows q = 16 i + 8 hi + g: k, k ^ 1++ // (q ^ 1: lanes ^ 4), then k ^ 4 (q ^ 2: lanes ^ 8), then k ^ 2 (q ^ 16: i) - a warp's 8-key partial++ // s_w, w = k >> 3 = 2 q3 + q2 = 2 hi + (lane ^ 16) - then (s0 + s2) + (s1 + s3) (an add commutes)++ float z[2][2];++#pragma unroll++ for (int hi = 0; hi < 2; ++hi)++#pragma unroll++ for (int e = 0; e < 2; ++e) {++ float y0 = p[0][2 * hi + e], y1 = p[1][2 * hi + e];++ y0 = __fadd_rn(y0, __shfl_xor_sync(0xffffffffu, y0, 4));++ y1 = __fadd_rn(y1, __shfl_xor_sync(0xffffffffu, y1, 4));++ y0 = __fadd_rn(y0, __shfl_xor_sync(0xffffffffu, y0, 8));++ y1 = __fadd_rn(y1, __shfl_xor_sync(0xffffffffu, y1, 8));++ z[hi][e] = __fadd_rn(y0, y1);++ }++#pragma unroll++ for (int e = 0; e < 2; ++e) {++ const float s02 = __fadd_rn(z[0][e], z[1][e]); // s0 + s2 (s1 + s3 on lanes 16-31)++ const float tot = __fadd_rn(s02, __shfl_xor_sync(0xffffffffu, s02, 16));++ l[e] = __fmaf_rn(l[e], alpha[e], tot);++ m[e] = nm[e];++ }++#pragma unroll++ for (int s = 24; s < 32; ++s) QKSTEP(s)++ if (T > 0) bar_sync(BAR_PFREE, N_QV); // the PV warps have read the previous P and alphas++#pragma unroll++ for (int i2 = 0; i2 < 2; ++i2)++#pragma unroll++ for (int x = 0; x < 4; ++x) {++ const int h = 8 * j + 2 * t4 + (x & 1), key = 16 * i2 + 8 * (x >> 1) + g;++ S.ps[h * KT + 8 * pswz(h, key >> 3) + (key & 7)] = bf16(__fmul_rn(p[i2][x], kk[i2][x >> 1]));++ }++ if (g == 0) {++ S.alpha[8 * j + 2 * t4] = alpha[0];++ S.alpha[8 * j + 2 * t4 + 1] = alpha[1];++ }++ bar_arrive(BAR_PREADY, N_QV);++#pragma unroll++ for (int i2 = 0; i2 < 2; ++i2)++#pragma unroll++ for (int e = 0; e < 4; ++e) acc[i2][e] = accn[i2][e];++ b = bn;++ }++ if (g == 0)++#pragma unroll++ for (int e = 0; e < 2; ++e)++ if (8 * j + 2 * t4 + e < H) {++ PM[pb + 8 * j + 2 * t4 + e] = m[e];++ PL[pb + 8 * j + 2 * t4 + e] = l[e];++ }++ }++#undef QKSTEP++#undef QKBEGIN++#undef LOADA++ return;++ }++++ // ------------------------------------------------- PV warps: latent rows 16 tb .. 16 (tb + nt) - 1 (nt m16 tiles)++ const int v = warp < 4 ? warp : 4;++ const int nt = pv_tiles(v);++ int tb = 0;++#pragma unroll++ for (int k = 0; k < NV - 1; ++k) tb += k < v ? pv_tiles(k) : 0;++ const unsigned psb = (unsigned)__cvta_generic_to_shared(S.ps);++ const int vk = (lane & 7) + 8 * (lane >> 4), vc = (lane >> 3) & 1;++ int b = 0, T = 0;++ Cur u;++ for (int it = blockIdx.x; it < w.items; it += w.step) {++ cur_item(w, u, it);++ float* po = PO + (size_t)((u.c * w.R + u.r) * H) * LW;++ float o[PV_MAX][3][4];++#pragma unroll++ for (int i = 0; i < PV_MAX; ++i)++#pragma unroll++ for (int nh = 0; nh < 3; ++nh)++#pragma unroll++ for (int e = 0; e < 4; ++e) o[i][nh][e] = 0.0f;++ for (int t = 0; t < u.nt; ++t, ++T) {++ const unsigned ktb = (unsigned)__cvta_generic_to_shared(S.kt[b]);++ bar_sync(BAR_PREADY, N_QV);++ uint32_t pf[3][4]; // P^T B fragments: heads 8 nh .., keys 0-15 (0, 1), 16-31++#pragma unroll++ for (int nh = 0; nh < 3; ++nh) {++ const int h = 8 * nh + (lane & 7), c = lane >> 3;++ ldsm_x4(pf[nh], psb + 2 * (h * KT + 8 * pswz(h, c)));++ }++ float al[3][2];++#pragma unroll++ for (int nh = 0; nh < 3; ++nh) {++ al[nh][0] = S.alpha[8 * nh + 2 * t4];++ al[nh][1] = S.alpha[8 * nh + 2 * t4 + 1];++ }++ if (T + 1 < ntot) bar_arrive(BAR_PFREE, N_QV); // the QK warps write the next P only after this++ bool one = true;++#pragma unroll++ for (int nh = 0; nh < 3; ++nh) one = one && al[nh][0] == 1.0f && al[nh][1] == 1.0f;++ if (!__all_sync(0xffffffffu, one)) {++#pragma unroll++ for (int i = 0; i < PV_MAX; ++i)++ if (i < nt)++#pragma unroll++ for (int nh = 0; nh < 3; ++nh)++#pragma unroll++ for (int e = 0; e < 4; ++e) o[i][nh][e] = __fmul_rn(o[i][nh][e], al[nh][e & 1]);++ }++ uint32_t a[2][2][4]; // K^T A fragments, two m16 tiles in flight++#define LOADV(slot, i) \++ { \++ ldsm_x4_t(a[slot][0], ktb + 16 * (vk * CH + swz(vk, 2 * (tb + (i)) + vc))); \++ ldsm_x4_t(a[slot][1], ktb + 16 * ((16 + vk) * CH + swz(16 + vk, 2 * (tb + (i)) + vc))); \++ }++ LOADV(0, 0)++#pragma unroll++ for (int i = 0; i < PV_MAX; ++i) {++ if (i < nt) {++ if (i + 1 < nt) LOADV((i + 1) & 1, i + 1)++#pragma unroll++ for (int nh = 0; nh < 3; ++nh) mma16816(o[i][nh], a[i & 1][0], pf[nh][0], pf[nh][1]);++#pragma unroll++ for (int nh = 0; nh < 3; ++nh) mma16816(o[i][nh], a[i & 1][1], pf[nh][2], pf[nh][3]);++ }++ }++#undef LOADV++ if (T + NB < ntot) bar_arrive(BAR_EMPTY + b, N_EMPTY);++ b = b + 1 == NB ? 0 : b + 1;++ }++ // the item's partial o (unnormalized, fp32), heads below H; an empty chunk's zeros (Triton's stores)++#pragma unroll++ for (int i = 0; i < PV_MAX; ++i)++ if (i < nt)++#pragma unroll++ for (int nh = 0; nh < 3; ++nh)++#pragma unroll++ for (int e = 0; e < 4; ++e) {++ // the tile's latent place 16 (tb + i) + g + 8 (e >> 1) holds latent column kw4(place)++ const int h = 8 * nh + 2 * t4 + (e & 1), lat = kw4(16 * (tb + i) + g + 8 * (e >> 1));++ if (h < H) po[(size_t)h * LW + lat] = o[i][nh][e];++ }++ }++}++++} // namespace++++// latent.seg_attention's chunk pass on an FP8 cache: QA [R, H, 512] bf16 (16 < H <= 24), LC the arena's rows (512++// e4m3 codes, then the row's fp32 scale; RS bytes apart), POS / BASE / SPR [R] int32 (segments.SegRows), TOK [R, W]++// int32 selected tokens (sparse rows' first CNT[r] read), PO / PM / PL the chunk partials of nch chunks++// [nch, R, H, (512)] fp32, as _seg_chunks stores them; ctas: CTAs (0: one an item, R * nch).++void seg_chunks(const at::Tensor& QA, const at::Tensor& LC, int64_t RS, const at::Tensor& POS, const at::Tensor& BASE,++ const at::Tensor& SPR, const at::Tensor& TOK, const at::Tensor& CNT, at::Tensor PO, at::Tensor PM,++ at::Tensor PL, int64_t nch, double scale, int64_t ctas) {++ TORCH_CHECK(QA.is_cuda() && QA.scalar_type() == at::kBFloat16 && LC.element_size() == 1 &&++ POS.scalar_type() == at::kInt && BASE.scalar_type() == at::kInt && SPR.scalar_type() == at::kInt &&++ TOK.scalar_type() == at::kInt && CNT.scalar_type() == at::kInt &&++ PO.scalar_type() == at::kFloat && PM.scalar_type() == at::kFloat && PL.scalar_type() == at::kFloat,++ "seg chunks: dtypes");++ const int64_t R = QA.size(0), H = QA.size(1);++ TORCH_CHECK(QA.dim() == 3 && QA.size(2) == LW && H > 16 && H <= HQ && QA.is_contiguous() && LC.is_contiguous() &&++ TOK.is_contiguous() && TOK.dim() == 2 && POS.numel() >= R && BASE.numel() >= R && SPR.numel() >= R &&++ PO.is_contiguous() && PM.is_contiguous() && PL.is_contiguous() && PO.numel() >= nch * R * H * LW &&++ PM.numel() >= nch * R * H && PL.numel() >= nch * R * H && nch >= 1, "seg chunks: shapes");++ TORCH_CHECK(RS % 16 == 0 && RS >= LW + 4, "seg chunks: an FP8 cache of 16-byte aligned rows");++ TORCH_CHECK(LC.numel() / RS < (int64_t(1) << 31), "seg chunks: arena rows past int32");++ if (R == 0) return;++ const c10::cuda::CUDAGuard guard(QA.device());++ static bool attr = false;++ if (!attr) {++ C10_CUDA_CHECK(cudaFuncSetAttribute(seg_chunks_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize,++ (int)sizeof(Smem)));++ attr = true;++ }++ const int items = (int)(R * nch);++ const int grid = ctas > 0 && ctas < items ? (int)ctas : items;++ Win w{POS.data_ptr<int32_t>(), BASE.data_ptr<int32_t>(), SPR.data_ptr<int32_t>(), TOK.data_ptr<int32_t>(),++ CNT.data_ptr<int32_t>(), (int)TOK.size(1), (int)R, items, grid};++ seg_chunks_kernel<<<grid, NT, sizeof(Smem), at::cuda::getCurrentCUDAStream()>>>(++ reinterpret_cast<const __nv_bfloat16*>(QA.data_ptr()), reinterpret_cast<const uint8_t*>(LC.data_ptr()),++ (int)RS, w, PO.data_ptr<float>(), PM.data_ptr<float>(), PL.data_ptr<float>(), (int)H, (float)scale);++ C10_CUDA_KERNEL_LAUNCH_CHECK();++}++++PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("seg_chunks", &seg_chunks); }diff --git a/tools/seg_chunks_check.py b/tools/seg_chunks_check.pynew file mode 100644index 0000000..60cabc2--- /dev/null+++ b/tools/seg_chunks_check.py@@ -0,0 +1,385 @@+#!/usr/bin/env python3+"""tools/seg_chunks_check.py [--time]: patch 0108 - the decode windows' latent attention chunk pass (latent._seg_chunks,+Triton) as a CUDA kernel (seg_chunks.cu) for FP8 arenas and 17-24 heads a rank (TP=3's 22 / 21) that computes every+chunk partial with the Triton kernel's own instruction sequence; Triton's _merge reads them as before. Run in the image+built with patch 0108, on one GPU:+docker run --rm --gpus all --entrypoint python -v "$PWD/tools/seg_chunks_check.py:/c.py" <image> /c.py++latent.seg_attention with the CUDA chunk pass (TF_GLM_SEG_CHUNKS_CUDA=1, SEG_CHUNKS_CTAS as shipped) against the+Triton one (=0, the head tile seg_head_block(R, H), TF_GLM_LATENT_STAGES 3): every output bit (out pre-filled with+NaN) AND every chunk partial (the scratch's PO / PM / PL slices pre-filled with NaN, so every store and every+non-store counts) identical, over:+ - windows at 22 and 21 heads: 4 x 8 / 4 x 4 / 4 x 2 / 4 x 1 @ 596k, 1 x 16 / 1 x 8 / 1 x 1 @ 596k, 2 x 8 @ 300k,+ 4 x 8 @ 180k, 3 x 5 @ 420k, uneven streams (rows 1 / 7 / 3 / 5) - each stream's rows sharing its selected pools+ plus their own incomplete pool, as seg_select gives them;+ - dense rows (keys 0 .. pos below the dense limit: pos 0, 30, 31, 32, 510, 511, 512, 1023, 2047, 2049 ..), mixed+ with sparse ones in one window, and sparse rows of 0, 1, 31-33, 511-513, 1024, 2050 and 2051 tokens (chunks+ straddled, empty chunks, partial key tiles), their token lists padded past the count with -1 and with ids far+ past the arena (never read);+ - adversarial data: every e4m3 code including NaN codes (0x7F / 0xFF) in rows that are read, arbitrary+ (non-power-of-two) row scales and a 192^-0.5 softmax scale, queries from 1e-30 to 3e4 with +-0, a row whose+ keys are all -0;+ - both head tiles (16 and 32: the same partials) and other SEG_CHUNKS_CTAS (1, 7, 48, 0);+ - one CUDA graph of the CUDA path captured, then replayed over new positions, tokens and queries;+ - routing: the extension is called once a window on an FP8 arena with 22 / 21 heads, never with the switch at 0, on+ a bf16 arena, or with 16 / 32 heads (32: TP=2);+ - start-up: decode.Engine's constructor loads (on a fresh image, builds) the extension before any request for FP8+ caches with DSA layers and the switch on; not with TF_GLM_SEG_CHUNKS_CUDA=0, bf16 caches or no DSA layer;+ - not used - a failed build or load, or another Triton release than sparse.TRITON_PTX: nothing raised, logged once+ and not retried, the switch reads off, and seg_attention gives the Triton kernel's output and partials.+One PASS / FAIL line a check; exits 1 on any FAIL (on an image without the patch: at the first check).++--time: the whole seg_attention call (chunk pass and merge), Triton -> CUDA, cold (11 calls a CUDA graph, each its+own selected tokens, L2 flushed between replays), 22 heads (TP=3 rank 0) and 21; at 32 heads (TP=2) the kernel does+not run."""+import os+import statistics+import sys+from types import SimpleNamespace++import torch++from tensorfold.families.glm5_next.cuda import decode, forward, hcsplit, kv8, latent, sparse+from tensorfold.families.glm5_next.cuda.segments import SegRows++dev = torch.device("cuda")+g = torch.Generator(device=dev).manual_seed(97)+LW = 512+TOK = sparse.TOKENS+fails = 0+++def check(ok, msg):+ global fails+ print(("PASS " if ok else "FAIL ") + msg, flush=True)+ fails += not ok+++class Count:+ """latent's CUDA extension, its ``seg_chunks`` calls counted."""++ def __init__(self, ext):+ self.ext, self.n = ext, 0++ def seg_chunks(self, *a):+ self.n += 1+ return self.ext.seg_chunks(*a)+++def cache_for(rows, codes="normal", scales="pow2"):+ c = kv8.zeros(rows, LW, "fp8", dev)+ if codes == "all": # every e4m3 code, NaN included+ c[:, :LW] = torch.randint(0, 256, (rows, LW), dtype=torch.uint8, device=dev, generator=g)+ else:+ c[:, :LW] = torch.randint(0, 0x7E, (rows, LW), dtype=torch.uint8, device=dev, generator=g)+ c[:, :LW] |= torch.randint(0, 2, (rows, LW), dtype=torch.uint8, device=dev, generator=g) * 0x80+ if scales == "pow2":+ s = 2.0 ** torch.randint(-12, -4, (rows,), device=dev, generator=g).float()+ else: # arbitrary: products with the scores round+ s = torch.rand((rows,), device=dev, generator=g) * 0.01 + 1e-4+ c[:, LW:LW + 4] = s.to(torch.float32).view(torch.uint8).reshape(rows, 4)+ return c+++def window(spec, ctx_of, H, cache_rows=None, q="normal", codes="normal", scales="pow2"):+ """spec: per stream (rows, ctx, sparse?) -> (qa, cache, rows, tokens, counts). Sparse streams' rows share the+ stream's 2,048 selected pool tokens plus their own incomplete pool (seg_select's shape). Each stream's extent of+ the arena holds its rows' keys (ctx + rows + 64 rows), one after another."""+ R = sum(r for r, _, _ in spec)+ ext = [0]+ for nr, ctx, _ in spec:+ ext.append(ext[-1] + ctx + nr + 64)+ cache = cache_for(cache_rows or ext[-1], codes, scales)+ rows = SegRows(R, dev, max_segs=max(4, min(len(spec), 8)))+ pos, base, spr = [], [], []+ toks = torch.full((R, TOK + 1), -1, dtype=torch.int32, device=dev)+ counts = torch.zeros((R,), dtype=torch.int32, device=dev)+ r0 = 0+ for si, (nr, ctx, sp) in enumerate(spec):+ if sp:+ pools = torch.randperm(ctx // 4 - 1, device=dev, generator=g)[:sparse.TOPK_POOLS].sort().values+ t = (pools[:, None] * 4 + torch.arange(4, device=dev)[None, :]).reshape(-1)+ for j in range(nr):+ p = ctx + j+ pos.append(p)+ base.append(ext[si])+ spr.append(1 if sp else 0)+ if sp:+ sel = torch.cat([t, torch.arange(p // 4 * 4, p + 1, device=dev)])[:TOK]+ toks[r0 + j, :sel.numel()] = sel.to(torch.int32)+ counts[r0 + j] = sel.numel()+ r0 += nr+ rows.pos.copy_(torch.tensor(pos, dtype=torch.int32))+ rows.base.copy_(torch.tensor(base, dtype=torch.int32))+ rows.sparse.copy_(torch.tensor(spr, dtype=torch.int32))+ if q == "normal":+ qa = (torch.randn((R, H, LW), device=dev, generator=g) * 0.5).to(torch.bfloat16)+ else: # 1e-30 .. 3e4 and +-0+ mag = 10.0 ** (torch.rand((R, H, LW), device=dev, generator=g) * 34.5 - 30)+ sgn = torch.randint(0, 2, (R, H, LW), device=dev, generator=g).float() * 2 - 1+ qa = (mag * sgn).to(torch.bfloat16)+ qa[torch.rand((R, H, LW), device=dev, generator=g) < 0.05] = 0.0+ qa[torch.rand((R, H, LW), device=dev, generator=g) < 0.05] = -0.0+ return qa, cache, rows, toks, counts+++def run(qa, cache, rows, toks, counts, cuda, hb=None, ctas=0, scale=0.0625):+ R, H, _ = qa.shape+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = "1" if cuda else "0"+ latent.SEG_CHUNKS_CTAS = ctas+ s = latent.LatentScratch(max(R, 32), H, NCH, dev, lw=LW)+ n = NCH * R * H+ for t in (s.po, s.pm, s.pl):+ t.fill_(float("nan"))+ out = torch.full((R, H, LW), float("nan"), dtype=torch.bfloat16, device=dev)+ latent.seg_attention(qa, cache, rows, toks, counts, s, scale=scale, out=out, hb=hb)+ torch.cuda.synchronize()+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = "1"+ latent.SEG_CHUNKS_CTAS = SHIPPED_CTAS+ return out, s.po[:n * LW].clone(), s.pm[:n].clone(), s.pl[:n].clone()+++def same(a, b):+ a, b = a.contiguous(), b.contiguous()+ v = torch.int16 if a.element_size() == 2 else torch.int32+ return torch.equal(a.view(v), b.view(v)), int((a.view(v) != b.view(v)).sum())+++def compare(name, w, hb=None, ctas=None, scale=0.0625):+ qa, cache, rows, toks, counts = w+ ref = run(qa, cache, rows, toks, counts, False, scale=scale)+ got = run(qa, cache, rows, toks, counts, True, hb=hb, ctas=SHIPPED_CTAS if ctas is None else ctas, scale=scale)+ res = [same(a, b) for a, b in zip(got, ref)]+ ok = all(r[0] for r in res)+ check(ok, f"{name}: out / PO / PM / PL identical to Triton's ({', '.join(str(r[1]) for r in res)} differ)")+ return ref+++def construct(loader, kv, kinds=("dsa",)):+ """decode.Engine's constructor with stand-ins for the buffers, caches, slots and state it builds; ``loader``+ (``module.name``) counted, nothing built. Returns its calls."""+ mod, name = loader+ calls = []+ real = getattr(mod, name)+ saved = [(decode, "Buffers"), (decode, "State"), (forward, "Caches"), (forward, "Slots"), (forward, "index_ring"),+ (hcsplit, "buffer_rows")]+ saved = [(m, a, getattr(m, a)) for m, a in saved]+ stub = SimpleNamespace+ try:+ setattr(mod, name, lambda: calls.append(name))+ decode.Buffers = decode.State = forward.Caches = forward.Slots = lambda *a, **k: stub(rows=1)+ forward.index_ring = lambda *a, **k: 0+ hcsplit.buffer_rows = lambda rows, *a, **k: rows+ os.environ["TF_GLM_L2PF"] = "0" # no L2 prefetch (it reads real weights)+ w = stub(meta={}, layers=[stub(kind=k) for k in kinds], world=3, rank=0, mtp=None, head=stub(n=1),+ cfg=stub(hidden=4096), device=torch.device("cuda"))+ decode.Engine(w, kv=kv)+ finally:+ setattr(mod, name, real)+ for m, a, v in saved:+ setattr(m, a, v)+ return len(calls)+++def timing():+ flush = torch.empty((256 << 20,), dtype=torch.uint8, device=dev)++ def graph_us(call):+ call()+ torch.cuda.synchronize()+ gr = torch.cuda.CUDAGraph()+ with torch.cuda.graph(gr):+ call()+ times = []+ for _ in range(20):+ flush.fill_(1)+ a, b = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)+ a.record()+ gr.replay()+ b.record()+ torch.cuda.synchronize()+ times.append(a.elapsed_time(b) * 1e3 / 11)+ return statistics.median(times)++ for H in (22, 21):+ for name, spec in (("4 x 8 @ 596k", [(8, 596000, 1)] * 4), ("4 x 4 @ 596k", [(4, 596000, 1)] * 4),+ ("4 x 2 @ 596k", [(2, 596000, 1)] * 4), ("1 x 16 @ 596k", [(16, 596000, 1)]),+ ("1 x 8 @ 596k", [(8, 596000, 1)]), ("1 x 1 @ 596k", [(1, 596000, 1)]),+ ("2 x 8 @ 300k", [(8, 300000, 1)] * 2), ("4 x 8 @ 180k", [(8, 180000, 1)] * 4)):+ qa, cache, rows, toks, counts = window(spec, None, H)+ tl = [toks] + [window(spec, None, H, cache_rows=16)[3] for _ in range(10)] # each layer its own tokens+ R = qa.shape[0]+ s = latent.LatentScratch(max(R, 32), H, NCH, dev, lw=LW)+ out = torch.empty((R, H, LW), dtype=torch.bfloat16, device=dev)+ res = []+ for env in ("0", "1"):+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = env+ res.append(graph_us(lambda: [latent.seg_attention(qa, cache, rows, t, counts, s, scale=0.0625, out=out)+ for t in tl]))+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = "1"+ print(f"TIME {H} heads, {name}: Triton {res[0]:.1f} -> CUDA {res[1]:.1f} us a call "+ f"({100 * (res[1] / res[0] - 1):+.1f}%; x11 layers a round {11e-3 * (res[0] - res[1]):.2f} ms)",+ flush=True)+ torch.cuda.empty_cache()+++check(hasattr(latent, "seg_chunks_cuda") and hasattr(latent, "_seg_chunks_ext"),+ "patch 0108 applied: latent.seg_chunks_cuda")+if fails:+ print("1 FAIL", flush=True)+ sys.exit(1)+NCH = latent.seg_chunks()+SHIPPED_CTAS = latent.SEG_CHUNKS_CTAS+++def main():+ # ---- decode windows+ for H in (22, 21):+ for name, spec in (("4 x 8 @ 596k", [(8, 596000, 1)] * 4), ("4 x 4 @ 596k", [(4, 596000, 1)] * 4),+ ("4 x 2 @ 596k", [(2, 596000, 1)] * 4), ("4 x 1 @ 596k", [(1, 596000, 1)] * 4),+ ("1 x 16 @ 596k", [(16, 596000, 1)]), ("1 x 8 @ 596k", [(8, 596000, 1)]),+ ("1 x 1 @ 596k", [(1, 596000, 1)]), ("2 x 8 @ 300k", [(8, 300000, 1)] * 2),+ ("4 x 8 @ 180k", [(8, 180000, 1)] * 4), ("3 x 5 @ 420k", [(5, 420000, 1)] * 3),+ ("uneven 1 / 7 / 3 / 5 @ 180k-600k",+ [(1, 600000, 1), (7, 180000, 1), (3, 333333, 1), (5, 512345, 1)])):+ ref = compare(f"H {H} {name}", window(spec, None, H))+ if H == 22 and name == "4 x 8 @ 596k":+ check(not torch.isnan(ref[0].float()).any(),+ "H 22 4 x 8 @ 596k: Triton's out has no NaN (a real case)")+ # ---- dense rows, mixed windows, ragged counts+ for H in (22, 21):+ spec = [(1, p, 0) for p in (0, 30, 31, 32, 510, 511, 512, 1023, 2047)] + [(3, 2049, 0), (4, 596000, 1)]+ compare(f"H {H} dense rows (pos 0 .. 2051) + a sparse stream", window(spec, None, H))+ qa, cache, rows, _, _ = window([(4, 2000, 0), (3, 700, 0), (2, 100, 0)], None, H)+ compare(f"H {H} a window without sparse rows (no token lists)", (qa, cache, rows, None, None))+ qa, cache, rows, toks, counts = window([(12, 300000, 1)], None, H)+ cnts = [0, 1, 31, 32, 33, 511, 512, 513, 1024, 2050, 2051, 1537]+ counts.copy_(torch.tensor(cnts, dtype=torch.int32))+ for r, c in enumerate(cnts):+ toks[r, c:] = -1 if r % 2 else 2 ** 30 # padding never read: -1, or far past the arena+ compare(f"H {H} sparse counts {cnts} (padded with -1 / 2^30)", (qa, cache, rows, toks, counts))+ # ---- adversarial data+ for H in (22, 21):+ compare(f"H {H} every e4m3 code (NaN codes read), arbitrary scales, softmax 192^-0.5",+ window([(8, 200000, 1)] * 2, None, H, codes="all", scales="any"), scale=192 ** -0.5)+ compare(f"H {H} queries 1e-30 .. 3e4 and +-0, arbitrary scales",+ window([(6, 250000, 1)] * 2, None, H, q="wide", scales="any"))+ qa, cache, rows, toks, counts = window([(4, 100000, 1), (3, 1500, 0)], None, 22)+ for r in range(7): # the rows' keys all -0 (code 0x80) in row 0's / row 4's chunk 0+ ids = (toks[r, :512].long() if r < 4 else torch.arange(512, device=dev)) + rows.base[r].long()+ cache[ids, :LW] = 0x80+ compare("H 22 keys all -0 in the first chunk", (qa, cache, rows, toks, counts))+ # ---- head tiles and CTA counts (speed constants: the same bits)+ w = window([(8, 596000, 1)] * 2 + [(2, 1800, 0)], None, 22, codes="all", scales="any")+ for hb in (16, 32):+ compare(f"H 22 head tile {hb} (CUDA) vs the default tile (Triton)", w, hb=hb)+ for ctas in (1, 7, 48, 0):+ compare(f"H 22 SEG_CHUNKS_CTAS {ctas}", w, ctas=ctas)+ # ---- one CUDA graph replayed over new positions, tokens and queries+ H = 22+ qa, cache, rows, toks, counts = window([(8, 400000, 1)] * 3, None, H)+ s = latent.LatentScratch(32, H, NCH, dev, lw=LW)+ out = torch.full((24, H, LW), float("nan"), dtype=torch.bfloat16, device=dev)+ side = torch.cuda.Stream()+ side.wait_stream(torch.cuda.current_stream())+ with torch.cuda.stream(side):+ latent.seg_attention(qa, cache, rows, toks, counts, s, scale=0.0625, out=out)+ torch.cuda.current_stream().wait_stream(side)+ gr = torch.cuda.CUDAGraph()+ with torch.cuda.graph(gr):+ latent.seg_attention(qa, cache, rows, toks, counts, s, scale=0.0625, out=out)+ for k in range(3):+ q2, _, r2, t2, c2 = window([(8, 399000 - 1000 * k, 1), (8, 380000 + 777 * k, 1), (8, 399000 - 555 * k, 1)],+ None, H, cache_rows=16)+ qa.copy_(q2)+ toks.copy_(t2)+ counts.copy_(c2)+ rows.pos.copy_(r2.pos)+ out.fill_(float("nan"))+ gr.replay()+ torch.cuda.synchronize()+ ref = run(qa, cache, rows, toks, counts, False)[0]+ check(same(out, ref)[0], f"CUDA graph replay {k} over new positions / tokens / queries: out identical "+ f"({same(out, ref)[1]} differ)")+ del gr+ # ---- routing+ ext = latent._seg_chunks_ext()+ cnt = Count(ext)+ latent._SEG_CHUNKS_EXT = cnt+ try:+ for name, H, kind, env, want in (("fp8, 22 heads", 22, "fp8", "1", 1), ("fp8, 21 heads", 21, "fp8", "1", 1),+ ("fp8, 22 heads, TF_GLM_SEG_CHUNKS_CUDA=0", 22, "fp8", "0", 0),+ ("bf16 arena, 22 heads", 22, "bf16", "1", 0),+ ("fp8, 16 heads", 16, "fp8", "1", 0), ("fp8, 32 heads", 32, "fp8", "1", 0)):+ qa, cache, rows, toks, counts = window([(4, 300000, 1)] * 2, None, H)+ if kind == "bf16":+ cache = kv8.dequantize(cache).to(torch.bfloat16).contiguous()+ s = latent.LatentScratch(32, H, NCH, dev, lw=LW)+ out = torch.empty((8, H, LW), dtype=torch.bfloat16, device=dev)+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = env+ cnt.n = 0+ try:+ latent.seg_attention(qa, cache, rows, toks, counts, s, scale=0.0625, out=out)+ torch.cuda.synchronize()+ check(cnt.n == want, f"routing {name}: the extension ran {cnt.n} time(s) (want {want})")+ except Exception as ex: # e.g. the extension refusing a shape routed to it+ check(False, f"routing {name}: {type(ex).__name__}: {str(ex)[:120]}")+ finally:+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = "1"+ latent._SEG_CHUNKS_EXT = ext+ os.environ.pop("TF_GLM_SEG_CHUNKS_CUDA", None)+ check(latent.seg_chunks_cuda(), "TF_GLM_SEG_CHUNKS_CUDA unset: on")+ loader = (latent, "_seg_chunks_ext")+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = "1"+ check(construct(loader, "fp8") == 1, "Engine(FP8 caches, DSA layers): the extension loaded at start")+ check(construct(loader, "bf16") == 0, "Engine(bf16 caches): not loaded (the kernel is FP8 only)")+ check(construct(loader, "fp8", ("mla",)) == 0, "Engine(no DSA layer): not loaded")+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = "0"+ check(construct(loader, "fp8") == 0, "TF_GLM_SEG_CHUNKS_CUDA=0: not loaded")+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = "1"+ # ---- the extension not used (a failed build or load; another Triton release): logged once, Triton serves+ import contextlib+ import io++ from tensorfold.cuda import build+ from tensorfold.families.glm5_next.cuda import sparse++ qa, cache, rows, toks, counts = window([(8, 300000, 1), (5, 450000, 1)], None, 22)+ ref = run(qa, cache, rows, toks, counts, False)+ real_load, real_ptx = build.load, sparse.TRITON_PTX+ for case, ptx, want_tries in (("a failed build", real_ptx, 1), ("another Triton release", "0.0", 0)):+ tries, buf = [], io.StringIO()++ def broken(*a, **k):+ tries.append(1)+ raise RuntimeError("Error building extension 'tensorfold_glm_segchunks_v1': nvcc not found")++ build.load, sparse.TRITON_PTX, latent._SEG_CHUNKS_EXT, latent._SEG_CHUNKS_FAILED = broken, ptx, None, None+ raised = first = second = got = None+ on = True+ try:+ with contextlib.redirect_stdout(buf):+ first, second = latent._seg_chunks_ext(), latent._seg_chunks_ext()+ on = latent.seg_chunks_cuda()+ got = run(qa, cache, rows, toks, counts, True)+ except Exception as exc:+ raised = exc+ finally:+ build.load, sparse.TRITON_PTX, latent._SEG_CHUNKS_EXT, latent._SEG_CHUNKS_FAILED = (real_load, real_ptx,+ ext, None)+ logged = buf.getvalue().count("seg_chunks.cu is not used")+ check(raised is None and first is None and second is None and len(tries) == want_tries and not on+ and logged == 1,+ f"{case}: nothing raised ({raised!r}), logged once ({logged}), loader called {len(tries)} "+ f"(want {want_tries}), the switch reads off ({not on})")+ check(got is not None and all(same(a, b)[0] for a, b in zip(got, ref)),+ f"{case}: seg_attention gives the Triton kernel's output and partials")+ os.environ["TF_GLM_SEG_CHUNKS_CUDA"] = "1"+ if "--time" in sys.argv and fails == 0:+ timing()+ print("ALL PASS" if fails == 0 else f"{fails} FAIL", flush=True)+ sys.exit(1 if fails else 0)+++if __name__ == "__main__":+ main()