Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/58d8c74d48b91092 # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## feat(q38): Qwen3.8-27B decode token as one command list (glue widths, GATHER, GATE, QMD 32 KiB) merged `58d8c74d48b91092` #559 路 opened 2026-10-18 15:51 UTC 路 closed 2026-10-24 17:56 UTC 路 merged 2026-10-24 17:56 UTC Imported from GitHub aien-dev/omega-mv#559, by drakestapleton Base `2ef40a1538c6cf85b39fc511da35cb85f13c5649` 路 Head `b11bd1f632afab7f7ec5368f8a618b04179fed63` Merged on GitHub as `b11bd1f632afab7f7ec5368f8a618b04179fed63`; reachable from main `c7acd2607139eedfabeac16b6042cefe8ede431f` (observed 2026-11-18T16:35:56Z) drakestapleton wrote on 2026-10-18 15:51 UTC ## What One Qwen3.8-27B decode token as one command list on the GB10 (sc #642): a dense hidden-5120 model mixing Gated DeltaNet layers (48) and full attention with head_dim 256 (16, every 4th). Stacked on #553 (prefill attention head_dim 256). - Glue kernels at the new widths (`omega_blackwell_codegen_dec`): RES generic over the hidden width plus a rows form, SWIGLU width, new GATHER (the attention kernel's permuted q|k|v row out of the fused projection) and GATE (sigmoid output gate). The default 2048-wide programs are byte-identical to before. - `omega_gpu_q38_graph`: per layer FP8 mix GEMV (fused qkv|z or q|gate|k|v), bf16 a|b GEMV (rows uploaded at 2^-64 so it reads the same 2^64-scaled row), GDN step or GATHER + attention256 + GATE, FP8 o_proj, ADD|NORM, FP8 gate_up, SWIGLU, FP8 down, ADD|NORM; then bf16 lm_head and a two-stage argmax. 596 launches for the whole model. - QMD shared-memory cap 16 -> 32 KiB: the bf16 GEMV at k 5120 needs 20 KiB. Word 36 already encodes MIN/TARGET 32 KB (clcec0qmd.h:252-255, mesa carve-out table); above 32 KiB would need per-size fields (follow-up). ## Evidence (chip) `evidence/q38-graph-chip-20261018T1547Z`: encode control 480/480, glue host 27/27, glue sim 74/74, graph host-only 4/4, graph chip 17/17 at 2 layers (GDN + attention) and 17/17 at 4 layers (3 GDN + attention): the one-list graph matches the per-call baseline and the submit-after-every-launch path, and the four mutants are caught. Red receipt kept: `evidence/q38-graph-chip-20261018T1543Z` (baseline refused by the 16 KiB cap). Timeline of one token, 4 layers + lm_head (GB/s of 273): lm_head 219 (calc:`851bed1a`), gate_up 191 (calc:`bba42e4b`), mix 189 (calc:`4750e7aa`), o_proj 167 (calc:`ebe87299`), down 143 (calc:`5a02eca5`). Speed work tracked in #558. ## Not in this PR Rust binding and engine, full-model parity, prefill (sc #642 next steps). No merge requested while the merge pause holds. 馃 Generated with Claude Code ### Fetch it A git bundle of the pull request's head, 34821 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/58d8c74d48b91092.bundle -o 58d8c74d48b91092.bundle git fetch 58d8c74d48b91092.bundle refs/pull/559/head ``` Raw bundle 路 Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 559 b11bd1f632afab7f7ec5368f8a618b04179fed63 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":559,"url":"https://github.com/aien-dev/omega-mv/pull/559","export_sha256":"7319147b350ad908bf0b731068ec3be7b8dee848041dc862f71455e8e4680140","imported_utc":"2026-11-18T16:35:56Z"} ``` ### Changes ``` diff --git a/.crumb b/.crumb index 2662ceb59..f323e2751 100644 --- a/.crumb +++ b/.crumb @@ -77,33 +77,33 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "fbe222132e0f413ae3f4230a7b3389a2c0fcca3a56fe1371eac7406658b21843", - "last_commit": "e3a26848", + "digest": "db2faa78b8de104dc889d825f567019be0cf72e173fe50f06df1b0e289bbe283", + "last_commit": "ce155d47", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "ef8690a977452cb604669c5e0f11a801b873f033c617e8530fa90b4a0074c111", - "last_commit": "acdeff74", + "digest": "27d5b38466b67976c685bfeb81d8c9fc6b5a8064e20a635649986f1f7fc84d54", + "last_commit": "3c90e430", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "e4a329c378a5873cf01d3636c85097718dcfb3e4ff4da8a1bf45801d2db96d21", - "last_commit": "acdeff74", + "digest": "532e8ff9eec685c6b8bec0eefbdb88d01b054e13fcd2cc83796bc9b8a6bddd60", + "last_commit": "ce155d47", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:a51accb120e9d3d8528c58b834196e71a63f5479c2bd16ad51c52732cee216a5", - "compiled_at": "2026-10-18T15:21:47Z", + "children_root": "sha256:6622051a6ccd17b7711d1ac7a6401b6059254d5a1e8f95dd56d99a98ecff6b60", + "compiled_at": "2026-10-18T15:51:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:2212d906a596f2560a3da6697cf4b31463d4bf348f2edd64db7475c9e7df7a80", - "evidence_files": 11245, - "evidence_root": "sha256:30a0ca91b7c3c0edbbae3712f5e1aef5cb5721cdd47ed5b628ceaf6f1a2d7997", - "files": 14245, - "generated_at_commit": "acdeff745c292cebb3b22cc7ae298409a0b7ee64", - "source_tree": "sha256:d89bdfc1e5b91f5891e76f2a3c7cd6991c2c3ffd82a99f1027abdf8d9403dbd4" + "digest": "sha256:f3705287ee8a6ad5b7632ae0d86499529a2c9c6bc6be341bcda6eb9055c15077", + "evidence_files": 11267, + "evidence_root": "sha256:46fe85934acd649948670bab6662ea04f780bd0bbe0f1a3eb4820b9bd482c5c5", + "files": 14271, + "generated_at_commit": "3c90e430b5282f00f426c2421200d1e8485f5ebd", + "source_tree": "sha256:83fba460728a53a8dcf3d2c933bf826fc00ef8b95d6e40fd8b3928634080bc69" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index e2b958d72..00d8968d4 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,8 @@ SRCS += src/omega_blackwell_codegen_gdn.c src/omega_gpu_gdn_api.c SRCS += src/omega_gpu_cmdlist.c src/omega_blackwell_codegen_dec.c SRCS += src/omega_gpu_mem_cached.c SRCS += src/omega_gpu_decode_graph.c +# sc #642 (2026-10-18): one Qwen3.8-27B decode token as one command list (GDN + hd256 attention layers, dense MLP) +SRCS += src/omega_gpu_q38_graph.c # OWN-30B (2026-10-13): bf16 streaming GEMV for the lm_head (omega_gpu_bf16_gemv_api.h), new files only SRCS += src/omega_blackwell_codegen_bf16.c src/omega_gpu_bf16_gemv_api.c # OWN-30B prefill (2026-10-13): FP8 grouped GEMM, each expert streamed once per pass (omega_gpu_fp8gemm_api.h), new files only @@ -2645,3 +2647,11 @@ $(GPU_LAUNCH_COST_TEST): tests/gpu_launch_cost_test.c src/omega_gpu_fp8_api.h sr GPU_L2_REUSE_TEST = $(OUT_DIR)/gpu_l2_reuse_test $(GPU_L2_REUSE_TEST): tests/gpu_l2_reuse_test.c src/omega_gpu_fp8_api.h src/omega_gpu_cmdlist.h $(OUT_DIR)/libomega_gpu.a $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_l2_reuse_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm + +# sc #642 (2026-10-18): Qwen3.8-27B decode graph (one token = one command list), chip test vs the APIs one call at a time +.PHONY: test-gpu-q38-graph-host +GPU_Q38_GRAPH_TEST = $(OUT_DIR)/gpu_q38_graph_test +$(GPU_Q38_GRAPH_TEST): tests/gpu_q38_graph_test.c src/omega_gpu_q38_graph.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_q38_graph_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-q38-graph-host: $(GPU_Q38_GRAPH_TEST) + ./$(GPU_Q38_GRAPH_TEST) --host-only diff --git a/evidence/q38-graph-chip-20261018T1543Z/SHA256SUMS b/evidence/q38-graph-chip-20261018T1543Z/SHA256SUMS new file mode 100644 index 000000000..fe52da933 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/SHA256SUMS @@ -0,0 +1,10 @@ +eee4097acea09e650f1946be5697f88a9c3827255585940b002f459ff8343ce7 build.log +37a6d523d0f3cdcf6e9d12e39895bbc8daff6e6a276144a0d16f55d68905e32c encode-control.log +9565a803dc089f73b58f1d6b475fed22f65f0448e24498d5d2b2902bdd3b033c glue-host.log +403eeac58828469f634273f2f87ab0edf027406ad66cc4e8bb0b06177def342f glue-sim.log +94f970accd1a57851d7a5df0f4ad882aa29bc7197878cf151e5578029475eed3 host-only.log +32c594ebdb48274ed4b0f220b94e369c03052c849cca21749f00e57d4489cb22 layers4.log +e4d91e56265fa6864d92c78db3262122ff2ee04f28ea3e5fd43f57306bbe7bec main.log +30c4e0edead0e24232046493b97c230d970389e3e2357b0afd4e0bb1baf0bd8e device.txt +b4d95767b719aa6bf80fbba90fd9f8b7f0caa669dd1343cd2f4fa3d090be8fb4 worktree.diff +bb187b0f106735365879f159aa22da0a459794c1a831fb142de0274e1e196a32 binaries.sha256 diff --git a/evidence/q38-graph-chip-20261018T1543Z/binaries.sha256 b/evidence/q38-graph-chip-20261018T1543Z/binaries.sha256 new file mode 100644 index 000000000..35ac1268b --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/binaries.sha256 @@ -0,0 +1,3 @@ +26a9048eb2ff53e535724377876b9fc3e2ca410e7d0a293f6e399744baf161a8 ./build/gpu_q38_graph_test +ca4e2338e5da7bae8f6d564b7eaebb3d6aa7c0472873bfe17586331b7fc83d2b build/gpu_cmdlist_test +987542e2429ae7af7e5d31737f44e38d9687142847b94abe9da9c0179db9168a build/libomega_gpu.a diff --git a/evidence/q38-graph-chip-20261018T1543Z/build.log b/evidence/q38-graph-chip-20261018T1543Z/build.log new file mode 100644 index 000000000..347417d79 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/build.log @@ -0,0 +1,3 @@ +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I/home/drakestapleton/workspace/physics-own30b-k/m16 -I/home/drakestapleton/workspace/physics-own30b-k/nvrm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -ffp-contract=off -o build/gpu_q38_graph_test tests/gpu_q38_graph_test.c build/libomega_gpu.a -lpthread -lm +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I/home/drakestapleton/workspace/physics-own30b-k/m16 -I/home/drakestapleton/workspace/physics-own30b-k/nvrm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -ffp-contract=off -Itests -o build/gpu_cmdlist_test tests/gpu_cmdlist_test.c build/libomega_gpu.a -lpthread -lm +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I/home/drakestapleton/workspace/physics-own30b-k/m16 -I/home/drakestapleton/workspace/physics-own30b-k/nvrm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -o build/bw_encode_control_test tests/bw_encode_control_test.c build/libomega_gpu.a -lpthread -lm diff --git a/evidence/q38-graph-chip-20261018T1543Z/device.txt b/evidence/q38-graph-chip-20261018T1543Z/device.txt new file mode 100644 index 000000000..80f566455 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/device.txt @@ -0,0 +1,6 @@ +start 2026-10-18T15:43:42Z omega=e842af2d7d026c2151ca0ce37f5b8b118fce10ff branch=feat/q38-graph dirty=6 physics=/home/drakestapleton/workspace/physics-own30b-k physics_commit=6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 + total used free shared buff/cache available +Mem: 121 105 11 0 11 16 +end 2026-10-18T15:43:51Z fail=1 diff --git a/evidence/q38-graph-chip-20261018T1543Z/encode-control.log b/evidence/q38-graph-chip-20261018T1543Z/encode-control.log new file mode 100644 index 000000000..b9e018794 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/encode-control.log @@ -0,0 +1 @@ +bw_encode_control_test: 480 encodings checked, 0 opcodes refused (no operands), 0 dropped the control word: PASS diff --git a/evidence/q38-graph-chip-20261018T1543Z/glue-host.log b/evidence/q38-graph-chip-20261018T1543Z/glue-host.log new file mode 100644 index 000000000..72d2c19e4 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/glue-host.log @@ -0,0 +1,8 @@ +codegen axpy: 23 insns, peak 12 gprs, declared 16 +codegen res add|norm: 128 insns, peak 40 gprs, declared 48 +codegen res combine|norm: 264 insns, peak 49 gprs, declared 56 +codegen res add: 45 insns, peak 28 gprs, declared 32 +codegen swiglu: 29 insns, peak 16 gprs, declared 24 +codegen argmax: 165 insns, peak 32 gprs, declared 40 +codegen argmax indexed: 160 insns, peak 40 gprs, declared 48 +host-only: 27 checks, 0 failed: PASS diff --git a/evidence/q38-graph-chip-20261018T1543Z/glue-sim.log b/evidence/q38-graph-chip-20261018T1543Z/glue-sim.log new file mode 100644 index 000000000..61ae9db56 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/glue-sim.log @@ -0,0 +1,29 @@ +sim res mode 5 scale 1 mutant 0: x identical, y 0/2048 outside tolerance +sim res mode 5 scale 1 mutant 1: x identical, y 2047/2048 outside tolerance +sim res mode 6 scale 1 mutant 0: x identical, y 0/2048 outside tolerance +sim res mode 6 scale 1 mutant 1: x DIFFERS, y 2047/2048 outside tolerance +sim res mode 5 scale 2^64 mutant 0: x identical, y 0/2048 outside tolerance +sim res mode 5 scale 2^64 mutant 1: x identical, y 2047/2048 outside tolerance +sim res mode 6 scale 2^64 mutant 0: x identical, y 0/2048 outside tolerance +sim res mode 6 scale 2^64 mutant 1: x DIFFERS, y 2048/2048 outside tolerance +sim swiglu mutant 0: 0/6144 outside tolerance +sim swiglu mutant 1: 6144/6144 outside tolerance +sim argmax tie 0 mutant 0: index 151625 value 49.9996 (ref 151625) +sim argmax tie 0 mutant 1: index 151625 value 49.9996 (ref 151625) +sim argmax tie 1 mutant 0: index 1007 value 60 (ref 1007) +sim argmax tie 1 mutant 1: index 28784 value 49.9999 (ref 1007) +sim res width 5120 mode 5 mutant 0: x identical, y 0/5120 outside tolerance +sim res width 5120 mode 5 mutant 1: x identical, y 5120/5120 outside tolerance +sim res width 5120 mode 4 mutant 0: x identical, y 0/5120 outside tolerance +sim res width 5120 mode 4 mutant 1: x identical, y 5120/5120 outside tolerance +sim res width 5120 mode 1 mutant 0: x identical, y 0/5120 outside tolerance +sim swiglu width 17408 mutant 0: 0/17408 outside tolerance +sim swiglu width 17408 mutant 1: 17407/17408 outside tolerance +sim res rows 3 x 5120 mutant 0: x identical, y 0/15360 outside tolerance +sim res rows 3 x 5120 mutant 1: x identical, y 15360/15360 outside tolerance +sim gather 3 x 14336 mutant 0: 0/43008 wrong +sim gather 3 x 14336 mutant 1: 43008/43008 wrong +sim gate 6244 mutant 0: 0/6244 outside tolerance +sim gate 6244 mutant 1: 6244/6244 outside tolerance +sim swiglu rows 3 x 17408: 0 outside tolerance +sim: 74 checks, 0 failed: PASS diff --git a/evidence/q38-graph-chip-20261018T1543Z/host-only.log b/evidence/q38-graph-chip-20261018T1543Z/host-only.log new file mode 100644 index 000000000..10bdb9d18 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/host-only.log @@ -0,0 +1,5 @@ +ok: gather table: q, k permuted, v and gate in place (0 wrong) +ok: gather table is a permutation of the 14336 mix values (0 repeats or out of range) +ok: create refuses an empty config +ok: create refuses NULL +gpu_q38_graph_test (host-only): 4 checks, 0 failed: PASS diff --git a/evidence/q38-graph-chip-20261018T1543Z/layers4.log b/evidence/q38-graph-chip-20261018T1543Z/layers4.log new file mode 100644 index 000000000..f20f4c30b --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/layers4.log @@ -0,0 +1,3 @@ +model: 4 layers (3 GDN, 1 attention, mask 0x8), vocab 248320 +FAIL: baseline token 0 +gpu_q38_graph_test: 1 checks, 1 failed: FAIL diff --git a/evidence/q38-graph-chip-20261018T1543Z/main.log b/evidence/q38-graph-chip-20261018T1543Z/main.log new file mode 100644 index 000000000..d7fab1d19 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/main.log @@ -0,0 +1,3 @@ +model: 2 layers (1 GDN, 1 attention, mask 0x2), vocab 248320 +FAIL: baseline token 0 +gpu_q38_graph_test: 1 checks, 1 failed: FAIL diff --git a/evidence/q38-graph-chip-20261018T1543Z/worktree.diff b/evidence/q38-graph-chip-20261018T1543Z/worktree.diff new file mode 100644 index 000000000..260b71425 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1543Z/worktree.diff @@ -0,0 +1,25 @@ +diff --git a/Makefile b/Makefile +index e2b958d7..00d8968d 100644 +--- a/Makefile ++++ b/Makefile +@@ -46,6 +46,8 @@ SRCS += src/omega_blackwell_codegen_gdn.c src/omega_gpu_gdn_api.c + SRCS += src/omega_gpu_cmdlist.c src/omega_blackwell_codegen_dec.c + SRCS += src/omega_gpu_mem_cached.c + SRCS += src/omega_gpu_decode_graph.c ++# sc #642 (2026-10-18): one Qwen3.8-27B decode token as one command list (GDN + hd256 attention layers, dense MLP) ++SRCS += src/omega_gpu_q38_graph.c + # OWN-30B (2026-10-13): bf16 streaming GEMV for the lm_head (omega_gpu_bf16_gemv_api.h), new files only + SRCS += src/omega_blackwell_codegen_bf16.c src/omega_gpu_bf16_gemv_api.c + # OWN-30B prefill (2026-10-13): FP8 grouped GEMM, each expert streamed once per pass (omega_gpu_fp8gemm_api.h), new files only +@@ -2645,3 +2647,11 @@ $(GPU_LAUNCH_COST_TEST): tests/gpu_launch_cost_test.c src/omega_gpu_fp8_api.h sr + GPU_L2_REUSE_TEST = $(OUT_DIR)/gpu_l2_reuse_test + $(GPU_L2_REUSE_TEST): tests/gpu_l2_reuse_test.c src/omega_gpu_fp8_api.h src/omega_gpu_cmdlist.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_l2_reuse_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm ++ ++# sc #642 (2026-10-18): Qwen3.8-27B decode graph (one token = one command list), chip test vs the APIs one call at a time ++.PHONY: test-gpu-q38-graph-host ++GPU_Q38_GRAPH_TEST = $(OUT_DIR)/gpu_q38_graph_test ++$(GPU_Q38_GRAPH_TEST): tests/gpu_q38_graph_test.c src/omega_gpu_q38_graph.h $(OUT_DIR)/libomega_gpu.a ++ $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_q38_graph_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm ++test-gpu-q38-graph-host: $(GPU_Q38_GRAPH_TEST) ++ ./$(GPU_Q38_GRAPH_TEST) --host-only diff --git a/evidence/q38-graph-chip-20261018T1547Z/SHA256SUMS b/evidence/q38-graph-chip-20261018T1547Z/SHA256SUMS new file mode 100644 index 000000000..32405ceb4 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/SHA256SUMS @@ -0,0 +1,10 @@ +157b23882ac83895cf1596d48d428edd1a81d22275f6199ef92aadadfd7541d4 build.log +37a6d523d0f3cdcf6e9d12e39895bbc8daff6e6a276144a0d16f55d68905e32c encode-control.log +9565a803dc089f73b58f1d6b475fed22f65f0448e24498d5d2b2902bdd3b033c glue-host.log +403eeac58828469f634273f2f87ab0edf027406ad66cc4e8bb0b06177def342f glue-sim.log +94f970accd1a57851d7a5df0f4ad882aa29bc7197878cf151e5578029475eed3 host-only.log +b2fdf8a32e7e68975104d0ffa76a8289eccec23fa17685135a8c6b9cda9cf379 layers4.log +4b347169afc62ad04958dd9559d7ad0941c85d04e29ccaa3372444a7a4b6303a main.log +37d5f7d91d56e00ca3144a4bfc1c95bc84047f483b8aab9b55202de40a3c6cc3 device.txt +c577d9cfb2143ceee13caff6e05fada7e2bd366cb5bf82ea8d1023ac508ad1a9 worktree.diff +1083f27bdc231920637709db865fcc0ebdc985f15bbf3437c2ced6615ae030ab binaries.sha256 diff --git a/evidence/q38-graph-chip-20261018T1547Z/binaries.sha256 b/evidence/q38-graph-chip-20261018T1547Z/binaries.sha256 new file mode 100644 index 000000000..e60d697eb --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/binaries.sha256 @@ -0,0 +1,3 @@ +04f22e8be6119b177e1d3b25f2386595f43576fa01dcba0a03b6c86009f8b652 ./build/gpu_q38_graph_test +db8389debf9776bcf98878ecaf5aacaf1c5ff511f186662d3b6b631d31ba5656 build/gpu_cmdlist_test +67b1a65efee1021828631575b849dce58a10f62fcd042d31130d807ee90af410 build/libomega_gpu.a diff --git a/evidence/q38-graph-chip-20261018T1547Z/build.log b/evidence/q38-graph-chip-20261018T1547Z/build.log new file mode 100644 index 000000000..1281f86a2 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/build.log @@ -0,0 +1,2 @@ +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I/home/drakestapleton/workspace/physics-own30b-k/m16 -I/home/drakestapleton/workspace/physics-own30b-k/nvrm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -ffp-contract=off -Itests -o build/gpu_cmdlist_test tests/gpu_cmdlist_test.c build/libomega_gpu.a -lpthread -lm +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I/home/drakestapleton/workspace/physics-own30b-k/m16 -I/home/drakestapleton/workspace/physics-own30b-k/nvrm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I/home/drakestapleton/workspace/physics-own30b-k/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -o build/bw_encode_control_test tests/bw_encode_control_test.c build/libomega_gpu.a -lpthread -lm diff --git a/evidence/q38-graph-chip-20261018T1547Z/device.txt b/evidence/q38-graph-chip-20261018T1547Z/device.txt new file mode 100644 index 000000000..4e047a028 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/device.txt @@ -0,0 +1,6 @@ +start 2026-10-18T15:47:29Z omega=e842af2d7d026c2151ca0ce37f5b8b118fce10ff branch=feat/q38-graph dirty=8 physics=/home/drakestapleton/workspace/physics-own30b-k physics_commit=6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 + total used free shared buff/cache available +Mem: 121 96 14 0 16 24 +end 2026-10-18T15:47:39Z fail=0 diff --git a/evidence/q38-graph-chip-20261018T1547Z/encode-control.log b/evidence/q38-graph-chip-20261018T1547Z/encode-control.log new file mode 100644 index 000000000..b9e018794 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/encode-control.log @@ -0,0 +1 @@ +bw_encode_control_test: 480 encodings checked, 0 opcodes refused (no operands), 0 dropped the control word: PASS diff --git a/evidence/q38-graph-chip-20261018T1547Z/glue-host.log b/evidence/q38-graph-chip-20261018T1547Z/glue-host.log new file mode 100644 index 000000000..72d2c19e4 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/glue-host.log @@ -0,0 +1,8 @@ +codegen axpy: 23 insns, peak 12 gprs, declared 16 +codegen res add|norm: 128 insns, peak 40 gprs, declared 48 +codegen res combine|norm: 264 insns, peak 49 gprs, declared 56 +codegen res add: 45 insns, peak 28 gprs, declared 32 +codegen swiglu: 29 insns, peak 16 gprs, declared 24 +codegen argmax: 165 insns, peak 32 gprs, declared 40 +codegen argmax indexed: 160 insns, peak 40 gprs, declared 48 +host-only: 27 checks, 0 failed: PASS diff --git a/evidence/q38-graph-chip-20261018T1547Z/glue-sim.log b/evidence/q38-graph-chip-20261018T1547Z/glue-sim.log new file mode 100644 index 000000000..61ae9db56 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/glue-sim.log @@ -0,0 +1,29 @@ +sim res mode 5 scale 1 mutant 0: x identical, y 0/2048 outside tolerance +sim res mode 5 scale 1 mutant 1: x identical, y 2047/2048 outside tolerance +sim res mode 6 scale 1 mutant 0: x identical, y 0/2048 outside tolerance +sim res mode 6 scale 1 mutant 1: x DIFFERS, y 2047/2048 outside tolerance +sim res mode 5 scale 2^64 mutant 0: x identical, y 0/2048 outside tolerance +sim res mode 5 scale 2^64 mutant 1: x identical, y 2047/2048 outside tolerance +sim res mode 6 scale 2^64 mutant 0: x identical, y 0/2048 outside tolerance +sim res mode 6 scale 2^64 mutant 1: x DIFFERS, y 2048/2048 outside tolerance +sim swiglu mutant 0: 0/6144 outside tolerance +sim swiglu mutant 1: 6144/6144 outside tolerance +sim argmax tie 0 mutant 0: index 151625 value 49.9996 (ref 151625) +sim argmax tie 0 mutant 1: index 151625 value 49.9996 (ref 151625) +sim argmax tie 1 mutant 0: index 1007 value 60 (ref 1007) +sim argmax tie 1 mutant 1: index 28784 value 49.9999 (ref 1007) +sim res width 5120 mode 5 mutant 0: x identical, y 0/5120 outside tolerance +sim res width 5120 mode 5 mutant 1: x identical, y 5120/5120 outside tolerance +sim res width 5120 mode 4 mutant 0: x identical, y 0/5120 outside tolerance +sim res width 5120 mode 4 mutant 1: x identical, y 5120/5120 outside tolerance +sim res width 5120 mode 1 mutant 0: x identical, y 0/5120 outside tolerance +sim swiglu width 17408 mutant 0: 0/17408 outside tolerance +sim swiglu width 17408 mutant 1: 17407/17408 outside tolerance +sim res rows 3 x 5120 mutant 0: x identical, y 0/15360 outside tolerance +sim res rows 3 x 5120 mutant 1: x identical, y 15360/15360 outside tolerance +sim gather 3 x 14336 mutant 0: 0/43008 wrong +sim gather 3 x 14336 mutant 1: 43008/43008 wrong +sim gate 6244 mutant 0: 0/6244 outside tolerance +sim gate 6244 mutant 1: 6244/6244 outside tolerance +sim swiglu rows 3 x 17408: 0 outside tolerance +sim: 74 checks, 0 failed: PASS diff --git a/evidence/q38-graph-chip-20261018T1547Z/host-only.log b/evidence/q38-graph-chip-20261018T1547Z/host-only.log new file mode 100644 index 000000000..10bdb9d18 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/host-only.log @@ -0,0 +1,5 @@ +ok: gather table: q, k permuted, v and gate in place (0 wrong) +ok: gather table is a permutation of the 14336 mix values (0 repeats or out of range) +ok: create refuses an empty config +ok: create refuses NULL +gpu_q38_graph_test (host-only): 4 checks, 0 failed: PASS diff --git a/evidence/q38-graph-chip-20261018T1547Z/layers4.log b/evidence/q38-graph-chip-20261018T1547Z/layers4.log new file mode 100644 index 000000000..a4293312b --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/layers4.log @@ -0,0 +1,20 @@ +model: 4 layers (3 GDN, 1 attention, mask 0x8), vocab 248320 +ok: token 0: graph 19649 == baseline 19649 +ok: token 0: logits max diff 7.09e-06 <= 1e-04 * 9.78 +ok: token 0: x max diff 1.79e-06 <= 1e-04 * 2.65 +ok: token 0: 41 launches in 1 submit +ok: token 1: graph 72160 == baseline 72160 +ok: token 1: logits max diff 6.68e-06 <= 1e-04 * 9.56 +ok: token 1: x max diff 1.53e-06 <= 1e-04 * 2.54 +ok: token 1: 41 launches in 1 submit +ok: token 2: graph 114197 == baseline 114197 +ok: token 2: logits max diff 4.29e-06 <= 1e-04 * 9.58 +ok: token 2: x max diff 1.07e-06 <= 1e-04 * 2.59 +ok: token 2: 41 launches in 1 submit +ok: flush_each == one list, bit for bit, all 3 tokens +ok: mutant 1 (swiglu up half -1) caught +ok: mutant 2 (gather index +1) caught +ok: mutant 3 (gate dropped) caught +ok: mutant 4 (post-norm weight dropped) caught +timing: 10 reps, median chip 20863.4 us, median total 20881.2 us per token (4 layers) +gpu_q38_graph_test: 17 checks, 0 failed: PASS diff --git a/evidence/q38-graph-chip-20261018T1547Z/main.log b/evidence/q38-graph-chip-20261018T1547Z/main.log new file mode 100644 index 000000000..c1535c7e7 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/main.log @@ -0,0 +1,20 @@ +model: 2 layers (1 GDN, 1 attention, mask 0x2), vocab 248320 +ok: token 0: graph 119117 == baseline 119117 +ok: token 0: logits max diff 0.000327 <= 1e-04 * 9.13 +ok: token 0: x max diff 0.000126 <= 1e-04 * 2.44 +ok: token 0: 23 launches in 1 submit +ok: token 1: graph 147744 == baseline 147744 +ok: token 1: logits max diff 6.68e-06 <= 1e-04 * 9.65 +ok: token 1: x max diff 2.03e-06 <= 1e-04 * 2.05 +ok: token 1: 23 launches in 1 submit +ok: token 2: graph 60455 == baseline 60455 +ok: token 2: logits max diff 7.99e-06 <= 1e-04 * 9.83 +ok: token 2: x max diff 3.16e-06 <= 1e-04 * 2.55 +ok: token 2: 23 launches in 1 submit +ok: flush_each == one list, bit for bit, all 3 tokens +ok: mutant 1 (swiglu up half -1) caught +ok: mutant 2 (gather index +1) caught +ok: mutant 3 (gate dropped) caught +ok: mutant 4 (post-norm weight dropped) caught +timing: 10 reps, median chip 16088.0 us, median total 16101.4 us per token (2 layers) +gpu_q38_graph_test: 17 checks, 0 failed: PASS diff --git a/evidence/q38-graph-chip-20261018T1547Z/worktree.diff b/evidence/q38-graph-chip-20261018T1547Z/worktree.diff new file mode 100644 index 000000000..c01559068 --- /dev/null +++ b/evidence/q38-graph-chip-20261018T1547Z/worktree.diff @@ -0,0 +1,47 @@ +diff --git a/Makefile b/Makefile +index e2b958d7..00d8968d 100644 +--- a/Makefile ++++ b/Makefile +@@ -46,6 +46,8 @@ SRCS += src/omega_blackwell_codegen_gdn.c src/omega_gpu_gdn_api.c + SRCS += src/omega_gpu_cmdlist.c src/omega_blackwell_codegen_dec.c + SRCS += src/omega_gpu_mem_cached.c + SRCS += src/omega_gpu_decode_graph.c ++# sc #642 (2026-10-18): one Qwen3.8-27B decode token as one command list (GDN + hd256 attention layers, dense MLP) ++SRCS += src/omega_gpu_q38_graph.c + # OWN-30B (2026-10-13): bf16 streaming GEMV for the lm_head (omega_gpu_bf16_gemv_api.h), new files only + SRCS += src/omega_blackwell_codegen_bf16.c src/omega_gpu_bf16_gemv_api.c + # OWN-30B prefill (2026-10-13): FP8 grouped GEMM, each expert streamed once per pass (omega_gpu_fp8gemm_api.h), new files only +@@ -2645,3 +2647,11 @@ $(GPU_LAUNCH_COST_TEST): tests/gpu_launch_cost_test.c src/omega_gpu_fp8_api.h sr + GPU_L2_REUSE_TEST = $(OUT_DIR)/gpu_l2_reuse_test + $(GPU_L2_REUSE_TEST): tests/gpu_l2_reuse_test.c src/omega_gpu_fp8_api.h src/omega_gpu_cmdlist.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_l2_reuse_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm ++ ++# sc #642 (2026-10-18): Qwen3.8-27B decode graph (one token = one command list), chip test vs the APIs one call at a time ++.PHONY: test-gpu-q38-graph-host ++GPU_Q38_GRAPH_TEST = $(OUT_DIR)/gpu_q38_graph_test ++$(GPU_Q38_GRAPH_TEST): tests/gpu_q38_graph_test.c src/omega_gpu_q38_graph.h $(OUT_DIR)/libomega_gpu.a ++ $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_q38_graph_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm ++test-gpu-q38-graph-host: $(GPU_Q38_GRAPH_TEST) ++ ./$(GPU_Q38_GRAPH_TEST) --host-only +diff --git a/src/omega_blackwell_qmd.h b/src/omega_blackwell_qmd.h +index f1c7018f..865887f1 100644 +--- a/src/omega_blackwell_qmd.h ++++ b/src/omega_blackwell_qmd.h +@@ -34,11 +34,14 @@ typedef struct { + uint32_t grid_y; + uint32_t gpr_count; + /* Shared memory per CTA in bytes (clcec0qmd.h SHARED_MEMORY_SIZE_SHIFTED7, 128-byte units). +- * 0 keeps the long-standing 1024. At most OMEGA_BW_QMD_MAX_SHARED_BYTES, well inside the +- * TARGET_SM_CONFIG this QMD declares (word 36). */ ++ * 0 keeps the long-standing 1024. At most OMEGA_BW_QMD_MAX_SHARED_BYTES: word 36 declares ++ * MIN_ and TARGET_SM_CONFIG_SHARED_MEM_SIZE 9 = 32 KB (mesa nak qmd.rs gv100_smem_size_to_hw: ++ * size_kb / 4 + 1; SM >= 120 configs 0, 8, 16, 32, 64, 100 KB, nouveau_device.c), and a CTA must fit ++ * the MIN config. Raised from 16 KiB for the bf16 GEMV at k 5120 (20 KiB of x, sc #642); above ++ * 32 KiB the MIN / TARGET fields must follow the size (not done yet: every launch keeps one word 36). */ + uint32_t shared_bytes; + } OmegaBlackwellQmdConfig; +-#define OMEGA_BW_QMD_MAX_SHARED_BYTES 16384u ++#define OMEGA_BW_QMD_MAX_SHARED_BYTES 32768u + + /* Build QMD 0: GRID_NULL null-barrier grid with dependence counter 2 */ + int omega_blackwell_build_qmd0(uint32_t *qmd0_words, uint64_t qmd0_va, uint64_t qmd1_va); diff --git a/src/.crumb b/src/.crumb index 6b955897f..17da01d13 100644 --- a/src/.crumb +++ b/src/.crumb @@ -182,14 +182,14 @@ } ], "children_root": "sha256:15012a017b26a269feb256b64875ece2912f5f82c57804baafa3777c4fcb80b9", - "compiled_at": "2026-10-18T15:21:47Z", + "compiled_at": "2026-10-18T15:51:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:fbe222132e0f413ae3f4230a7b3389a2c0fcca3a56fe1371eac7406658b21843", + "digest": "sha256:db2faa78b8de104dc889d825f567019be0cf72e173fe50f06df1b0e289bbe283", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 549, - "generated_at_commit": "acdeff745c292cebb3b22cc7ae298409a0b7ee64", - "source_tree": "sha256:daf12136780bc80d1f4b7a6ee4050a3e32324c1af51d0fd3801ec848dad4eab4" + "files": 551, + "generated_at_commit": "3c90e430b5282f00f426c2421200d1e8485f5ebd", + "source_tree": "sha256:18fc0d6a5cb8f33cf63d824cce398eae1e4dc76e4ac0a68d0b529439a5f76032" }, "owner_lane": "", "provenance": { diff --git a/src/omega_blackwell_codegen_dec.c b/src/omega_blackwell_codegen_dec.c index b2ec67d7f..867bf8e80 100644 --- a/src/omega_blackwell_codegen_dec.c +++ b/src/omega_blackwell_codegen_dec.c @@ -146,6 +146,7 @@ static const uint32_t C_INV2048 = 0x3A000000u; /* 1/2048 */ static const uint32_t C_NLOG2E = 0xBFB8AA3Bu; /* -1.4426950f */ static const uint32_t C_ONE = 0x3F800000u; static const uint32_t C_2P23 = 0x4B000000u; +static uint32_t f32_bits(float f) { uint32_t u; memcpy(&u, &f, sizeof u); return u; } static int gen_axpy(Em *e, const OmegaDecSpec *sp) { if (sp->stride == 0 || sp->stride % OMEGA_DEC_THREADS) return -1; @@ -174,13 +175,23 @@ static int gen_axpy(Em *e, const OmegaDecSpec *sp) { return e->err; } +/* n quads of a thread's contiguous run at a + 16 q: one read barrier chain (the first load opens it) */ +static void ldg_quads(Em *e, const int *q, unsigned n, int a, uint32_t base, int udesc) { + for (unsigned k = 0; k < n; k++) ldg(e, BW_IR_LDG_E_128, q[k], a, base + 16u * k, udesc, k == 0); +} +static int new_quads(Em *e, int *q, unsigned n) { for (unsigned k = 0; k < n; k++) q[k] = V128(e); return e->err; } + +#define RES_MAX_VALS 32u /* width 8192 */ + static int gen_res(Em *e, const OmegaDecSpec *sp) { - const uint32_t M = sp->mode; - if (M == 0 || M > 7 || ((M & OMEGA_DEC_RES_ADD) && (M & OMEGA_DEC_RES_COMBINE))) return -1; + const uint32_t M = sp->mode, H = sp->width ? sp->width : OMEGA_DEC_HIDDEN, N = H / OMEGA_DEC_THREADS, Q = N / 4u; + if ((M & 7u) == 0 || M > 15 || ((M & OMEGA_DEC_RES_ADD) && (M & OMEGA_DEC_RES_COMBINE))) return -1; + if ((M & OMEGA_DEC_RES_ROWS) && (M & OMEGA_DEC_RES_COMBINE)) return -1; + if (H % (4u * OMEGA_DEC_THREADS) || N > RES_MAX_VALS || ((M & OMEGA_DEC_RES_COMBINE) && H != OMEGA_DEC_HIDDEN)) return -1; int udesc = UV64(e), px = V64(e), pa = V64(e), pw = V64(e), pnw = V64(e), py = V64(e), eps = V(e), scale = V(e); int tid = V(e), warp = V(e), lane = V(e), one = V(e), toff = V(e), t = V(e), t2 = V(e), a0 = V(e); - int xa = V64(e), xq0 = V128(e), xq1 = V128(e); - if (e->err) return -1; + int xa = V64(e), xq[RES_MAX_VALS / 4]; + if (new_quads(e, xq, Q)) return -1; ldcu64(e, udesc, 0x358); ldc64(e, px, OMEGA_DEC_RES_X); ldc64(e, pa, OMEGA_DEC_RES_A); ldc64(e, pw, OMEGA_DEC_RES_W); ldc64(e, pnw, OMEGA_DEC_RES_NW); ldc64(e, py, OMEGA_DEC_RES_Y); ldc32(e, eps, OMEGA_DEC_RES_EPS); ldc32(e, scale, OMEGA_DEC_RES_SCALE); @@ -188,17 +199,24 @@ static int gen_res(Em *e, const OmegaDecSpec *sp) { movi(e, one, 1); shr(e, warp, tid, 5); andi(e, R(lane), R(tid), 31u); - imadi(e, R(toff), R(tid), 32u, NOREG); - imadw(e, xa, toff, 1u, px); - ldg(e, BW_IR_LDG_E_128, xq0, xa, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, xq1, xa, 16, udesc, 0); - Op x[8]; - for (unsigned i = 0; i < 8; i++) x[i] = i < 4 ? S(xq0, i) : S(xq1, i - 4); - if (M & OMEGA_DEC_RES_ADD) { - int aa = V64(e), aq0 = V128(e), aq1 = V128(e); + imadi(e, R(toff), R(tid), 4u * N, NOREG); + int roff = toff; /* the row's x, a and y offset: toff, plus ctaid.y * width in the rows form */ + if (M & OMEGA_DEC_RES_ROWS) { + int cy = V(e); roff = V(e); if (e->err) return -1; - imadw(e, aa, toff, 1u, pa); - ldg(e, BW_IR_LDG_E_128, aq0, aa, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, aq1, aa, 16, udesc, 0); - for (unsigned i = 0; i < 8; i++) fadd(e, x[i], x[i], i < 4 ? S(aq0, i) : S(aq1, i - 4)); + s2r(e, cy, BW_SR_CTAID_Y); + imadi(e, R(roff), R(cy), 4u * H, toff); + } + imadw(e, xa, roff, 1u, px); + ldg_quads(e, xq, Q, xa, 0, udesc); + Op x[RES_MAX_VALS]; + for (unsigned i = 0; i < N; i++) x[i] = S(xq[i / 4], i % 4); + if (M & OMEGA_DEC_RES_ADD) { + int aa = V64(e), aq[RES_MAX_VALS / 4]; + if (new_quads(e, aq, Q)) return -1; + imadw(e, aa, roff, 1u, pa); + ldg_quads(e, aq, Q, aa, 0, udesc); + for (unsigned i = 0; i < N; i++) fadd(e, x[i], x[i], S(aq[i / 4], i % 4)); } if (M & OMEGA_DEC_RES_COMBINE) { int wq0 = V128(e), wq1 = V128(e), da = V64(e), dq0 = V128(e), dq1 = V128(e), m[8]; @@ -221,27 +239,29 @@ static int gen_res(Em *e, const OmegaDecSpec *sp) { if (M & (OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_COMBINE)) { int sa = V64(e); if (e->err) return -1; - for (unsigned i = 0; i < 8; i++) { if (i == 0) imadw(e, sa, toff, 1u, px); else imadw(e, sa, one, 4u, sa); stg(e, sa, x[i], udesc); } + for (unsigned i = 0; i < N; i++) { if (i == 0) imadw(e, sa, roff, 1u, px); else imadw(e, sa, one, 4u, sa); stg(e, sa, x[i], udesc); } } if (M & OMEGA_DEC_RES_NORM) { - int sq = V(e), rs = V(e), na = V64(e), nq0 = V128(e), nq1 = V128(e), ya = V64(e), yv = V(e); + int sq = V(e), rs = V(e), na = V64(e), nq[RES_MAX_VALS / 4]; + new_quads(e, nq, Q); + int ya = V64(e), yv = V(e); /* allocated after the quads, as before the width field: the same program at 2048 */ if (e->err) return -1; imadw(e, na, toff, 1u, pnw); - ldg(e, BW_IR_LDG_E_128, nq0, na, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, nq1, na, 16, udesc, 0); + ldg_quads(e, nq, Q, na, 0, udesc); fmul(e, R(sq), x[0], x[0]); - for (unsigned i = 1; i < 8; i++) ffma(e, R(sq), x[i], x[i], R(sq)); + for (unsigned i = 1; i < N; i++) ffma(e, R(sq), x[i], x[i], R(sq)); warp_sum(e, sq, t); imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DEC_SM_RED, a0); lane0_sts(e, lane, one, a0, R(sq)); bar_sync(e); cta_reduce8(e, sq, OMEGA_DEC_SM_RED, 0, a0); - movi(e, t2, C_INV2048); + movi(e, t2, H == OMEGA_DEC_HIDDEN ? C_INV2048 : f32_bits(1.0f / (float)H)); ffma(e, R(rs), R(sq), R(t2), R(eps)); mufu(e, BW_IR_MUFU_RSQ, R(rs), R(rs)); - imadw(e, ya, toff, 1u, py); - for (unsigned i = 0; i < 8; i++) { + imadw(e, ya, roff, 1u, py); + for (unsigned i = 0; i < N; i++) { fmul(e, R(yv), x[i], R(rs)); - if (sp->mutant != 1) fmul(e, R(yv), R(yv), i < 4 ? S(nq0, i) : S(nq1, i - 4)); + if (sp->mutant != 1) fmul(e, R(yv), R(yv), S(nq[i / 4], i % 4)); fmul(e, R(yv), R(yv), R(scale)); if (i) imadw(e, ya, one, 4u, ya); stg(e, ya, R(yv), udesc); @@ -252,6 +272,8 @@ static int gen_res(Em *e, const OmegaDecSpec *sp) { } static int gen_swiglu(Em *e, const OmegaDecSpec *sp) { + const uint32_t I = sp->width ? sp->width : OMEGA_DEC_INTER; + if (I % OMEGA_DEC_THREADS) return -1; int udesc = UV64(e), pgu = V64(e), pact = V64(e), scale = V(e), tid = V(e), cx = V(e), cy = V(e), c = V(e), gi = V(e), ai = V(e); int ga = V64(e), ua = V64(e), aa = V64(e), gv = V(e), uv = V(e), t = V(e), cst = V(e), ui = V(e); if (e->err) return -1; @@ -259,11 +281,11 @@ static int gen_swiglu(Em *e, const OmegaDecSpec *sp) { ldc64(e, pgu, OMEGA_DEC_SW_GU); ldc64(e, pact, OMEGA_DEC_SW_ACT); ldc32(e, scale, OMEGA_DEC_SW_SCALE); s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); imadi(e, R(c), R(cx), OMEGA_DEC_THREADS, tid); - imadi(e, R(gi), R(cy), 2u * OMEGA_DEC_INTER, c); - imadi(e, R(ai), R(cy), OMEGA_DEC_INTER, c); + imadi(e, R(gi), R(cy), 2u * I, c); + imadi(e, R(ai), R(cy), I, c); /* the up half sits INTER floats after the gate: its own address (the 32-bit LDG.E encoding carries no immediate * offset, omega_blackwell_codegen.c BW_IR_LDG_E; the warp simulator caught the dropped offset, 2026-10-13) */ - movi(e, cst, sp->mutant == 1 ? OMEGA_DEC_INTER - 1u : OMEGA_DEC_INTER); + movi(e, cst, sp->mutant == 1 ? I - 1u : I); imadi(e, R(ui), R(cst), 1u, gi); imadw(e, ga, gi, 4u, pgu); imadw(e, ua, ui, 4u, pgu); @@ -343,6 +365,62 @@ static int gen_argmax(Em *e, const OmegaDecSpec *sp) { return e->err; } +static int gen_gather(Em *e, const OmegaDecSpec *sp) { + const uint32_t N = sp->width, IS = sp->stride; + if (N == 0 || IS == 0) return -1; + int udesc = UV64(e), pin = V64(e), ptab = V64(e), pout = V64(e), tid = V(e), cx = V(e), cy = V(e), n = V(e), lim = V(e); + int ta = V64(e), tv = V(e), idx = V(e), ia = V64(e), val = V(e), o = V(e), oa = V64(e); + if (e->err) return -1; + ldcu64(e, udesc, 0x358); + ldc64(e, pin, OMEGA_DEC_GA_IN); ldc64(e, ptab, OMEGA_DEC_GA_TAB); ldc64(e, pout, OMEGA_DEC_GA_OUT); + s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); + imadi(e, R(n), R(cx), OMEGA_DEC_THREADS, tid); + movi(e, lim, N); + isetp_ge_u32(e, n, lim); + Region r = region_skip_if_p0(e); + imadw(e, ta, n, 4u, ptab); + ldg(e, BW_IR_LDG_E, tv, ta, 0, udesc, 1); + imadi(e, R(idx), R(cy), IS, tv); + if (sp->mutant == 1) { movi(e, lim, 1); imadi(e, R(idx), R(lim), 1u, idx); } + imadw(e, ia, idx, 4u, pin); + ldg(e, BW_IR_LDG_E, val, ia, 0, udesc, 1); + imadi(e, R(o), R(cy), N, n); + imadw(e, oa, o, 4u, pout); + stg(e, oa, R(val), udesc); + region_end(e, r); + tail(e); + return e->err; +} + +static int gen_gate(Em *e, const OmegaDecSpec *sp) { + int udesc = UV64(e), pa = V64(e), pg = V64(e), pout = V64(e), lim = V(e), tid = V(e), cx = V(e), n = V(e); + int aa = V64(e), ga = V64(e), oa = V64(e), av = V(e), gv = V(e), t = V(e), cst = V(e); + if (e->err) return -1; + ldcu64(e, udesc, 0x358); + ldc64(e, pa, OMEGA_DEC_GT_A); ldc64(e, pg, OMEGA_DEC_GT_G); ldc64(e, pout, OMEGA_DEC_GT_OUT); ldc32(e, lim, OMEGA_DEC_GT_N); + s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); + imadi(e, R(n), R(cx), OMEGA_DEC_THREADS, tid); + isetp_ge_u32(e, n, lim); + Region r = region_skip_if_p0(e); + imadw(e, aa, n, 4u, pa); imadw(e, ga, n, 4u, pg); imadw(e, oa, n, 4u, pout); + ldg(e, BW_IR_LDG_E, av, aa, 0, udesc, 1); + ldg(e, BW_IR_LDG_E, gv, ga, 0, udesc, 0); + if (sp->mutant == 1) mov(e, R(t), R(av)); + else { + movi(e, cst, C_NLOG2E); + fmul(e, R(t), R(gv), R(cst)); + mufu(e, BW_IR_MUFU_EX2, R(t), R(t)); + movi(e, cst, C_ONE); + fadd(e, R(t), R(t), R(cst)); + mufu(e, BW_IR_MUFU_RCP, R(t), R(t)); + fmul(e, R(t), R(av), R(t)); + } + stg(e, oa, R(t), udesc); + region_end(e, r); + tail(e); + return e->err; +} + int omega_blackwell_codegen_dec_prog(const OmegaDecSpec *spec, BlackwellIRProgram *prog) { if (!spec || !prog) return -1; omega_bw_ir_init(prog); @@ -353,6 +431,8 @@ int omega_blackwell_codegen_dec_prog(const OmegaDecSpec *spec, BlackwellIRProgra case OMEGA_DEC_RES: rc = gen_res(&em, spec); break; case OMEGA_DEC_SWIGLU: rc = gen_swiglu(&em, spec); break; case OMEGA_DEC_ARGMAX: rc = gen_argmax(&em, spec); break; + case OMEGA_DEC_GATHER: rc = gen_gather(&em, spec); break; + case OMEGA_DEC_GATE: rc = gen_gate(&em, spec); break; default: return -1; } if (rc != 0 || em.err || em.depth != 0) return -1; diff --git a/src/omega_blackwell_codegen_dec.h b/src/omega_blackwell_codegen_dec.h index 0ea33231e..98cd9216f 100644 --- a/src/omega_blackwell_codegen_dec.h +++ b/src/omega_blackwell_codegen_dec.h @@ -7,13 +7,19 @@ * * AXPY x[i] += a[i] in place over iters * stride elements (grid-stride, uniform trip count). Chip test only: a * chain of these in one list catches overlapping grids (a lost increment). - * RES one CTA of 256 threads over the 2048-wide residual row, 8 values per thread. Mode bits: + * RES one CTA of 256 threads over the residual row (2048 wide by default, spec.width), width / 256 values per + * thread. Mode bits: * ADD x += a (a = o_proj output) * COMBINE x += sum_g w[g] * d[g] (g = 0..7, as the host: m = w0*d0, then m = m + w_g*d_g, x = x + m) * NORM y = x * RSQ(mean(x^2) + eps) * nw * scale (scale 2^64 = the FP8 GEMV x' row, 1 = plain) * x is written back when ADD or COMBINE is set. - * SWIGLU act[g][c] = silu(gu[g][c]) * gu[g][768 + c] * scale, g = ctaid.y (8 experts), c = ctaid.x * 256 + tid - * (grid 3 x 8). silu(v) = v * RCP(1 + EX2(-v * log2 e)). + * SWIGLU act[g][c] = silu(gu[g][c]) * gu[g][I + c] * scale, g = ctaid.y (8 experts, or 1 dense row), c = ctaid.x * 256 + * + tid, I the intermediate size (768 by default, spec.width; grid I / 256 x groups). + * silu(v) = v * RCP(1 + EX2(-v * log2 e)). + * GATHER out[r][n] = in[r * stride + tab[n]] for n < width (spec.width, spec.stride: floats), r = ctaid.y. Qwen3.8-27B + * attention: the fused q|gate|k|v GEMV row to the attention kernel's permuted q|k|v plus the gate (sc #642; + * the permutation crosses the FP8 weights' 128-row scale blocks, so it cannot be folded into the weights). + * GATE out[n] = a[n] * sigmoid(g[n]) for n < count, sigmoid(v) = RCP(1 + EX2(-v * log2 e)): the attention output gate. * ARGMAX CTA c reads 2048 values v[c*2048 + 8*tid + j] (and indices, or the identity index), writes the max and the * lowest index holding it to out_v[c], out_i[c]. Two launches give the greedy token of 151936 logits * (75 CTAs, then 1 CTA over the 75 partials padded with -1e30). The lowest-index rule is the host's @@ -26,11 +32,12 @@ #include "omega_blackwell_codegen.h" #include -typedef enum { OMEGA_DEC_AXPY = 1, OMEGA_DEC_RES = 2, OMEGA_DEC_SWIGLU = 3, OMEGA_DEC_ARGMAX = 4 } OmegaDecKind; +typedef enum { OMEGA_DEC_AXPY = 1, OMEGA_DEC_RES = 2, OMEGA_DEC_SWIGLU = 3, OMEGA_DEC_ARGMAX = 4, OMEGA_DEC_GATHER = 5, OMEGA_DEC_GATE = 6 } OmegaDecKind; #define OMEGA_DEC_RES_ADD 1u #define OMEGA_DEC_RES_COMBINE 2u #define OMEGA_DEC_RES_NORM 4u +#define OMEGA_DEC_RES_ROWS 8u /* row ctaid.y of a [rows][width] block: x, a and y move by width per row, nw does not */ #define OMEGA_DEC_HIDDEN 2048u #define OMEGA_DEC_THREADS 256u @@ -63,6 +70,17 @@ typedef enum { OMEGA_DEC_AXPY = 1, OMEGA_DEC_RES = 2, OMEGA_DEC_SWIGLU = 3, OMEG #define OMEGA_DEC_AM_OV 0x390u /* f32 [ctas] */ #define OMEGA_DEC_AM_OI 0x398u /* u32 [ctas] */ #define OMEGA_DEC_AM_WORDS 8u +/* GATHER args (out[r][n] = in[r * stride + tab[n]], n < width; grid ceil(width / 256) x rows) */ +#define OMEGA_DEC_GA_IN 0x380u +#define OMEGA_DEC_GA_TAB 0x388u /* u32 [width] source index within an input row */ +#define OMEGA_DEC_GA_OUT 0x390u +#define OMEGA_DEC_GA_WORDS 6u +/* GATE args (out[n] = a[n] * sigmoid(g[n]), n < count; grid ceil(count / 256); out may be a) */ +#define OMEGA_DEC_GT_A 0x380u +#define OMEGA_DEC_GT_G 0x388u +#define OMEGA_DEC_GT_OUT 0x390u +#define OMEGA_DEC_GT_N 0x398u /* u32 count */ +#define OMEGA_DEC_GT_WORDS 7u #define OMEGA_DEC_SM_RED 0u /* 8 per-warp partials */ #define OMEGA_DEC_SM_RED2 64u @@ -71,9 +89,13 @@ typedef enum { OMEGA_DEC_AXPY = 1, OMEGA_DEC_RES = 2, OMEGA_DEC_SWIGLU = 3, OMEG typedef struct { OmegaDecKind kind; uint32_t mode; /* RES mode bits; ARGMAX: 1 = indexed (indices loaded), 0 = identity */ - uint32_t stride; /* AXPY: elements per iteration (grid_x * 256) */ + uint32_t stride; /* AXPY: elements per iteration (grid_x * 256). GATHER: the input row stride (floats) */ int mutant; /* TEST ONLY. RES: 1 = norm without the weight; 2 = COMBINE skips expert 7. - SWIGLU: 1 = up half read at +767. ARGMAX: 1 = last equal index instead of first. 0 none */ + SWIGLU: 1 = up half read at +767. ARGMAX: 1 = last equal index instead of first. + GATHER: 1 = source index + 1. GATE: 1 = the gate dropped (out = a). 0 none */ + uint32_t width; /* RES: the row width (0 = OMEGA_DEC_HIDDEN; a multiple of 1024 up to 8192, COMBINE only at 2048). + SWIGLU: the intermediate size (0 = OMEGA_DEC_INTER; a multiple of 256). Qwen3.8-27B: 5120 and + 17408 (sc #642). The default widths emit the same program as before the field existed. */ } OmegaDecSpec; int omega_blackwell_codegen_dec_prog(const OmegaDecSpec *spec, BlackwellIRProgram *prog); diff --git a/src/omega_blackwell_qmd.h b/src/omega_blackwell_qmd.h index f1c7018f0..865887f19 100644 --- a/src/omega_blackwell_qmd.h +++ b/src/omega_blackwell_qmd.h @@ -34,11 +34,14 @@ typedef struct { uint32_t grid_y; uint32_t gpr_count; /* Shared memory per CTA in bytes (clcec0qmd.h SHARED_MEMORY_SIZE_SHIFTED7, 128-byte units). - * 0 keeps the long-standing 1024. At most OMEGA_BW_QMD_MAX_SHARED_BYTES, well inside the - * TARGET_SM_CONFIG this QMD declares (word 36). */ + * 0 keeps the long-standing 1024. At most OMEGA_BW_QMD_MAX_SHARED_BYTES: word 36 declares + * MIN_ and TARGET_SM_CONFIG_SHARED_MEM_SIZE 9 = 32 KB (mesa nak qmd.rs gv100_smem_size_to_hw: + * size_kb / 4 + 1; SM >= 120 configs 0, 8, 16, 32, 64, 100 KB, nouveau_device.c), and a CTA must fit + * the MIN config. Raised from 16 KiB for the bf16 GEMV at k 5120 (20 KiB of x, sc #642); above + * 32 KiB the MIN / TARGET fields must follow the size (not done yet: every launch keeps one word 36). */ uint32_t shared_bytes; } OmegaBlackwellQmdConfig; -#define OMEGA_BW_QMD_MAX_SHARED_BYTES 16384u +#define OMEGA_BW_QMD_MAX_SHARED_BYTES 32768u /* Build QMD 0: GRID_NULL null-barrier grid with dependence counter 2 */ int omega_blackwell_build_qmd0(uint32_t *qmd0_words, uint64_t qmd0_va, uint64_t qmd1_va); diff --git a/src/omega_gpu_q38_graph.c b/src/omega_gpu_q38_graph.c new file mode 100644 index 000000000..9495752fc --- /dev/null +++ b/src/omega_gpu_q38_graph.c @@ -0,0 +1,425 @@ +/* + * omega_gpu_q38_graph.c: one Qwen3.8-27B decode token as one command list (see the header). Records the FP8 / bf16 GEMV, + * GDN and hd256 attention APIs' *_record launches and the omega_blackwell_codegen_dec glue kernels at width 5120 into an + * omega_gpu_cmdlist. Buffers: omega_gpu_session_alloc only (GPU-uncached, nvos.h:1117); the host touches x (before), + * token and logits (after). + */ +#include "omega_gpu_q38_graph.h" +#include "omega_gpu_cmdlist.h" +#include "omega_blackwell_codegen_dec.h" +#include "omega_gpu_session.h" +#include "omega_gpu_fmt.h" +#include + +#define TIMEOUT_MS 20000ull +#define SCALE_2P64 18446744073709551616.0f +#define NEG_PAD (-1e30f) +#define TOKEN_POISON 0xffffffffu +#define H OMEGA_GPU_Q38_HIDDEN +#define I OMEGA_GPU_Q38_INTER + +/* glue kernels */ +enum { K_NORM, K_ADDNORM, K_SW, K_GATHER, K_GATE, K_AM0, K_AM1, K_SW_M1, K_GATHER_M1, K_GATE_M1, K_ADDNORM_M1, K_COUNT }; + +typedef struct { OmegaGpuMem code; uint32_t gpr; } Kern; + +/* the one buffer: byte offsets, every region 256-byte aligned (RES loads 16-byte vectors) */ +typedef struct { + size_t x, xq, mix, ab, agg, att, o, gu, act, hidden, logits, am_v, am_i, token, norms, fnorm, tabs, gtab, bytes; +} Layout; + +struct OmegaGpuQ38Graph { + OmegaGpuQ38GraphConfig cfg; + uint32_t am_ctas; /* stage-1 ARGMAX CTAs = ceil(vocab / 2048) */ + Layout lo; + OmegaGpuMem buf; + Kern k[K_COUNT]; + OmegaGpuCmdList *cl; + struct { const OmegaGpuFp8Tensor *mix, *o, *gu, *dn; const OmegaGpuBf16Tensor *ab; uint32_t kind_index; int attn, set; } layer[OMEGA_GPU_Q38_MAX_LAYERS]; + int final_set; + /* token state */ + int in_token, failed; + int xq_layer; /* layer whose input-norm x' sits in xq, -1 none */ + int hidden_ready; + uint64_t t_begin; + OmegaGpuDecodeGraphStats acc; + int flush_each, mutant; + int stamps; + uint32_t n_kind, tagged_in_list, n_stamp; + uint8_t kind[OMEGA_GPU_CMDLIST_MAX_LAUNCHES]; + uint64_t stamp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; +}; + +static size_t al(size_t v) { return (v + 255u) & ~(size_t)255u; } +static uint64_t va_of(const OmegaGpuQ38Graph *g, size_t off) { return g->buf.va + off; } +static void *cpu_of(const OmegaGpuQ38Graph *g, size_t off) { return (uint8_t *)g->buf.cpu + off; } +static void put64(uint32_t *a, unsigned off, uint64_t v) { a[(off - 0x380) / 4] = (uint32_t)v; a[(off - 0x380) / 4 + 1] = (uint32_t)(v >> 32); } +static void putf(uint32_t *a, unsigned off, float f) { memcpy(&a[(off - 0x380) / 4], &f, 4); } +static int fail(OmegaGpuQ38Graph *g, const char *what, int rc) { + if (g) g->failed = 1; + if (what) omega_gpu_session_set_error(what); + return rc; +} + +void omega_q38_gather_table(uint32_t *tab) { + const uint32_t R = OMEGA_GPU_Q38_ROTARY, QP = 24u * 512u, KP = QP + 4u * 256u; + for (uint32_t n = 0; n < OMEGA_GPU_Q38_ATT_MIX; n++) { + uint32_t s; + if (n < 6144u) s = (n / 256u) * 512u + omega_dattn256_perm_slot(n % 256u, R); /* q, permuted */ + else if (n < 7168u) s = QP + ((n - 6144u) / 256u) * 256u + omega_dattn256_perm_slot(n % 256u, R); /* k, permuted */ + else if (n < 8192u) s = KP + (n - 7168u); /* v */ + else s = ((n - 8192u) / 256u) * 512u + 256u + (n - 8192u) % 256u; /* gate */ + tab[n] = s; + } +} + +/* lock held */ +static int kern_build(Kern *k, const OmegaDecSpec *s) { + BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); + if (!prog) return OMEGA_GPU_DGRAPH_CODEGEN_FAIL; + int rc = OMEGA_GPU_DGRAPH_OK; + size_t need = 0; + if (omega_blackwell_codegen_dec_prog(s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL; + else if (omega_gpu_session_alloc_code(need, &k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; + else { + OmegaBlackwellKernel kk; + if (omega_blackwell_kernel_encode_into(prog, k->code.cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_DGRAPH_CODEGEN_FAIL; + else if (omega_gpu_session_sync_to_gpu(&k->code) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; + else { k->gpr = omega_bw_gpr_declared(kk.gpr_count); if (k->gpr < 64u) k->gpr = 64u; } /* as the decode graph */ + if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_session_free(&k->code); memset(k, 0, sizeof *k); } + } + omega_gpu_host_free(prog); + return rc; +} + +void omega_gpu_q38_graph_free(OmegaGpuQ38Graph *g) { + if (!g) return; + if (g->cl) omega_gpu_cmdlist_free(g->cl); + omega_gpu_session_lock(); + for (unsigned i = 0; i < K_COUNT; i++) if (g->k[i].code.cpu) omega_gpu_session_free(&g->k[i].code); + if (g->buf.cpu) omega_gpu_session_free(&g->buf); + omega_gpu_session_unlock(); + omega_gpu_host_free(g); +} + +int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Graph **out) { + if (out) *out = NULL; + if (!cfg || !out || cfg->layers == 0 || cfg->layers > OMEGA_GPU_Q38_MAX_LAYERS || !cfg->gdn || !cfg->attn || !cfg->lm_head || + cfg->vocab == 0 || cfg->vocab > OMEGA_DEC_ARGMAX_SPAN * OMEGA_DEC_ARGMAX_SPAN) return OMEGA_GPU_DGRAPH_BAD_ARGS; + if (cfg->layers < 64u && (cfg->attn_mask >> cfg->layers) != 0) return OMEGA_GPU_DGRAPH_BAD_ARGS; + uint32_t n = 0, k = 0; + omega_gpu_bf16_tensor_shape(cfg->lm_head, &n, &k); + if (n != cfg->vocab || k != H) return OMEGA_GPU_DGRAPH_BAD_ARGS; + OmegaGpuQ38Graph *g = omega_gpu_host_calloc(1, sizeof *g); + if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS; + g->cfg = *cfg; + g->am_ctas = (cfg->vocab + OMEGA_DEC_ARGMAX_SPAN - 1) / OMEGA_DEC_ARGMAX_SPAN; + g->xq_layer = -1; + for (uint32_t l = 0, na = 0, ng = 0; l < cfg->layers; l++) { + g->layer[l].attn = (int)((cfg->attn_mask >> l) & 1u); + g->layer[l].kind_index = g->layer[l].attn ? na++ : ng++; + } + Layout *L = &g->lo; size_t o = 0; +#define REG(f, bytes) do { L->f = o; o = al(o + (bytes)); } while (0) + REG(x, H * 4); REG(xq, H * 4); REG(mix, OMEGA_GPU_Q38_GDN_MIX * 4); REG(ab, OMEGA_GPU_Q38_AB * 4); REG(agg, OMEGA_GPU_Q38_ATT_MIX * 4); + REG(att, OMEGA_GPU_Q38_MIXER_OUT * 4); REG(o, H * 4); REG(gu, 2 * I * 4); REG(act, I * 4); REG(hidden, H * 4); + REG(logits, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN * 4); REG(am_v, OMEGA_DEC_ARGMAX_SPAN * 4); REG(am_i, OMEGA_DEC_ARGMAX_SPAN * 4); + REG(token, 16); REG(norms, (size_t)cfg->layers * 2 * H * 4); REG(fnorm, H * 4); REG(tabs, (size_t)cfg->layers * 4 * 16); + REG(gtab, OMEGA_GPU_Q38_ATT_MIX * 4); +#undef REG + L->bytes = o; + int rc = OMEGA_GPU_DGRAPH_OK; + if (omega_gpu_cmdlist_create(OMEGA_GPU_CMDLIST_MAX_LAUNCHES, &g->cl) != 0) { omega_gpu_q38_graph_free(g); return OMEGA_GPU_DGRAPH_CHIP_FAIL; } + omega_gpu_session_lock(); + if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->bytes, &g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; + static const OmegaDecSpec spec[K_COUNT] = { + [K_NORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_NORM, .width = H }, + [K_ADDNORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, .width = H }, + [K_SW] = { .kind = OMEGA_DEC_SWIGLU, .width = I }, + [K_GATHER] = { .kind = OMEGA_DEC_GATHER, .width = OMEGA_GPU_Q38_ATT_MIX, .stride = OMEGA_GPU_Q38_ATT_MIX }, + [K_GATE] = { .kind = OMEGA_DEC_GATE }, + [K_AM0] = { .kind = OMEGA_DEC_ARGMAX, .mode = 0 }, + [K_AM1] = { .kind = OMEGA_DEC_ARGMAX, .mode = 1 }, + [K_SW_M1] = { .kind = OMEGA_DEC_SWIGLU, .width = I, .mutant = 1 }, + [K_GATHER_M1] = { .kind = OMEGA_DEC_GATHER, .width = OMEGA_GPU_Q38_ATT_MIX, .stride = OMEGA_GPU_Q38_ATT_MIX, .mutant = 1 }, + [K_GATE_M1] = { .kind = OMEGA_DEC_GATE, .mutant = 1 }, + [K_ADDNORM_M1] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, .width = H, .mutant = 1 }, + }; + for (unsigned i = 0; i < K_COUNT && rc == OMEGA_GPU_DGRAPH_OK; i++) rc = kern_build(&g->k[i], &spec[i]); + if (rc == OMEGA_GPU_DGRAPH_OK) { + memset(g->buf.cpu, 0, L->bytes); + float *lg = cpu_of(g, L->logits), *amv = cpu_of(g, L->am_v); + for (size_t i = cfg->vocab; i < (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN; i++) lg[i] = NEG_PAD; /* lm_head writes [0, vocab) only */ + for (size_t i = 0; i < OMEGA_DEC_ARGMAX_SPAN; i++) amv[i] = NEG_PAD; /* stage 1 writes [0, ctas) only */ + omega_q38_gather_table(cpu_of(g, L->gtab)); + if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; + } + omega_gpu_session_unlock(); + if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_q38_graph_free(g); return rc; } + *out = g; + return OMEGA_GPU_DGRAPH_OK; +} + +static int shape_is(const OmegaGpuFp8Tensor *t, uint32_t n, uint32_t k) { + uint32_t te = 0, tn = 0, tk = 0; if (t) omega_gpu_fp8_tensor_shape(t, &te, &tn, &tk); + return t && te == 1 && tn == n && tk == k; +} + +int omega_gpu_q38_graph_set_layer(OmegaGpuQ38Graph *g, uint32_t layer, const OmegaGpuFp8Tensor *mix, const OmegaGpuBf16Tensor *ab, + const OmegaGpuFp8Tensor *o, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, + const float *in_norm, const float *post_norm) { + if (!g || layer >= g->cfg.layers || !in_norm || !post_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; + const int attn = g->layer[layer].attn; + if (!shape_is(mix, attn ? OMEGA_GPU_Q38_ATT_MIX : OMEGA_GPU_Q38_GDN_MIX, H) || !shape_is(o, H, OMEGA_GPU_Q38_MIXER_OUT) || + !shape_is(gate_up, 2 * I, H) || !shape_is(down, H, I)) return OMEGA_GPU_DGRAPH_BAD_ARGS; + if (attn ? ab != NULL : ab == NULL) return OMEGA_GPU_DGRAPH_BAD_ARGS; + if (!attn) { uint32_t n = 0, k = 0; omega_gpu_bf16_tensor_shape(ab, &n, &k); if (n != OMEGA_GPU_Q38_AB || k != H) return OMEGA_GPU_DGRAPH_BAD_ARGS; } + uint64_t t[8]; + if (omega_gpu_fp8_tensor_entry(mix, 0, &t[0], &t[1]) != 0 || omega_gpu_fp8_tensor_entry(o, 0, &t[2], &t[3]) != 0 || + omega_gpu_fp8_tensor_entry(gate_up, 0, &t[4], &t[5]) != 0 || omega_gpu_fp8_tensor_entry(down, 0, &t[6], &t[7]) != 0) return OMEGA_GPU_DGRAPH_BAD_ARGS; + omega_gpu_session_lock(); + memcpy((uint8_t *)cpu_of(g, g->lo.tabs) + (size_t)layer * 64u, t, sizeof t); + memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * 2 * H, in_norm, H * 4u); + memcpy((float *)cpu_of(g, g->lo.norms) + (size_t)layer * 2 * H + H, post_norm, H * 4u); + int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; + omega_gpu_session_unlock(); + if (rc == OMEGA_GPU_DGRAPH_OK) { + g->layer[layer].mix = mix; g->layer[layer].ab = ab; g->layer[layer].o = o; g->layer[layer].gu = gate_up; g->layer[layer].dn = down; + g->layer[layer].set = 1; + } + return rc; +} + +int omega_gpu_q38_graph_set_final_norm(OmegaGpuQ38Graph *g, const float *final_norm) { + if (!g || !final_norm || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; + omega_gpu_session_lock(); + memcpy(cpu_of(g, g->lo.fnorm), final_norm, H * 4u); + int rc = omega_gpu_session_sync_to_gpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; + omega_gpu_session_unlock(); + if (rc == OMEGA_GPU_DGRAPH_OK) g->final_set = 1; + return rc; +} + +void omega_gpu_q38_graph_set_flush_each(OmegaGpuQ38Graph *g, int on) { if (g) g->flush_each = on ? 1 : 0; } +void omega_gpu_q38_graph_test_set_mutant(OmegaGpuQ38Graph *g, int mutant) { if (g) g->mutant = mutant; } + +/* ---- the list ---- */ + +static OmegaGpuCmdListOpts list_opts(const OmegaGpuQ38Graph *g) { + OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); + o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = (OmegaGpuCmdListBarrier)omega_gpu_decode_graph_barrier(); o.timeout_ms = TIMEOUT_MS; o.stamps = g->stamps; + return o; +} + +static int submit_now(OmegaGpuQ38Graph *g) { + if (omega_gpu_cmdlist_count(g->cl) == 0) return OMEGA_GPU_DGRAPH_OK; + OmegaGpuCmdListOpts o = list_opts(g); + OmegaGpuCmdListStats st; memset(&st, 0, sizeof st); + int rc = omega_gpu_cmdlist_submit(g->cl, &o, &st); + g->acc.launches += st.launches; g->acc.submits += st.submits; g->acc.chip_ns += st.elapsed_ns; g->acc.sems_done += st.sems_done; + if (rc == 0 && g->stamps) { + uint64_t tmp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; + int cnt = omega_gpu_cmdlist_stamps(g->cl, tmp, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1); + if (cnt < 0) rc = -1; + else { const int from = g->n_stamp == 0 ? 0 : 1; for (int i = from; i < cnt; i++) g->stamp[g->n_stamp++] = tmp[i]; } + } + g->tagged_in_list = 0; + omega_gpu_cmdlist_reset(g->cl); + if (rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); + if (st.sems_done != st.launches) { + char e[128]; omega_gpu_fmt(e, sizeof e, "q38 graph: %u of %u launch semaphores DONE (first missing %d)", st.sems_done, st.launches, st.first_missing); + return fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL); + } + return OMEGA_GPU_DGRAPH_OK; +} + +static void tag(OmegaGpuQ38Graph *g, uint8_t kind) { + const uint32_t n = omega_gpu_cmdlist_count(g->cl); + for (; g->tagged_in_list < n && g->n_kind < OMEGA_GPU_CMDLIST_MAX_LAUNCHES; g->tagged_in_list++) g->kind[g->n_kind++] = kind; +} + +static int after_op(OmegaGpuQ38Graph *g, uint8_t kind, int api_rc) { + if (api_rc != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); + tag(g, kind); + return g->flush_each ? submit_now(g) : OMEGA_GPU_DGRAPH_OK; +} + +static int add_glue(OmegaGpuQ38Graph *g, uint8_t kind, int ki, uint32_t gx, uint32_t gy, size_t elems, const uint32_t *args, uint32_t n_args) { + const Kern *k = &g->k[ki]; + OmegaGpuLaunch L; memset(&L, 0, sizeof L); + L.code_va = k->code.va; L.gpr_count = k->gpr; L.threads_x = OMEGA_DEC_THREADS; L.threads_y = 1; L.grid_x = gx; L.grid_y = gy; + L.num_elements = elems; L.args = args; L.n_args = n_args; L.timeout_ms = TIMEOUT_MS; + return after_op(g, kind, omega_gpu_cmdlist_add(g->cl, &L) < 0 ? -1 : 0); +} + +/* RES at width 5120: NORM (x -> y) or ADD|NORM (x += a; y = norm(x)) */ +static int rec_res(OmegaGpuQ38Graph *g, int ki, uint64_t x, uint64_t a, uint64_t nw, uint64_t y, float scale) { + uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w); + put64(w, OMEGA_DEC_RES_X, x); put64(w, OMEGA_DEC_RES_A, a); put64(w, OMEGA_DEC_RES_NW, nw); put64(w, OMEGA_DEC_RES_Y, y); + putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale); + return add_glue(g, ki == K_NORM ? OMEGA_GPU_Q38_K_NORM : OMEGA_GPU_Q38_K_RES, ki, 1, 1, H, w, OMEGA_DEC_RES_WORDS); +} + +static int rec_fp8(OmegaGpuQ38Graph *g, uint8_t kind, const OmegaGpuFp8Tensor *t, uint64_t tab, uint64_t x, uint64_t y) { + return after_op(g, kind, omega_gpu_fp8_gemv_grouped_record(g->cl, t, 1, tab, x, 1, y)); +} + +int omega_gpu_q38_graph_begin_token(OmegaGpuQ38Graph *g, const float *x_embed) { + if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; + omega_gpu_session_lock(); + g->t_begin = omega_gpu_session_now_ns(); + g->in_token = 1; g->failed = 0; g->xq_layer = -1; g->hidden_ready = 0; + memset(&g->acc, 0, sizeof g->acc); + g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0; + omega_gpu_cmdlist_reset(g->cl); + if (!omega_gpu_session_open()) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); + uint32_t *tok = cpu_of(g, g->lo.token); + tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON; + if (x_embed) memcpy(cpu_of(g, g->lo.x), x_embed, H * 4u); + if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); + return OMEGA_GPU_DGRAPH_OK; +} + +int omega_gpu_q38_graph_record_layer(OmegaGpuQ38Graph *g, uint32_t l, uint32_t pos) { + if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; + if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; + if (l >= g->cfg.layers || !g->layer[l].set || g->hidden_ready || (l + 1 == g->cfg.layers && !g->final_set)) + return fail(g, "q38 graph: layer not set, out of order, or final norm missing", OMEGA_GPU_DGRAPH_BAD_ARGS); + const uint64_t x = va_of(g, g->lo.x), xq = va_of(g, g->lo.xq), mix = va_of(g, g->lo.mix), agg = va_of(g, g->lo.agg); + const uint64_t att = va_of(g, g->lo.att), o = va_of(g, g->lo.o), gu = va_of(g, g->lo.gu), act = va_of(g, g->lo.act); + const uint64_t in_norm = va_of(g, g->lo.norms) + (uint64_t)l * 2 * H * 4u, post_norm = in_norm + H * 4u, tab = va_of(g, g->lo.tabs) + (uint64_t)l * 64u; + const uint32_t ki = g->layer[l].kind_index; + int rc = OMEGA_GPU_DGRAPH_OK; + if (g->xq_layer != (int)l) rc = rec_res(g, K_NORM, x, 0, in_norm, xq, SCALE_2P64); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_fp8(g, OMEGA_GPU_Q38_K_MIX, g->layer[l].mix, tab, xq, mix); + uint64_t mixer_out = att; + if (!g->layer[l].attn) { + const uint64_t ab = va_of(g, g->lo.ab); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_Q38_K_AB, omega_gpu_bf16_gemv_record(g->cl, g->layer[l].ab, xq, ab)); + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = after_op(g, OMEGA_GPU_Q38_K_GDN, omega_gpu_gdn_record(g->cl, g->cfg.gdn, ki, mix, mix + OMEGA_GPU_Q38_GDN_QKV * 4u, ab, + g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mixer_out)); + } else { + uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); + put64(w, OMEGA_DEC_GA_IN, mix); put64(w, OMEGA_DEC_GA_TAB, va_of(g, g->lo.gtab)); put64(w, OMEGA_DEC_GA_OUT, agg); + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = add_glue(g, OMEGA_GPU_Q38_K_GATHER, g->mutant == 2 ? K_GATHER_M1 : K_GATHER, (OMEGA_GPU_Q38_ATT_MIX + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, 1, + OMEGA_GPU_Q38_ATT_MIX, w, OMEGA_DEC_GA_WORDS); + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = after_op(g, OMEGA_GPU_Q38_K_ATTN, omega_gpu_decode_attn256_record(g->cl, g->cfg.attn, ki, pos, agg, g->cfg.eps, SCALE_2P64, att)); + uint32_t w2[OMEGA_DEC_GT_WORDS]; memset(w2, 0, sizeof w2); + put64(w2, OMEGA_DEC_GT_A, att); put64(w2, OMEGA_DEC_GT_G, agg + OMEGA_GPU_Q38_ATT_QKV * 4u); put64(w2, OMEGA_DEC_GT_OUT, att); + w2[(OMEGA_DEC_GT_N - 0x380) / 4] = OMEGA_GPU_Q38_MIXER_OUT; + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = add_glue(g, OMEGA_GPU_Q38_K_GATE, g->mutant == 3 ? K_GATE_M1 : K_GATE, OMEGA_GPU_Q38_MIXER_OUT / OMEGA_DEC_THREADS, 1, + OMEGA_GPU_Q38_MIXER_OUT, w2, OMEGA_DEC_GT_WORDS); + } + if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_fp8(g, OMEGA_GPU_Q38_K_O, g->layer[l].o, tab + 16u, mixer_out, o); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res(g, g->mutant == 4 ? K_ADDNORM_M1 : K_ADDNORM, x, o, post_norm, xq, SCALE_2P64); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_fp8(g, OMEGA_GPU_Q38_K_GATE_UP, g->layer[l].gu, tab + 32u, xq, gu); + uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3); + put64(w3, OMEGA_DEC_SW_GU, gu); put64(w3, OMEGA_DEC_SW_ACT, act); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q38_K_SWIGLU, g->mutant == 1 ? K_SW_M1 : K_SW, I / OMEGA_DEC_THREADS, 1, I, w3, OMEGA_DEC_SW_WORDS); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_fp8(g, OMEGA_GPU_Q38_K_DOWN, g->layer[l].dn, tab + 48u, act, o); + if (rc == OMEGA_GPU_DGRAPH_OK) { + const int last = l + 1 == g->cfg.layers; + const uint64_t nw = last ? va_of(g, g->lo.fnorm) : in_norm + 2 * H * 4u; + rc = rec_res(g, K_ADDNORM, x, o, nw, last ? va_of(g, g->lo.hidden) : xq, last ? 1.0f : SCALE_2P64); + if (rc == OMEGA_GPU_DGRAPH_OK) { if (last) g->hidden_ready = 1; else g->xq_layer = (int)l + 1; } + } + return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); +} + +int omega_gpu_q38_graph_record_final(OmegaGpuQ38Graph *g) { + if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; + if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL; + if (!g->final_set) return fail(g, "q38 graph: final norm not set", OMEGA_GPU_DGRAPH_BAD_ARGS); + const uint64_t x = va_of(g, g->lo.x), hidden = va_of(g, g->lo.hidden), logits = va_of(g, g->lo.logits), token = va_of(g, g->lo.token); + int rc = OMEGA_GPU_DGRAPH_OK; + if (!g->hidden_ready) rc = rec_res(g, K_NORM, x, 0, va_of(g, g->lo.fnorm), hidden, 1.0f); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_Q38_K_LM_HEAD, omega_gpu_bf16_gemv_record(g->cl, g->cfg.lm_head, hidden, logits)); + uint32_t a0[OMEGA_DEC_AM_WORDS], a1[OMEGA_DEC_AM_WORDS]; memset(a0, 0, sizeof a0); memset(a1, 0, sizeof a1); + const uint64_t amv = va_of(g, g->lo.am_v), ami = va_of(g, g->lo.am_i); + put64(a0, OMEGA_DEC_AM_V, logits); put64(a0, OMEGA_DEC_AM_I, 0); put64(a0, OMEGA_DEC_AM_OV, amv); put64(a0, OMEGA_DEC_AM_OI, ami); + put64(a1, OMEGA_DEC_AM_V, amv); put64(a1, OMEGA_DEC_AM_I, ami); put64(a1, OMEGA_DEC_AM_OV, token + 4u); put64(a1, OMEGA_DEC_AM_OI, token); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q38_K_ARGMAX, K_AM0, g->am_ctas, 1, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN, a0, OMEGA_DEC_AM_WORDS); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = add_glue(g, OMEGA_GPU_Q38_K_ARGMAX, K_AM1, 1, 1, OMEGA_DEC_ARGMAX_SPAN, a1, OMEGA_DEC_AM_WORDS); + return rc == OMEGA_GPU_DGRAPH_OK ? rc : fail(g, NULL, rc); +} + +int omega_gpu_q38_graph_end_token(OmegaGpuQ38Graph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st) { + if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; + int rc = g->failed ? OMEGA_GPU_DGRAPH_CHIP_FAIL : OMEGA_GPU_DGRAPH_OK; + const uint64_t t_rec = omega_gpu_session_now_ns(); + if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g); + if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_decode_attn256_counters_check(g->cfg.attn) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); + if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); + if (rc == OMEGA_GPU_DGRAPH_OK) { + const uint32_t t = ((const uint32_t *)cpu_of(g, g->lo.token))[0]; + if (t == TOKEN_POISON || t >= g->cfg.vocab) { + char e[96]; omega_gpu_fmt(e, sizeof e, "q38 graph: token output %u not written or out of range", t); + rc = fail(g, e, OMEGA_GPU_DGRAPH_CHIP_FAIL); + } else if (token) *token = t; + } + omega_gpu_cmdlist_reset(g->cl); + g->in_token = 0; + const uint64_t t_end = omega_gpu_session_now_ns(); + if (st) { *st = g->acc; st->record_ns = (g->flush_each ? t_end : t_rec) - g->t_begin; st->total_ns = t_end - g->t_begin; } + omega_gpu_session_unlock(); + return rc; +} + +int omega_gpu_q38_graph_token(OmegaGpuQ38Graph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st) { + if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS; + if (omega_gpu_q38_graph_begin_token(g, x_embed) == OMEGA_GPU_DGRAPH_BAD_ARGS) return OMEGA_GPU_DGRAPH_BAD_ARGS; + for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) (void)omega_gpu_q38_graph_record_layer(g, l, pos); + if (!g->failed) (void)omega_gpu_q38_graph_record_final(g); + return omega_gpu_q38_graph_end_token(g, token, st); +} + +int omega_gpu_q38_graph_read(OmegaGpuQ38Graph *g, float *x, float *hidden, float *logits) { + if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; + omega_gpu_session_lock(); + int rc = omega_gpu_session_sync_to_cpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; + if (rc == OMEGA_GPU_DGRAPH_OK) { + if (x) memcpy(x, cpu_of(g, g->lo.x), H * 4u); + if (hidden) memcpy(hidden, cpu_of(g, g->lo.hidden), H * 4u); + if (logits) memcpy(logits, cpu_of(g, g->lo.logits), (size_t)g->cfg.vocab * 4u); + } + omega_gpu_session_unlock(); + return rc; +} + +int omega_gpu_q38_graph_read_buf(OmegaGpuQ38Graph *g, int which, float *out, uint32_t n) { + if (!g || g->in_token || !out || which < 0 || which >= OMEGA_GPU_Q38_BUF_COUNT) return OMEGA_GPU_DGRAPH_BAD_ARGS; + const struct { size_t off; uint32_t cap; } b[OMEGA_GPU_Q38_BUF_COUNT] = { + [OMEGA_GPU_Q38_BUF_X] = { g->lo.x, H }, [OMEGA_GPU_Q38_BUF_XQ] = { g->lo.xq, H }, [OMEGA_GPU_Q38_BUF_MIX] = { g->lo.mix, OMEGA_GPU_Q38_GDN_MIX }, + [OMEGA_GPU_Q38_BUF_AB] = { g->lo.ab, OMEGA_GPU_Q38_AB }, [OMEGA_GPU_Q38_BUF_AGG] = { g->lo.agg, OMEGA_GPU_Q38_ATT_MIX }, + [OMEGA_GPU_Q38_BUF_ATT] = { g->lo.att, OMEGA_GPU_Q38_MIXER_OUT }, [OMEGA_GPU_Q38_BUF_O] = { g->lo.o, H }, + [OMEGA_GPU_Q38_BUF_GU] = { g->lo.gu, 2 * I }, [OMEGA_GPU_Q38_BUF_ACT] = { g->lo.act, I }, [OMEGA_GPU_Q38_BUF_HIDDEN] = { g->lo.hidden, H }, + }; + if (n > b[which].cap) return OMEGA_GPU_DGRAPH_BAD_ARGS; + omega_gpu_session_lock(); + int rc = omega_gpu_session_sync_to_cpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; + if (rc == OMEGA_GPU_DGRAPH_OK) memcpy(out, cpu_of(g, b[which].off), (size_t)n * 4u); + omega_gpu_session_unlock(); + return rc; +} + +/* ---- timeline ---- */ +void omega_gpu_q38_graph_set_stamps(OmegaGpuQ38Graph *g, int on) { if (g && !g->in_token) g->stamps = on ? 1 : 0; } + +int omega_gpu_q38_graph_stamps(const OmegaGpuQ38Graph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap) { + if (!g || g->in_token || !stamps || !kinds) return OMEGA_GPU_DGRAPH_BAD_ARGS; + if (!g->stamps || g->n_stamp != g->n_kind + 1u) { omega_gpu_session_set_error("q38 graph: no timeline for the last token (stamps off or it failed)"); return OMEGA_GPU_DGRAPH_BAD_ARGS; } + if (cap < g->n_stamp) { omega_gpu_session_set_error("q38 graph: timeline buffers too small"); return OMEGA_GPU_DGRAPH_BAD_ARGS; } + memcpy(stamps, g->stamp, (size_t)g->n_stamp * sizeof stamps[0]); + memcpy(kinds, g->kind, (size_t)g->n_kind); + return (int)g->n_kind; +} + +const char *omega_gpu_q38_graph_kind_name(uint8_t kind) { + static const char *const names[OMEGA_GPU_Q38_K_COUNT] = { "norm", "mix", "ab", "gdn", "gather", "attn", "gate", "o_proj", "res", + "gate_up", "swiglu", "down", "lm_head", "argmax" }; + return kind < OMEGA_GPU_Q38_K_COUNT ? names[kind] : "?"; +} diff --git a/src/omega_gpu_q38_graph.h b/src/omega_gpu_q38_graph.h new file mode 100644 index 000000000..768dcf0c5 --- /dev/null +++ b/src/omega_gpu_q38_graph.h @@ -0,0 +1,122 @@ +#ifndef OMEGA_GPU_Q38_GRAPH_H +#define OMEGA_GPU_Q38_GRAPH_H +/* + * omega_gpu_q38_graph: one Qwen3.8-27B decode token as ONE command list (sc #642, 2026-10-18). The 30B's + * omega_gpu_decode_graph (MoE, hidden 2048) is the template; this one is dense, hidden 5120, and mixes two layer kinds: + * Gated DeltaNet (48 layers) and full attention with head_dim 256 (16 layers, every 4th). + * + * (layer 0 only) NORM xq = RMSNorm(x) * in_norm[0] * 2^64 (the FP8 GEMV x' row) + * GDN layer l: fp8 mix mix[16384] = W_qkvz[l] . xq (in_proj_qkv 10240 | in_proj_z 6144) + * bf16 ab ab[96] = W_ab[l] . xq (a/b rows interleaved, uploaded * 2^-64: plain ab out) + * gdn att[6144] = GDN step(mix, mix + 10240, ab) * 2^64 omega_gpu_gdn_record + * attention l: fp8 mix mix[14336] = W_qgkv[l] . xq (q_proj 24 x [q 256 | gate 256], k_proj 1024, v_proj 1024) + * GATHER agg[14336] = permuted q | permuted k | v (8192, the attention kernel's row) | gate (6144) + * attn att[6144] = attention(agg, KV[l], pos) * 2^64 omega_gpu_decode_attn256_record + * GATE att *= sigmoid(agg + 8192) + * every layer: fp8 o o[5120] = W_o[l] . att + * ADD|NORM x += o; xr = RMSNorm(x) * post_norm[l] * 2^64 + * fp8 gu gu[34816] = W_gu[l] . xr (gate 17408 | up 17408) + * SWIGLU act[17408] = silu(gate) * up * 2^64 + * fp8 down o[5120] = W_down[l] . act + * ADD|NORM x += o; xq = RMSNorm(x) * in_norm[l + 1] * 2^64, or (last layer) hidden = RMSNorm(x) * final_norm + * final: bf16 lm_head logits = W_lm . hidden; ARGMAX x2 (122 CTAs over 248320, then 1) + * + * Launches: GDN layer 9, attention layer 10, plus 1 + 3: 48 * 9 + 16 * 10 + 4 = 596 for the whole model. + * Norm weights are passed already folded to (1 + w) (the model's zero-centred RMSNorm). The GDN and attention state live + * in the caller's OmegaGpuGdn (one entry per GDN layer, in model order) and OmegaGpuDecodeAttn256Cache (one per attention + * layer, in model order); the graph maps model layer l to its index of that kind. + * + * Memory: one omega_gpu_session_alloc buffer (GPU-uncached; nvos.h:1117 "For system memory this will not be coherent with + * direct CPU mappings"): the host writes x before the submit and reads the token / logits after. + * Threading and kernel caches: as omega_gpu_decode_graph.h (begin_token takes omega_gpu_session_lock(), end_token + * releases it; the record APIs refuse a cache eviction, so warm each shape up with one non-record call first). + * + * ============ C signatures (for the Rust binding) ============ + * int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Graph **out); + * void omega_gpu_q38_graph_free(OmegaGpuQ38Graph *g); + * int omega_gpu_q38_graph_set_layer(g, layer, mix, ab, o, gate_up, down, in_norm, post_norm); + * int omega_gpu_q38_graph_set_final_norm(g, final_norm); + * int omega_gpu_q38_graph_token(g, x_embed, pos, &token, &stats); + * int omega_gpu_q38_graph_read(g, x, hidden, logits); + * int omega_gpu_q38_graph_read_buf(g, which, out, n); + * Return codes: the decode graph's OMEGA_GPU_DGRAPH_* (0 OK, -1 BAD_ARGS, -3 CODEGEN_FAIL, -4 CHIP_FAIL); the failing + * stage is in omega_gpu_session_last_error(). + */ +#include "omega_gpu_decode_graph.h" +#include "omega_gpu_decode_attn256_api.h" +#include "omega_gpu_gdn_api.h" +#include +#ifdef __cplusplus +extern "C" { +#endif + +#define OMEGA_GPU_Q38_HIDDEN 5120u +#define OMEGA_GPU_Q38_INTER 17408u +#define OMEGA_GPU_Q38_GDN_MIX 16384u /* in_proj_qkv 10240 | in_proj_z 6144 */ +#define OMEGA_GPU_Q38_GDN_QKV 10240u +#define OMEGA_GPU_Q38_AB 96u /* 48 heads x {a, b} */ +#define OMEGA_GPU_Q38_ATT_MIX 14336u /* q_proj 12288 | k_proj 1024 | v_proj 1024 */ +#define OMEGA_GPU_Q38_ATT_QKV 8192u /* the attention kernel's row: q 24 x 256 | k 4 x 256 | v 4 x 256 */ +#define OMEGA_GPU_Q38_MIXER_OUT 6144u /* GDN 48 x 128 = attention 24 x 256 */ +#define OMEGA_GPU_Q38_ROTARY 64u +#define OMEGA_GPU_Q38_MAX_LAYERS 64u + +typedef struct { + uint32_t layers; /* 1..64 (a model prefix in tests) */ + uint64_t attn_mask; /* bit l set: model layer l is full attention, else GDN */ + uint32_t vocab; /* lm_head rows (248320), <= 2048 * 2048 */ + float eps; /* rms_norm_eps (1e-6) */ + float eps_l2; /* GDN q/k l2norm eps (1e-6) */ + OmegaGpuGdn *gdn; /* >= (layers - attention layers) entries, parameters set */ + OmegaGpuDecodeAttn256Cache *attn; /* >= attention layers entries, q/k norms set; rotary 64 */ + const OmegaGpuBf16Tensor *lm_head; /* [vocab][5120] */ +} OmegaGpuQ38GraphConfig; + +typedef struct OmegaGpuQ38Graph OmegaGpuQ38Graph; + +int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Graph **out); +void omega_gpu_q38_graph_free(OmegaGpuQ38Graph *g); +/* Shapes are checked. GDN layer: mix [1][16384][5120], ab bf16 [96][5120] (row 2h = in_proj_a[h], 2h + 1 = in_proj_b[h], + * each value * 2^-64), o [1][5120][6144]. Attention layer: mix [1][14336][5120], ab NULL, o [1][5120][6144]. + * Both: gate_up [1][34816][5120], down [1][5120][17408], in_norm and post_norm f32 [5120] as (1 + w). The tensors stay + * owned by the caller. */ +int omega_gpu_q38_graph_set_layer(OmegaGpuQ38Graph *g, uint32_t layer, const OmegaGpuFp8Tensor *mix, const OmegaGpuBf16Tensor *ab, + const OmegaGpuFp8Tensor *o, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, + const float *in_norm, const float *post_norm); +int omega_gpu_q38_graph_set_final_norm(OmegaGpuQ38Graph *g, const float *final_norm); /* f32 [5120] as (1 + w) */ +/* One whole token: x_embed (f32 [5120]) -> greedy token; pos: the token's position (attention layers append K/V there, + * GDN layers advance their state). */ +int omega_gpu_q38_graph_token(OmegaGpuQ38Graph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st); +/* The same in steps (between begin and end only record calls on this thread). */ +int omega_gpu_q38_graph_begin_token(OmegaGpuQ38Graph *g, const float *x_embed); +int omega_gpu_q38_graph_record_layer(OmegaGpuQ38Graph *g, uint32_t layer, uint32_t pos); +int omega_gpu_q38_graph_record_final(OmegaGpuQ38Graph *g); +int omega_gpu_q38_graph_end_token(OmegaGpuQ38Graph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st); +/* Takes the lock; copies the device x, hidden and logits (vocab values) of the last token; any pointer may be NULL. */ +int omega_gpu_q38_graph_read(OmegaGpuQ38Graph *g, float *x, float *hidden, float *logits); +/* Test and trace reads of one intermediate of the last token (f32, n values from the start of the buffer). */ +enum { OMEGA_GPU_Q38_BUF_X, OMEGA_GPU_Q38_BUF_XQ, OMEGA_GPU_Q38_BUF_MIX, OMEGA_GPU_Q38_BUF_AB, OMEGA_GPU_Q38_BUF_AGG, + OMEGA_GPU_Q38_BUF_ATT, OMEGA_GPU_Q38_BUF_O, OMEGA_GPU_Q38_BUF_GU, OMEGA_GPU_Q38_BUF_ACT, OMEGA_GPU_Q38_BUF_HIDDEN, + OMEGA_GPU_Q38_BUF_COUNT }; +int omega_gpu_q38_graph_read_buf(OmegaGpuQ38Graph *g, int which, float *out, uint32_t n); +/* The gather table (u32 [14336]): agg[n] = mix[tab[n]] (host side; the graph uploads it at create). */ +void omega_q38_gather_table(uint32_t *tab); + +/* ---- timeline (measurement; as the decode graph) ---- */ +enum { + OMEGA_GPU_Q38_K_NORM = 0, OMEGA_GPU_Q38_K_MIX, OMEGA_GPU_Q38_K_AB, OMEGA_GPU_Q38_K_GDN, OMEGA_GPU_Q38_K_GATHER, OMEGA_GPU_Q38_K_ATTN, + OMEGA_GPU_Q38_K_GATE, OMEGA_GPU_Q38_K_O, OMEGA_GPU_Q38_K_RES, OMEGA_GPU_Q38_K_GATE_UP, OMEGA_GPU_Q38_K_SWIGLU, OMEGA_GPU_Q38_K_DOWN, + OMEGA_GPU_Q38_K_LM_HEAD, OMEGA_GPU_Q38_K_ARGMAX, OMEGA_GPU_Q38_K_COUNT +}; +void omega_gpu_q38_graph_set_stamps(OmegaGpuQ38Graph *g, int on); +int omega_gpu_q38_graph_stamps(const OmegaGpuQ38Graph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap); +const char *omega_gpu_q38_graph_kind_name(uint8_t kind); +/* 1: submit after every launch (the comparison path). */ +void omega_gpu_q38_graph_set_flush_each(OmegaGpuQ38Graph *g, int on); +/* TEST ONLY: 1 SWIGLU up half off by one, 2 GATHER index + 1, 3 GATE dropped, 4 post-norm without its weight. 0 none. */ +void omega_gpu_q38_graph_test_set_mutant(OmegaGpuQ38Graph *g, int mutant); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/tests/.crumb b/tests/.crumb index 4f3a08ca5..8d5f2b233 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -296,14 +296,14 @@ } ], "children_root": "sha256:ddc3618190458bc5073e3bee67ffccc68a2ee9f065f2cb0d86545386a29fddf8", - "compiled_at": "2026-10-18T15:21:47Z", + "compiled_at": "2026-10-18T15:51:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:ef8690a977452cb604669c5e0f11a801b873f033c617e8530fa90b4a0074c111", + "digest": "sha256:27d5b38466b67976c685bfeb81d8c9fc6b5a8064e20a635649986f1f7fc84d54", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1274, - "generated_at_commit": "acdeff745c292cebb3b22cc7ae298409a0b7ee64", - "source_tree": "sha256:d713e4bedd04a8ecafe0343b1e3c372f480dc0f07b8a6efe63b17c6fadda6ea9" + "files": 1275, + "generated_at_commit": "3c90e430b5282f00f426c2421200d1e8485f5ebd", + "source_tree": "sha256:4d6d5d41535e622041c6933e099d16c35f1767d53b748033bc24efdfefcb7ae9" }, "owner_lane": "", "provenance": { diff --git a/tests/gpu_cmdlist_test.c b/tests/gpu_cmdlist_test.c index f0647d615..075c0f4d7 100644 --- a/tests/gpu_cmdlist_test.c +++ b/tests/gpu_cmdlist_test.c @@ -122,13 +122,13 @@ static int specs_build(void) { /* ---- host-only ---- */ static int host_only(void) { const struct { const char *name; OmegaDecSpec s; } cases[] = { - { "axpy", { OMEGA_DEC_AXPY, 0, AXPY_STRIDE, 0 } }, - { "res add|norm", { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, 0, 0 } }, - { "res combine|norm", { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 0, 0 } }, - { "res add", { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD, 0, 0 } }, - { "swiglu", { OMEGA_DEC_SWIGLU, 0, 0, 0 } }, - { "argmax", { OMEGA_DEC_ARGMAX, 0, 0, 0 } }, - { "argmax indexed", { OMEGA_DEC_ARGMAX, 1, 0, 0 } }, + { "axpy", { OMEGA_DEC_AXPY, 0, AXPY_STRIDE, 0, 0 } }, + { "res add|norm", { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, 0, 0, 0 } }, + { "res combine|norm", { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 0, 0, 0 } }, + { "res add", { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD, 0, 0, 0 } }, + { "swiglu", { OMEGA_DEC_SWIGLU, 0, 0, 0, 0 } }, + { "argmax", { OMEGA_DEC_ARGMAX, 0, 0, 0, 0 } }, + { "argmax indexed", { OMEGA_DEC_ARGMAX, 1, 0, 0, 0 } }, }; for (unsigned i = 0; i < sizeof cases / sizeof cases[0]; i++) { uint32_t insns = 0, gprs = 0; @@ -137,9 +137,18 @@ static int host_only(void) { CHECK(gprs <= 200, "%s gprs %u within budget", cases[i].name, gprs); printf("codegen %s: %u insns, peak %u gprs, declared %u\n", cases[i].name, insns, gprs, omega_bw_gpr_declared(gprs)); } - OmegaDecSpec bad[] = { { OMEGA_DEC_AXPY, 0, 0, 0 }, { OMEGA_DEC_AXPY, 0, 100, 0 }, { OMEGA_DEC_RES, 0, 0, 0 }, - { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_COMBINE, 0, 0 }, { (OmegaDecKind)0, 0, 0, 0 }, { OMEGA_DEC_RES, 8, 0, 0 } }; + OmegaDecSpec bad[] = { { OMEGA_DEC_AXPY, 0, 0, 0, 0 }, { OMEGA_DEC_AXPY, 0, 100, 0, 0 }, { OMEGA_DEC_RES, 0, 0, 0, 0 }, + { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_COMBINE, 0, 0, 0 }, { (OmegaDecKind)0, 0, 0, 0, 0 }, { OMEGA_DEC_RES, 8, 0, 0, 0 } }; for (unsigned i = 0; i < sizeof bad / sizeof bad[0]; i++) CHECK(omega_dec_prog_stats(&bad[i], NULL, NULL) != 0, "refusal %u", i); + /* widths (sc #642): 5120 and 8192 build; not a multiple of 1024, above 8192, COMBINE off 2048, SWIGLU off 256 refuse */ + OmegaDecSpec w; memset(&w, 0, sizeof w); w.kind = OMEGA_DEC_RES; w.mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM; + w.width = 5120; CHECK(omega_dec_prog_stats(&w, NULL, NULL) == 0, "codegen res width 5120"); + w.width = 8192; CHECK(omega_dec_prog_stats(&w, NULL, NULL) == 0, "codegen res width 8192"); + w.width = 3000; CHECK(omega_dec_prog_stats(&w, NULL, NULL) != 0, "refusal res width 3000"); + w.width = 9216; CHECK(omega_dec_prog_stats(&w, NULL, NULL) != 0, "refusal res width 9216"); + w.width = 5120; w.mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM; CHECK(omega_dec_prog_stats(&w, NULL, NULL) != 0, "refusal combine width 5120"); + memset(&w, 0, sizeof w); w.kind = OMEGA_DEC_SWIGLU; w.width = 17408; CHECK(omega_dec_prog_stats(&w, NULL, NULL) == 0, "codegen swiglu width 17408"); + w.width = 17409; CHECK(omega_dec_prog_stats(&w, NULL, NULL) != 0, "refusal swiglu width 17409"); return g_failed ? 1 : 0; } @@ -216,6 +225,150 @@ static int sim_mode(void) { return g_failed ? 1 : 0; } +/* ---- Qwen3.8-27B widths (sc #642): RES over 5120, SWIGLU over one dense 17408 row ---- */ +#define WH 5120u +#define WI 17408u +static int sim_wide(void) { + static Kern kw[4]; + static float x[WH], xr[WH], a[WH], nw[WH], y[WH], yr[WH], gu[2 * WI], act[WI], actr[WI]; + const uint32_t modes[3] = { OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, OMEGA_DEC_RES_NORM, OMEGA_DEC_RES_ADD }; + for (int mi = 0; mi < 3; mi++) for (int mut = 0; mut < 2; mut++) { + if (mut && !(modes[mi] & OMEGA_DEC_RES_NORM)) continue; + OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = modes[mi]; s.mutant = mut; s.width = WH; + if (kern_prog(&kw[0], &s)) { CHECK(0, "codegen res width %u mode %u", WH, modes[mi]); return 1; } + uint32_t sd = 300 + mi; + for (unsigned i = 0; i < WH; i++) { x[i] = frand(&sd, -2, 2); a[i] = frand(&sd, -1, 1); nw[i] = frand(&sd, 0.5f, 1.5f); y[i] = -7.0f; } + memcpy(xr, x, sizeof x); + /* ref_res at width WH */ + if (modes[mi] & OMEGA_DEC_RES_ADD) for (unsigned i = 0; i < WH; i++) xr[i] = xr[i] + a[i]; + if (modes[mi] & OMEGA_DEC_RES_NORM) { + double sq = 0; for (unsigned i = 0; i < WH; i++) sq += (double)xr[i] * xr[i]; + float rs = (float)(1.0 / sqrt(sq / WH + (double)EPS)); + for (unsigned i = 0; i < WH; i++) { float v = xr[i] * rs; v = v * nw[i]; yr[i] = v * SCALE_2P64; } + } + uint32_t args[OMEGA_DEC_RES_WORDS] = { 0 }; + put64(args, OMEGA_DEC_RES_X, (uintptr_t)x); put64(args, OMEGA_DEC_RES_A, (uintptr_t)a); put64(args, OMEGA_DEC_RES_NW, (uintptr_t)nw); + put64(args, OMEGA_DEC_RES_Y, (uintptr_t)y); putf(args, OMEGA_DEC_RES_EPS, EPS); putf(args, OMEGA_DEC_RES_SCALE, SCALE_2P64); + WsRange r[4] = { { (uintptr_t)x, (uintptr_t)x + sizeof x }, { (uintptr_t)a, (uintptr_t)a + sizeof a }, + { (uintptr_t)nw, (uintptr_t)nw + sizeof nw }, { (uintptr_t)y, (uintptr_t)y + sizeof y } }; + if (sim_one(&kw[0], args, OMEGA_DEC_RES_WORDS, 1, 1, r, 4, "res wide")) return 1; + int xbad = memcmp(x, xr, sizeof x) != 0, ybad = 0; + if (modes[mi] & OMEGA_DEC_RES_NORM) for (unsigned i = 0; i < WH; i++) if (!close_rel(y[i], yr[i], 2e-5f, 1e-6f)) ybad++; + if (!mut) { CHECK(!xbad, "sim res width %u mode %u: x bit-identical", WH, modes[mi]); CHECK(ybad == 0, "sim res width %u mode %u: y within 2e-5 (%d bad)", WH, modes[mi], ybad); } + else CHECK(ybad > 0, "sim res width %u mode %u mutant caught", WH, modes[mi]); + printf("sim res width %u mode %u mutant %d: x %s, y %d/%u outside tolerance\n", WH, modes[mi], mut, xbad ? "DIFFERS" : "identical", ybad, WH); + } + for (int mut = 0; mut < 2; mut++) { + OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_SWIGLU; s.mutant = mut; s.width = WI; + if (kern_prog(&kw[1], &s)) { CHECK(0, "codegen swiglu width %u", WI); return 1; } + uint32_t sd = 400; + for (unsigned i = 0; i < 2 * WI; i++) gu[i] = frand(&sd, -4, 4); + for (unsigned c = 0; c < WI; c++) { float v = gu[c], u = gu[WI + c]; actr[c] = v / (1.0f + expf(-v)) * u * SCALE_2P64; act[c] = -7.0f; } + uint32_t args[OMEGA_DEC_SW_WORDS] = { 0 }; put64(args, OMEGA_DEC_SW_GU, (uintptr_t)gu); put64(args, OMEGA_DEC_SW_ACT, (uintptr_t)act); putf(args, OMEGA_DEC_SW_SCALE, SCALE_2P64); + WsRange r[2] = { { (uintptr_t)gu, (uintptr_t)gu + sizeof gu }, { (uintptr_t)act, (uintptr_t)act + sizeof act } }; + if (sim_one(&kw[1], args, OMEGA_DEC_SW_WORDS, WI / OMEGA_DEC_THREADS, 1, r, 2, "swiglu wide")) return 1; + int bad = 0; for (unsigned i = 0; i < WI; i++) if (!close_rel(act[i], actr[i], 5e-5f, 1e-6f)) bad++; + if (!mut) CHECK(bad == 0, "sim swiglu width %u within 5e-5 (%d bad)", WI, bad); else CHECK(bad > 0, "sim swiglu width %u mutant caught", WI); + printf("sim swiglu width %u mutant %d: %d/%u outside tolerance\n", WI, mut, bad, WI); + } + /* RES rows form: 3 rows of [5120], ADD|NORM, row r at r * 5120 (nw shared); the mutant drops the weight */ + { + enum { ROWS = 3 }; + static float rx[ROWS * WH], rxr[ROWS * WH], ra[ROWS * WH], ry[ROWS * WH], ryr[ROWS * WH]; + for (int mut = 0; mut < 2; mut++) { + OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM | OMEGA_DEC_RES_ROWS; s.mutant = mut; s.width = WH; + if (kern_prog(&kw[2], &s)) { CHECK(0, "codegen res rows"); return 1; } + uint32_t sd = 500; + for (unsigned i = 0; i < ROWS * WH; i++) { rx[i] = frand(&sd, -2, 2); ra[i] = frand(&sd, -1, 1); ry[i] = -7.0f; } + for (unsigned i = 0; i < WH; i++) nw[i] = frand(&sd, 0.5f, 1.5f); + for (unsigned r = 0; r < ROWS; r++) { + float *xx = rxr + r * WH; + for (unsigned i = 0; i < WH; i++) xx[i] = rx[r * WH + i] + ra[r * WH + i]; + double sq = 0; for (unsigned i = 0; i < WH; i++) sq += (double)xx[i] * xx[i]; + float rs = (float)(1.0 / sqrt(sq / WH + (double)EPS)); + for (unsigned i = 0; i < WH; i++) { float v = xx[i] * rs; v = v * nw[i]; ryr[r * WH + i] = v * SCALE_2P64; } + } + uint32_t args[OMEGA_DEC_RES_WORDS] = { 0 }; + put64(args, OMEGA_DEC_RES_X, (uintptr_t)rx); put64(args, OMEGA_DEC_RES_A, (uintptr_t)ra); put64(args, OMEGA_DEC_RES_NW, (uintptr_t)nw); + put64(args, OMEGA_DEC_RES_Y, (uintptr_t)ry); putf(args, OMEGA_DEC_RES_EPS, EPS); putf(args, OMEGA_DEC_RES_SCALE, SCALE_2P64); + WsRange r[4] = { { (uintptr_t)rx, (uintptr_t)rx + sizeof rx }, { (uintptr_t)ra, (uintptr_t)ra + sizeof ra }, + { (uintptr_t)nw, (uintptr_t)nw + sizeof nw }, { (uintptr_t)ry, (uintptr_t)ry + sizeof ry } }; + if (sim_one(&kw[2], args, OMEGA_DEC_RES_WORDS, 1, ROWS, r, 4, "res rows")) return 1; + int xbad = memcmp(rx, rxr, sizeof rx) != 0, ybad = 0; + for (unsigned i = 0; i < ROWS * WH; i++) if (!close_rel(ry[i], ryr[i], 2e-5f, 1e-6f)) ybad++; + if (!mut) { CHECK(!xbad, "sim res rows: x bit-identical"); CHECK(ybad == 0, "sim res rows: y within 2e-5 (%d bad)", ybad); } + else CHECK(ybad > 0, "sim res rows mutant caught"); + printf("sim res rows %d x %u mutant %d: x %s, y %d/%u outside tolerance\n", ROWS, WH, mut, xbad ? "DIFFERS" : "identical", ybad, ROWS * WH); + } + OmegaDecSpec bad; memset(&bad, 0, sizeof bad); bad.kind = OMEGA_DEC_RES; bad.mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_ROWS; + CHECK(omega_dec_prog_stats(&bad, NULL, NULL) != 0, "refusal res rows with combine"); + bad.mode = OMEGA_DEC_RES_ROWS; CHECK(omega_dec_prog_stats(&bad, NULL, NULL) != 0, "refusal res rows alone"); + } + /* GATHER: 3 rows, input stride 14343, out width 14336 (the Qwen3.8 attention row), random source table */ + { + enum { ROWS = 3, GW = 14336, GS = 14343 }; + static float gin[ROWS * GS], gout[ROWS * GW]; + static uint32_t tab[GW]; + for (int mut = 0; mut < 2; mut++) { + OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_GATHER; s.mutant = mut; s.width = GW; s.stride = GS; + if (kern_prog(&kw[3], &s)) { CHECK(0, "codegen gather"); return 1; } + uint32_t sd = 600; + for (unsigned i = 0; i < ROWS * GS; i++) gin[i] = frand(&sd, -3, 3); + for (unsigned i = 0; i < GW; i++) { sd = sd * 1664525u + 1013904223u; tab[i] = sd % (GS - 1); } + for (unsigned i = 0; i < ROWS * GW; i++) gout[i] = -7.0f; + uint32_t args[OMEGA_DEC_GA_WORDS] = { 0 }; + put64(args, OMEGA_DEC_GA_IN, (uintptr_t)gin); put64(args, OMEGA_DEC_GA_TAB, (uintptr_t)tab); put64(args, OMEGA_DEC_GA_OUT, (uintptr_t)gout); + WsRange r[3] = { { (uintptr_t)gin, (uintptr_t)gin + sizeof gin }, { (uintptr_t)tab, (uintptr_t)tab + sizeof tab }, { (uintptr_t)gout, (uintptr_t)gout + sizeof gout } }; + if (sim_one(&kw[3], args, OMEGA_DEC_GA_WORDS, (GW + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, ROWS, r, 3, "gather")) return 1; + int bad = 0; + for (unsigned rr = 0; rr < ROWS; rr++) for (unsigned n = 0; n < GW; n++) if (memcmp(&gout[rr * GW + n], &gin[rr * GS + tab[n]], 4)) bad++; + if (!mut) CHECK(bad == 0, "sim gather bit-exact (%d bad)", bad); else CHECK(bad > 0, "sim gather mutant caught"); + printf("sim gather %d x %u mutant %d: %d/%u wrong\n", ROWS, GW, mut, bad, ROWS * GW); + } + OmegaDecSpec bad; memset(&bad, 0, sizeof bad); bad.kind = OMEGA_DEC_GATHER; bad.width = 0; bad.stride = 1; + CHECK(omega_dec_prog_stats(&bad, NULL, NULL) != 0, "refusal gather width 0"); + } + /* GATE in place over 6144 + 100 values (a tail CTA), mutant drops the gate */ + { + enum { GN = 6244 }; + static float a[GN], g[GN], ref[GN]; + for (int mut = 0; mut < 2; mut++) { + OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_GATE; s.mutant = mut; + if (kern_prog(&kw[3], &s)) { CHECK(0, "codegen gate"); return 1; } + uint32_t sd = 700; + for (unsigned i = 0; i < GN; i++) { a[i] = frand(&sd, -3, 3) * SCALE_2P64; g[i] = frand(&sd, -8, 8); ref[i] = a[i] * (1.0f / (1.0f + expf(-g[i]))); } + uint32_t args[OMEGA_DEC_GT_WORDS] = { 0 }; + put64(args, OMEGA_DEC_GT_A, (uintptr_t)a); put64(args, OMEGA_DEC_GT_G, (uintptr_t)g); put64(args, OMEGA_DEC_GT_OUT, (uintptr_t)a); + args[(OMEGA_DEC_GT_N - 0x380) / 4] = GN; + WsRange r[2] = { { (uintptr_t)a, (uintptr_t)a + sizeof a }, { (uintptr_t)g, (uintptr_t)g + sizeof g } }; + if (sim_one(&kw[3], args, OMEGA_DEC_GT_WORDS, (GN + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, 1, r, 2, "gate")) return 1; + int bad = 0; for (unsigned i = 0; i < GN; i++) if (!close_rel(a[i], ref[i], 5e-5f, 1e-6f)) bad++; + if (!mut) CHECK(bad == 0, "sim gate within 5e-5 (%d bad)", bad); else CHECK(bad > 0, "sim gate mutant caught"); + printf("sim gate %u mutant %d: %d/%u outside tolerance\n", GN, mut, bad, GN); + } + } + /* SWIGLU over rows: the group index is the row (gu [3][2 * 17408] -> act [3][17408]) */ + { + enum { ROWS = 3 }; + static float rgu[ROWS * 2 * WI], ract[ROWS * WI]; + OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_SWIGLU; s.width = WI; + if (kern_prog(&kw[1], &s)) { CHECK(0, "codegen swiglu rows"); return 1; } + uint32_t sd = 800; + for (unsigned i = 0; i < ROWS * 2 * WI; i++) rgu[i] = frand(&sd, -4, 4); + uint32_t args[OMEGA_DEC_SW_WORDS] = { 0 }; put64(args, OMEGA_DEC_SW_GU, (uintptr_t)rgu); put64(args, OMEGA_DEC_SW_ACT, (uintptr_t)ract); putf(args, OMEGA_DEC_SW_SCALE, 1.0f); + WsRange r[2] = { { (uintptr_t)rgu, (uintptr_t)rgu + sizeof rgu }, { (uintptr_t)ract, (uintptr_t)ract + sizeof ract } }; + if (sim_one(&kw[1], args, OMEGA_DEC_SW_WORDS, WI / OMEGA_DEC_THREADS, ROWS, r, 2, "swiglu rows")) return 1; + int bad = 0; + for (unsigned rr = 0; rr < ROWS; rr++) for (unsigned c = 0; c < WI; c++) { + float v = rgu[rr * 2 * WI + c], u = rgu[rr * 2 * WI + WI + c], want = v / (1.0f + expf(-v)) * u; + if (!close_rel(ract[rr * WI + c], want, 5e-5f, 1e-6f)) bad++; + } + CHECK(bad == 0, "sim swiglu rows within 5e-5 (%d bad)", bad); + printf("sim swiglu rows %d x %u: %d outside tolerance\n", ROWS, WI, bad); + } + return g_failed ? 1 : 0; +} + /* ---- chip ---- */ static OmegaGpuLaunch axpy_launch(const Kern *k, uint32_t *args, uint32_t stride, uint32_t iters) { OmegaGpuLaunch L; memset(&L, 0, sizeof L); @@ -458,7 +611,7 @@ int main(int argc, char **argv) { else { printf("usage: %s [--host-only|--sim] [--reps N] [--spin-us U] [--out receipt.json]\n", argv[0]); return 2; } } if (reps == 0) reps = 1; - int rc = host ? host_only() : sim ? sim_mode() : chip_run(out, reps); + int rc = host ? host_only() : sim ? (sim_mode() | sim_wide()) : chip_run(out, reps); printf("%s: %d checks, %d failed: %s\n", host ? "host-only" : sim ? "sim" : "chip", g_checks, g_failed, rc ? "FAIL" : "PASS"); return rc; } diff --git a/tests/gpu_q38_graph_test.c b/tests/gpu_q38_graph_test.c new file mode 100644 index 000000000..92fbf7d84 --- /dev/null +++ b/tests/gpu_q38_graph_test.c @@ -0,0 +1,293 @@ +/* + * gpu_q38_graph_test: one Qwen3.8-27B decode token as ONE command list (omega_gpu_q38_graph.h, sc #642). + * --host-only the gather table against the attention API's permutation, and the create / set_layer refusals that + * need no device beyond the session + * (default) chip, caller holds the GB10 lock. A synthetic Qwen3.8-shaped model on random weights: hidden 5120, + * intermediate 17408, vocab 248320; --layers N (2) and --mask M (0x2: bit l = layer l is attention), so by + * default one GDN layer then one attention layer: both kinds and the handover between them in every token. + * Tokens at positions 0, 1, 2: + * A baseline = the APIs one call at a time (FP8 / bf16 GEMV, GDN step, hd256 attention), host glue in + * C: rmsnorm f64, the gather table, sigmoid gate expf, SwiGLU expf, argmax strict >; its own GDN + * state and KV cache + * C main = the same token recorded as one list (omega_gpu_q38_graph_token) + * B the same list submitted one launch per submission (flush_each) + * Checks per token: B == C bit for bit (x, logits, token); A vs C token identical and logits within + * tolerance; x within tolerance. Negative controls: graph mutants 1..4 (SWIGLU, GATHER, GATE, post-norm + * weight) must each fail the A-vs-C comparison. Then --reps timed tokens (median). + * Verdict: exit 0 when every CHECK passed. + */ +#include "omega_gpu_q38_graph.h" +#include "omega_gpu_cmdlist.h" +#include "omega_gpu_session.h" +#include +#include +#include +#include +#include + +#define H OMEGA_GPU_Q38_HIDDEN +#define I OMEGA_GPU_Q38_INTER +#define EPS 1e-6f +#define MAX_POS 256u +#define TOKENS 3u +#define SCALE_2P64 18446744073709551616.0f +#define SCALE_2M64 5.42101086242752217e-20f +/* A vs C: |d| <= TOL_REL * max|ref|. The differences: RMSNorm f32 FFMA + MUFU.RSQ vs f64 host, SwiGLU / gate MUFU EX2 / + * RCP vs expf (~1e-7 relative each) through 2 layers and 5120- to 17408-term sums; 1e-4 is ~100x that. */ +#define TOL_REL 1e-4 + +static int g_checks, g_failed; +#define CHECK(c, ...) do { g_checks++; if (!(c)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } else { printf("ok: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } +static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); } +static uint16_t bf16_rne(float f) { uint32_t x; memcpy(&x, &f, 4); uint32_t r = x + 0x7fffu + ((x >> 16) & 1u); return (uint16_t)(r >> 16); } +static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; } +static uint64_t median_u64(uint64_t *v, unsigned n) { qsort(v, n, sizeof *v, cmp_u64); return v[n / 2]; } + +static void rmsnorm(const float *x, const float *w, float *out) { + double sq = 0; for (unsigned i = 0; i < H; i++) sq += (double)x[i] * x[i]; + float s = (float)(1.0 / sqrt(sq / H + (double)EPS)); + for (unsigned i = 0; i < H; i++) out[i] = x[i] * s * w[i]; +} +static uint32_t argmax(const float *v, uint32_t n) { uint32_t b = 0; for (uint32_t i = 1; i < n; i++) if (v[i] > v[b]) b = i; return b; } +static double maxabs(const float *v, size_t n) { double m = 0; for (size_t i = 0; i < n; i++) if (fabs(v[i]) > m) m = fabs(v[i]); return m; } +static double maxdiff(const float *a, const float *b, size_t n) { double m = 0; for (size_t i = 0; i < n; i++) { double d = fabs((double)a[i] - b[i]); if (d > m || d != d) m = d != d ? 1e30 : d; } return m; } + +static int fp8_random(uint32_t seed, uint32_t n, uint32_t k, OmegaGpuFp8Tensor **out) { + size_t wb = (size_t)n * k, sb = (size_t)(n / 128) * (k / 128); + uint8_t *w = malloc(wb); uint16_t *s = malloc(sb * 2); + if (!w || !s) { free(w); free(s); return -1; } + uint32_t st = seed; + for (size_t i = 0; i < wb; i += 4) { uint32_t r = lcg(&st); for (unsigned j = 0; j < 4 && i + j < wb; j++) w[i + j] = (uint8_t)((r >> (8 * j)) & 0xbfu); } /* |v| <= 1.875: never a NaN code */ + const float sc = 1.5f / sqrtf((float)k); /* outputs O(1) for O(1) inputs */ + for (size_t i = 0; i < sb; i++) s[i] = bf16_rne(frand(&st, 0.5f * sc, sc)); + int rc = omega_gpu_fp8_tensor_upload(1, n, k, w, s, out); + free(w); free(s); + return rc; +} +static int bf16_random(uint32_t seed, uint32_t n, uint32_t k, float lo, float hi, float mul, OmegaGpuBf16Tensor **out) { + uint16_t *w = malloc((size_t)n * k * 2); + if (!w) return -1; + uint32_t st = seed; + for (size_t i = 0; i < (size_t)n * k; i++) w[i] = bf16_rne(frand(&st, lo, hi) * mul); + int rc = omega_gpu_bf16_tensor_upload(n, k, w, out); + free(w); + return rc; +} + +/* ---- the synthetic model ---- */ +typedef struct { + uint32_t layers, vocab, n_gdn, n_attn; + uint64_t mask; + struct { OmegaGpuFp8Tensor *mix, *o, *gu, *dn; OmegaGpuBf16Tensor *ab; float in_norm[H], post_norm[H]; } L[8]; + OmegaGpuBf16Tensor *lm; + float fnorm[H]; + float x0[TOKENS][H]; + /* the two state copies: [0] the graph's, [1] the baseline's */ + OmegaGpuGdn *gdn[2]; + OmegaGpuDecodeAttn256Cache *attn[2]; +} Model; + +static int state_alloc(Model *m, int which, uint32_t seed) { + uint32_t s = seed; + if (omega_gpu_gdn_alloc(m->n_gdn ? m->n_gdn : 1, &m->gdn[which]) != 0) { printf("gdn alloc: %s\n", omega_gpu_gdn_last_error()); return -1; } + float *conv = malloc((size_t)OMEGA_GPU_Q38_GDN_QKV * 4 * 4), a_log[48], dtb[48], nw[128]; + if (!conv) return -1; + for (uint32_t l = 0; l < (m->n_gdn ? m->n_gdn : 1); l++) { + for (size_t i = 0; i < (size_t)OMEGA_GPU_Q38_GDN_QKV * 4; i++) conv[i] = frand(&s, -0.6f, 0.6f); + for (unsigned h = 0; h < 48; h++) { a_log[h] = frand(&s, -1.5f, 1.5f); dtb[h] = frand(&s, -3.0f, 1.0f); } + for (unsigned i = 0; i < 128; i++) nw[i] = frand(&s, 0.8f, 1.2f); + if (omega_gpu_gdn_set_layer(m->gdn[which], l, conv, a_log, dtb, nw) != 0) { printf("gdn set: %s\n", omega_gpu_gdn_last_error()); free(conv); return -1; } + } + free(conv); + if (omega_gpu_decode_attn256_cache_alloc(m->n_attn ? m->n_attn : 1, MAX_POS, 10000000.0, OMEGA_GPU_Q38_ROTARY, &m->attn[which]) != 0) { printf("attn alloc: %s\n", omega_gpu_decode_attn_last_error()); return -1; } + for (uint32_t l = 0; l < (m->n_attn ? m->n_attn : 1); l++) { + float qn[256], kn[256], qp[256], kp[256]; + for (unsigned i = 0; i < 256; i++) { qn[i] = frand(&s, 0.8f, 1.2f); kn[i] = frand(&s, 0.8f, 1.2f); } + omega_dattn256_perm_vec(qn, qp, OMEGA_GPU_Q38_ROTARY); omega_dattn256_perm_vec(kn, kp, OMEGA_GPU_Q38_ROTARY); + if (omega_gpu_decode_attn256_set_norms(m->attn[which], l, qp, kp) != 0) return -1; + } + return 0; +} + +static int model_build(Model *m, uint32_t layers, uint64_t mask, uint32_t vocab) { + memset(m, 0, sizeof *m); m->layers = layers; m->mask = mask; m->vocab = vocab; + uint32_t s = 4321; + for (uint32_t l = 0; l < layers; l++) { + const int attn = (int)((mask >> l) & 1u); + if (attn) m->n_attn++; else m->n_gdn++; + if (fp8_random(100 + l, attn ? OMEGA_GPU_Q38_ATT_MIX : OMEGA_GPU_Q38_GDN_MIX, H, &m->L[l].mix) || fp8_random(200 + l, H, OMEGA_GPU_Q38_MIXER_OUT, &m->L[l].o) || + fp8_random(300 + l, 2 * I, H, &m->L[l].gu) || fp8_random(400 + l, H, I, &m->L[l].dn)) { printf("fp8 upload: %s\n", omega_gpu_fp8_last_error()); return -1; } + if (!attn && bf16_random(500 + l, OMEGA_GPU_Q38_AB, H, -0.03f, 0.03f, SCALE_2M64, &m->L[l].ab)) { printf("bf16 upload: %s\n", omega_gpu_bf16_last_error()); return -1; } + for (unsigned i = 0; i < H; i++) { m->L[l].in_norm[i] = frand(&s, 0.8f, 1.2f); m->L[l].post_norm[i] = frand(&s, 0.8f, 1.2f); } + } + if (bf16_random(600, vocab, H, -0.05f, 0.05f, 1.0f, &m->lm)) { printf("lm_head upload: %s\n", omega_gpu_bf16_last_error()); return -1; } + for (unsigned i = 0; i < H; i++) m->fnorm[i] = frand(&s, 0.8f, 1.2f); + for (unsigned t = 0; t < TOKENS; t++) for (unsigned i = 0; i < H; i++) m->x0[t][i] = frand(&s, -1.0f, 1.0f); + if (state_alloc(m, 0, 777) || state_alloc(m, 1, 777)) return -1; + return 0; +} + +static int state_reset(Model *m) { + /* GDN state to zero; the KV rows at the test's positions are rewritten by every token */ + return omega_gpu_gdn_reset(m->gdn[0]) || omega_gpu_gdn_reset(m->gdn[1]); +} + +/* ---- A: the APIs one call at a time ---- */ +typedef struct { float x[H], logits[248320]; uint32_t token; } Out; + +static int baseline_token(Model *m, const float *x0, uint32_t pos, Out *o) { + static float xn[H], xs[H], mix[OMEGA_GPU_Q38_GDN_MIX], ab[OMEGA_GPU_Q38_AB], agg[OMEGA_GPU_Q38_ATT_MIX], att[OMEGA_GPU_Q38_MIXER_OUT], y[H], gu[2 * I], act[I]; + static uint32_t tab[OMEGA_GPU_Q38_ATT_MIX]; + omega_q38_gather_table(tab); + memcpy(o->x, x0, sizeof o->x); + uint32_t ng = 0, na = 0; + for (uint32_t l = 0; l < m->layers; l++) { + rmsnorm(o->x, m->L[l].in_norm, xn); + { int r_ = omega_gpu_fp8_gemv_f32(m->L[l].mix, 0, xn, mix, NULL); if (r_ != 0) { printf("fp8 gemv rc %d: %s\n", r_, omega_gpu_fp8_last_error()); return -1; } } + if (!((m->mask >> l) & 1u)) { + for (unsigned i = 0; i < H; i++) xs[i] = xn[i] * SCALE_2P64; /* the a|b rows are uploaded * 2^-64 */ + { int r_ = omega_gpu_bf16_gemv_f32(m->L[l].ab, xs, ab, NULL); if (r_ != 0) { printf("bf16 gemv rc %d: %s\n", r_, omega_gpu_bf16_last_error()); return -1; } } + if (omega_gpu_gdn_step(m->gdn[1], ng++, mix, mix + OMEGA_GPU_Q38_GDN_QKV, ab, EPS, EPS, 1.0f, 1.0f, att, NULL) != 0) { printf("gdn step: %s\n", omega_gpu_gdn_last_error()); return -1; } + } else { + for (unsigned n = 0; n < OMEGA_GPU_Q38_ATT_MIX; n++) agg[n] = mix[tab[n]]; + if (omega_gpu_decode_attn256(m->attn[1], na++, pos, agg, EPS, 1.0f, att, NULL) != 0) { printf("attn: %s\n", omega_gpu_decode_attn_last_error()); return -1; } + for (unsigned n = 0; n < OMEGA_GPU_Q38_MIXER_OUT; n++) att[n] = att[n] * (1.0f / (1.0f + expf(-agg[OMEGA_GPU_Q38_ATT_QKV + n]))); + } + { int r_ = omega_gpu_fp8_gemv_f32(m->L[l].o, 0, att, y, NULL); if (r_ != 0) { printf("fp8 gemv rc %d: %s\n", r_, omega_gpu_fp8_last_error()); return -1; } } + for (unsigned i = 0; i < H; i++) o->x[i] = o->x[i] + y[i]; + rmsnorm(o->x, m->L[l].post_norm, xn); + { int r_ = omega_gpu_fp8_gemv_f32(m->L[l].gu, 0, xn, gu, NULL); if (r_ != 0) { printf("fp8 gemv rc %d: %s\n", r_, omega_gpu_fp8_last_error()); return -1; } } + for (unsigned j = 0; j < I; j++) { float v = gu[j]; act[j] = v / (1.0f + expf(-v)) * gu[I + j]; } + { int r_ = omega_gpu_fp8_gemv_f32(m->L[l].dn, 0, act, y, NULL); if (r_ != 0) { printf("fp8 gemv rc %d: %s\n", r_, omega_gpu_fp8_last_error()); return -1; } } + for (unsigned i = 0; i < H; i++) o->x[i] = o->x[i] + y[i]; + } + rmsnorm(o->x, m->fnorm, xn); + { int r_ = omega_gpu_bf16_gemv_f32(m->lm, xn, o->logits, NULL); if (r_ != 0) { printf("bf16 gemv rc %d: %s\n", r_, omega_gpu_bf16_last_error()); return -1; } } + o->token = argmax(o->logits, m->vocab); + return 0; +} + +static int graph_build(Model *m, OmegaGpuQ38Graph **out) { + OmegaGpuQ38GraphConfig c; memset(&c, 0, sizeof c); + c.layers = m->layers; c.attn_mask = m->mask; c.vocab = m->vocab; c.eps = EPS; c.eps_l2 = EPS; + c.gdn = m->gdn[0]; c.attn = m->attn[0]; c.lm_head = m->lm; + int rc = omega_gpu_q38_graph_create(&c, out); + if (rc != 0) { printf("graph create %d: %s\n", rc, omega_gpu_session_last_error()); return -1; } + for (uint32_t l = 0; l < m->layers; l++) + if ((rc = omega_gpu_q38_graph_set_layer(*out, l, m->L[l].mix, m->L[l].ab, m->L[l].o, m->L[l].gu, m->L[l].dn, m->L[l].in_norm, m->L[l].post_norm)) != 0) { printf("set_layer %u: %d\n", l, rc); return -1; } + if (omega_gpu_q38_graph_set_final_norm(*out, m->fnorm) != 0) return -1; + return 0; +} + +static int graph_token(OmegaGpuQ38Graph *g, Model *m, const float *x0, uint32_t pos, Out *o, OmegaGpuDecodeGraphStats *st) { + int rc = omega_gpu_q38_graph_token(g, x0, pos, &o->token, st); + if (rc != 0) { printf("graph token rc %d: %s\n", rc, omega_gpu_session_last_error()); return -1; } + (void)m; + return omega_gpu_q38_graph_read(g, o->x, NULL, o->logits) != 0 ? -1 : 0; +} + +static int host_only(void) { + static uint32_t tab[OMEGA_GPU_Q38_ATT_MIX]; + omega_q38_gather_table(tab); + int bad = 0; + for (uint32_t h = 0; h < 24; h++) for (uint32_t i = 0; i < 256; i++) if (tab[h * 256 + i] != h * 512 + omega_dattn256_perm_slot(i, 64)) bad++; + for (uint32_t h = 0; h < 4; h++) for (uint32_t i = 0; i < 256; i++) if (tab[6144 + h * 256 + i] != 12288 + h * 256 + omega_dattn256_perm_slot(i, 64)) bad++; + for (uint32_t i = 0; i < 1024; i++) if (tab[7168 + i] != 13312 + i) bad++; + for (uint32_t h = 0; h < 24; h++) for (uint32_t d = 0; d < 256; d++) if (tab[8192 + h * 256 + d] != h * 512 + 256 + d) bad++; + CHECK(bad == 0, "gather table: q, k permuted, v and gate in place (%d wrong)", bad); + static uint8_t seen[OMEGA_GPU_Q38_ATT_MIX]; + int dup = 0; for (uint32_t n = 0; n < OMEGA_GPU_Q38_ATT_MIX; n++) { if (tab[n] >= OMEGA_GPU_Q38_ATT_MIX || seen[tab[n]]) dup++; else seen[tab[n]] = 1; } + CHECK(dup == 0, "gather table is a permutation of the 14336 mix values (%d repeats or out of range)", dup); + OmegaGpuQ38Graph *g = NULL; + OmegaGpuQ38GraphConfig c; memset(&c, 0, sizeof c); + CHECK(omega_gpu_q38_graph_create(&c, &g) == OMEGA_GPU_DGRAPH_BAD_ARGS && !g, "create refuses an empty config"); + CHECK(omega_gpu_q38_graph_create(NULL, &g) == OMEGA_GPU_DGRAPH_BAD_ARGS, "create refuses NULL"); + return g_failed ? 1 : 0; +} + +static int chip_run(uint32_t layers, uint64_t mask, uint32_t reps) { + Model m; + const uint32_t vocab = 248320u; + if (model_build(&m, layers, mask, vocab) != 0) { CHECK(0, "model build"); return 1; } + printf("model: %u layers (%u GDN, %u attention, mask 0x%" PRIx64 "), vocab %u\n", layers, m.n_gdn, m.n_attn, mask, vocab); + static Out A[TOKENS], C[TOKENS], B, M; + /* A first: it also warms every kernel the record paths need */ + for (uint32_t t = 0; t < TOKENS; t++) if (baseline_token(&m, m.x0[t], t, &A[t]) != 0) { CHECK(0, "baseline token %u", t); return 1; } + OmegaGpuQ38Graph *g = NULL; + if (graph_build(&m, &g) != 0) { CHECK(0, "graph build"); return 1; } + OmegaGpuDecodeGraphStats st; + for (uint32_t t = 0; t < TOKENS; t++) { + if (graph_token(g, &m, m.x0[t], t, &C[t], &st) != 0) { CHECK(0, "graph token %u", t); return 1; } + const double ref = maxabs(A[t].logits, vocab), dl = maxdiff(A[t].logits, C[t].logits, vocab); + const double xref = maxabs(A[t].x, H), dx = maxdiff(A[t].x, C[t].x, H); + CHECK(C[t].token == A[t].token, "token %u: graph %u == baseline %u", t, C[t].token, A[t].token); + CHECK(dl <= TOL_REL * ref, "token %u: logits max diff %.3g <= %.0e * %.3g", t, dl, TOL_REL, ref); + CHECK(dx <= TOL_REL * xref, "token %u: x max diff %.3g <= %.0e * %.3g", t, dx, TOL_REL, xref); + CHECK(st.launches == m.n_gdn * 9u + m.n_attn * 10u + 4u && st.submits == 1u, "token %u: %u launches in %u submit", t, st.launches, st.submits); + } + /* B: flush_each over the same three tokens from a reset state, bit-identical to C */ + if (state_reset(&m) != 0) { CHECK(0, "state reset"); return 1; } + omega_gpu_q38_graph_set_flush_each(g, 1); + int bsame = 1; + for (uint32_t t = 0; t < TOKENS; t++) { + if (graph_token(g, &m, m.x0[t], t, &B, &st) != 0) { CHECK(0, "flush_each token %u", t); return 1; } + if (memcmp(B.x, C[t].x, sizeof B.x) || memcmp(B.logits, C[t].logits, vocab * 4u) || B.token != C[t].token) bsame = 0; + } + omega_gpu_q38_graph_set_flush_each(g, 0); + CHECK(bsame, "flush_each == one list, bit for bit, all %u tokens", TOKENS); + /* negative controls: each graph mutant must fail the A comparison on some token */ + const char *mname[5] = { "", "swiglu up half -1", "gather index +1", "gate dropped", "post-norm weight dropped" }; + for (int mu = 1; mu <= 4; mu++) { + if (state_reset(&m) != 0) return 1; + omega_gpu_q38_graph_test_set_mutant(g, mu); + int caught = 0; + for (uint32_t t = 0; t < TOKENS; t++) { + if (graph_token(g, &m, m.x0[t], t, &M, &st) != 0) { caught = 1; break; } + if (maxdiff(A[t].x, M.x, H) > TOL_REL * maxabs(A[t].x, H)) caught = 1; + } + CHECK(caught, "mutant %d (%s) caught", mu, mname[mu]); + } + omega_gpu_q38_graph_test_set_mutant(g, 0); + /* timing: reps tokens at position 2 over the reset state (the time, not the values, is measured here) */ + uint64_t *chip = calloc(reps, 8), *tot = calloc(reps, 8); + if (!chip || !tot) return 1; + for (uint32_t r = 0; r < reps; r++) { + if (state_reset(&m) != 0 || graph_token(g, &m, m.x0[0], 2, &M, &st) != 0) { CHECK(0, "timed token %u", r); return 1; } + chip[r] = st.chip_ns; tot[r] = st.total_ns; + } + printf("timing: %u reps, median chip %.1f us, median total %.1f us per token (%u layers)\n", reps, median_u64(chip, reps) / 1e3, median_u64(tot, reps) / 1e3, layers); + free(chip); free(tot); + /* timeline: one stamped token, GPU time per launch kind */ + { + static uint64_t stp[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; static uint8_t kd[OMEGA_GPU_CMDLIST_MAX_LAUNCHES]; + omega_gpu_q38_graph_set_stamps(g, 1); + if (state_reset(&m) != 0 || graph_token(g, &m, m.x0[0], 2, &M, &st) != 0) { CHECK(0, "stamped token"); return 1; } + int n = omega_gpu_q38_graph_stamps(g, stp, kd, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1); + CHECK(n == (int)st.launches, "timeline: %d stamped launches", n); + uint64_t ns[OMEGA_GPU_Q38_K_COUNT] = { 0 }; uint32_t cnt[OMEGA_GPU_Q38_K_COUNT] = { 0 }; + for (int i = 0; i < n; i++) { ns[kd[i]] += stp[i + 1] - stp[i]; cnt[kd[i]]++; } + for (unsigned k = 0; k < OMEGA_GPU_Q38_K_COUNT; k++) + if (cnt[k]) printf("timeline %-8s %3u launches %9.1f us\n", omega_gpu_q38_graph_kind_name((uint8_t)k), cnt[k], ns[k] / 1e3); + if (n > 0) printf("timeline span %.1f us\n", (stp[n] - stp[0]) / 1e3); + omega_gpu_q38_graph_set_stamps(g, 0); + } + omega_gpu_q38_graph_free(g); + return g_failed ? 1 : 0; +} + +int main(int argc, char **argv) { + int host = 0; uint32_t layers = 2, reps = 10; uint64_t mask = 0x2; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--layers") == 0 && i + 1 < argc) layers = (uint32_t)atoi(argv[++i]); + else if (strcmp(argv[i], "--mask") == 0 && i + 1 < argc) mask = strtoull(argv[++i], NULL, 0); + else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); + else { printf("usage: %s [--host-only] [--layers N (<= 8)] [--mask M] [--reps R]\n", argv[0]); return 2; } + } + if (layers == 0 || layers > 8 || reps == 0) { printf("layers 1..8, reps >= 1\n"); return 2; } + int rc = host ? host_only() : chip_run(layers, mask, reps); + printf("gpu_q38_graph_test%s: %d checks, %d failed: %s\n", host ? " (host-only)" : "", g_checks, g_failed, rc ? "FAIL" : "PASS"); + return rc; +} diff --git a/tools/.crumb b/tools/.crumb index baad8ca16..9f9b27c7c 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -62,14 +62,14 @@ } ], "children_root": "sha256:e1f826c2beb8604bb0dcba49575f7c6b48cf5488c40b616b212fda2ed70c62ee", - "compiled_at": "2026-10-18T15:21:47Z", + "compiled_at": "2026-10-18T15:51:07Z", "compiler": "crumb-compile/1", - "digest": "sha256:e4a329c378a5873cf01d3636c85097718dcfb3e4ff4da8a1bf45801d2db96d21", + "digest": "sha256:532e8ff9eec685c6b8bec0eefbdb88d01b054e13fcd2cc83796bc9b8a6bddd60", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 286, - "generated_at_commit": "acdeff745c292cebb3b22cc7ae298409a0b7ee64", - "source_tree": "sha256:04a1158249096adc2c94103936edd76aa5ed3c5b7d1227158f7ee1ac98144b7f" + "files": 287, + "generated_at_commit": "3c90e430b5282f00f426c2421200d1e8485f5ebd", + "source_tree": "sha256:a43a39979d0ea4c03d48a87c74f61e58c2b8841fd3c753075e403fc7741939ab" }, "owner_lane": "", "provenance": { diff --git a/tools/run_gpu_q38_graph_chip.sh b/tools/run_gpu_q38_graph_chip.sh new file mode 100755 index 000000000..d359bbfa7 --- /dev/null +++ b/tools/run_gpu_q38_graph_chip.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# run_gpu_q38_graph_chip.sh EVIDENCE_DIR [PHYSICS_DIR] +# sc #642 chip gate for omega_gpu_q38_graph (one Qwen3.8-27B decode token as one command list). +# Caller holds the GB10 lock: flock /tmp/aien-gb10.lock tools/run_gpu_q38_graph_chip.sh EVID +# 0. host proofs first (never hardware results): glue-kernel codegen + sim (gpu_cmdlist_test), the graph's --host-only +# 1. main: A (the APIs one call at a time, host glue) vs C (one list) vs B (one launch per submission), a 2-layer +# synthetic model (GDN then attention) on random weights, vocab 248320, tokens at positions 0..2; token identical, +# logits and x within tolerance, B == C bit for bit, the four graph mutants caught; REPS timed tokens. +# 2. LAYERS=4 MASK=0x8 (three GDN layers then attention, the checkpoint's period) as a second shape. +# Verdict line last: "GPU_Q38_GRAPH_CHIP: PASS|FAIL". +set -u +EVID="${1:?evidence dir}"; PHYS="${2:-$HOME/workspace/physics-own30b-k}" +HERE="$(cd "$(dirname "$0")/.." && pwd)" +mkdir -p "$EVID" +cd "$HERE" || exit 2 +{ + echo "start $(date -u +%FT%TZ) omega=$(git rev-parse HEAD) branch=$(git rev-parse --abbrev-ref HEAD) dirty=$(git status --porcelain | wc -l) physics=$PHYS physics_commit=$(git -C "$PHYS" rev-parse HEAD)" + uname -a; nvidia-smi --query-gpu=name,driver_version --format=csv,noheader 2>/dev/null; free -g | head -2 +} > "$EVID/device.txt" +git diff HEAD > "$EVID/worktree.diff" +make PHYSICS_DIR="$PHYS" -j10 build/gpu_q38_graph_test build/gpu_cmdlist_test build/bw_encode_control_test > "$EVID/build.log" 2>&1 || { echo "GPU_Q38_GRAPH_CHIP: FAIL build"; exit 1; } +T=./build/gpu_q38_graph_test +sha256sum "$T" build/gpu_cmdlist_test build/libomega_gpu.a > "$EVID/binaries.sha256" +fail=0 +./build/bw_encode_control_test 2>&1 | tee "$EVID/encode-control.log"; [ "${PIPESTATUS[0]}" = 0 ] || fail=1 +./build/gpu_cmdlist_test --host-only > "$EVID/glue-host.log" 2>&1 || fail=1; tail -1 "$EVID/glue-host.log" +./build/gpu_cmdlist_test --sim > "$EVID/glue-sim.log" 2>&1 || fail=1; tail -1 "$EVID/glue-sim.log" +$T --host-only 2>&1 | tee "$EVID/host-only.log"; [ "${PIPESTATUS[0]}" = 0 ] || fail=1 +[ $fail = 0 ] || { echo "GPU_Q38_GRAPH_CHIP: FAIL host proofs"; exit 1; } +$T --reps "${REPS:-10}" 2>&1 | tee "$EVID/main.log"; [ "${PIPESTATUS[0]}" = 0 ] || fail=1 +$T --layers 4 --mask 0x8 --reps "${REPS:-10}" 2>&1 | tee "$EVID/layers4.log"; [ "${PIPESTATUS[0]}" = 0 ] || fail=1 +echo "end $(date -u +%FT%TZ) fail=$fail" >> "$EVID/device.txt" +(cd "$EVID" && sha256sum *.log *.txt *.diff *.sha256 > SHA256SUMS) +if [ $fail = 0 ]; then echo "GPU_Q38_GRAPH_CHIP: PASS"; else echo "GPU_Q38_GRAPH_CHIP: FAIL"; exit 1; fi ```