# Ergo decode on trn3 One program runs a whole decode step: all 64 layers and the device LM head for R requests x S tokens (S = 1 when serving), on 128 ranks at LNC1. ## Files | File | Role | |---|---| | `attn.py` | decode attention: per-step bias prologue, single-pass attention, 16-device merge; the paged block loop for long contexts | | `moe.py` | routed experts, EP16 x TP8 (the prefill pack's layout): run-time loop over the touched experts, or static over all 16 at large batch | | `moe_tp.py` | routed experts, expert-TP (`pack_dec.py` layout): every expert split over the 128 ranks | | `proj.py` | QKV / o_proj / shared-expert (and dense layer 0) GEMMs split over the 16 devices | | `layer.py` | one decode layer (`decode_layer_body`) | | `head.py` | final norm, vocab-sharded LM head and greedy argmax on the device | | `first.py` | `ergo_first_token`: the first token from the prefill's last rows | | `step.py` | `ergo_dec_step`: layers 0..63 in one program over the whole-model weight stacks and the paged KV | ## Layout * A step has Bp = 8 Tr token rows (Tr in 1, 2, 4, 8, 16, sized by the batch bucket). Every device holds every row: rank (d, j) owns its share of the fp32 residual's rows, identical on all 16 devices. Row-wise elementwise work (norms, gated residuals) runs on the view of the own rows (block-ones matmul for the row sums). * bf16 projections: every device holds the same TP8 weights (the prefill's layout), so device d computes only its 1/16 column slice (QKV: 80 of 1280 columns; o_proj: 512 of 8192; shared / dense: 64 / 256 intermediate columns), and the head group (one rank per device, same j) exchanges slices. bf16 GEMMs stream weights through the PE at ~350 GB/s a core; the split cuts that work 16x for one collective per projection. * Attention, rank (d, j) = KV head j: * global layers (NoPE, causal): the KV of position p lives on device (p // 1024) % 16 in its page (the prefill's store layout, `serve/PAGING.md`), so a prefill hands its stores over row for row. Each device computes the partial (O, max, sum) over its share for all R requests in one pass (<= 2048 rows a device; beyond, a run-time loop over the request's page table); an all_to_all sends request i's 16 partials to one device, which merges them (vectorized log-sum-exp), and a head-group all-gather of the bf16 outputs rebuilds every request. * long contexts: the grouped loop packs two requests a 32-partition band (`ERGO_LG_PACK2`) and prefetches the next page's DMAs under the current block (`ERGO_LG_PIPE`); both default to on for steps of R >= 8. * local layers (RoPE, window 511): request i's ring of 1024 positions (index p % 1024) lives on device g % 16 (ring slot g // 16); each device computes only its requests, a head-group all-gather rebuilds every request's rows. * all score masks are built once per step on the device (`build_biases`) and shared by the layers. * KV appends: the new rows of each request go to the owner device's store row (the others to the sink page) or the ring index, before the reads. * MoE, EP16 x TP8 (`moe.py`), device d = 16 experts, rank j = their up I-slice j and down H-slice j: * x quantized once (MX, K-major), route weights of the device's experts from the router. * small batches: a run-time loop over the touched experts only (3 weight buffers in flight, expert indices read ahead, fetches past the list skipped with `oob_mode.skip`). * large batches: static code over all 16 experts (untouched ones weighted 0): equal work on every device, static weight DMAs. * expert RMSNorm + route weight + sum in expert order; combine = a fixed-order reduction over the head group (`kernels/fixed_order.py`), so a request's bytes do not depend on its batch row. * the shared expert is issued while the first experts' weights stream in. * MoE, expert-TP (`moe_tp.py`): every rank holds 1/128 of every expert (its up columns and down columns of each), so a touched expert's weights stream from all 128 ranks at once and the load is balanced whatever the routing. h is all-gathered over the world between up and down. Used on a PD decode up to 32 requests a step (16 rows x 8 routes fill one 128-column pair tile, 32 two). `pack_dec.py --blob` writes a slot-blob variant (a slot's up and down bytes in one run) for two DMAs a touched expert instead of four; the plain decode pack stays the default. * Head: final norm, LM head rows 2048 r .. 2048 r + 2047 on rank r, first-max argmax, world all-gather of (max, id) -> token ids. * Global / local K is loaded already transposed by a hardware-DGE `dma_transpose` ([1, n, d] -> [d, n, 1]). ## The serving prefill writes the decode caches `kernels/layer.py ergo_segment(..., serve=True)` runs a prompt chunk straight into the decode caches of its cache slot g (rinfo row = (d, j, c, g, L_c, L0, o, src); one program per (dense_first, prior tier) serves every segment position): * global layers: chunk c's block goes to its page `ptab[c]` of the store pool; * local layers: the device holding position L - 1 builds the ring image of positions [L - 512, L) (its block up to (L - 1) % 1024, its halo above); a head-group all-gather brings it to device g % 16, which writes ring slot g // 16 (the other devices write the scratch slot); * a partial last chunk is zero-padded (L_c bounds the export; decode masks positions >= L); * `first.py ergo_first_token`: the first token (row L - 1 all-reduced, device LM head argmax). ## Hardware notes * bf16 matmul streams the moving operand at ~350 GB/s a core, MXFP8 at ~560 GB/s; DMA ~614 GB/s a core. * Run-time-offset DMAs must use the hardware DGE (`dge_mode=hwdge`, engine sync or scalar); on the software DGE the expert loops were descriptor-bound. Vector-offset (indirect) DMAs need the software DGE. * Each run-time-offset DMA costs its issuing engine ~1 us (register load + trigger); the hwdge queues are FIFO, so a DMA waiting for a dependency blocks the queue behind it. * A tile partially written inside `nl.fori_loop` is loop-carried (NCC_IGCA108); memset it (MX scale tiles, padded runs). * Small DMA runs (< ~512 B) cost DMA-engine time out of proportion; pack per-partition data into longer runs. * A slot index past an HBM buffer is NRT_EXEC_OOB (1006) at run time, not a compile error. * Decode stores past 2^31 bytes a rank (8-16 slots at 1M) are reached correctly with block-scaled dynamic offsets. ## Limitations * One step per execution: each step pays the runtime's per-execution launch overhead (~4 ms). * At small batches on EP the most loaded device sets the MoE's pace; expert-TP removes that skew at the cost of a world all-gather. * At 1M context the step is dominated by the global layers' page loop (~55 % of an R 8 step).