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/blob/main/tests/test_omega_codegen_freestanding.c # 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 branch `main` Branches Tags History `omega-mv / tests / test_omega_codegen_freestanding.c` 241 lines ยท 13101 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* test_omega_codegen_freestanding.c -- native v1 P07 cut A.2 (aienos#286, aien-architecture#190). 18 * 19 * Built twice by mk/omega-gpu-engine.mk (make test-codegen-freestanding): 20 * - hosted: linked against the hosted codegen/matmul objects; every fixture goes through the existing 21 * allocating generators (malloc'd code buffer, omega_blackwell_kernel_free); 22 * - freestanding (-DOMEGA_GPU_NO_POSIX): linked against the -ffreestanding objects of 23 * `make gpu-engine-freestanding`; every fixture goes through the *_prog generator and 24 * omega_blackwell_kernel_encode_into with a caller-supplied buffer, no allocator anywhere. 25 * `--dump` prints every fixture's instruction stream as hex; the make target requires the two dumps to be 26 * byte-identical (cmp). Without --dump the freestanding build also checks the bounded buffer contract 27 * (every short buffer refused before a byte is written, nothing written past the reported size), the 28 * golden-word fixture verifiers against the freestanding object, and the integer bf16 rounding path 29 * against the hosted __bf16 cast on all 2^32 inputs. 30 * Host only: opens no device. */ 31 #include "omega_blackwell_codegen.h" 32 #include "omega_blackwell_matmul.h" 33 #include 34 #include 35 #include 36 37 typedef enum { FX_I32, FX_TENSOR, FX_LOOP, FX_FRAG, FX_BLOCK } FxKind; 38 typedef struct { 39 const char *name; 40 FxKind kind; 41 uint32_t m, k, n; 42 OmegaMatMulPrecision prec; 43 uint32_t grid_x, unroll, rt, ct; /* unroll 0 = the API's rule (omega_matmul_frag_unroll / _block_unroll) */ 44 int mutant; 45 } Fixture; 46 47 /* The shapes the existing tests and the matmul API generate: the integer and K=16 tensor kernels of the 48 * gates, the looped kernel, the decode fragment kernel at Qwen3 shapes and the blocked prefill kernel. */ 49 static const Fixture FX[] = { 50 {"i32_16x16x16", FX_I32, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32, 0, 0, 0, 0, 0}, 51 {"i32_32x16x64", FX_I32, 32, 16, 64, OMEGA_MATMUL_PRECISION_INT32, 0, 0, 0, 0, 0}, 52 {"i32_16x32x8", FX_I32, 16, 32, 8, OMEGA_MATMUL_PRECISION_INT32, 0, 0, 0, 0, 0}, 53 {"tensor_f16_16x16x8", FX_TENSOR, 16, 16, 8, OMEGA_MATMUL_PRECISION_FP16, 0, 0, 0, 0, 0}, 54 {"tensor_bf16_32x16x16", FX_TENSOR, 32, 16, 16, OMEGA_MATMUL_PRECISION_BF16, 0, 0, 0, 0, 0}, 55 {"loop_bf16_16x64x32_g2", FX_LOOP, 16, 64, 32, OMEGA_MATMUL_PRECISION_BF16, 2, 0, 0, 0, 0}, 56 {"loop_f16_32x128x64_g4", FX_LOOP, 32, 128, 64, OMEGA_MATMUL_PRECISION_FP16, 4, 0, 0, 0, 0}, 57 {"frag_k128_n64_g1", FX_FRAG, 16, 128, 64, OMEGA_MATMUL_PRECISION_BF16, 1, 0, 0, 0, 0}, 58 {"frag_k1024_n3072_g64", FX_FRAG, 16, 1024, 3072, OMEGA_MATMUL_PRECISION_BF16, 64, 0, 0, 0, 0}, 59 {"frag_k4096_n4096_g48", FX_FRAG, 16, 4096, 4096, OMEGA_MATMUL_PRECISION_BF16, 48, 0, 0, 0, 0}, 60 {"frag_k4096_n4096_g48_mutant", FX_FRAG, 16, 4096, 4096, OMEGA_MATMUL_PRECISION_BF16, 48, 0, 0, 0, 1}, 61 {"block_k256_n128_r4c2", FX_BLOCK, 16, 256, 128, OMEGA_MATMUL_PRECISION_BF16, 1, 0, 4, 2, 0}, 62 {"block_k1024_n3072_r2c2", FX_BLOCK, 16, 1024, 3072, OMEGA_MATMUL_PRECISION_BF16, 8, 0, 2, 2, 0}, 63 {"block_k4096_n4096_r4c4", FX_BLOCK, 16, 4096, 4096, OMEGA_MATMUL_PRECISION_BF16, 16, 0, 4, 4, 0}, 64 }; 65 #define NFX (sizeof FX / sizeof FX[0]) 66 67 static uint32_t fx_unroll(const Fixture *f) { 68 if (f->unroll) return f->unroll; 69 if (f->kind == FX_BLOCK) return omega_matmul_frag_block_unroll(f->k, f->rt, f->ct); 70 uint32_t u = omega_matmul_frag_unroll(f->k); 71 if (f->mutant) while (u > 1 && f->k / 16u / u < 2) u /= 2; /* the API's mutant rule */ 72 return u; 73 } 74 75 static int fails; 76 #define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL "); printf(__VA_ARGS__); printf("\n"); } } while (0) 77 78 #ifdef OMEGA_GPU_NO_POSIX 79 static BlackwellIRProgram g_prog; /* one program at a time; static keeps the stack small */ 80 static uint8_t g_code[OMEGA_BW_CODE_CAP_MAX + 64]; 81 82 static int fx_prog(const Fixture *f, BlackwellIRProgram *prog) { 83 OmegaMatMulSpec spec; 84 if (omega_matmul_spec_init(&spec, f->m, f->k, f->n, f->prec) != 0) return -1; 85 switch (f->kind) { 86 case FX_I32: return omega_blackwell_codegen_matmul_i32_prog(&spec, prog); 87 case FX_TENSOR: return omega_blackwell_codegen_matmul_tensor_prog(&spec, prog); 88 case FX_LOOP: return omega_blackwell_codegen_matmul_tensor_loop_prog(&spec, f->grid_x, f->mutant, prog); 89 case FX_FRAG: return omega_blackwell_codegen_matmul_frag_prog(&spec, f->grid_x, fx_unroll(f), f->mutant, prog); 90 case FX_BLOCK: 91 return omega_blackwell_codegen_matmul_frag_block_prog(&spec, f->grid_x, fx_unroll(f), f->rt, f->ct, f->mutant, prog); 92 } 93 return -1; 94 } 95 96 /* The cap each hosted generator always had: the i32 buffer is exactly the program, frag_block the IR maximum, 97 * the others 4096 bytes. The freestanding build passes the same cap so a program the hosted build refuses is 98 * refused here too. */ 99 static size_t fx_cap(const Fixture *f, const BlackwellIRProgram *prog) { 100 if (f->kind == FX_I32) return omega_bw_program_code_bytes(prog); 101 if (f->kind == FX_BLOCK) return OMEGA_BW_CODE_CAP_MAX; 102 return OMEGA_BW_CODE_CAP_SMALL; 103 } 104 105 static int fx_kernel(const Fixture *f, OmegaBlackwellKernel *k) { 106 if (fx_prog(f, &g_prog) != 0) { memset(k, 0, sizeof *k); return -1; } 107 return omega_blackwell_kernel_encode_into(&g_prog, g_code, fx_cap(f, &g_prog), k); 108 } 109 static void fx_release(OmegaBlackwellKernel *k) { memset(k, 0, sizeof *k); } /* caller-owned buffer: nothing to free */ 110 #else 111 static int fx_kernel(const Fixture *f, OmegaBlackwellKernel *k) { 112 OmegaMatMulSpec spec; 113 memset(k, 0, sizeof *k); 114 if (omega_matmul_spec_init(&spec, f->m, f->k, f->n, f->prec) != 0) return -1; 115 switch (f->kind) { 116 case FX_I32: return omega_blackwell_codegen_matmul_i32(&spec, k); 117 case FX_TENSOR: return omega_blackwell_codegen_matmul_tensor(&spec, k); 118 case FX_LOOP: return omega_blackwell_codegen_matmul_tensor_loop(&spec, f->grid_x, f->mutant, k); 119 case FX_FRAG: return omega_blackwell_codegen_matmul_frag(&spec, f->grid_x, fx_unroll(f), f->mutant, k); 120 case FX_BLOCK: return omega_blackwell_codegen_matmul_frag_block(&spec, f->grid_x, fx_unroll(f), f->rt, f->ct, f->mutant, k); 121 } 122 return -1; 123 } 124 static void fx_release(OmegaBlackwellKernel *k) { omega_blackwell_kernel_free(k); } 125 #endif 126 127 static int dump(void) { 128 for (size_t i = 0; i < NFX; i++) { 129 OmegaBlackwellKernel k; 130 int rc = fx_kernel(&FX[i], &k); 131 printf("fixture %s rc %d bytes %zu insns %zu gpr %u ugpr %u\n", FX[i].name, rc, k.code_size, k.insn_count, 132 k.gpr_count, k.uniform_gpr_count); 133 if (rc != 0 || !k.code || k.code_size == 0) { fails++; printf("FAIL fixture %s did not generate\n", FX[i].name); continue; } 134 printf("digest "); 135 for (int j = 0; j < 32; j++) printf("%02x", k.code_digest[j]); 136 printf("\n"); 137 for (size_t j = 0; j < k.code_size; j++) printf("%02x%s", k.code[j], (j % 32u == 31u) ? "\n" : ""); 138 if (k.code_size % 32u) printf("\n"); 139 fx_release(&k); 140 } 141 return fails ? 1 : 0; 142 } 143 144 #ifdef OMEGA_GPU_NO_POSIX 145 static int all_bytes(const uint8_t *p, size_t n, uint8_t v) { 146 for (size_t i = 0; i < n; i++) if (p[i] != v) return 0; 147 return 1; 148 } 149 150 /* The bounded-buffer contract, on every fixture's program. */ 151 static void buffer_contract(void) { 152 for (size_t i = 0; i < NFX; i++) { 153 const Fixture *f = &FX[i]; 154 OmegaBlackwellKernel k; 155 if (fx_prog(f, &g_prog) != 0) { CHECK(0, "%s: program", f->name); continue; } 156 const size_t need = omega_bw_program_code_bytes(&g_prog); 157 CHECK(need >= 32u * 16u && need % 128u == 0, "%s: code bytes %zu (padded to 8 instructions, at least 32)", f->name, need); 158 CHECK(need <= OMEGA_BW_CODE_CAP_MAX, "%s: code bytes %zu above the IR maximum", f->name, need); 159 160 /* one byte short: refused, not a byte written, kernel zeroed */ 161 memset(g_code, 0xa5, sizeof g_code); 162 memset(&k, 0x5a, sizeof k); 163 int rc = omega_blackwell_kernel_encode_into(&g_prog, g_code, need - 1u, &k); 164 CHECK(rc == OMEGA_BW_CODE_BUFFER_TOO_SMALL, "%s: need-1 rc %d", f->name, rc); 165 CHECK(all_bytes(g_code, sizeof g_code, 0xa5), "%s: refused encode wrote into the buffer", f->name); 166 CHECK(k.code == NULL && k.code_size == 0 && k.insn_count == 0, "%s: refused encode left a kernel", f->name); 167 168 /* zero capacity and NULL buffer refused */ 169 CHECK(omega_blackwell_kernel_encode_into(&g_prog, g_code, 0, &k) == OMEGA_BW_CODE_BUFFER_TOO_SMALL, "%s: cap 0", f->name); 170 CHECK(omega_blackwell_kernel_encode_into(&g_prog, NULL, need, &k) == -1 && k.code == NULL, "%s: NULL buffer", f->name); 171 CHECK(omega_blackwell_kernel_encode_into(NULL, g_code, need, &k) == -1, "%s: NULL program", f->name); 172 CHECK(omega_blackwell_kernel_encode_into(&g_prog, g_code, need, NULL) == -1, "%s: NULL kernel", f->name); 173 174 /* exactly enough, and a larger buffer: same bytes, nothing written past the reported size */ 175 memset(g_code, 0xa5, sizeof g_code); 176 rc = omega_blackwell_kernel_encode_into(&g_prog, g_code, need + 64u, &k); 177 CHECK(rc == 0 && k.code == g_code && k.code_size == need && k.insn_count == need / 16u, 178 "%s: roomy encode rc %d size %zu", f->name, rc, k.code_size); 179 CHECK(all_bytes(g_code + need, 64u, 0xa5), "%s: encode wrote past code_size", f->name); 180 static uint8_t first[OMEGA_BW_CODE_CAP_MAX]; 181 memcpy(first, g_code, need); 182 uint8_t d1[32]; 183 memcpy(d1, k.code_digest, 32); 184 memset(g_code, 0, sizeof g_code); 185 rc = omega_blackwell_kernel_encode_into(&g_prog, g_code, need, &k); 186 CHECK(rc == 0 && memcmp(first, g_code, need) == 0 && memcmp(d1, k.code_digest, 32) == 0, 187 "%s: exact-size encode differs (rc %d)", f->name, rc); 188 } 189 } 190 #endif 191 192 /* Golden words (nvcc/nvdisasm oracles, independent of either encoder). The hosted build runs them with --golden 193 * through the .om encoder (om/encoder/bw_enc_sm121.om); the freestanding build through the C encoder. */ 194 static void golden_fixtures(void) { 195 CHECK(omega_blackwell_verify_codegen_fixtures() == 0, "golden words (rc %d)", omega_blackwell_verify_codegen_fixtures()); 196 CHECK(omega_blackwell_verify_codegen_fixtures_fb1cut4() == 0, "fb1cut4 golden words"); 197 CHECK(omega_blackwell_verify_codegen_fixtures_intops() == 0, "intops golden words"); 198 CHECK(omega_blackwell_verify_codegen_fixtures_fragmm() == 0, "fragmm golden words"); 199 CHECK(omega_blackwell_verify_codegen_fixtures_reconv() == 0, "reconv golden words"); 200 CHECK(omega_blackwell_verify_codegen_fixtures_bfly_f2fp() == 0, "SHFL.BFLY / F2FP.BF16 golden words (rc %d)", omega_blackwell_verify_codegen_fixtures_bfly_f2fp()); 201 CHECK(omega_blackwell_verify_codegen_fixtures_atomf32() == 0, "ATOMG.E.ADD.F32 golden words (rc %d)", omega_blackwell_verify_codegen_fixtures_atomf32()); 202 CHECK(omega_blackwell_test_regalloc_bounds() == 0, "regalloc bounds"); 203 } 204 205 #ifdef OMEGA_GPU_NO_POSIX 206 /* The freestanding omega_fp32_to_bf16 (integer rounding) against the hosted __bf16 cast, every input. */ 207 static void bf16_sweep(void) { 208 uint64_t bad = 0; 209 uint32_t first_bad = 0; 210 for (uint64_t x = 0; x <= 0xffffffffull; x++) { 211 uint32_t u = (uint32_t)x; 212 float f; 213 memcpy(&f, &u, 4); 214 __bf16 b = (__bf16)f; 215 uint16_t want; 216 memcpy(&want, &b, 2); 217 if (omega_fp32_to_bf16(f) != want) { if (!bad) first_bad = u; bad++; } 218 } 219 CHECK(bad == 0, "bf16: %llu of 2^32 inputs differ from the __bf16 cast (first 0x%08x)", (unsigned long long)bad, first_bad); 220 if (!bad) printf("bf16 sweep: all 2^32 inputs equal the hosted __bf16 cast\n"); 221 } 222 #endif 223 224 int main(int argc, char **argv) { 225 if (argc > 1 && strcmp(argv[1], "--dump") == 0) return dump(); 226 #ifdef OMEGA_GPU_NO_POSIX 227 buffer_contract(); 228 golden_fixtures(); 229 if (!(argc > 1 && strcmp(argv[1], "--no-sweep") == 0)) bf16_sweep(); 230 printf("%s: freestanding codegen contract (%zu fixtures, %d failing checks)\n", fails ? "FAIL" : "PASS", (size_t)NFX, fails); 231 return fails ? 1 : 0; 232 #else 233 if (argc > 1 && strcmp(argv[1], "--golden") == 0) { 234 golden_fixtures(); 235 printf("%s: hosted golden-word fixtures through the .om encoder (%d failing checks)\n", fails ? "FAIL" : "PASS", fails); 236 return fails ? 1 : 0; 237 } 238 fprintf(stderr, "hosted build: only --dump or --golden\n"); 239 return 2; 240 #endif 241 }