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