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 / regalloc_om / bind_diff.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* om/codegen/cg_bind.om against the CUDAPP_MIGRATION_PENDING_C_BIND oracle: spec bytes, realization binding, |
| 3 | * machine descriptor, matmul dispatch, vector geometry, program padding/fit/insns and the digest admission, over |
| 4 | * every partition boundary (0, 1, the limits, limit+1, uint32 and size_t wrap). */ |
| 5 | #include <stdint.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include "omega_blackwell_codegen.h" |
| 10 | #include "omega_blackwell_encoder.h" |
| 11 | #include "omega_blackwell_matmul.h" |
| 12 | #include "omega_blackwell_realize.h" |
| 13 | #include "sha256.h" |
| 14 | |
| 15 | int legacy_omega_matmul_spec_init(OmegaMatMulSpec *, uint32_t, uint32_t, uint32_t, OmegaMatMulPrecision); |
| 16 | int legacy_omega_blackwell_bind_matmul_realization(const OmegaMatMulSpec *, const OmegaBlackwellKernel *, |
| 17 | OmegaBlackwellRealizationIdentity *); |
| 18 | int legacy_omega_blackwell_codegen_matmul(const OmegaMatMulSpec *, OmegaBlackwellKernel *); |
| 19 | int legacy_omega_blackwell_get_machine_id(SemanticId *); |
| 20 | int legacy_omega_blackwell_realization_compute_id(OmegaBlackwellRealization *); |
| 21 | int legacy_omega_blackwell_realize_vector(const OmegaVectorSpec *, OmegaBlackwellRealization *); |
| 22 | size_t legacy_omega_bw_program_code_bytes(const BlackwellIRProgram *); |
| 23 | int legacy_omega_blackwell_kernel_encode_into(const BlackwellIRProgram *, uint8_t *, size_t, OmegaBlackwellKernel *); |
| 24 | |
| 25 | static unsigned long cases, refused; |
| 26 | static uint64_t rng = 0x9e3779b97f4a7c15ull; |
| 27 | static uint64_t next(void) { |
| 28 | rng ^= rng << 13; |
| 29 | rng ^= rng >> 7; |
| 30 | rng ^= rng << 17; |
| 31 | return rng; |
| 32 | } |
| 33 | static void fail(const char *what, unsigned long a, unsigned long b) { |
| 34 | printf("FAIL bind %s %lu %lu\n", what, a, b); |
| 35 | exit(1); |
| 36 | } |
| 37 | |
| 38 | int main(void) { |
| 39 | static const uint32_t dims[] = {0, 1, 2, 63, 64, 8191, 8192, 8193, 16383, 16384, 16385, |
| 40 | 262143, 262144, 262145, 0x7fffffffu, 0x80000000u, 0xffffffffu}; |
| 41 | static const uint32_t precs[] = {0, 1, 2, 3, 255, 256, 257, 0x7fffffffu, 0xffffffffu}; |
| 42 | const size_t nd = sizeof dims / sizeof dims[0], np = sizeof precs / sizeof precs[0]; |
| 43 | for (size_t a = 0; a < nd; a++) |
| 44 | for (size_t b = 0; b < nd; b++) |
| 45 | for (size_t c = 0; c < nd; c++) |
| 46 | for (size_t p = 0; p < np; p++) { |
| 47 | OmegaMatMulSpec x, y; |
| 48 | memset(&x, 0xa5, sizeof x); |
| 49 | memset(&y, 0xa5, sizeof y); |
| 50 | int rx = legacy_omega_matmul_spec_init(&x, dims[a], dims[b], dims[c], (OmegaMatMulPrecision)precs[p]); |
| 51 | int ry = omega_matmul_spec_init(&y, dims[a], dims[b], dims[c], (OmegaMatMulPrecision)precs[p]); |
| 52 | cases++; |
| 53 | if (rx != ry || memcmp(&x, &y, sizeof x)) fail("spec", a * 10000 + b * 100 + c, p); |
| 54 | if (rx) refused++; |
| 55 | if (rx) continue; |
| 56 | OmegaBlackwellKernel k; |
| 57 | memset(&k, 0, sizeof k); |
| 58 | for (int i = 0; i < 32; i++) k.code_digest[i] = (uint8_t)next(); |
| 59 | OmegaBlackwellRealizationIdentity ix, iy; |
| 60 | memset(&ix, 0x5a, sizeof ix); |
| 61 | memset(&iy, 0x5a, sizeof iy); |
| 62 | rx = legacy_omega_blackwell_bind_matmul_realization(&x, &k, &ix); |
| 63 | ry = omega_blackwell_bind_matmul_realization(&y, &k, &iy); |
| 64 | cases++; |
| 65 | if (rx != ry || memcmp(&ix, &iy, sizeof ix)) fail("bound", a * 10000 + b * 100 + c, p); |
| 66 | } |
| 67 | { |
| 68 | OmegaMatMulSpec s; |
| 69 | OmegaBlackwellKernel k; |
| 70 | OmegaBlackwellRealizationIdentity id; |
| 71 | memset(&s, 0, sizeof s); |
| 72 | memset(&k, 0, sizeof k); |
| 73 | if (omega_matmul_spec_init(NULL, 1, 1, 1, 0) != -1 || legacy_omega_matmul_spec_init(NULL, 1, 1, 1, 0) != -1) |
| 74 | fail("spec null", 0, 0); |
| 75 | if (omega_blackwell_bind_matmul_realization(NULL, &k, &id) != -1 || |
| 76 | omega_blackwell_bind_matmul_realization(&s, NULL, &id) != -1 || |
| 77 | omega_blackwell_bind_matmul_realization(&s, &k, NULL) != -1) |
| 78 | fail("bound null", 0, 0); |
| 79 | cases += 4; |
| 80 | refused += 4; |
| 81 | } |
| 82 | /* dispatch: unsupported precisions refuse in both; supported ones generate the same kernel */ |
| 83 | for (size_t p = 0; p < np; p++) { |
| 84 | OmegaMatMulSpec s; |
| 85 | memset(&s, 0, sizeof s); |
| 86 | s.m = 16; |
| 87 | s.k = 16; |
| 88 | s.n = 16; |
| 89 | s.precision = (OmegaMatMulPrecision)precs[p]; |
| 90 | OmegaBlackwellKernel kx, ky; |
| 91 | memset(&kx, 0, sizeof kx); |
| 92 | memset(&ky, 0, sizeof ky); |
| 93 | int rx = legacy_omega_blackwell_codegen_matmul(&s, &kx); |
| 94 | int ry = omega_blackwell_codegen_matmul(&s, &ky); |
| 95 | cases++; |
| 96 | if (rx != ry) fail("dispatch", p, (unsigned long)(rx - ry)); |
| 97 | if (rx) { |
| 98 | refused++; |
| 99 | continue; |
| 100 | } |
| 101 | if (kx.code_size != ky.code_size || memcmp(kx.code, ky.code, kx.code_size) || |
| 102 | memcmp(kx.code_digest, ky.code_digest, 32)) |
| 103 | fail("dispatch kernel", p, 0); |
| 104 | omega_blackwell_kernel_free(&kx); |
| 105 | omega_blackwell_kernel_free(&ky); |
| 106 | } |
| 107 | { |
| 108 | SemanticId mx, my; |
| 109 | if (legacy_omega_blackwell_get_machine_id(&mx) || omega_blackwell_get_machine_id(&my) || |
| 110 | memcmp(&mx, &my, sizeof mx) || omega_blackwell_get_machine_id(NULL) != -1) |
| 111 | fail("machine", 0, 0); |
| 112 | cases += 2; |
| 113 | } |
| 114 | static const uint32_t archs[] = {0, 1, 121, 255, 256, 65535, 65536, 0x80000000u, 0xffffffffu}; |
| 115 | for (size_t i = 0; i < sizeof archs / sizeof archs[0]; i++) |
| 116 | for (int r = 0; r < 64; r++) { |
| 117 | OmegaBlackwellRealization x, y; |
| 118 | memset(&x, 0, sizeof x); |
| 119 | for (size_t j = 0; j < sizeof x; j++) ((uint8_t *)&x)[j] = (uint8_t)next(); |
| 120 | x.sm_architecture = archs[i]; |
| 121 | y = x; |
| 122 | int rx = legacy_omega_blackwell_realization_compute_id(&x); |
| 123 | int ry = omega_blackwell_realization_compute_id(&y); |
| 124 | cases++; |
| 125 | if (rx != ry || memcmp(&x, &y, sizeof x)) fail("compute id", i, (unsigned long)r); |
| 126 | } |
| 127 | if (omega_blackwell_realization_compute_id(NULL) != -1) fail("compute id null", 0, 0); |
| 128 | static const uint32_t counts[] = {0, 1, 2, 63, 64, 65, 127, 128, 129, 0xffffffbfu, 0xffffffc0u, |
| 129 | 0xffffffc1u, 0xfffffffeu, 0xffffffffu}; |
| 130 | for (size_t i = 0; i < sizeof counts / sizeof counts[0]; i++) { |
| 131 | OmegaVectorSpec v; |
| 132 | memset(&v, 0, sizeof v); |
| 133 | for (int j = 0; j < 32; j++) v.spec_id.bytes[j] = (uint8_t)next(); |
| 134 | v.element_count = counts[i]; |
| 135 | OmegaBlackwellRealization x, y; |
| 136 | memset(&x, 0x33, sizeof x); |
| 137 | memset(&y, 0x33, sizeof y); |
| 138 | int rx = legacy_omega_blackwell_realize_vector(&v, &x); |
| 139 | int ry = omega_blackwell_realize_vector(&v, &y); |
| 140 | cases++; |
| 141 | if (rx != ry || memcmp(&x, &y, sizeof x)) fail("vector", i, x.qmd_cfg.grid_width); |
| 142 | } |
| 143 | { |
| 144 | uint8_t buf[3] = {1, 2, 3}, dx[32], dy[32]; |
| 145 | if (omega_blackwell_compute_code_digest(buf, 0, dx) != -1 || omega_blackwell_compute_code_digest(NULL, 1, dx) != -1 || |
| 146 | omega_blackwell_compute_code_digest(buf, 1, NULL) != -1) |
| 147 | fail("digest refuse", 0, 0); |
| 148 | for (size_t n = 1; n <= 3; n++) { |
| 149 | sha256_hash(buf, n, dx); |
| 150 | if (omega_blackwell_compute_code_digest(buf, n, dy) || memcmp(dx, dy, 32)) fail("digest", n, 0); |
| 151 | } |
| 152 | cases += 6; |
| 153 | refused += 3; |
| 154 | } |
| 155 | static BlackwellIRProgram prog; |
| 156 | static const size_t pc[] = {0, 1, 7, 8, 9, 24, 25, 31, 32, 33, 39, 40, 41, 2047, 2048, |
| 157 | SIZE_MAX / 16, SIZE_MAX - 8, SIZE_MAX - 7, SIZE_MAX - 6, SIZE_MAX}; |
| 158 | for (size_t i = 0; i < sizeof pc / sizeof pc[0]; i++) { |
| 159 | prog.count = pc[i]; |
| 160 | cases++; |
| 161 | if (legacy_omega_bw_program_code_bytes(&prog) != omega_bw_program_code_bytes(&prog)) fail("pad", i, 0); |
| 162 | } |
| 163 | if (omega_bw_program_code_bytes(NULL) != 0) fail("pad null", 0, 0); |
| 164 | { |
| 165 | omega_bw_ir_init(&prog); |
| 166 | static uint8_t cx[1024], cy[1024]; |
| 167 | static const size_t caps[] = {0, 1, 511, 512, 513, 1024}; |
| 168 | for (size_t i = 0; i < sizeof caps / sizeof caps[0]; i++) { |
| 169 | OmegaBlackwellKernel kx, ky; |
| 170 | memset(cx, 0, sizeof cx); |
| 171 | memset(cy, 0, sizeof cy); |
| 172 | int rx = legacy_omega_blackwell_kernel_encode_into(&prog, cx, caps[i], &kx); |
| 173 | int ry = omega_blackwell_kernel_encode_into(&prog, cy, caps[i], &ky); |
| 174 | cases++; |
| 175 | if (rx != ry) fail("fit", i, (unsigned long)(rx - ry)); |
| 176 | if (rx) { |
| 177 | refused++; |
| 178 | continue; |
| 179 | } |
| 180 | if (kx.code_size != ky.code_size || kx.insn_count != ky.insn_count || memcmp(cx, cy, sizeof cx)) |
| 181 | fail("fit kernel", i, 0); |
| 182 | } |
| 183 | } |
| 184 | printf("PASS bind differential cases=%lu refused=%lu\n", cases, refused); |
| 185 | return 0; |
| 186 | } |