interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / codegen_gdn_diff_test.c

149 lines · 8255 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* codegen_gdn_diff_test.c: the seeded Omega GDN builders (om/codegen/cg_gdn*.om, replayed by omega_gpu_om_ir_replay)
3 * against the retained C oracle (CUDAPP_MIGRATION_PENDING_C_CODEGEN_GDN) over the whole finite spec domain, refusals
4 * included: IR instructions, register allocation and encoded bytes must be identical, and so must the shape policy and
5 * the stats accessors. Ends with a corrupted-record negative control. */
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#define CUDAPP_MIGRATION_PENDING_C_CODEGEN_GDN 1
10#define omega_gdn_shape oracle_shape
11#define omega_blackwell_codegen_gdn_prog oracle_decode
12#define omega_blackwell_codegen_gdn_multi_prog oracle_multi
13#define omega_blackwell_codegen_gdn_copy_prog oracle_copy
14#define omega_blackwell_codegen_gdn_prep_prog oracle_prep
15#define omega_blackwell_codegen_gdn_scan_prog oracle_scan
16#define omega_blackwell_codegen_gdn_post_prog oracle_post
17#define omega_gdn_prog_stats oracle_stats
18#define omega_gdn_pf_prog_stats oracle_pf_stats
19#define omega_gdn_multi_prog_stats oracle_multi_stats
20#include "../src/omega_blackwell_codegen_gdn.c"
21#undef omega_gdn_shape
22#undef omega_blackwell_codegen_gdn_prog
23#undef omega_blackwell_codegen_gdn_multi_prog
24#undef omega_blackwell_codegen_gdn_copy_prog
25#undef omega_blackwell_codegen_gdn_prep_prog
26#undef omega_blackwell_codegen_gdn_scan_prog
27#undef omega_blackwell_codegen_gdn_post_prog
28#undef omega_gdn_prog_stats
29#undef omega_gdn_pf_prog_stats
30#undef omega_gdn_multi_prog_stats
31#include "omega_gpu_om_ir.h"
32#include "cudapp_native.h"
33extern int omega_gdn_shape(const OmegaGdnSpec *, OmegaGdnShape *);
34extern int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *, BlackwellIRProgram *);
35extern int omega_blackwell_codegen_gdn_multi_prog(const OmegaGdnSpec *, BlackwellIRProgram *);
36extern int omega_blackwell_codegen_gdn_copy_prog(const OmegaGdnSpec *, BlackwellIRProgram *);
37extern int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *, BlackwellIRProgram *);
38extern int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *, BlackwellIRProgram *);
39extern int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *, BlackwellIRProgram *);
40extern int omega_gdn_prog_stats(const OmegaGdnSpec *, uint32_t *, uint32_t *);
41extern int omega_gdn_pf_prog_stats(unsigned, const OmegaGdnSpec *, uint32_t *, uint32_t *);
42extern int omega_gdn_multi_prog_stats(const OmegaGdnSpec *, uint32_t *, uint32_t *);
43
44typedef int (*Build)(const OmegaGdnSpec *, BlackwellIRProgram *);
45static const char *const names[6] = {"decode", "multi", "copy", "prep", "scan", "post"};
46static const Build oracle[6] = {oracle_decode, oracle_multi, oracle_copy, oracle_prep, oracle_scan, oracle_post};
47static const Build om[6] = {omega_blackwell_codegen_gdn_prog, omega_blackwell_codegen_gdn_multi_prog,
48 omega_blackwell_codegen_gdn_copy_prog, omega_blackwell_codegen_gdn_prep_prog,
49 omega_blackwell_codegen_gdn_scan_prog, omega_blackwell_codegen_gdn_post_prog};
50static BlackwellIRProgram a, b;
51static unsigned cases, refused, per[6];
52
53static int equal(unsigned k, const OmegaGdnSpec *s) {
54 int ra = oracle[k](s, &a), rb = om[k](s, &b);
55 if ((ra == 0) != (rb == 0)) goto fail;
56 /* A refused program has no specified IR output; compare the refusal, not stale scratch state. */
57 if (ra) { refused++; return 1; }
58 if (a.count != b.count || memcmp(a.insns, b.insns, a.count * sizeof a.insns[0]) ||
59 memcmp(&a.regalloc, &b.regalloc, sizeof a.regalloc))
60 goto fail;
61 static uint8_t x[BW_MAX_IR_INSNS * 16], y[BW_MAX_IR_INSNS * 16];
62 size_t nx = 0, ny = 0;
63 if (omega_bw_encode_program(&a, x, sizeof x, &nx) || omega_bw_encode_program(&b, y, sizeof y, &ny) || nx != ny ||
64 memcmp(x, y, nx))
65 goto fail;
66 cases++; per[k]++;
67 return 1;
68fail:
69 fprintf(stderr, "GDN DIFF %s mutant=%d copy=%d ratio=%u bf16=%u C=%d Omega=%d count=%zu/%zu\n", names[k], s->mutant,
70 s->copy, s->ratio, s->state_bf16, ra, rb, a.count, b.count);
71 for (size_t i = 0; i < a.count && i < b.count; i++)
72 if (memcmp(&a.insns[i], &b.insns[i], sizeof a.insns[i])) {
73 fprintf(stderr, "first IR difference %zu op=%d/%d ctrl=%x/%x dst=%d/%d src=%d,%d,%d/%d,%d,%d imm=%x/%x u=%d/%d\n", i,
74 a.insns[i].op, b.insns[i].op, a.insns[i].control, b.insns[i].control, a.insns[i].dst_vreg,
75 b.insns[i].dst_vreg, a.insns[i].src1_vreg, a.insns[i].src2_vreg, a.insns[i].src3_vreg,
76 b.insns[i].src1_vreg, b.insns[i].src2_vreg, b.insns[i].src3_vreg, a.insns[i].imm, b.insns[i].imm,
77 a.insns[i].ureg, b.insns[i].ureg);
78 break;
79 }
80 return 0;
81}
82
83static int same_shape(const OmegaGdnSpec *s) {
84 OmegaGdnShape x, y;
85 memset(&x, 0xa5, sizeof x); memset(&y, 0xa5, sizeof y);
86 int rx = oracle_shape(s, &x), ry = omega_gdn_shape(s, &y);
87 if (rx != ry || memcmp(&x, &y, sizeof x)) {
88 fprintf(stderr, "GDN SHAPE DIFF ratio=%u bf16=%u C=%d Omega=%d\n", s->ratio, s->state_bf16, rx, ry);
89 return 0;
90 }
91 return 1;
92}
93
94static int same_stats(const OmegaGdnSpec *s) {
95 uint32_t i1 = 1, g1 = 2, i2 = 1, g2 = 2;
96 int ok = oracle_stats(s, &i1, &g1) == omega_gdn_prog_stats(s, &i2, &g2) && i1 == i2 && g1 == g2;
97 i1 = 1; g1 = 2; i2 = 1; g2 = 2;
98 ok = ok && oracle_multi_stats(s, &i1, &g1) == omega_gdn_multi_prog_stats(s, &i2, &g2) && i1 == i2 && g1 == g2;
99 for (unsigned kind = 0; ok && kind < 4; kind++) {
100 i1 = 1; g1 = 2; i2 = 1; g2 = 2;
101 ok = oracle_pf_stats(kind, s, &i1, &g1) == omega_gdn_pf_prog_stats(kind, s, &i2, &g2) && i1 == i2 && g1 == g2;
102 }
103 if (!ok)
104 fprintf(stderr, "GDN DIFF stats mutant=%d copy=%d ratio=%u bf16=%u insns=%u/%u gprs=%u/%u\n", s->mutant, s->copy,
105 s->ratio, s->state_bf16, i1, i2, g1, g2);
106 return ok;
107}
108
109static uint32_t corrupt(uint64_t *r, uint64_t k, uint64_t x, uint64_t y, uint64_t z, void *b0, uint64_t nb, void *m,
110 uint64_t nm) {
111 uint32_t rc = omega_cg_gdn(r, k, x, y, z, b0, nb, m, nm);
112 if (!rc && !*r) ((uint64_t *)b0)[6] ^= 1;
113 return rc;
114}
115
116int main(void) {
117 /* The whole finite domain the builders branch on: every mutant selector and one past each end, both copy modes and
118 * the values around them, every supported ratio plus 1, 4 and UINT32_MAX, and state_bf16 0, nonzero and UINT32_MAX. */
119 const uint32_t ratios[] = {0, 1, 2, 3, 4, UINT32_MAX};
120 const uint32_t bf16s[] = {0, 1, 2, UINT32_MAX};
121 for (int mutant = -1; mutant <= 5; mutant++)
122 for (int copy = -1; copy <= 3; copy++)
123 for (unsigned r = 0; r < sizeof ratios / sizeof ratios[0]; r++)
124 for (unsigned f = 0; f < sizeof bf16s / sizeof bf16s[0]; f++) {
125 OmegaGdnSpec s = {mutant, copy, ratios[r], bf16s[f]};
126 if (!same_shape(&s)) return 1;
127 for (unsigned k = 0; k < 6; k++)
128 if (!equal(k, &s)) return 1;
129 if (!same_stats(&s)) return 1;
130 }
131 OmegaGdnSpec s = {0, 0, 0, 0};
132 for (unsigned k = 0; k < 6; k++)
133 if (om[k](NULL, &b) != -1 || om[k](&s, NULL) != -1) { fprintf(stderr, "GDN NULL spec/prog accepted\n"); return 1; }
134 if (omega_gdn_prog_stats(NULL, NULL, NULL) != -1 || omega_gdn_multi_prog_stats(NULL, NULL, NULL) != -1 ||
135 omega_gdn_pf_prog_stats(0, NULL, NULL, NULL) != -1) {
136 fprintf(stderr, "GDN NULL stats accepted\n");
137 return 1;
138 }
139 /* Negative control: one flipped bit in the first emitted record must surface as an IR difference. */
140 if (oracle_decode(&s, &a) || omega_gpu_om_ir_replay(corrupt, 0, 0, 0, 0, &b, NULL)) return 1;
141 if (a.count == b.count && !memcmp(a.insns, b.insns, a.count * sizeof a.insns[0])) {
142 fprintf(stderr, "record negative control escaped\n");
143 return 1;
144 }
145 puts("CODEGEN GDN RECORD NEGATIVE CONTROL CAUGHT");
146 printf("CODEGEN GDN DIFF PASS (%u programs: decode %u multi %u copy %u prep %u scan %u post %u; %u refusals)\n", cases,
147 per[0], per[1], per[2], per[3], per[4], per[5], refused);
148 return 0;
149}