interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / docs / numeric / FB1_CUT5_ATTENTION.md

217 lines · 16662 bytes Raw Blame History Source

FB-1 cut 5: native gqa + paged attention (no CUDA)

Status: host battery PASS, host IR simulator PASS (45/45), chip gate PASS (45/45, receipt ~/workspace/evidence-out/FB1-CUT5-b016322, PR #261). Files: src/omega_gpu_attention_api.{h,c}, tests/gpu_attention_test.c, tools/run_gpu_attention_chip.sh. Library: libomega_gpu.a.

DOCS READ (recorded with docs-read omega ... before the codegen edits)

Oracle (aien-sovereign-core crates/aien-inference-abi/src/backend.rs, main 8b5caed)

Documented deviation: a block id >= layout.num_blocks is refused with BAD_ARGS; the oracle reads zeros for it. A layer index past num_layers is refused the same way.

Kernel

One CTA per (query head = CTAID.X, sequence = CTAID.Y), 64 threads, grid (num_q_heads, sequences per launch) with at most 64 CTAs per launch (I42 envelope). Shared memory map (1024 bytes): [0,256) q, [256,512) scores then probabilities, [512,768) per-token byte offsets, [768,1024) warp partials.

The context is walked in chunks of 64 tokens with online softmax:

  1. Phase A, thread = token: block-table lookup, K.q over 64 dims in f32 FFMA (K read as 32-bit pairs with LDG.E: two f32 dims or two bf16 dims per load), s = dot * scale with scale = log2(e) / sqrt(64) folded in so EX2 gives e^x.
  2. Chunk max: SHFL.DOWN tree per warp, two warp partials through shared memory, BAR.SYNC. alpha = EX2(m_old - m_new), p = EX2(s - m_new), chunk sum the same way, L = L * alpha + sum, acc *= alpha.
  3. Phase B, thread = dimension: acc += p_t * V[t][tid] over the chunk's tokens (V bf16 by LDG.E.U16, f32 by LDG.E).
  4. Epilogue: out = acc * RCP(L). The first chunk starts with m = -inf, so alpha = 0 wipes the initial L = 1, acc = 0. Context 0 jumps straight to the epilogue (zeros).

Both kernels are 152 instructions, 42 GPRs, 6 UGPRs (budget 64 GPRs). Kernels are specialised on {kv f32 or bf16, log2 block size, log2 gqa ratio} and cached (8 slots). The gqa path stages [K plane | V plane] as one 4096-slot block, so context <= 4096 there. The paged path stages the referenced blocks' layer slice renumbered in table order; the kernel itself walks the (renumbered) block table.

Tolerances

Mutants (deliberately wrong kernels the test must catch)

mutantwhat it breakswhere it shows
NO_MAXno running-max subtractiononly when EX2 overflows: the large-score case (softmax is shift invariant on unit-scale data)
KV_HEADreads kv head (h / ratio) ^ 1every context length including 1
SLOTK read from slot (t + 1) % block_size, V from slot tevery context >= 2 (with one token the softmax weight is 1 whatever K says). Shifting both K and V would only permute matched pairs inside a full block, which softmax cannot see, so the mutant is K-only
NO_RESCALEaccumulator not multiplied by alpha when the max movescontext > 64 (more than one chunk)
Q_ROWq loaded from query-head row h ^ 1 (kv head and output row stay h): a wrong query-head-row addressevery context (heads h and h^1 come out swapped); needs num_q_heads even
OUT_ROWthe correct result for head h stored in flattened output row h ^ 1: a wrong output-head slot (merge order)every context; needs num_q_heads even

The enum ends in OMEGA_GPU_ATTN_MUTANT_COUNT; every battery (host-only digests, simulator, chip, JSON mutants summary) enumerates 1 .. COUNT-1 and fails if any mutant never ran or was ever missed, so a mutant added to the enum without a battery entry fails the gate. Host-only also checks that every mutant's kernel digest differs from the baseline and from every other mutant's. Names come from omega_gpu_attention_mutant_name (table checked against COUNT).

Negative control: the comparison must reject the oracle with kv heads swapped. Repeat run on a cache hit must be bit-identical.

Single vs batched parity and repeat (L6-KV, 2026-10-05): for the 3-sequence batch (padded table entries, an empty context, a context truncated by its table) and both shared-prefix batches, each sequence is run alone through omega_gpu_paged_attention_bf16 with the batch's rules applied by hand (negatives removed, ctx <= 0 -> 0) and must equal its batched row bit for bit; then the batch is called again and must be a kernel cache hit, bit-identical, with the same staging counters. A throwaway run comparing each sequence against the neighbouring row failed all 11 single-call checks (the check has teeth). Host simulator: 122 checks (was 108).

Staging dedupe per launch (OM-2)

The paged path stages the referenced blocks' layer slice into a GPU-uncached staging buffer. Within one batched launch the staged slot is now keyed by the physical block id: a block that several sequences reference (a shared prompt prefix) is copied once and every row of the renumbered block table points at the same slot. OmegaGpuAttnInfo reports the effect: kv_blocks_logical (table entries walked), kv_blocks_unique (slices actually copied), kv_bytes_staged (bytes copied) and kv_bytes_naive (what per-reference staging would have copied); kv_source is OMEGA_GPU_ATTN_KV_STAGED (0) in this cut, there is no resident KV (see "Limits"). omega_gpu_attention_test_set_dedupe(false) restores per-reference staging as a negative control; the test proves the legacy counters equal the naive numbers and that the deduped output is bit-identical to the legacy output. Simulator evidence, 4 sequences sharing 10 scattered prefix blocks with private tails {1,2,3,3}: 8q/2kv (one launch) 19 unique of 49 logical, 155648 of 401408 bytes; 32q/8kv (two launches, dedupe is per launch) 29 of 49, 950272 of 1605632 bytes.

Head and kv-head scaling sweep (gpu_attention_test --sweep --out <json>)

q heads {1,2,4,8,16,24,32,64} x every divisor as kv heads x context {256, 2048}, bs 16, scattered block ids with a shared prefix across 4 sequences. Non power-of-two ratios (the 24-head rows) must be refused with BAD_ARGS and are recorded as unsupported_by_kernel_envelope. Supported points: oracle parity on the first call, then N timed calls (20 on chip, 1 under --sim), recording median/p90 call and kernel milliseconds, launches, CTAs, threads per CTA, q/out widths and the staging counters above. Schema OMEGA_GPU_ATTENTION_SWEEP_V1; the file says host_simulator and that hardware counters are unavailable (no invented counters). The sweep asserts no timing thresholds; the 3 ms gate stays in --timing.

Host IR simulator (gpu_attention_test --sim)

Executes the allocated IR program (physical registers, so the register allocator's loop handling is under test) for every CTA: 64 threads, 1 KB shared, host pointers as addresses, SHFL and BAR as lockstep points. It found the one kernel bug before chip time: the kv-head byte offset was added to the V base twice (once inside the per-token offset phase A leaves in shared memory, once in the V column base), so every head mapped to a kv head other than 0 read V from the wrong head. It also found a kernel-cache key compared with memcmp over struct padding (cache misses at random). ATTN_SIM_TRACE=<ctaid.x> prints thread 0 of that CTA instruction by instruction.

Limits of this cut

head_dim 128 (2026-10-07)

Qwen3-4B: 32 query heads, 8 KV heads (GQA 4:1), head_dim 128. head_dim is now a kernel parameter (KSpec.hd, 64 or 128) instead of a constant; the head_dim 64 program is the same bytes as before (see Evidence). Status: host battery, host IR simulator and host warp simulator PASS at head_dim 128.

GB10 (2026-10-07, omega 7dd92dc = omega#323 on main 917e8b9 with the omega#324 code-buffer tail): hd128 battery PASS 126/0 including every ctx256 case, hd64 regression 122/0, sweep 73/0, timing 2/0, no Xid. Receipts: evidence/FB1-CUT5-HD128-7dd92dc/. The first window (pre-omega#324) faulted at ctx256 with Xid 31 (evidence/FB1-CUT5-HD128-5a9f759/, kept); cause and red/green in omega#324, evidence/CODEPAD-PREFETCH-f1d57c5/.

Where 64 was assumed (omega main c0369e6, before this change) and what changed

AssumptionFile:line (c0369e6)head_dim 128
HD = 64, threads per CTA = head_dimomega_gpu_attention_api.h:76, .c:215, .c:612 (launch threads_x), .c:627threads_x = KSpec.hd; QMD CTA width 128
head_dim refused unless 64.c:663, header comments .h:43, .h:7064 and 128 accepted, anything else TOO_LARGE
shared layout 4 x 256 B: q 0, s/p 256, token offsets 512, warp partials 768.c:216-219, .c:240-242, .c:2864 x 512 B: 0, 512, 1024, 1536 (4hd, 8hd, 12hd)
QMD shared size 1024 (default)omega_blackwell_qmd.c:117 (shared_bytes == 0 gives 8 x 128 B), session launch omega_gpu_session.c:167launcher passes shared_bytes = 16 * hd: 1024 (same QMD word as before) or 2048
2 warps: partials read at 768 and 896 (c768, c896).c:241-242, .c:381, .c:4014 warps: partials of warps 2 and 3 at +256 and +384, two extra LDS and two extra FMNMX / FADD per reduction (emitted only for hd 128)
chunk = 64 tokens: base += 64, phase B loop bound 64 (c64).c:241, .c:423, .c:428c64 holds hd (chunk size)
dim-pair inner loop bound 32 (c32).c:241, .c:361c32 holds hd / 2
q / out row stride HD * 4.c:253, .c:261, .c:688, .c:705, .c:730head_dim argument
score scale log2(e) / sqrt(64).c:658, .c:705, .c:814score_scale(head_dim)
paged layout must have head stride HD * 2.c:726head_dim * 2
kernel cache key lacks head_dim.c:528key includes hd
per-thread / warp simulators: 64 threads, 1024 B sharedtests/gpu_attention_test.c:535-536, :792sized from --hd; shared bound is the QMD declaration (16 * hd)
Not 64-dependent, unchanged32-lane SHFL tree (.c:376-378), 16 argument words, MAX_CTAS 64, MAX_Q_HEADS 64, GPR budget 64Qwen3-4B 32 heads: 2 sequences per launch

Limits for one head_dim 128 CTA (GB10, SM 12.1)

ResourceNeedLimitSource
Shared memory per block2048 BSM 12.x SMEM capacity 0 to 100 KB per SM; 49152 B per block is shown for a CC 10.0 exampleCUDA Programming Guide, Compute Capabilities appendix (Context7 /websites/nvidia_cuda_cuda-programming-guide): "Compute Capability 12.x has 128 KB of Unified Data Cache and SMEM capacities from 0 KB up to 100 KB"; "sharedMemPerBlock (49152 bytes)" (printed for CC 10.0; the 12.x per-block row was not returned: DOCS SILENT on the exact 12.1 per-block figure)
Registers per block128 threads x 64 GPR = 819264K 32-bit registers per block, 255 per threadsame appendix: "The maximum number of 32-bit registers per thread block is 64K, and per thread is 255"
Threads per block128DOCS SILENT (the retrieved snippets give 2048 threads per SM, not the per-block cap)64 threads already run on the chip (receipt FB1-CUT5-cd80bf4); QMD field is 16 bits wide (clcec0qmd.h:248, NVCEC0_QMDV05_00_CTA_THREAD_DIMENSION0 MW(1103:1088))
QMD shared field2048 B = 16 units of 128 Bfield is 11 bits, NVCEC0_QMDV05_00_SHARED_MEMORY_SIZE_SHIFTED7 MW(1162:1152) (clcec0qmd.h:254); MIN/MAX/TARGET SM_CONFIG fields (:255-257) keep the value omega already sends (word 36)the header gives field widths only: DOCS SILENT on what SM_CONFIG code 9 allows; UNKNOWN until the chip run (OMEGA_BW_QMD_MAX_SHARED_BYTES 16384 is omega's own bound)
BAR.SYNC with 128 threadsone barrier, all CTA threadsQMD declares 1 barrier (omega_blackwell_qmd.c:112)INFERRED: since Volta the barrier counts arriving threads (see tests/bw_warp_sim.h header); not chip-verified for sm_121

Evidence (CPU only, 2026-10-07, omega main c0369e6 + this change)

GB10 parity plan (later, coordinated window; NOT_RUN)

See the pull request body for the commands, shapes, tolerance and hold estimate.