interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_numeric_reduce_gb10.c

581 lines · 32610 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * E1 WP-D GB10 realization of the frozen reduction order: SUM, MAX, MIN, MEAN.
19 *
20 * Each level of OMEGA_REDUCE_DECLARED_ORDER is a tile-of-32 lane tree. The
21 * host pads a level with the op's identity (SUM/MEAN -0.0, MAX/MIN quiet NaN)
22 * to a multiple of 32, launches one warp kernel per chunk of at most
23 * OMEGA_NUMERIC_MAX_COUNT values (multiples of 32, so no tile straddles a
24 * chunk) and gathers lane 0 of every warp as the next level. The gather moves
25 * bits only; every combine runs on the chip.
26 *
27 * SUM the chip-proven REDUCE_SUM patch words of the WP-C table
28 * (omega_numeric_patch_words), checked by omega_numeric_check_patch,
29 * launched by this file's executor (see below).
30 * MAX this file's warp patch: five SHFL.DOWN (deltas 16, 8, 4, 2, 1,
31 * MIN clamp 0x1f) each followed by FMNMX R2, R2, R9, !PT (MAX) or PT
32 * (MIN), the last one writing R9, then STG, EXIT. The words are the
33 * registry's chip-proven FMNMX_MAX/FMNMX_MIN form (registers changed)
34 * and the REDUCE_SUM SHFL.DOWN words and control words; every word was
35 * decoded with nvdisasm 13.0 -b SM121 (make test-numeric-reduce-nvdisasm).
36 *
37 * Every op runs through this file's executor run_chunk: a copy of the vecadd
38 * launch of omega_numeric_gb10.c (which this lane does not edit) plus a wait on
39 * the QMD release semaphore and a dsb before outputs are read. The shared
40 * executor reads after the host marker only, which PR #141 showed can precede
41 * the last stores.
42 * MEAN SUM levels on the chip, then the one final division
43 * omega_math_div(SUM, u2f(n)) on the chip: the whole-program GB10 DIV
44 * kernel of E1 row 7 (omega_ds_gb10_run, one element), bit-identical to
45 * the CPU division. No host step remains.
46 *
47 * Every pre-submission check carries a CHECK: marker. With
48 * -DOMEGA_NUMERIC_CPU_ONLY the checks still run but no device is touched
49 * (omega_reduce_gb10 returns OMEGA_NUMERIC_ERR_DEVICE after the checks).
50 */
51#include "omega_numeric_reduce.h"
52#include "omega_numeric.h"
53#include "omega_numeric_divsqrt_gb10.h"
54#include "omega_blackwell_encoder.h"
55#include "omega_blackwell_qmd.h"
56#ifndef OMEGA_NUMERIC_CPU_ONLY
57#include "omega_blackwell_submit.h"
58#include "omega_numeric_native.h"
59#endif
60
61#include <stdarg.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <errno.h>
66#include <time.h>
67#include "omega_gpu_code_alloc.h"
68
69static unsigned g_last_launches;
70
71unsigned omega_reduce_gb10_last_launches(void) { return g_last_launches; }
72
73static int refuse(char *err, size_t err_len, int code, const char *fmt, ...) __attribute__((format(printf, 4, 5)));
74static int refuse(char *err, size_t err_len, int code, const char *fmt, ...) {
75 if (err && err_len) {
76 va_list ap;
77 va_start(ap, fmt);
78 vsnprintf(err, err_len, fmt, ap);
79 va_end(ap);
80 }
81 return code;
82}
83
84/* ---- MAX/MIN warp patch ----------------------------------------------------
85 * Register map of the vecadd baseline: R2 = a[i] (LDG, SB4), R6:R7 = &out[i],
86 * R9 = stored value. Control words are those of REDUCE_SUM: the first
87 * SHFL waits SB4 and sets SB0, later SHFLs set SB0 without waiting, each
88 * FMNMX waits SB0 (stall 5; FMNMX is fixed latency like FADD, and the
89 * registry's FMNMX ops are followed by a no-wait STG after the same stall). */
90#define RED_CTRL_SHFL_FIRST 0x010e2800U
91#define RED_CTRL_SHFL_NEXT 0x000e2400U
92#define RED_CTRL_FMNMX_SB0 0x001fca00U
93#define RED_FMNMX_W2_MIN 0x03800000U /* predicate PT -> min */
94#define RED_FMNMX_W2_MAX 0x07800000U /* predicate !PT -> max */
95
96int omega_reduce_gb10_minmax_patch(OmegaReduceOp op, OmegaNumericPatchInsn out[OMEGA_NUMERIC_PATCH_MAX]) {
97 if (op != OMEGA_RED_MAX && op != OMEGA_RED_MIN) return OMEGA_NUMERIC_ERR_NOT_ENCODED;
98 static const uint32_t SHFL_W1[5] = { 0x0a001f00U, 0x09001f00U, 0x08801f00U, 0x08401f00U, 0x08201f00U };
99 static const char *const SHFL_TXT[5] = {
100 "SHFL.DOWN PT, R9, R2, 0x10, 0x1f", "SHFL.DOWN PT, R9, R2, 0x8, 0x1f", "SHFL.DOWN PT, R9, R2, 0x4, 0x1f",
101 "SHFL.DOWN PT, R9, R2, 0x2, 0x1f", "SHFL.DOWN PT, R9, R2, 0x1, 0x1f" };
102 const bool mx = (op == OMEGA_RED_MAX);
103 int n = 0;
104 for (int s = 0; s < 5; s++) {
105 out[n].w[0] = 0x02097f89U; out[n].w[1] = SHFL_W1[s]; out[n].w[2] = 0x000e0000U;
106 out[n].w[3] = s == 0 ? RED_CTRL_SHFL_FIRST : RED_CTRL_SHFL_NEXT;
107 out[n].text = SHFL_TXT[s]; out[n].provenance_key = "RED_MINMAX_SHFL_DOWN"; n++;
108 out[n].w[0] = s == 4 ? 0x02097209U : 0x02027209U; out[n].w[1] = 0x00000009U;
109 out[n].w[2] = mx ? RED_FMNMX_W2_MAX : RED_FMNMX_W2_MIN; out[n].w[3] = RED_CTRL_FMNMX_SB0;
110 out[n].text = s == 4 ? (mx ? "FMNMX R9, R2, R9, !PT" : "FMNMX R9, R2, R9, PT")
111 : (mx ? "FMNMX R2, R2, R9, !PT" : "FMNMX R2, R2, R9, PT");
112 out[n].provenance_key = "RED_MINMAX_FMNMX"; n++;
113 }
114 static const OmegaNumericPatchInsn STG = { { 0x06007986U, 0x00000009U, 0x0c101904U, 0x000fe200U },
115 "STG.E desc[UR4][R6.64], R9", NULL };
116 static const OmegaNumericPatchInsn EXIT = { { 0x0000794dU, 0x00000000U, 0x03800000U, 0x000fea00U }, "EXIT", NULL };
117 out[n++] = STG;
118 out[n++] = EXIT;
119 return n;
120}
121
122#define P_OP(p) ((p).w[0] & 0xffffu)
123#define P_DST(p) (((p).w[0] >> 16) & 0xffu)
124#define P_SRCA(p) (((p).w[0] >> 24) & 0xffu)
125#define P_SRCB(p) ((p).w[1] & 0xffu)
126#define P_WBAR(p) (((p).w[3] >> 14) & 7u)
127#define P_WAIT(p) (((p).w[3] >> 20) & 0x3fu)
128#define P_SHFL_DELTA(p) (((p).w[1] >> 21) & 0x1fu)
129
130int omega_reduce_gb10_check_minmax_patch(OmegaReduceOp op, const OmegaNumericPatchInsn *p, int n,
131 const uint32_t *qmd1, char *err, size_t err_len) {
132 if (err && err_len) err[0] = '\0';
133 if (op != OMEGA_RED_MAX && op != OMEGA_RED_MIN)
134 return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "minmax patch check: op %d is not MAX or MIN", (int)op); /* CHECK:mm_op */
135 /* Generic rules of the numeric executor (length, scoreboard waits, SHFL.DOWN
136 * form and clamp, STG form) under the matching single-FMNMX op. */
137 int rc = omega_numeric_check_patch(op == OMEGA_RED_MAX ? OMEGA_NOP_FMNMX_MAX : OMEGA_NOP_FMNMX_MIN,
138 p, n, qmd1, err, err_len);
139 if (rc != OMEGA_NUMERIC_OK) return rc; /* CHECK:mm_generic_rules */
140 const char *name = omega_reduce_op_name(op);
141 static const uint32_t DELTA[5] = { 16u, 8u, 4u, 2u, 1u };
142 if (n != 12) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: expected five SHFL.DOWN + FMNMX pairs, STG, EXIT (got %d instructions)", name, n); /* CHECK:mm_shape */
143 const uint32_t want_w2 = op == OMEGA_RED_MAX ? RED_FMNMX_W2_MAX : RED_FMNMX_W2_MIN;
144 const uint32_t acc = 2u;
145 for (int s = 0; s < 5; s++) {
146 const OmegaNumericPatchInsn *sh = &p[2 * s], *mm = &p[2 * s + 1];
147 if (P_OP(*sh) != 0x7f89u || P_SHFL_DELTA(*sh) != DELTA[s]) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: step %d must be SHFL.DOWN by %u (declared order 16,8,4,2,1)", name, s, DELTA[s]); /* CHECK:mm_delta_order */
148 if (P_SRCA(*sh) != acc) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: step %d shuffles R%u, not the running value R2", name, s, P_SRCA(*sh)); /* CHECK:mm_shfl_src */
149 if (P_DST(*sh) != 9u) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: step %d shuffles into R%u, not the scratch register R9 (R2 is the running value, R6:R7 the store address)", name, s, P_DST(*sh)); /* CHECK:mm_shfl_dst */
150 if (s == 0 && !(P_WAIT(*sh) & (1u << 4))) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: first SHFL does not wait on SB4 (the input load of R2)", name); /* CHECK:mm_first_wait_load */
151 if (P_OP(*mm) != 0x7209u || (mm->w[1] & ~0xffu) != 0) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: step %d is not followed by a register FMNMX (no negate, no abs)", name, s); /* CHECK:mm_fmnmx_form */
152 if (mm->w[2] != want_w2) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: FMNMX of step %d has w2 0x%08x, not the %s form 0x%08x (wrong min/max predicate or modifier)", name, s, mm->w[2], name, want_w2); /* CHECK:mm_fmnmx_pred */
153 if (P_WBAR(*sh) > 5u || !(P_WAIT(*mm) & (1u << P_WBAR(*sh)))) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: FMNMX of step %d does not wait on the shuffle", name, s); /* CHECK:mm_fmnmx_wait */
154 if (P_SRCA(*mm) != acc || P_SRCB(*mm) != P_DST(*sh)) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: FMNMX of step %d does not combine the running value (left) with the shuffled value (right)", name, s); /* CHECK:mm_fmnmx_srcs */
155 if (P_DST(*mm) != (s == 4 ? 9u : acc)) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: FMNMX of step %d writes R%u", name, s, P_DST(*mm)); /* CHECK:mm_fmnmx_dst */
156 if (((mm->w[3] >> 9) & 0xfu) < 5u) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: FMNMX of step %d stalls %u cycles, under the 5 the next reader needs", name, s, (mm->w[3] >> 9) & 0xfu); /* CHECK:mm_fmnmx_stall */
157 }
158 if (p[10].w[0] != 0x06007986u || p[10].w[1] != 0x00000009u || p[10].w[2] != 0x0c101904u) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: R9 is not stored by STG.E desc[UR4][R6.64] (unpredicated, 32-bit)", name); /* CHECK:mm_store */
159 if (P_OP(p[11]) != 0x794du) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s: last instruction is not EXIT", name); /* CHECK:mm_exit */
160 return OMEGA_NUMERIC_OK;
161}
162
163int omega_reduce_gb10_build_minmax_kernel(OmegaReduceOp op, uint8_t *code, size_t code_len, size_t *out_len) {
164 OmegaNumericPatchInsn patch[OMEGA_NUMERIC_PATCH_MAX];
165 int n = omega_reduce_gb10_minmax_patch(op, patch);
166 if (n <= 0) return n < 0 ? n : OMEGA_NUMERIC_ERR_BAD_ARGS;
167 size_t len = 0;
168 if (omega_blackwell_encode_vecadd(code, code_len, &len) != 0) return OMEGA_NUMERIC_ERR_BAD_ARGS;
169 if (len < OMEGA_NUMERIC_PATCH_OFFSET + (size_t)n * 16u) return OMEGA_NUMERIC_ERR_BAD_ARGS;
170 for (int i = 0; i < n; i++) memcpy(code + OMEGA_NUMERIC_PATCH_OFFSET + (size_t)i * 16u, patch[i].w, 16);
171 if (out_len) *out_len = len;
172 return OMEGA_NUMERIC_OK;
173}
174
175/* QMD for a launch of count values (same shape as the numeric executor). */
176static int launch_qmd(size_t count, uint32_t qmd1[OMEGA_BW_QMD_WORDS]) {
177 OmegaBlackwellQmdConfig cfg;
178 memset(&cfg, 0, sizeof(cfg));
179 cfg.num_elements = (uint32_t)count;
180 omega_numeric_launch_shape(count, &cfg.threads_per_block, &cfg.grid_width);
181 return omega_blackwell_build_qmd1(qmd1, &cfg);
182}
183
184/* ---- plan and pre-submission checks --------------------------------------- */
185
186static size_t plan_launches(size_t n, unsigned *levels_out) {
187 size_t launches = 0;
188 unsigned levels = 0;
189 size_t len = n;
190 if (n == 0) { *levels_out = 0; return 0; }
191 do {
192 size_t padded = (len + 31) & ~(size_t)31;
193 launches += (padded + OMEGA_NUMERIC_MAX_COUNT - 1) / OMEGA_NUMERIC_MAX_COUNT;
194 len = padded / 32;
195 levels++;
196 } while (len > 1);
197 *levels_out = levels;
198 return launches;
199}
200
201/* Each distinct chunk size the plan submits, checked as it will be launched. */
202static int check_each_chunk(OmegaReduceOp op, size_t n, char *err, size_t err_len) {
203 size_t len = n;
204 char serr[256];
205 static float dummy;
206 OmegaNumericPatchInsn patch[OMEGA_NUMERIC_PATCH_MAX];
207 int np = 0;
208 if (op == OMEGA_RED_MAX || op == OMEGA_RED_MIN) np = omega_reduce_gb10_minmax_patch(op, patch);
209 while (len > 0) {
210 size_t padded = (len + 31) & ~(size_t)31;
211 size_t full = padded / OMEGA_NUMERIC_MAX_COUNT, rest = padded % OMEGA_NUMERIC_MAX_COUNT;
212 size_t sizes[2] = { full ? OMEGA_NUMERIC_MAX_COUNT : 0, rest };
213 for (int k = 0; k < 2; k++) {
214 if (!sizes[k]) continue;
215 int rc;
216 if (op == OMEGA_RED_SUM || op == OMEGA_RED_MEAN) {
217 rc = omega_numeric_submit_check("REDUCE_SUM", &dummy, &dummy, NULL, &dummy, sizes[k], serr, sizeof(serr));
218 } else {
219 uint32_t qmd1[OMEGA_BW_QMD_WORDS];
220 if (sizes[k] % 32u != 0 || sizes[k] > OMEGA_NUMERIC_MAX_COUNT)
221 return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s chunk of %zu is not whole warps within %u", omega_reduce_op_name(op), sizes[k], OMEGA_NUMERIC_MAX_COUNT); /* CHECK:red_mm_chunk_shape */
222 rc = launch_qmd(sizes[k], qmd1) != 0 ? OMEGA_NUMERIC_ERR_BAD_ARGS
223 : omega_reduce_gb10_check_minmax_patch(op, patch, np, qmd1, serr, sizeof(serr));
224 }
225 if (rc != OMEGA_NUMERIC_OK)
226 return refuse(err, err_len, rc, "%s chunk of %zu refused: %s", omega_reduce_op_name(op), sizes[k], serr); /* CHECK:red_submit_each_chunk */
227 }
228 if (padded == 32) break;
229 len = padded / 32;
230 }
231 return OMEGA_NUMERIC_OK;
232}
233
234int omega_reduce_gb10_check(OmegaReduceOp op, const float *x, size_t n, const float *out,
235 char *err, size_t err_len) {
236 if (err && err_len) err[0] = '\0';
237 if ((unsigned)op >= OMEGA_RED_COUNT)
238 return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "unknown reduction op %d", (int)op); /* CHECK:red_op_known */
239 if (!out || (n > 0 && !x))
240 return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "missing input or output buffer"); /* CHECK:red_buffers */
241 if (n > OMEGA_REDUCE_GB10_MAX_N)
242 return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "n=%zu exceeds %zu", n, OMEGA_REDUCE_GB10_MAX_N); /* CHECK:red_max_n */
243 if (op == OMEGA_RED_MEAN && n > OMEGA_REDUCE_MEAN_MAX_N)
244 return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "MEAN n=%zu exceeds 2^24 (n not exact in FP32)", n); /* CHECK:red_mean_max_n */
245 if (strcmp(OMEGA_WARP_REDUCTION_DECLARED_ORDER, "PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1") != 0 ||
246 strstr(OMEGA_REDUCE_DECLARED_ORDER, OMEGA_WARP_REDUCTION_DECLARED_ORDER) == NULL)
247 return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS,
248 "warp kernel order %s is not the tile order of %s",
249 OMEGA_WARP_REDUCTION_DECLARED_ORDER, OMEGA_REDUCE_DECLARED_ORDER); /* CHECK:red_order_string */
250 if (op == OMEGA_RED_SUM || op == OMEGA_RED_MEAN) {
251 const OmegaNumericOpInfo *info = omega_numeric_op_find("REDUCE_SUM");
252 if (!info || info->op != OMEGA_NOP_REDUCE_SUM || !info->gb10_encoded || info->compare != OMEGA_CMP_BIT_EXACT)
253 return refuse(err, err_len, OMEGA_NUMERIC_ERR_NOT_ENCODED,
254 "REDUCE_SUM is not a GB10-encoded BIT_EXACT op in the registry"); /* CHECK:red_kernel_registered */
255 if (omega_float_to_bits(omega_reduce_identity(op)) != OMEGA_REDUCE_SUM_PAD_BITS)
256 return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s padding is not -0.0", omega_reduce_op_name(op)); /* CHECK:red_pad_identity */
257 } else {
258 /* the single-pair FMNMX form this patch reuses is chip-proven BIT_EXACT */
259 const OmegaNumericOpInfo *info = omega_numeric_op_find(op == OMEGA_RED_MAX ? "FMNMX_MAX" : "FMNMX_MIN");
260 if (!info || !info->gb10_encoded || info->compare != OMEGA_CMP_BIT_EXACT)
261 return refuse(err, err_len, OMEGA_NUMERIC_ERR_NOT_ENCODED,
262 "%s: FMNMX is not a GB10-encoded BIT_EXACT op in the registry", omega_reduce_op_name(op)); /* CHECK:red_mm_fmnmx_registered */
263 if (omega_float_to_bits(omega_reduce_identity(op)) != OMEGA_REDUCE_MINMAX_PAD_BITS)
264 return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "%s padding is not quiet NaN", omega_reduce_op_name(op)); /* CHECK:red_mm_pad_identity */
265 }
266 unsigned levels = 0;
267 (void)plan_launches(n, &levels);
268 if (levels != omega_reduce_levels(n))
269 return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "launch plan has %u levels, contract says %u",
270 levels, omega_reduce_levels(n)); /* CHECK:red_levels */
271 if ((OMEGA_NUMERIC_MAX_COUNT % 32u) != 0)
272 return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "chunk limit is not whole warps"); /* CHECK:red_chunk_whole_warps */
273 return check_each_chunk(op, n, err, err_len);
274}
275
276/* ---- MAX/MIN executor ------------------------------------------------------ */
277#ifndef OMEGA_NUMERIC_CPU_ONLY
278static const uint32_t RED_SETUP_WORDS[18] = {
279 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003,
280 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000,
281 0x20012559, 0x00000000,
282};
283
284/* Patch words for one level launch of op: REDUCE_SUM's (WP-C table) for SUM
285 * and MEAN, the reduce-owned FMNMX patch for MAX and MIN. */
286static int chunk_patch(OmegaReduceOp op, OmegaNumericPatchInsn patch[OMEGA_NUMERIC_PATCH_MAX]) {
287 if (op == OMEGA_RED_MAX || op == OMEGA_RED_MIN) return omega_reduce_gb10_minmax_patch(op, patch);
288 if (op == OMEGA_RED_SUM || op == OMEGA_RED_MEAN) return omega_numeric_patch_words(OMEGA_NOP_REDUCE_SUM, patch);
289 return OMEGA_NUMERIC_ERR_BAD_ARGS;
290}
291
292static int chunk_check(OmegaReduceOp op, const OmegaNumericPatchInsn *patch, int np, const uint32_t *qmd1,
293 char *err, size_t err_len) {
294 if (op == OMEGA_RED_MAX || op == OMEGA_RED_MIN)
295 return omega_reduce_gb10_check_minmax_patch(op, patch, np, qmd1, err, err_len);
296 return omega_numeric_check_patch(OMEGA_NOP_REDUCE_SUM, patch, np, qmd1, err, err_len);
297}
298
299/* One level chunk on GB10, every op. Like the SIMT launcher, require both
300 * the host marker and the QMD release semaphore before reading results. */
301/* Device-failure diagnostics (M20 GB10 instrumentation). Every
302 * OMEGA_NUMERIC_ERR_DEVICE return below goes through gb10_devfail, which
303 * prints one GB10_DEVFAIL line to stderr: the step that failed, the driver
304 * return code, errno, the nvrm error text, the wait value and the marker word
305 * for waits, and the elapsed ms since the launch began. The lifecycle wrapper
306 * retains resources after uncertain completion and refuses later numeric
307 * launches. There is no retry and no wait value changes. */
308static double gb10_ms_since(const struct timespec *t0) {
309 struct timespec t;
310 timespec_get(&t, TIME_UTC); /* C11; no feature macro needed */
311 return (double)(t.tv_sec - t0->tv_sec) * 1e3 + (double)(t.tv_nsec - t0->tv_nsec) / 1e6;
312}
313
314static int gb10_devfail(const char *fn, const char *step, M16NativeContext *ctx, int do_close, int drv_rc,
315 int saved_errno, long wait_ms, const volatile uint32_t *word, uint32_t want,
316 const struct timespec *t0) {
317 double ms = gb10_ms_since(t0);
318 fprintf(stderr, "GB10_DEVFAIL fn=%s step=%s drv_rc=%d errno=%d rm_err=\"%s\" live_allocs=%u faulted=%u",
319 fn, step, drv_rc, saved_errno, ctx->rm.err, (unsigned)ctx->rm.live_count, (unsigned)ctx->rm.faulted);
320 if (wait_ms >= 0)
321 fprintf(stderr, " wait_ms=%ld word=0x%08x want=0x%08x", wait_ms, word ? (unsigned)*word : 0u, (unsigned)want);
322 fprintf(stderr, " elapsed_ms=%.3f\n", ms);
323 if (do_close) omega_numeric_native_close(ctx);
324 return OMEGA_NUMERIC_ERR_DEVICE;
325}
326/* Slow-wait note: one stderr line when a successful wait finished more than 1000 ms after launch began. */
327static void gb10_note_slow(const char *step, const struct timespec *t0) {
328 double ms = gb10_ms_since(t0);
329 if (ms > 1000.0) fprintf(stderr, "GB10_SLOW_WAIT step=%s elapsed_ms=%.3f\n", step, ms);
330}
331/* Plain step: drv_rc is the value the call returned. */
332#define GB10_FAIL(step, close_, rc_) gb10_devfail(__func__, (step), &ctx, (close_), (rc_), errno, -1, NULL, 0u, &t0)
333/* Wait step: also log the wait value and the word read. */
334#define GB10_FAIL_WAIT(step, rc_, ms_, w_, want_) \
335 gb10_devfail(__func__, (step), &ctx, 1, (rc_), errno, (long)(ms_), (w_), (want_), &t0)
336
337static int run_chunk(OmegaReduceOp op, const float *in, float *out_res, size_t count) {
338 OmegaNumericPatchInsn patch[OMEGA_NUMERIC_PATCH_MAX];
339 int np = chunk_patch(op, patch);
340 if (np <= 0 || count == 0 || count > OMEGA_NUMERIC_MAX_COUNT || count % 32u) return OMEGA_NUMERIC_ERR_BAD_ARGS;
341
342 struct timespec t0;
343 timespec_get(&t0, TIME_UTC);
344 M16NativeContext ctx;
345 int drc_;
346 if ((drc_ = omega_numeric_native_open(&ctx)) != 0) return GB10_FAIL("open", 0, drc_);
347 if ((drc_ = m16_native_create_channel(&ctx)) != 0) return GB10_FAIL("channel", 1, drc_);
348 NvrmMem large_pb;
349 if ((drc_ = nvrm_alloc(&ctx.rm, 0x10000, &large_pb)) != 0) return GB10_FAIL("alloc_pb", 1, drc_);
350 ctx.pb_mem = large_pb;
351
352 size_t bytes = (count * sizeof(float) + 0xfffULL) & ~0xfffULL;
353 if (bytes < 0x1000) bytes = 0x1000;
354 NvrmMem code_mem, cbank_mem, a_mem, b_mem, c_mem, out_mem, marker_mem, qmd_mem;
355 if (nvrm_alloc(&ctx.rm, omega_gpu_code_alloc_bytes(0x1000), &code_mem) != 0 || nvrm_alloc(&ctx.rm, 0x1000, &cbank_mem) != 0 || /* code: 4 KB + 2 KB prefetch tail */
356 nvrm_alloc(&ctx.rm, bytes, &a_mem) != 0 || nvrm_alloc(&ctx.rm, bytes, &b_mem) != 0 ||
357 nvrm_alloc(&ctx.rm, bytes, &c_mem) != 0 || nvrm_alloc(&ctx.rm, bytes, &out_mem) != 0 ||
358 nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &marker_mem) != 0 || nvrm_alloc(&ctx.rm, 0x10000, &qmd_mem) != 0) {
359 return GB10_FAIL("alloc_buffers", 1, -1);
360 }
361 memcpy(a_mem.cpu, in, count * sizeof(float));
362 memset(b_mem.cpu, 0, count * sizeof(float));
363 memset(out_mem.cpu, 0x55, count * sizeof(float));
364
365 size_t code_len = 0;
366 if (omega_blackwell_encode_vecadd(code_mem.cpu, 0x1000, &code_len) != 0 ||
367 code_len < OMEGA_NUMERIC_PATCH_OFFSET + (size_t)np * 16u) {
368 return GB10_FAIL("build_kernel", 1, -1);
369 }
370 for (int i = 0; i < np; i++) memcpy((uint8_t *)code_mem.cpu + OMEGA_NUMERIC_PATCH_OFFSET + (size_t)i * 16u, patch[i].w, 16);
371
372 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS];
373 omega_blackwell_build_cbank_driver(cbank_data, cbank_mem.va);
374 cbank_data[223] = omega_float_to_bits(1.0f); /* R1 at entry; unused by this patch */
375 uint32_t cbank_args[10] = {
376 (uint32_t)a_mem.va, (uint32_t)(a_mem.va >> 32), (uint32_t)b_mem.va, (uint32_t)(b_mem.va >> 32),
377 (uint32_t)out_mem.va, (uint32_t)(out_mem.va >> 32), (uint32_t)count, 0,
378 (uint32_t)c_mem.va, (uint32_t)(c_mem.va >> 32) };
379 memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data));
380 memcpy((uint8_t *)cbank_mem.cpu + 0x380, cbank_args, sizeof(cbank_args));
381
382 uint64_t qmd0_va = qmd_mem.va, qmd1_va = qmd_mem.va + 0x1000, sem_va = qmd_mem.va + 0x2000;
383 OmegaBlackwellQmdConfig qmd_cfg = {
384 .code_va = code_mem.va, .cbank_va = cbank_mem.va, .scratch_va = qmd_mem.va + 0x4000,
385 .sem_va = sem_va, .qmd0_va = qmd0_va, .qmd1_va = qmd1_va, .num_elements = (uint32_t)count,
386 };
387 omega_numeric_launch_shape(count, &qmd_cfg.threads_per_block, &qmd_cfg.grid_width);
388 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS];
389 if (omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va) != 0 ||
390 omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg) != 0 ||
391 omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) {
392 return GB10_FAIL("qmd", 1, -1);
393 }
394 { /* the structural check again, on the QMD actually submitted and the code image */
395 char perr[256];
396 if (chunk_check(op, patch, np, qmd1_words, perr, sizeof(perr)) != OMEGA_NUMERIC_OK) {
397 fprintf(stderr, "omega_reduce_gb10: %s\n", perr);
398 omega_numeric_native_close(&ctx);
399 return OMEGA_NUMERIC_ERR_OPERANDS;
400 }
401 for (int i = 0; i < np; i++)
402 if (memcmp((uint8_t *)code_mem.cpu + OMEGA_NUMERIC_PATCH_OFFSET + (size_t)i * 16u, patch[i].w, 16) != 0) {
403 fprintf(stderr, "omega_reduce_gb10: code image differs from the checked patch at %d\n", i);
404 omega_numeric_native_close(&ctx);
405 return OMEGA_NUMERIC_ERR_OPERANDS;
406 }
407 }
408 memcpy(qmd_mem.cpu, qmd0_words, sizeof(qmd0_words));
409 memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1_words, sizeof(qmd1_words));
410
411 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)qmd_mem.cpu + 0x2000);
412 volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu;
413 *hsem = 0;
414 *hmarker = 0;
415#ifndef OMEGA_C3_PROTECT_OFF /* C3 hardening, same as omega_ds_gb10_run (-DOMEGA_C3_PROTECT_OFF = A/B control arm) */
416 *(volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10) = 0;
417#endif
418 __asm__ volatile("dsb sy" ::: "memory");
419
420 uint32_t pb[1024];
421 size_t pb_len = 0;
422 memcpy(&pb[pb_len], RED_SETUP_WORDS, sizeof(RED_SETUP_WORDS));
423 pb_len += sizeof(RED_SETUP_WORDS) / 4;
424 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2);
425 pb[pb_len++] = (uint32_t)(cbank_mem.va >> 32);
426 pb[pb_len++] = (uint32_t)cbank_mem.va;
427 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2);
428 pb[pb_len++] = 0x00000380;
429 pb[pb_len++] = 0x00000001;
430 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1);
431 pb[pb_len++] = 0x00000041;
432 pb[pb_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
433 memcpy(&pb[pb_len], cbank_data, 224 * 4);
434 pb_len += 224;
435 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2);
436 pb[pb_len++] = (uint32_t)((cbank_mem.va + 0x380) >> 32);
437 pb[pb_len++] = (uint32_t)(cbank_mem.va + 0x380);
438 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2);
439 pb[pb_len++] = 0x00000028;
440 pb[pb_len++] = 0x00000001;
441 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1);
442 pb[pb_len++] = 0x00000041;
443 pb[pb_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
444 memcpy(&pb[pb_len], cbank_args, 10 * 4);
445 pb_len += 10;
446 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
447 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff);
448 pb[pb_len++] = (uint32_t)(qmd0_va >> 8);
449 memcpy(&pb[pb_len], qmd0_words, 96 * 4);
450 pb_len += 96;
451 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2);
452 pb[pb_len++] = (uint32_t)(sem_va >> 32);
453 pb[pb_len++] = (uint32_t)sem_va;
454 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2);
455 pb[pb_len++] = 0x00000004;
456 pb[pb_len++] = 0x00000001;
457 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1);
458 pb[pb_len++] = 0x00000041;
459 pb[pb_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
460 pb[pb_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
461 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
462 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff);
463 pb[pb_len++] = (uint32_t)(qmd1_va >> 8);
464 memcpy(&pb[pb_len], qmd1_words, 96 * 4);
465 pb_len += 96;
466 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5);
467 pb[pb_len++] = (uint32_t)marker_mem.va;
468 pb[pb_len++] = (uint32_t)(marker_mem.va >> 32);
469 pb[pb_len++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD;
470 pb[pb_len++] = 0;
471 pb[pb_len++] = 0x1 | (1u << 20);
472#ifndef OMEGA_C3_PROTECT_OFF
473 /* C3 hardening, copied from omega_ds_gb10_run (src/omega_numeric_divsqrt_gb10.c): after the WFI
474 * marker flush GPU L2 dirty lines to memory (NVC96F_MEM_OP_A..D = 0x28..0x34, D bits 31:27
475 * OPERATION = L2_FLUSH_DIRTY 0x10), then a second WFI marker the host waits for before the readback. */
476 pb[pb_len++] = nvrm_mthd(0, 0x0028, 4);
477 pb[pb_len++] = 0;
478 pb[pb_len++] = 0;
479 pb[pb_len++] = 0;
480 pb[pb_len++] = (0x10u << 27);
481 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5);
482 pb[pb_len++] = (uint32_t)(marker_mem.va + 0x10);
483 pb[pb_len++] = (uint32_t)((marker_mem.va + 0x10) >> 32);
484 pb[pb_len++] = 0x46464646u;
485 pb[pb_len++] = 0;
486 pb[pb_len++] = 0x1 | (1u << 20);
487#endif
488
489 if ((drc_ = m16_native_submit_methods(&ctx, pb, pb_len)) != 0) return GB10_FAIL("submit", 1, drc_);
490 if ((drc_ = omega_numeric_native_wait(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, 600000)) != 0) {
491 return GB10_FAIL_WAIT("marker_wait", drc_, 600000, hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD);
492 }
493 gb10_note_slow("marker", &t0);
494#ifndef OMEGA_C3_PROTECT_OFF
495 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10);
496 if ((drc_ = omega_numeric_native_wait(hmarker2, 0x46464646u, 600000)) != 0) {
497 return GB10_FAIL_WAIT("marker2_wait", drc_, 600000, hmarker2, 0x46464646u);
498 }
499 gb10_note_slow("marker2", &t0);
500#endif
501 /* The host marker can land before the last CTAs' stores are visible (seen
502 * by the DIV/SQRT gate, PR #141). Read only after the QMD's own release
503 * semaphore, written after the grid completes, is DONE. */
504 if ((drc_ = omega_numeric_native_wait(hsem, OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE, 600000)) != 0) {
505 return GB10_FAIL_WAIT("sem_wait", drc_, 600000, hsem, OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE);
506 }
507 gb10_note_slow("sem", &t0);
508 __asm__ volatile("dsb sy" ::: "memory");
509 memcpy(out_res, out_mem.cpu, count * sizeof(float));
510 if (omega_numeric_native_close(&ctx) != 0) return OMEGA_NUMERIC_ERR_DEVICE;
511 return OMEGA_NUMERIC_OK;
512}
513#endif
514
515/* ---- level loop ------------------------------------------------------------ */
516
517int omega_reduce_gb10(OmegaReduceOp op, const float *x, size_t n, float *out) {
518 char err[320];
519 g_last_launches = 0;
520 int rc = omega_reduce_gb10_check(op, x, n, out, err, sizeof(err));
521 if (rc != OMEGA_NUMERIC_OK) {
522 fprintf(stderr, "omega_reduce_gb10: refused before submission: %s\n", err);
523 return rc;
524 }
525 if (n == 0) {
526 *out = omega_bits_to_float(op == OMEGA_RED_SUM ? OMEGA_REDUCE_EMPTY_SUM_BITS : OMEGA_REDUCE_EMPTY_OTHER_BITS);
527 return OMEGA_NUMERIC_OK;
528 }
529#ifdef OMEGA_NUMERIC_CPU_ONLY
530 return OMEGA_NUMERIC_ERR_DEVICE;
531#else
532 const bool minmax = (op == OMEGA_RED_MAX || op == OMEGA_RED_MIN);
533 const uint32_t pad = minmax ? OMEGA_REDUCE_MINMAX_PAD_BITS : OMEGA_REDUCE_SUM_PAD_BITS;
534 size_t cap = (n + 31) & ~(size_t)31;
535 float *cur = malloc(cap * sizeof(float));
536 float *res = malloc(OMEGA_NUMERIC_MAX_COUNT * sizeof(float));
537 if (!cur || !res) { free(cur); free(res); return OMEGA_NUMERIC_ERR_BAD_ARGS; }
538 memcpy(cur, x, n * sizeof(float));
539 size_t len = n;
540 for (;;) {
541 size_t padded = (len + 31) & ~(size_t)31;
542 for (size_t i = len; i < padded; i++) cur[i] = omega_bits_to_float(pad);
543 size_t next_len = padded / 32;
544 for (size_t base = 0; base < padded; base += OMEGA_NUMERIC_MAX_COUNT) {
545 size_t count = padded - base;
546 if (count > OMEGA_NUMERIC_MAX_COUNT) count = OMEGA_NUMERIC_MAX_COUNT;
547 rc = run_chunk(op, cur + base, res, count);
548 g_last_launches++;
549 if (rc != OMEGA_NUMERIC_OK) {
550 fprintf(stderr, "GB10_REDUCE_FAIL op=%d n=%zu level_len=%zu base=%zu count=%zu inner_launch=%u rc=%d\n",
551 (int)op, n, len, base, count, g_last_launches, rc);
552 free(cur); free(res); return rc;
553 }
554 /* lane 0 of warp w holds tile (base/32 + w); base/32 + w <= base + 32w */
555 for (size_t w = 0; w < count / 32; w++) cur[base / 32 + w] = res[w * 32];
556 }
557 len = next_len;
558 if (len == 1) break;
559 }
560 float s = cur[0];
561 free(cur);
562 free(res);
563 if (op == OMEGA_RED_MEAN) {
564 /* MEAN: the one final division runs on the chip too: the GB10 DIV kernel of
565 * E1 row 7 (correctly rounded, bit-identical to omega_math_div). */
566 uint32_t sb, nb, ob;
567 float nf = omega_ref_u2f((uint32_t)n);
568#ifdef OMEGA_REDUCE_MUTATE_MEAN_DIV /* test builds only: divide by n + 1 (must FAIL chip parity) */
569 nf = omega_ref_u2f((uint32_t)n + 1u);
570#endif
571 memcpy(&sb, &s, 4);
572 memcpy(&nb, &nf, 4);
573 rc = omega_ds_gb10_run(OMEGA_DS_DIV, &sb, &nb, &ob, 1);
574 if (rc != OMEGA_NUMERIC_OK) return rc;
575 memcpy(out, &ob, 4);
576 return OMEGA_NUMERIC_OK;
577 }
578 *out = s;
579 return OMEGA_NUMERIC_OK;
580#endif
581}