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 / src / omega_eng_gates.h

304 lines · 11643 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Decision gates of the Blackwell submit and engine shells, the M16 requalify and
3 * concurrent programs and the NVRM lifecycle and submission-visibility gates.
4 * Each function asks om/engine/eng.om (ops LIFE, REQF, VIS, BWE, TOMS, SUBM, CONC)
5 * and keeps its C oracle for the file's CUDAPP_MIGRATION_PENDING_C_<PART> switch
6 * (the includer defines ENG_C_PART) and for the freestanding build.
7 * Every function takes plain integers so tests/om_eng_diff_test.c can drive both
8 * paths with the same inputs. A binding failure never passes a gate: a predicate
9 * returns 0 and a shape or rounding returns -1. Include omega_eng_dec.h first. */
10#ifndef OMEGA_ENG_GATES_H
11#define OMEGA_ENG_GATES_H
12
13#include <stdint.h>
14
15#include "omega_eng_dec.h"
16
17/* Lifecycle predicate kinds (nvrm/lifecycle_gates.c). Arguments a..f in order. */
18enum {
19 OMEGA_LIFE_EQ = 0, /* a == b */
20 OMEGA_LIFE_ZEROED = 1, /* a, b, c, d all 0 */
21 OMEGA_LIFE_NONZERO = 2, /* a != 0 */
22 OMEGA_LIFE_UNMAPPED = 3, /* msync rc a != 0 and errno b == ENOMEM 12 */
23 OMEGA_LIFE_QUARANTINE = 4, /* a, b, c != 0, d == 1, e == f */
24 OMEGA_LIFE_ZERO = 5, /* a == 0 */
25 OMEGA_LIFE_RECOVERED = 6, /* a == 0, b > c, d <= e */
26 OMEGA_LIFE_OVERFLOW = 7, /* a, b != 0, c == d, e == f */
27 OMEGA_LIFE_CHURN = 8, /* a == 0, b > 256 GiB, c < 2 GiB */
28 OMEGA_LIFE_RETRIED = 9, /* a == 0, b != 0, c == 3, d == 0 */
29 OMEGA_LIFE_BOUNDED = 10, /* a != 0, b == 0, c == d, e == 3 */
30 OMEGA_LIFE_LE = 11, /* a <= b */
31 OMEGA_LIFE_CORRUPT = 13, /* a, b != 0, c == 1, d != 0 */
32 OMEGA_LIFE_DISTINCT = 14, /* a != 0, a != b */
33 OMEGA_LIFE_TORNDOWN = 15, /* a == 0, b == 0, c == d */
34 OMEGA_LIFE_BOTHZERO = 16 /* a == 0, b == 0 */
35};
36
37static inline int omega_gate_life(uint32_t k, uint64_t a, uint64_t b, uint64_t c, uint64_t d, uint64_t e,
38 uint64_t f)
39{
40 if (!eng_on()) {
41 switch (k) {
42 case OMEGA_LIFE_EQ: return a == b;
43 case OMEGA_LIFE_ZEROED: return (a | b | c | d) == 0u;
44 case OMEGA_LIFE_NONZERO: return a != 0u;
45 case OMEGA_LIFE_UNMAPPED: return a != 0u && b == 12u;
46 case OMEGA_LIFE_QUARANTINE: return a != 0u && b != 0u && c != 0u && d == 1u && e == f;
47 case OMEGA_LIFE_ZERO: return a == 0u;
48 case OMEGA_LIFE_RECOVERED: return a == 0u && b > c && d <= e;
49 case OMEGA_LIFE_OVERFLOW: return a != 0u && b != 0u && c == d && e == f;
50 case OMEGA_LIFE_CHURN: return a == 0u && b > 256ull * 1024 * 1024 * 1024 && c < 2ull * 1024 * 1024 * 1024;
51 case OMEGA_LIFE_RETRIED: return a == 0u && b != 0u && c == 3u && d == 0u;
52 case OMEGA_LIFE_BOUNDED: return a != 0u && b == 0u && c == d && e == 3u;
53 case OMEGA_LIFE_LE: return a <= b;
54 case OMEGA_LIFE_CORRUPT: return a != 0u && b != 0u && c == 1u && d != 0u;
55 case OMEGA_LIFE_DISTINCT: return a != 0u && a != b;
56 case OMEGA_LIFE_TORNDOWN: return a == 0u && b == 0u && c == d;
57 case OMEGA_LIFE_BOTHZERO: return a == 0u && b == 0u;
58 default: return 0;
59 }
60 }
61 {
62 uint64_t in[7] = {k, a, b, c, d, e, f}, w[16];
63 if (eng_ask(ENG_OP_LIFE, in, 7, w) != 0) return 0;
64 return w[0] == 1u;
65 }
66}
67
68/* M16 requalify: REQF k0 marker a stayed 0, k1 word count a (an int) above 0,
69 * k2 wait rc a is 0, k3 libcuda-mapped flag a is 0. */
70enum { OMEGA_REQF_MARKER_ZERO = 0, OMEGA_REQF_WORDS = 1, OMEGA_REQF_WAIT_OK = 2, OMEGA_REQF_NO_LIBCUDA = 3 };
71#define OMEGA_REQF_PAYLOAD 0x16c0ffeeu
72
73static inline int omega_gate_reqf(uint32_t k, int64_t a)
74{
75 if (!eng_on()) {
76 if (k == OMEGA_REQF_WORDS) return a > 0;
77 return a == 0;
78 }
79 {
80 uint64_t in[2] = {k, (uint64_t)a}, w[16];
81 if (eng_ask(ENG_OP_REQF, in, 2, w) != 0) return 0;
82 return w[0] == 1u;
83 }
84}
85
86/* The requalify release payload, from REQF w1; 0 on binding failure. */
87static inline uint32_t omega_gate_reqf_payload(void)
88{
89 if (!eng_on()) return OMEGA_REQF_PAYLOAD;
90 {
91 uint64_t in[2] = {0u, 0u}, w[16];
92 if (eng_ask(ENG_OP_REQF, in, 2, w) != 0) return 0u;
93 return (uint32_t)w[1];
94 }
95}
96
97/* Submission visibility: VIS k0 negative control (wait rc a != 0, marker b == 0),
98 * k1 positive (wait rc a == 0, marker b == payload c), k2 put == retired. */
99enum { OMEGA_VIS_NEGATIVE = 0, OMEGA_VIS_POSITIVE = 1, OMEGA_VIS_RETIRED = 2 };
100
101static inline int omega_gate_vis(uint32_t k, uint64_t a, uint64_t b, uint64_t c)
102{
103 if (!eng_on()) {
104 if (k == OMEGA_VIS_NEGATIVE) return a != 0u && b == 0u;
105 if (k == OMEGA_VIS_POSITIVE) return a == 0u && b == c;
106 if (k == OMEGA_VIS_RETIRED) return a == b;
107 return 0;
108 }
109 {
110 uint64_t in[6] = {k, a, b, c, 0u, 0u}, w[16];
111 if (k == OMEGA_VIS_RETIRED) {
112 in[1] = 0u;
113 in[2] = 0u;
114 in[4] = a;
115 in[5] = b;
116 }
117 if (eng_ask(ENG_OP_VIS, in, 6, w) != 0) return 0;
118 return w[0] == 1u;
119 }
120}
121
122/* BWE k0: page rounding with a 4096 minimum; -1 for 0 or overflow. */
123static inline int omega_gate_round_pages(uint64_t bytes, uint64_t *out)
124{
125 if (!eng_on()) {
126 uint64_t v;
127 if (bytes == 0u || bytes > UINT64_MAX - 0xfffu) return -1;
128 v = (bytes + 0xfffu) & ~(uint64_t)0xfffu;
129 if (v < 0x1000u) v = 0x1000u;
130 *out = v;
131 return 0;
132 }
133 {
134 uint64_t in[2] = {0u, bytes}, w[16];
135 if (eng_ask(ENG_OP_BWE, in, 2, w) != 0 || w[0] != 0u) return -1;
136 *out = w[1];
137 return 0;
138 }
139}
140
141/* BWE k1: VECTOR_1D shape. -1 for n == 0 or n > UINT32_MAX - 63. */
142static inline int omega_gate_vec_shape(uint64_t n, uint32_t *grid, uint32_t *tpb, uint64_t *need)
143{
144 if (!eng_on()) {
145 if (n == 0u || n > UINT32_MAX - 63u) return -1;
146 *grid = (uint32_t)((n + 63u) / 64u);
147 *tpb = 64u;
148 *need = n * 4u;
149 return 0;
150 }
151 {
152 uint64_t in[2] = {1u, n}, w[16];
153 if (eng_ask(ENG_OP_BWE, in, 2, w) != 0 || w[0] != 0u) return -1;
154 *grid = (uint32_t)w[1];
155 *tpb = (uint32_t)w[2];
156 *need = w[3];
157 return 0;
158 }
159}
160
161/* BWE k2: QMD1 release words 9, 15, 16, 17, 19 against sem_va. 1 when they match. */
162static inline int omega_gate_qmd1_release(uint32_t w9, uint32_t w15, uint32_t w16, uint32_t w17, uint32_t w19,
163 uint64_t sem_va)
164{
165 if (!eng_on())
166 return w9 == 3u && w15 == (uint32_t)sem_va && w16 == (uint32_t)(sem_va >> 32) && w17 == 6u &&
167 w19 == 0x81810000u;
168 {
169 uint64_t in[7] = {2u, w9, w15, w16, w17, w19, sem_va}, w[16];
170 if (eng_ask(ENG_OP_BWE, in, 7, w) != 0) return 0;
171 return w[0] == 1u;
172 }
173}
174
175/* BWE k3: persistence (env present, its first byte) and device reuse.
176 * Returns persist; *reuse is dev_open && persist && !faulted. On binding failure
177 * persistence is off and nothing is reused (the old one-open-per-job path). */
178static inline int omega_gate_persist(int env_present, unsigned char e0, int dev_open, int faulted, int *reuse)
179{
180 if (!eng_on()) {
181 int p = !(env_present && e0 == '1');
182 *reuse = dev_open && p && !faulted;
183 return p;
184 }
185 {
186 uint64_t in[5] = {3u, env_present != 0, e0, dev_open != 0, faulted != 0}, w[16];
187 if (eng_ask(ENG_OP_BWE, in, 5, w) != 0) {
188 *reuse = 0;
189 return 0;
190 }
191 *reuse = (int)w[1];
192 return (int)w[0];
193 }
194}
195
196/* TOMS: marker, release semaphore, marker2 and visibility limits in ms. */
197static inline int omega_gate_timeouts(uint32_t t[4])
198{
199 if (!eng_on()) {
200 t[0] = t[1] = t[2] = t[3] = 5000u;
201 return 0;
202 }
203 {
204 uint64_t w[16];
205 if (eng_ask(ENG_OP_TOMS, NULL, 0, w) != 0) return -1;
206 t[0] = (uint32_t)w[0];
207 t[1] = (uint32_t)w[1];
208 t[2] = (uint32_t)w[2];
209 t[3] = (uint32_t)w[3];
210 return 0;
211 }
212}
213
214#define OMEGA_GATE_MM_MAX_M 8192u
215#define OMEGA_GATE_MM_MAX_K 16384u
216#define OMEGA_GATE_MM_MAX_N 262144u
217
218/* SUBM k0: matmul arguments. 1 when accepted. */
219static inline int omega_gate_matmul_args(int ptrs, int code, uint64_t m, uint64_t k, uint64_t n)
220{
221 if (!eng_on())
222 return ptrs && code && m != 0u && k != 0u && n != 0u && m <= OMEGA_GATE_MM_MAX_M &&
223 k <= OMEGA_GATE_MM_MAX_K && n <= OMEGA_GATE_MM_MAX_N;
224 {
225 uint64_t in[6] = {0u, ptrs != 0, code != 0, m, k, n}, w[16];
226 if (eng_ask(ENG_OP_SUBM, in, 6, w) != 0) return 0;
227 return w[0] == 1u;
228 }
229}
230
231/* SUBM k1 (16x16 tiles) and k2 (MMA: 32x1 threads, grid n/8 by m/16, minimum 1).
232 * s[0] grid_x, s[1] grid_y, s[2] threads_x, s[3] threads_y. */
233static inline int omega_gate_matmul_shape(int mma, uint64_t m, uint64_t n, uint32_t s[4])
234{
235 if (!eng_on()) {
236 if (mma) {
237 s[0] = (uint32_t)(n / 8u);
238 s[1] = (uint32_t)(m / 16u);
239 if (s[0] == 0u) s[0] = 1u;
240 if (s[1] == 0u) s[1] = 1u;
241 s[2] = 32u;
242 s[3] = 1u;
243 } else {
244 s[0] = (uint32_t)((n + 15u) / 16u);
245 s[1] = (uint32_t)((m + 15u) / 16u);
246 s[2] = 16u;
247 s[3] = 16u;
248 }
249 return 0;
250 }
251 {
252 uint64_t in[6] = {mma ? 2u : 1u, 0u, 0u, m, 0u, n}, w[16];
253 if (eng_ask(ENG_OP_SUBM, in, 6, w) != 0) return -1;
254 s[0] = (uint32_t)w[0];
255 s[1] = (uint32_t)w[1];
256 s[2] = (uint32_t)w[2];
257 s[3] = (uint32_t)w[3];
258 return 0;
259 }
260}
261
262/* SUBM k3: tensor precision is one of the two accepted values. */
263static inline int omega_gate_precision(uint64_t p, uint64_t fp16, uint64_t bf16)
264{
265 if (!eng_on()) return p == fp16 || p == bf16;
266 {
267 uint64_t in[4] = {3u, p, fp16, bf16}, w[16];
268 if (eng_ask(ENG_OP_SUBM, in, 4, w) != 0) return 0;
269 return w[0] == 1u;
270 }
271}
272
273/* CONC: m16_concurrent.c checks. Returns 1 on pass; *exit_code is the failure exit.
274 * k0 withheld (all four words 0, exit 2), k1 parity reread (a == want b, marker2 c,
275 * exit 7), k2 cross-talk (markerB, marker2B 0, exit 4), k3 final pages (exit 7),
276 * k4 distinct roots a != b (exit 6). On binding failure the gate fails with exit 1. */
277enum { OMEGA_CONC_WITHHELD = 0, OMEGA_CONC_PARITY = 1, OMEGA_CONC_CROSSTALK = 2, OMEGA_CONC_FINAL = 3,
278 OMEGA_CONC_ROOTS = 4 };
279
280static inline int omega_gate_conc(uint32_t k, uint64_t a, uint64_t b, uint64_t c, uint64_t d, int *exit_code)
281{
282 if (!eng_on()) {
283 switch (k) {
284 case OMEGA_CONC_WITHHELD: *exit_code = 2; return (a | b | c | d) == 0u;
285 case OMEGA_CONC_PARITY: *exit_code = 7; return a == b && c == 0x46464646u;
286 case OMEGA_CONC_CROSSTALK: *exit_code = 4; return a == 0u && b == 0u;
287 case OMEGA_CONC_FINAL:
288 *exit_code = 7;
289 return a == 0x16000001u && b == 0x46464646u && c == 0x16000002u && d == 0x46464646u;
290 default: *exit_code = 6; return a != b;
291 }
292 }
293 {
294 uint64_t in[5] = {k, a, b, c, d}, w[16];
295 if (eng_ask(ENG_OP_CONC, in, 5, w) != 0) {
296 *exit_code = 1;
297 return 0;
298 }
299 *exit_code = (int)w[1];
300 return w[0] == 1u;
301 }
302}
303
304#endif