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 / codegen_attn_diff.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_blackwell_codegen_attn.h" |
| 3 | #include "omega_blackwell_codegen_pattn.h" |
| 4 | #include "omega_gpu_om_ir.h" |
| 5 | #include "cudapp_native.h" |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <limits.h> |
| 9 | int omega_codegen_attn_oracle(const OmegaDecodeAttnSpec *, BlackwellIRProgram *); |
| 10 | int omega_codegen_pattn_oracle(const OmegaPrefillAttnSpec *, BlackwellIRProgram *); |
| 11 | static BlackwellIRProgram c, o; |
| 12 | static unsigned char cb[BW_MAX_IR_INSNS * 16], ob[BW_MAX_IR_INSNS * 16]; |
| 13 | static int same(int verbose) { |
| 14 | if (c.count != o.count) return 0; |
| 15 | for (size_t i = 0; i < c.count; ++i) if (memcmp(&c.insns[i], &o.insns[i], sizeof c.insns[i])) { |
| 16 | const unsigned char *a = (const unsigned char *)&c.insns[i], *b = (const unsigned char *)&o.insns[i]; |
| 17 | for (size_t j = 0; j < sizeof c.insns[i]; ++j) if (a[j] != b[j]) { |
| 18 | if (verbose) printf("IR mismatch record=%zu byte=%zu c=%u om=%u\n", i, j, a[j], b[j]); |
| 19 | break; |
| 20 | } |
| 21 | return 0; |
| 22 | } |
| 23 | if (memcmp(&c.regalloc, &o.regalloc, sizeof c.regalloc)) { if (verbose) puts("allocation mismatch"); return 0; } |
| 24 | OmegaBlackwellKernel ck, ok; |
| 25 | if (omega_blackwell_kernel_encode_into(&c, cb, sizeof cb, &ck) || |
| 26 | omega_blackwell_kernel_encode_into(&o, ob, sizeof ob, &ok)) return 0; |
| 27 | return ck.code_size == ok.code_size && memcmp(cb, ob, ck.code_size) == 0; |
| 28 | } |
| 29 | int main(void) { |
| 30 | unsigned cases = 0, accepted = 0, changed = 0, mutants = 0; |
| 31 | uint64_t error; |
| 32 | for (unsigned d = 0; d <= OMEGA_DATTN_MAX_DEPTH + 1; ++d) for (int m = -1; m <= 5; ++m) { |
| 33 | OmegaDecodeAttnSpec s = {d, m}; |
| 34 | int cr = omega_codegen_attn_oracle(&s, &c), rc = omega_blackwell_codegen_decode_attn_prog(&s, &o); |
| 35 | ++cases; |
| 36 | if ((cr == 0) != (rc == 0) || (!cr && !same(1))) { printf("FAIL decode depth=%u mutant=%d c=%d om=%d records=%zu/%zu\n", d, m, cr, rc, c.count, o.count); return 1; } |
| 37 | if (!cr) { |
| 38 | ++accepted; o.insns[0].imm ^= 1; if (same(0)) return 2; ++changed; |
| 39 | if (omega_gpu_om_ir_replay((OmegaOmIrUnit)omega_cg_attn, 1, d, (uint64_t)m, 0, &o, &error) || same(0)) return 4; |
| 40 | ++mutants; |
| 41 | } |
| 42 | } |
| 43 | /* All accepted builder inputs: W in {4,8}, mutant 0..4, modes 0..2, |
| 44 | mirror zero except APPEND, where both mirror values are accepted. */ |
| 45 | const unsigned warps[] = {0, 4, 8, 16}; |
| 46 | for (unsigned w = 0; w < sizeof warps / sizeof warps[0]; ++w) |
| 47 | for (int m = -1; m <= 5; ++m) for (unsigned mode = 0; mode <= 3; ++mode) for (unsigned mirror = 0; mirror <= 2; ++mirror) { |
| 48 | OmegaPrefillAttnSpec s = {warps[w], m, mode, mirror}; |
| 49 | int cr = omega_codegen_pattn_oracle(&s, &c), rc = omega_blackwell_codegen_prefill_attn_prog(&s, &o); |
| 50 | ++cases; |
| 51 | if ((cr == 0) != (rc == 0) || (!cr && !same(1))) { printf("FAIL prefill W=%u mutant=%d mode=%u mirror=%u c=%d om=%d records=%zu/%zu\n", s.warps, m, mode, mirror, cr, rc, c.count, o.count); return 1; } |
| 52 | if (!cr) { |
| 53 | ++accepted; o.insns[0].imm ^= 1; if (same(0)) return 2; ++changed; |
| 54 | if (omega_gpu_om_ir_replay((OmegaOmIrUnit)omega_cg_pattn, 1, s.warps, (uint64_t)m, |
| 55 | mode | ((uint64_t)mirror << 8), &o, &error) || same(0)) return 4; |
| 56 | ++mutants; |
| 57 | } |
| 58 | } |
| 59 | if (omega_blackwell_codegen_decode_attn_prog(NULL, &o) != -1 || omega_blackwell_codegen_prefill_attn_prog(NULL, &o) != -1) return 3; |
| 60 | const OmegaDecodeAttnSpec bad_decode[] = {{UINT32_MAX, 0}, {1, INT_MIN}, {1, INT_MAX}}; |
| 61 | const OmegaPrefillAttnSpec bad_prefill[] = { |
| 62 | {UINT32_MAX, 0, 0, 0}, {4, INT_MIN, 0, 0}, {4, INT_MAX, 0, 0}, |
| 63 | {4, 0, UINT32_MAX, 0}, {4, 0, 1, UINT32_MAX} |
| 64 | }; |
| 65 | for (unsigned i = 0; i < sizeof bad_decode / sizeof bad_decode[0]; ++i) |
| 66 | if (omega_codegen_attn_oracle(&bad_decode[i], &c) != -1 || |
| 67 | omega_blackwell_codegen_decode_attn_prog(&bad_decode[i], &o) != -1) return 5; |
| 68 | for (unsigned i = 0; i < sizeof bad_prefill / sizeof bad_prefill[0]; ++i) |
| 69 | if (omega_codegen_pattn_oracle(&bad_prefill[i], &c) != -1 || |
| 70 | omega_blackwell_codegen_prefill_attn_prog(&bad_prefill[i], &o) != -1) return 5; |
| 71 | const OmegaDecodeAttnSpec ds = {1, 0}; |
| 72 | const OmegaPrefillAttnSpec ps = {4, 0, 0, 0}; |
| 73 | if (omega_blackwell_codegen_decode_attn_prog(&ds, NULL) != -1 || |
| 74 | omega_blackwell_codegen_prefill_attn_prog(&ps, NULL) != -1) return 5; |
| 75 | /* Freeze the complete accepted set measured from the retained oracle. */ |
| 76 | if (accepted != 60 || changed != accepted || mutants != accepted) return 6; |
| 77 | printf("PASS attention differential cases=%u accepted=%u record-negative-controls=%u Omega-mutants-killed=%u IR+allocation+encoded-bytes\n", cases, accepted, changed, mutants); |
| 78 | return 0; |
| 79 | } |