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 / numeric_om_diff_test.c

212 lines · 9779 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * C-vs-Omega differential for om/numeric/numeric.om and om/numeric/tile.om.
5 * Built with -DCUDAPP_MIGRATION_PENDING_C_NUMERIC_DIFF, so every public
6 * function below runs the C oracle (numeric_c_*) and the generated Omega unit
7 * and aborts on the first bit difference (src/omega_numeric.c). This driver
8 * only supplies inputs:
9 * - exhaustive: all 2^16 F16 and BF16 patterns (decode), every F16/BF16
10 * boundary of FP32 (each representable value, its neighbours and the
11 * rounding midpoints) for the narrowing conversions;
12 * - edges: every ordered pair/triple of a fixed edge list (signed zeros,
13 * subnormals, infinities, quiet and signalling NaNs with payloads,
14 * integer limits, ties, exp/log limits) for every operation;
15 * - random: NUMERIC_DIFF_RANDOM uniform bit patterns per operation
16 * (2^20: a defect hit by 1e-5 of inputs escapes with probability
17 * (1-10^-5)^(2^20), about 2.8e-5, Wolfram).
18 * Prints NUMERIC OM DIFF PASS <count> on success.
19 */
20#include "omega_numeric.h"
21#include "omega_numeric_reduce.h"
22#include "omega_numeric_om.h"
23
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#if !defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_DIFF)
30#error "build with -DCUDAPP_MIGRATION_PENDING_C_NUMERIC_DIFF"
31#endif
32
33#ifndef NUMERIC_DIFF_RANDOM
34#define NUMERIC_DIFF_RANDOM 1048576u
35#endif
36
37static const uint32_t EDGES[] = {
38 0x00000000u, 0x80000000u, 0x00000001u, 0x80000001u, 0x007fffffu, 0x807fffffu, 0x00800000u, 0x80800000u,
39 0x00400000u, 0x3f800000u, 0xbf800000u, 0x3f000000u, 0xbf000000u, 0x3fc00000u, 0x40200000u, 0xc0200000u,
40 0x3f800001u, 0x3f7fffffu, 0x4b000000u, 0x4b7fffffu, 0x4b800000u, 0x4effffffu, 0x4f000000u, 0xcf000000u,
41 0xcf000001u, 0x4f800000u, 0x4f7fffffu, 0x42b17218u, 0x42b17217u, 0x42b20000u, 0xc2d00000u, 0xc2cfffffu,
42 0xc2d00001u, 0xc2fe0000u, 0x7f7fffffu, 0xff7fffffu, 0x33800000u, 0x33000000u, 0x34000000u, 0x387fe000u,
43 0x477fe000u, 0x477ff000u, 0x7f800000u, 0xff800000u, 0x7fc00000u, 0xffc00000u, 0x7f800001u, 0xff800001u,
44 0x7fc00001u, 0x7fbfffffu, 0x7fffffffu, 0xffffffffu, 0x40490fdbu, 0x3eaaaaabu,
45};
46#define NEDGE (sizeof(EDGES) / sizeof(EDGES[0]))
47
48static uint64_t rng = 0x9e3779b97f4a7c15ull;
49static uint32_t next32(void) {
50 rng ^= rng << 13;
51 rng ^= rng >> 7;
52 rng ^= rng << 17;
53 return (uint32_t)(rng >> 16);
54}
55
56static unsigned long long checks;
57
58/* Documented GPU NaN: every NaN a float-producing reference op returns is the
59 * canonical 0x7fffffff, on any host (src/omega_numeric_om.h). */
60static float cn(const char *what, float r) {
61 uint32_t b = omega_float_to_bits(r);
62 if ((b & 0x7fffffffu) > 0x7f800000u && b != OMEGA_NUMERIC_CANON_NAN_BITS) {
63 fprintf(stderr, "FAIL numeric canon %s: NaN 0x%08x, documented 0x%08x\n", what, b, OMEGA_NUMERIC_CANON_NAN_BITS);
64 exit(1);
65 }
66 return r;
67}
68#define CN(call) (void)cn(#call, call)
69
70static void unary(uint32_t u) {
71 float a = omega_bits_to_float(u);
72 (void)omega_ref_i2f((int32_t)u);
73 (void)omega_ref_u2f(u);
74 (void)omega_ref_f2i(a);
75 (void)omega_ref_f2i_floor(a);
76 (void)omega_ref_f2i_ceil(a);
77 (void)omega_ref_f2i_rni(a);
78 (void)omega_ref_f2u(a);
79 (void)omega_ref_f32_to_f16(a);
80 (void)omega_ref_f32_to_bf16(a);
81 CN(omega_math_sqrt(a));
82 CN(omega_math_exp(a));
83 CN(omega_math_log(a));
84 CN(omega_ieee_sqrt(a));
85 (void)omega_numeric_host_rcp_seed(a);
86 for (int m = 0; m <= OMEGA_CMP_BF16_BITS; m++) (void)omega_numeric_compare_equal((OmegaNumericCompare)m, u, u ^ 1u);
87 checks += 20;
88}
89
90static void binary(uint32_t ua, uint32_t ub) {
91 float a = omega_bits_to_float(ua), b = omega_bits_to_float(ub);
92 (void)omega_numeric_bits_equal(a, b);
93 CN(omega_ref_fadd(a, b));
94 CN(omega_ref_fsub(a, b));
95 CN(omega_ref_fmul(a, b));
96 CN(omega_ref_fmin(a, b));
97 CN(omega_ref_fmax(a, b));
98 (void)omega_ref_fsetp_ge(a, b);
99 for (int op = 0; op < OMEGA_NOP_COUNT; op++) (void)omega_ref_fsetp_pred(op, a, b);
100 CN(omega_math_div(a, b));
101 CN(omega_ieee_div(a, b));
102 for (int m = 0; m <= OMEGA_CMP_BF16_BITS; m++) (void)omega_numeric_compare_equal((OmegaNumericCompare)m, ua, ub);
103 checks += 10 + OMEGA_NOP_COUNT + 5;
104}
105
106static void ternary(uint32_t ua, uint32_t ub, uint32_t uc) {
107 float a = omega_bits_to_float(ua), b = omega_bits_to_float(ub), c = omega_bits_to_float(uc);
108 CN(omega_ref_ffma(a, b, c));
109 CN(omega_ref_ffma_int(a, b, c));
110 checks += 2;
111}
112
113static void tile(const uint32_t t[32]) {
114 float x[32], out;
115 memcpy(x, t, sizeof(x));
116 for (int op = 0; op < OMEGA_RED_COUNT; op++) {
117 if (omega_reduce_reference((OmegaReduceOp)op, x, 32, &out) != OMEGA_NUMERIC_OK) {
118 fprintf(stderr, "FAIL numeric diff: reduce_reference op %d rc\n", op);
119 exit(1);
120 }
121 }
122 CN(omega_warp_reduce_sum(x));
123 checks += 5;
124}
125
126int main(void) {
127 if (!omega_numeric_fpenv_ok()) {
128 fprintf(stderr, "FAIL numeric diff: FP environment is not the required one\n");
129 return 1;
130 }
131 /* host-vs-documented NaN: inf*0 (x86 SSE gives 0xffc00000), payload and
132 * signalling inputs (AArch64 keeps the payload) all give 0x7fffffff */
133 {
134 const float inf = omega_bits_to_float(0x7f800000u), z = 0.0f, one = 1.0f;
135 const float pay = omega_bits_to_float(0xffc12345u), snan = omega_bits_to_float(0x7f800001u);
136 const struct { const char *what; float r; } want[] = {
137 { "fmul(inf,0)", omega_ref_fmul(inf, z) }, { "fsub(inf,inf)", omega_ref_fsub(inf, inf) },
138 { "fadd(payload,1)", omega_ref_fadd(pay, one) }, { "fadd(snan,1)", omega_ref_fadd(snan, one) },
139 { "ffma(inf,0,1)", omega_ref_ffma(inf, z, one) }, { "ffma(1,1,payload)", omega_ref_ffma(one, one, pay) },
140 { "ffma_int(1,1,payload)", omega_ref_ffma_int(one, one, pay) }, { "ieee_div(0,0)", omega_ieee_div(z, z) },
141 { "ieee_sqrt(-1)", omega_ieee_sqrt(-one) }, { "ieee_div(payload,1)", omega_ieee_div(pay, one) },
142 { "math_div(0,0)", omega_math_div(z, z) }, { "math_sqrt(-1)", omega_math_sqrt(-one) },
143 { "math_exp(payload)", omega_math_exp(pay) }, { "math_log(-1)", omega_math_log(-one) },
144 { "fmin(payload,snan)", omega_ref_fmin(pay, snan) }, { "fmax(snan,payload)", omega_ref_fmax(snan, pay) },
145 };
146 for (size_t i = 0; i < sizeof(want) / sizeof(want[0]); i++) {
147 uint32_t b = omega_float_to_bits(want[i].r);
148 if (b != OMEGA_NUMERIC_CANON_NAN_BITS) {
149 fprintf(stderr, "FAIL numeric canon %s: 0x%08x, documented 0x%08x\n", want[i].what, b, OMEGA_NUMERIC_CANON_NAN_BITS);
150 return 1;
151 }
152 checks++;
153 }
154 printf("numeric documented NaN: %zu cases 0x%08x\n", sizeof(want) / sizeof(want[0]), OMEGA_NUMERIC_CANON_NAN_BITS);
155 }
156 /* exhaustive 16-bit decode and the F16/BF16 boundaries of FP32 */
157 for (uint32_t h = 0; h < 65536u; h++) {
158 float f = omega_ref_f16_to_f32((uint16_t)h);
159 (void)omega_ref_bf16_to_f32((uint16_t)h);
160 uint32_t fb = omega_float_to_bits(f), bb = h << 16;
161 for (int32_t d = -1; d <= 1; d++) {
162 (void)omega_ref_f32_to_f16(omega_bits_to_float(fb + (uint32_t)d));
163 (void)omega_ref_f32_to_bf16(omega_bits_to_float(bb + (uint32_t)d));
164 }
165 /* midpoint to the next F16 / BF16 value and one ULP either side */
166 uint32_t fn = omega_float_to_bits(omega_ref_f16_to_f32((uint16_t)(h + 1u)));
167 if ((fb >> 31) == (fn >> 31) && fn > fb) {
168 uint32_t mid = fb + (fn - fb) / 2u;
169 for (int32_t d = -1; d <= 1; d++) (void)omega_ref_f32_to_f16(omega_bits_to_float(mid + (uint32_t)d));
170 }
171 for (int32_t d = -1; d <= 1; d++) (void)omega_ref_f32_to_bf16(omega_bits_to_float(bb + 0x8000u + (uint32_t)d));
172 checks += 14;
173 }
174 for (size_t i = 0; i < NEDGE; i++) {
175 unary(EDGES[i]);
176 for (size_t j = 0; j < NEDGE; j++) {
177 binary(EDGES[i], EDGES[j]);
178 for (size_t k = 0; k < NEDGE; k++) ternary(EDGES[i], EDGES[j], EDGES[k]);
179 }
180 }
181 for (uint32_t n = 0; n < NUMERIC_DIFF_RANDOM; n++) {
182 uint32_t a = next32(), b = next32(), c = next32();
183 unary(a);
184 binary(a, b);
185 /* same-exponent-ish pairs exercise cancellation and ties */
186 binary(a, (a & 0xff800000u) | (b & 0x007fffffu));
187 ternary(a, b, c);
188 ternary(a, b, (a & 0xff800000u) + (b & 0x7f000000u) - 0x3f800000u + (c & 0x807fffffu));
189 /* the finite exp/log/div/sqrt range: exponent field 0x60..0x9f */
190 uint32_t m = (a & 0x807fffffu) | ((0x60u + (b & 0x3fu)) << 23);
191 (void)omega_math_exp(omega_bits_to_float(m));
192 (void)omega_math_log(omega_bits_to_float(m & 0x7fffffffu));
193 (void)omega_math_sqrt(omega_bits_to_float(m & 0x7fffffffu));
194 (void)omega_math_div(omega_bits_to_float(m), omega_bits_to_float((c & 0x807fffffu) | ((0x60u + (c >> 26)) << 23)));
195 checks += 4;
196 (void)omega_reduce_levels((size_t)a * b);
197 uint32_t tc = 0, gw = 0;
198 omega_numeric_launch_shape((size_t)a, &tc, &gw);
199 checks += 2;
200 }
201 for (size_t n = 0; n < 70; n++) (void)omega_reduce_levels(n < 64 ? ((size_t)1 << n) - 1u : (size_t)-1 - n);
202 for (uint32_t n = 0; n < 4096u; n++) {
203 uint32_t t[32];
204 for (int i = 0; i < 32; i++) t[i] = (next32() & 7u) ? next32() : EDGES[next32() % NEDGE];
205 tile(t);
206 }
207 uint32_t tc = 0, gw = 0;
208 omega_numeric_launch_shape(0, &tc, &gw);
209 omega_numeric_launch_shape((size_t)-1, &tc, &gw);
210 printf("NUMERIC OM DIFF PASS %llu\n", checks);
211 return 0;
212}