diff --git a/Makefile b/Makefile index c1c714d3..050e736b 100644 --- a/Makefile +++ b/Makefile @@ -350,7 +350,8 @@ HOST_TESTS = test-drift test-gpu-matmul-api test-gpu-elementwise test-gpu-reconv test-codegen-freestanding test-gpu-session-platform test-gpu-native-runtime test-gpu-wait test-gpu-wait-mutants host-tests-m16 \ test-runtime-api test-runtime-portability test-doctor test-hello-sim test-native-artifact-manifest test-gemm-selectors \ test-native-artifact zero-cuda-gate verify-vendored test-swap-cli test-om-encoder test-om-spec-encoder test-om-cubin \ - test-portable-receipts test-gpu-qmd-shared test-model-capacity test-gpu-qmd-shared-clean-build $(CARRIED_HOST_TESTS) + test-portable-receipts test-gpu-qmd-shared test-model-capacity test-gpu-qmd-shared-clean-build \ + test-gpu-imadhi-rc test-dagsim $(CARRIED_HOST_TESTS) test: $(HOST_TESTS) @echo "CUDA++ HOST TESTS PASS ($(words $(HOST_TESTS)) targets)" @@ -392,3 +393,50 @@ test-swap-cli: zero-cuda-gate: $(HELLO_GPU) $(GPU_API_TEST) tools/zero-cuda-gate.sh --self-test tools/zero-cuda-gate.sh $(HELLO_GPU) $(GPU_API_TEST) + +# omega #492: IMAD.HI.U32 with a register Rc. The chip reads Rc as a 64-bit register pair (nvcc 13.0 +# sm_121 oracle, evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016; chip probe pending), so the four warp +# simulators refuse a non-RZ Rc instead of modelling a 32-bit one. One binary per simulator header. +.PHONY: test-gpu-imadhi-rc +GPU_IMADHI_SIMS = bw_warp_sim bw_warp_sim_tc bw_warp_sim_k bw_warp_sim_pa +GPU_IMADHI_TESTS = $(GPU_IMADHI_SIMS:%=$(OUT_DIR)/gpu_imadhi_rc_test_%) +$(OUT_DIR)/gpu_imadhi_rc_test_%: tests/gpu_imadhi_rc_test.c tests/%.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -Itests -DSIM_HEADER='"$*.h"' -o $@ tests/gpu_imadhi_rc_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-imadhi-rc: $(GPU_IMADHI_TESTS) + @for t in $(GPU_IMADHI_TESTS); do ./$$t || exit 1; done + +# Offline resource-DAG evaluator, imported verbatim from closed PR #590 +# (commit 3bd56731). Host arithmetic on the numbers written in the fixtures. +# Not a validated cost model and not a hardware measurement. +# src/omega_om_units.c is not in this tree. src/sha256.c is sha256_hash on every host. +# tests/dagsim/sha256_host.c is omega's non-AArch64 stub and aborts if a digest is requested. +# dagsim.o keeps the #590 (3bd56731) nm guard: no generation store, authority, +# world, or a call that maps or runs code. +.PHONY: test-dagsim +DS_OBJ = $(OUT_DIR)/dagsim.o +DS_UNIT = $(OUT_DIR)/dagsim_unit +DS_CLI = $(OUT_DIR)/dagsim +DS_SHA_SRC = src/sha256.c +RX_CM_OBJ = $(OUT_DIR)/rx_costmodel.o + +$(RX_CM_OBJ): src/runtime/rx_costmodel.c src/runtime/rx_costmodel.h | $(OUT_DIR) + $(CC) $(CFLAGS) -c -o $@ src/runtime/rx_costmodel.c + @if nm -u $@ | grep -E 'rx_gen_|aienos_cap_|rx_caproot_|rx_capadmin_|rx_world_|mmap|mprotect|fork|exec|dlopen|system' ; then \ + echo "rx_costmodel.o references an operation a cost model must not have"; \ + rm -f $@; exit 1; fi + +$(DS_OBJ): tools/dagsim/dagsim.c tools/dagsim/dagsim.h src/runtime/rx_costmodel.h | $(OUT_DIR) + $(CC) $(CFLAGS) -Itools/dagsim -c -o $@ tools/dagsim/dagsim.c + @if nm -u $@ | grep -E 'rx_gen_|aienos_cap_|rx_caproot_|rx_capadmin_|rx_world_|mmap|mprotect|fork|exec|dlopen|system' ; then \ + echo "dagsim.o references an operation an offline evaluator must not have"; \ + rm -f $@; exit 1; fi + +$(DS_UNIT): tests/dagsim/dagsim_unit.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) | $(OUT_DIR) + $(CC) $(CFLAGS) -Itools/dagsim -o $@ tests/dagsim/dagsim_unit.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) -lm + +$(DS_CLI): tools/dagsim/dagsim_cli.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) | $(OUT_DIR) + $(CC) $(CFLAGS) -Itools/dagsim -o $@ tools/dagsim/dagsim_cli.c $(DS_OBJ) $(RX_CM_OBJ) $(DS_SHA_SRC) -lm + +test-dagsim: $(DS_UNIT) $(DS_CLI) + ./$(DS_UNIT) + sh tests/dagsim/run_cli.sh $(DS_CLI) $(OUT_DIR)/dagsim-cli-out diff --git a/examples/hello_gpu/hello_gpu.c b/examples/hello_gpu/hello_gpu.c index ef952529..bc12ba39 100644 --- a/examples/hello_gpu/hello_gpu.c +++ b/examples/hello_gpu/hello_gpu.c @@ -16,10 +16,10 @@ * Prints PASS or FAIL. Exit code: 0 PASS, 1 FAIL (wrong or missing results), 2 the GPU could not be used. * * `hello_gpu --sim` runs the same kernel program on the host warp simulator instead (tests/bw_warp_sim.h): - * no GPU is opened. A bad memory access there stops at the launch that made it and is reported with the - * kernel, launch, instruction, block, thread, address and the buffer it missed (developer bar D3 and D4, - * docs/PAIN_POINTS.md). `--sim --bug=past-end` shows such a report: the last launch is given one element - * too many, a classic off-by-one. + * no GPU is opened. A bad memory access there stops at the launch that made it. The simulator names the + * pc. This program names the block, thread, address and buffer from the launch it issued (developer bar + * D3 and D4, docs/PAIN_POINTS.md): the mirrored simulator does not store those fields. `--sim --bug=past-end` + * shows such a report: the last launch is given one element too many, a classic off-by-one. * * `--bug=short` is the negative control for the check itself, on the GPU or with --sim: the last launch is * given one element too FEW, so c[1048575] is never written. That writes nothing outside a buffer, so it is @@ -30,7 +30,9 @@ * hello of aien-dev/CUDApp#79 (OM-GPU-RETIRE), which replaces this whole C program. * * Both paths print the work the job takes (bytes moved, floating-point operations) and the least time - * those bytes need at this GB10's measured read ceiling (D12), the time the kernel took to build and the + * those bytes need at this GB10's measured read ceiling (D12). The mirrored simulator no longer counts + * those bytes, so --sim accounts the vecadd this program issued: two f32 loads, one f32 store and one + * FADD per element. Both paths also print the time the kernel took to build, the * hash of its machine code (D10, no compiling at run time), and the hash of the output (D7: the same * inputs give the same bits on every run). * @@ -197,6 +199,7 @@ static int run_sim(const BlackwellIRProgram *prog, int bug) { s.nranges = 3; /* every global access must land inside a, b or c */ const uint32_t launches = (N_TOTAL + CHUNK - 1) / CHUNK; uint32_t li = 0; + uint64_t read_b = 0, wrote_b = 0, flops = 0; for (uint32_t base = 0; base < N_TOTAL; base += CHUNK) { li++; uint64_t off = (uint64_t)base * sizeof(float); @@ -215,23 +218,30 @@ static int run_sim(const BlackwellIRProgram *prog, int bug) { if (ws_run(&s, grid, 1) != 0) { const BlackwellIRInsn *in = (s.err_pc >= 0 && (size_t)s.err_pc < prog->count) ? &prog->insns[s.err_pc] : NULL; int store = in && in->op == BW_IR_STG_E; + /* --bug=past-end: the first out-of-range access is a[N_TOTAL], a 4-byte LDG.E. + The mirrored simulator records the pc, not the lane or the address. */ + uint32_t fault_elem = base + n - 1; + uint32_t rel = fault_elem - base; + uint32_t block = rel / THREADS, thread = rel % THREADS; + uint64_t fault_addr = (uint64_t)(uintptr_t)fa + (uint64_t)fault_elem * sizeof(float); + unsigned fault_bytes = 4; if (s.err == WS_E_MEM) printf("FAULT at launch %u of %u of kernel vecadd (simulator): global %s outside every buffer\n", li, launches, store ? "store" : "load"); else printf("FAULT at launch %u of %u of kernel vecadd (simulator): %s\n", li, launches, ws_err_name(s.err)); printf(" launch: grid %u x 1 blocks of %u threads, n = %u, first element %u\n", grid, THREADS, n, base); if (in) printf(" instruction: %d of %zu, %s\n", s.err_pc, prog->count, op_text(in->op)); - printf(" thread: block %u, thread %u (element %llu of the whole job)\n", s.err_cx, s.err_tid, - (unsigned long long)base + (unsigned long long)s.err_cx * THREADS + s.err_tid); + printf(" thread: block %u, thread %u (element %llu of the whole job)\n", block, thread, + (unsigned long long)base + (unsigned long long)block * THREADS + thread); int near = 0; uint64_t dist = UINT64_MAX; int past = 1; if (s.err == WS_E_MEM) { for (int k = 0; k < 3; k++) { uint64_t lo = s.ranges[k].lo, hi = s.ranges[k].hi, d; int pst; - if (s.err_addr >= hi) { d = s.err_addr - hi; pst = 1; } - else if (s.err_addr + s.err_bytes <= lo) { d = lo - (s.err_addr + s.err_bytes); pst = 0; } + if (fault_addr >= hi) { d = fault_addr - hi; pst = 1; } + else if (fault_addr + fault_bytes <= lo) { d = lo - (fault_addr + fault_bytes); pst = 0; } else { d = 0; pst = 1; } /* starts inside, runs past the end */ if (d < dist) { dist = d; near = k; past = pst; } } printf(" address: 0x%llx (%u bytes), %llu bytes %s buffer %s = [0x%llx, 0x%llx) (%zu bytes)\n", - (unsigned long long)s.err_addr, s.err_bytes, (unsigned long long)dist, past ? "past the end of" : "before the start of", + (unsigned long long)fault_addr, fault_bytes, (unsigned long long)dist, past ? "past the end of" : "before the start of", names[near], (unsigned long long)s.ranges[near].lo, (unsigned long long)s.ranges[near].hi, bytes); } printf(" simulator: %s\n", s.msg); @@ -244,9 +254,12 @@ static int run_sim(const BlackwellIRProgram *prog, int bug) { free(fa); free(fb); free(wc); return 1; } + read_b += (uint64_t)n * 2u * sizeof(float); + wrote_b += (uint64_t)n * sizeof(float); + flops += n; } int rc = check(fa, fb, wc); - print_work(s.gld_bytes, s.gst_bytes, s.fp_ops, 0); + print_work(read_b, wrote_b, flops, 0); print_sha("output", wc, bytes); printf("%s\n", rc ? "FAIL" : "PASS"); free(fa); free(fb); free(wc); diff --git a/src/omega_blackwell_codegen.h b/src/omega_blackwell_codegen.h index 9dd4f621..1f0b70ee 100644 --- a/src/omega_blackwell_codegen.h +++ b/src/omega_blackwell_codegen.h @@ -1,4 +1,19 @@ -/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef OMEGA_BLACKWELL_CODEGEN_H #define OMEGA_BLACKWELL_CODEGEN_H @@ -86,8 +101,12 @@ typedef enum { * truth table over Ra = 0xf0, Rb = 0xcc, Rc = 0xaa (a|b = 0xfc, ~(a|b) = 0x03) */ BW_IR_SHF_L_U32, /* SHF.L.U32 Rd, Ra, Rb, RZ: Rd = Ra << Rb, register amount (the form nvcc emits * for PTX shl.b32). Amounts >= 32 are NOT chip-verified: callers keep Rb < 32 */ - BW_IR_IMAD_HI_U32, /* IMAD.HI.U32 Rd, Ra, Rb, Rc: Rd = hi32(Ra * Rb) + Rc mod 2^32 (nvcc emits this - * word for __umulhi(a, b) + c) */ + BW_IR_IMAD_HI_U32, /* IMAD.HI.U32 Rd, Ra, Rb, Rc: Rd = hi32(Ra * Rb + Rc:Rc+1). Rc is a 64-bit register pair, + * NOT one 32-bit word: nvcc 13.0 zeroes the upper half before using a 32-bit c, puts + * __umulhi(a, b) + c through the pair {0, c}, and emits only even pair bases (R2, R4, UR4 + * in evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016). Odd-Rc chip behaviour is unknown until + * the chip probe (omega #492); until then the warp simulators refuse a non-RZ Rc. + * Callers pass Rc = RZ (src3_vreg = -1) */ /* Structured warp reconvergence (omega #308, 2026-10-05), additive. Words matched against * nvcc 13.0.88 -arch=sm_121 output (BSSY.RECONVERGENT / BSYNC.RECONVERGENT; the oracle * kernels are listed in docs/gpu-reconvergence-308.md) and the Mesa NAK encoder diff --git a/src/runtime/rx_costmodel.c b/src/runtime/rx_costmodel.c new file mode 100644 index 00000000..d8ddf956 --- /dev/null +++ b/src/runtime/rx_costmodel.c @@ -0,0 +1,528 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * rx_costmodel.c -- Omega's empirical cost model. See rx_costmodel.h. + * + * Pure arithmetic over measurements the caller reports. No execution, no + * authority, no generation store. + */ +#include "runtime/rx_costmodel.h" +#include "sha256.h" + +#include +#include +#include + +#define PRIOR_A 1.0 +#define PRIOR_B 0.01 /* prior noise variance ~0.01 log2^2 (about 7%) */ +#define NOISE_FLOOR 0.0009 /* never claim better than ~2% sd per call */ +#define FAIL_PRIOR 0.05 + +void rx_cm_default_policy(RxCmPolicy *p) { + p->ei_min = 0.08; + p->max_fail_p = 0.0; /* any wrong result excludes the arm */ + p->fallback_arm = 0; + p->explore_frac = 0.05; + p->explore_allow_ps = 500000000ull; /* 0.5 ms to get started */ +} + +void rx_cm_init(RxCostModel *m, uint32_t n_arms) { + memset(m, 0, sizeof *m); + m->n_arms = n_arms > RX_CM_ARMS ? RX_CM_ARMS : n_arms; + m->lambda = 1e-3; + m->sd_scale = 1.0; + for (uint32_t c = 0; c < RX_CM_CELLS; c++) + for (uint32_t a = 0; a < RX_CM_ARMS; a++) + for (uint32_t i = 0, k = 0; i < RX_CM_D; i++) + for (uint32_t j = i; j < RX_CM_D; j++, k++) + m->cell[c][a].tri[k] = i == j ? m->lambda : 0.0; +} + +uint32_t rx_cm_cell(const RxCmFeatures *f) { + uint32_t op = f->op < RX_CM_OPS ? f->op : RX_CM_OPS - 1; + uint32_t core = f->core < RX_CM_CORES ? f->core : RX_CM_CORE_OTHER; + uint32_t pr = f->pressure < RX_CM_PRESSURE ? f->pressure : RX_CM_PRESSURE - 1; + return (op * RX_CM_CORES + core) * RX_CM_PRESSURE + pr; +} + +void rx_cm_basis(const RxCmFeatures *f, double phi[RX_CM_D]) { + double lm = log2((double)(f->M ? f->M : 1)); + double ln = log2((double)(f->N ? f->N : 1)); + double ov = 0.0; + if (f->cache_bytes && f->state_bytes > f->cache_bytes) + ov = log2((double)f->state_bytes / (double)f->cache_bytes); + phi[0] = 1.0; + phi[1] = lm; + phi[2] = ln; + phi[3] = lm * ln; + phi[4] = ln * ln; + phi[5] = f->N <= 3 ? 1.0 : 0.0; + phi[6] = (f->N % 4u) != 0 ? 1.0 : 0.0; + phi[7] = ov; +} + +/* Cholesky of the D x D matrix held as an upper triangle. 0 on success. */ +static int cholesky(const double *tri, double L[RX_CM_D][RX_CM_D]) { + double A[RX_CM_D][RX_CM_D]; + for (uint32_t i = 0, k = 0; i < RX_CM_D; i++) + for (uint32_t j = i; j < RX_CM_D; j++, k++) A[i][j] = A[j][i] = tri[k]; + memset(L, 0, sizeof(double) * RX_CM_D * RX_CM_D); + for (uint32_t j = 0; j < RX_CM_D; j++) { + double s = A[j][j]; + for (uint32_t k = 0; k < j; k++) s -= L[j][k] * L[j][k]; + if (!(s > 0.0)) return -1; + L[j][j] = sqrt(s); + for (uint32_t i = j + 1; i < RX_CM_D; i++) { + double t = A[i][j]; + for (uint32_t k = 0; k < j; k++) t -= L[i][k] * L[j][k]; + L[i][j] = t / L[j][j]; + } + } + return 0; +} + +static void forward(double L[RX_CM_D][RX_CM_D], const double *b, double *v) { + for (uint32_t i = 0; i < RX_CM_D; i++) { + double s = b[i]; + for (uint32_t k = 0; k < i; k++) s -= L[i][k] * v[k]; + v[i] = s / L[i][i]; + } +} + +static void backward(double L[RX_CM_D][RX_CM_D], const double *v, double *x) { + for (int i = (int)RX_CM_D - 1; i >= 0; i--) { + double s = v[i]; + for (uint32_t k = (uint32_t)i + 1; k < RX_CM_D; k++) s -= L[k][i] * x[k]; + x[i] = s / L[i][i]; + } +} + +/* Predictive t for one (cell, arm). Returns 0 if there is no data. */ +static int fit_predict(const RxCmArmStats *s, const double phi[RX_CM_D], double *mean, + double *sd, double *dof) { + if (s->n == 0) return 0; + double L[RX_CM_D][RX_CM_D], v[RX_CM_D], mu[RX_CM_D], w[RX_CM_D]; + if (cholesky(s->tri, L) != 0) return 0; + forward(L, s->xty, v); + backward(L, v, mu); + double quad = 0.0; /* mu' Lambda mu = |L' mu|^2 = v . v */ + for (uint32_t i = 0; i < RX_CM_D; i++) quad += v[i] * v[i]; + double a = PRIOR_A + 0.5 * (double)s->n; + double b = PRIOR_B + 0.5 * (s->yty - quad); + if (b < PRIOR_B) b = PRIOR_B; + double sigma2 = b / a; + if (sigma2 < NOISE_FLOOR) sigma2 = NOISE_FLOOR; + forward(L, phi, w); + double lev = 0.0; + for (uint32_t i = 0; i < RX_CM_D; i++) lev += w[i] * w[i]; + double m = 0.0; + for (uint32_t i = 0; i < RX_CM_D; i++) m += mu[i] * phi[i]; + *mean = m; + *sd = sqrt(sigma2 * (1.0 + lev)); + *dof = 2.0 * a; + return 1; +} + +static uint64_t failures_of(const RxCostModel *m, uint32_t arm) { + uint64_t f = 0; + for (uint32_t c = 0; c < RX_CM_CELLS; c++) f += m->cell[c][arm].failures; + return f; +} + +static double fail_p(const RxCostModel *m, uint32_t arm) { + uint64_t t = 0, f = 0; + for (uint32_t c = 0; c < RX_CM_CELLS; c++) { + t += m->cell[c][arm].trials; + f += m->cell[c][arm].failures; + } + return ((double)f + FAIL_PRIOR) / ((double)t + 1.0); +} + +/* Pressure cells hold a correction over the same core class at pressure 0: + * log2 cost under pressure = quiet prediction + a + b * (doublings past the + * private cache). Two numbers per arm, readable as "pressure costs this much, + * and this much more per doubling out of cache". */ +static void residual_basis(const RxCmFeatures *f, double phi[RX_CM_D]) { + double full[RX_CM_D]; + rx_cm_basis(f, full); + memset(phi, 0, sizeof(double) * RX_CM_D); + phi[0] = 1.0; + phi[1] = full[7]; +} + +static int quiet_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, double *mean, + double *sd, double *dof, uint64_t *n) { + RxCmFeatures g = *f; + g.pressure = 0; + const RxCmArmStats *b = &m->cell[rx_cm_cell(&g)][arm]; + double phi[RX_CM_D]; + rx_cm_basis(f, phi); + if (b->n < RX_CM_MIN_OBS || !fit_predict(b, phi, mean, sd, dof)) return 0; + *n = b->n; + return 1; +} + +static uint32_t bucket(const RxCmFeatures *f) { + return f->pressure < RX_CM_PRESSURE ? f->pressure : RX_CM_PRESSURE - 1; +} + +void rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, + RxCmPrediction *out) { + memset(out, 0, sizeof *out); + if (arm >= m->n_arms) return; + double mean = 0, sd = 0, dof = 0; + uint64_t n = 0; + out->fail_p = fail_p(m, arm); + if (!quiet_predict(m, f, arm, &mean, &sd, &dof, &n)) return; + if (bucket(f) == 0) { + out->known = 1; + } else { + const RxCmArmStats *r = &m->cell[rx_cm_cell(f)][arm]; + double phi[RX_CM_D], rm = 0, rsd = 0, rdof = 0; + residual_basis(f, phi); + if (r->n >= RX_CM_RES_MIN_OBS && fit_predict(r, phi, &rm, &rsd, &rdof)) { + mean += rm; + sd = sqrt(sd * sd + rsd * rsd); + if (rdof < dof) dof = rdof; + n = r->n; + out->known = 1; + } else { + /* Never seen under this pressure: the quiet prediction, with the + * prior on how far an unseen condition may move cost. */ + sd = sqrt(sd * sd + RX_CM_UNSEEN_SHIFT * RX_CM_UNSEEN_SHIFT); + out->borrowed = 1; + } + } + out->n = (uint32_t)n; + out->mean_log2_ps = mean; + out->sd_log2 = sd * (m->sd_scale >= 1.0 ? m->sd_scale : 1.0); + out->dof = dof; + uint32_t core = f->core < RX_CM_CORES ? f->core : RX_CM_CORE_OTHER; + double mw = m->power_mw[core][arm]; + if (mw > 0.0) out->energy_pj = exp2(mean) * mw * 1e-3; /* ps * mW = 1e-3 pJ */ +} + +int rx_cm_observe(RxCostModel *m, const RxCmFeatures *f, uint32_t arm, const RxCmObservation *o) { + if (!m || !f || !o || arm >= m->n_arms) return RX_CM_ERR_ARG; + RxCmArmStats *s = &m->cell[rx_cm_cell(f)][arm]; + s->trials++; + if (o->failed) { + s->failures++; + return RX_CM_OK; + } + if (o->ps_per_call == 0) return RX_CM_ERR_ARG; + double phi[RX_CM_D]; + double y = log2((double)o->ps_per_call); + if (bucket(f) == 0) { + rx_cm_basis(f, phi); + } else { + double qm, qsd, qdof; + uint64_t qn; + if (!quiet_predict(m, f, arm, &qm, &qsd, &qdof, &qn)) return RX_CM_SKIPPED; + residual_basis(f, phi); + y -= qm; + } + for (uint32_t i = 0, k = 0; i < RX_CM_D; i++) { + for (uint32_t j = i; j < RX_CM_D; j++, k++) s->tri[k] += phi[i] * phi[j]; + s->xty[i] += phi[i] * y; + } + s->yty += y * y; + s->n++; + return RX_CM_OK; +} + +static double norm_cdf(double x) { return 0.5 * erfc(-x / sqrt(2.0)); } + +/* Acklam's inverse normal. */ +static double norm_inv(double p) { + static const double a[] = { -3.969683028665376e+01, 2.209460984245205e+02, + -2.759285104469687e+02, 1.383577518672690e+02, + -3.066479806614716e+01, 2.506628277459239e+00 }; + static const double b[] = { -5.447609879822406e+01, 1.615858368580409e+02, + -1.556989798598866e+02, 6.680131188771972e+01, + -1.328068155288572e+01 }; + static const double c[] = { -7.784894002430293e-03, -3.223964580411365e-01, + -2.400758277161838e+00, -2.549732539343734e+00, + 4.374664141464968e+00, 2.938163982698783e+00 }; + static const double d[] = { 7.784695709041462e-03, 3.224671290700398e-01, + 2.445134137142996e+00, 3.754408661907416e+00 }; + if (p <= 0.0) return -INFINITY; + if (p >= 1.0) return INFINITY; + double q, r; + if (p < 0.02425) { + q = sqrt(-2 * log(p)); + return (((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5]) / + ((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1); + } + if (p > 1 - 0.02425) { + q = sqrt(-2 * log(1 - p)); + return -(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5]) / + ((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1); + } + q = p - 0.5; + r = q * q; + return (((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r + a[4]) * r + a[5]) * q / + (((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r + b[4]) * r + 1); +} + +double rx_cm_t_quantile(double q, double dof) { + double z = norm_inv(0.5 + 0.5 * q); + if (!(dof > 0.0)) return z; + double z3 = z * z * z, z5 = z3 * z * z; + return z + (z3 + z) / (4.0 * dof) + (5.0 * z5 + 16.0 * z3 + 3.0 * z) / (96.0 * dof * dof); +} + +static int explore_allowed(const RxCostModel *m, const RxCmPolicy *p) { + double cap = p->explore_frac * (double)m->work_ps + (double)p->explore_allow_ps; + return (double)m->explore_ps < cap; +} + +/* E[max(0, X)] for X ~ N(d, s^2): the expected gain, in log2 units, of + * switching to a competitor whose advantage over the chosen arm is d. */ +static double expected_improvement(double d, double s) { + if (!(s > 0.0)) return d > 0.0 ? d : 0.0; + double u = d / s; + return s * exp(-0.5 * u * u) / sqrt(2.0 * M_PI) + d * norm_cdf(u); +} + +void rx_cm_decide(const RxCostModel *m, const RxCmPolicy *p, const RxCmFeatures *f, + int allow_measure, RxCmDecision *out) { + memset(out, 0, sizeof *out); + uint32_t fb = p->fallback_arm < m->n_arms ? p->fallback_arm : 0; + uint32_t elig = (f->eligible | (1u << fb)) & ((1u << m->n_arms) - 1u); + int have[RX_CM_ARMS] = { 0 }; + int n_elig = 0; + for (uint32_t a = 0; a < m->n_arms; a++) { + rx_cm_predict(m, f, a, &out->pred[a]); + if (!(elig & (1u << a))) continue; + if (a != fb && failures_of(m, a) > 0 && out->pred[a].fail_p > p->max_fail_p) { + elig &= ~(1u << a); + continue; + } + n_elig++; + have[a] = out->pred[a].known || out->pred[a].borrowed; + } + out->action = RX_CM_FALLBACK; + out->arm = fb; + if (n_elig <= 1) { + out->why = RX_CM_WHY_NO_ELIGIBLE; + return; + } + + /* Best by predicted latency among arms that meet the budgets; if none + * meets them, best overall and say so. */ + int best = -1, best_fit = -1; + for (uint32_t a = 0; a < m->n_arms; a++) { + if (!have[a] || !(elig & (1u << a))) continue; + const RxCmPrediction *pr = &out->pred[a]; + double hi = exp2(pr->mean_log2_ps + 1.2816 * pr->sd_log2); + int fits = (!f->latency_budget_ps || hi <= (double)f->latency_budget_ps) && + (!f->energy_budget_pj || pr->energy_pj == 0.0 || + pr->energy_pj <= (double)f->energy_budget_pj); + if (best < 0 || pr->mean_log2_ps < out->pred[best].mean_log2_ps) best = (int)a; + if (fits && (best_fit < 0 || pr->mean_log2_ps < out->pred[best_fit].mean_log2_ps)) + best_fit = (int)a; + } + int over = 0; + if (best_fit >= 0) best = best_fit; + else if (best >= 0) over = 1; + + /* Competitor: the eligible arm with the largest expected improvement + * over `best`. An arm with no prediction at all is always worth a look. */ + int comp = -1, comp_unknown = 0; + double ei = 0.0, pc = 0.0; + for (uint32_t a = 0; a < m->n_arms; a++) { + if (!(elig & (1u << a)) || (int)a == best) continue; + double e, pa; + int unknown = !have[a] || best < 0; + if (unknown) { + e = INFINITY; + pa = 0.5; + } else { + const RxCmPrediction *x = &out->pred[a], *b = &out->pred[best]; + double s = sqrt(x->sd_log2 * x->sd_log2 + b->sd_log2 * b->sd_log2); + double d = b->mean_log2_ps - x->mean_log2_ps; + e = expected_improvement(d, s); + pa = norm_cdf(d / (s > 0 ? s : 1e-9)); + } + if (comp < 0 || e > ei) { + comp = (int)a; + ei = e; + pc = pa; + comp_unknown = unknown; + } + } + out->p_runner_up_better = pc; + out->expected_improvement = isinf(ei) ? -1.0 : ei; + + uint32_t why = RX_CM_WHY_CONFIDENT; + if (best < 0 || comp_unknown) why = RX_CM_WHY_UNKNOWN_ARM; + else if (ei > p->ei_min) why = RX_CM_WHY_WORTH_MEASURING; + + if (why != RX_CM_WHY_CONFIDENT && allow_measure && explore_allowed(m, p) && comp >= 0) { + out->action = RX_CM_MEASURE; + out->measure[0] = best >= 0 ? (uint32_t)best : fb; + out->measure[1] = (uint32_t)comp; + if (out->measure[0] == out->measure[1]) out->measure[1] = fb; + out->why = why; + out->arm = out->measure[0]; + return; + } + if (best < 0) { + out->why = allow_measure && !explore_allowed(m, p) ? RX_CM_WHY_BUDGET_SPENT + : RX_CM_WHY_UNKNOWN_ARM; + return; /* fallback */ + } + out->action = RX_CM_SELECT; + out->arm = (uint32_t)best; + if (over) out->why = RX_CM_WHY_OVER_BUDGET; + else if (why != RX_CM_WHY_CONFIDENT && allow_measure) out->why = RX_CM_WHY_BUDGET_SPENT; + else out->why = RX_CM_WHY_CONFIDENT; +} + +static int cmp_double(const void *a, const void *b) { + double x = *(const double *)a, y = *(const double *)b; + return (x > y) - (x < y); +} + +double rx_cm_calibrate(RxCostModel *m, const RxCmFeatures *f, const uint32_t *arm, + const uint64_t *ps, uint32_t n, double q) { + if (!m || !f || !arm || !ps || n == 0 || !(q > 0.0 && q < 1.0)) return 0.0; + double *r = malloc(sizeof(double) * n); + if (!r) return 0.0; + double saved = m->sd_scale; + m->sd_scale = 1.0; + uint32_t k = 0; + for (uint32_t i = 0; i < n; i++) { + RxCmPrediction p; + rx_cm_predict(m, &f[i], arm[i], &p); + if (!(p.known || p.borrowed) || ps[i] == 0 || !(p.sd_log2 > 0.0)) continue; + double z = fabs(log2((double)ps[i]) - p.mean_log2_ps) / p.sd_log2; + r[k++] = z / rx_cm_t_quantile(q, p.dof); + } + double scale = 0.0; + if (k >= 10) { + qsort(r, k, sizeof(double), cmp_double); + uint32_t idx = (uint32_t)ceil(q * (double)k) - 1u; + scale = r[idx < k ? idx : k - 1]; + if (scale < 1.0) scale = 1.0; + m->sd_scale = scale; + } else { + m->sd_scale = saved; + } + free(r); + return scale; +} + +void rx_cm_charge(RxCostModel *m, uint64_t work_ps, uint64_t explore_ps) { + m->work_ps += work_ps; + m->explore_ps += explore_ps; +} + +/* ---- canonical bytes ---- */ + +#define MAGIC 0x4d43474fu /* "OGCM" little-endian */ +#define VERSION 1u + +typedef struct { + uint32_t magic, version, n_arms, d, cells, arms_cap, cores; + uint32_t reserved; + double lambda; +} Header; + +static size_t body_size(void) { + const RxCostModel *m = 0; + return sizeof m->cell + sizeof m->power_mw + sizeof m->verify_ns + sizeof m->synth_ns + + sizeof m->code_bytes + sizeof m->sd_scale; +} + +size_t rx_cm_blob_size(void) { return sizeof(Header) + body_size() + 32u; } + +static size_t pack(const RxCostModel *m, uint8_t *out) { + Header h; + memset(&h, 0, sizeof h); + h.magic = MAGIC; + h.version = VERSION; + h.n_arms = m->n_arms; + h.d = RX_CM_D; + h.cells = RX_CM_CELLS; + h.arms_cap = RX_CM_ARMS; + h.cores = RX_CM_CORES; + h.lambda = m->lambda; + uint8_t *p = out; + memcpy(p, &h, sizeof h); + p += sizeof h; + memcpy(p, m->cell, sizeof m->cell); + p += sizeof m->cell; + memcpy(p, m->power_mw, sizeof m->power_mw); + p += sizeof m->power_mw; + memcpy(p, m->verify_ns, sizeof m->verify_ns); + p += sizeof m->verify_ns; + memcpy(p, m->synth_ns, sizeof m->synth_ns); + p += sizeof m->synth_ns; + memcpy(p, m->code_bytes, sizeof m->code_bytes); + p += sizeof m->code_bytes; + memcpy(p, &m->sd_scale, sizeof m->sd_scale); + p += sizeof m->sd_scale; + return (size_t)(p - out); +} + +int rx_cm_serialize(const RxCostModel *m, uint8_t *out, size_t cap, size_t *out_len) { + if (!m || !out || cap < rx_cm_blob_size()) return RX_CM_ERR_ARG; + size_t n = pack(m, out); + sha256_hash(out, n, out + n); + if (out_len) *out_len = n + 32u; + return RX_CM_OK; +} + +int rx_cm_deserialize(RxCostModel *m, const uint8_t *in, size_t len) { + if (!m || !in || len != rx_cm_blob_size()) return RX_CM_ERR_FORMAT; + uint8_t d[32]; + sha256_hash(in, len - 32u, d); + if (memcmp(d, in + len - 32u, 32) != 0) return RX_CM_ERR_DIGEST; + Header h; + memcpy(&h, in, sizeof h); + if (h.magic != MAGIC || h.version != VERSION || h.d != RX_CM_D || h.cells != RX_CM_CELLS || + h.arms_cap != RX_CM_ARMS || h.cores != RX_CM_CORES || h.n_arms == 0 || + h.n_arms > RX_CM_ARMS) + return RX_CM_ERR_FORMAT; + RxCostModel t; + memset(&t, 0, sizeof t); + t.n_arms = h.n_arms; + t.lambda = h.lambda; + const uint8_t *p = in + sizeof h; + memcpy(t.cell, p, sizeof t.cell); + p += sizeof t.cell; + memcpy(t.power_mw, p, sizeof t.power_mw); + p += sizeof t.power_mw; + memcpy(t.verify_ns, p, sizeof t.verify_ns); + p += sizeof t.verify_ns; + memcpy(t.synth_ns, p, sizeof t.synth_ns); + p += sizeof t.synth_ns; + memcpy(t.code_bytes, p, sizeof t.code_bytes); + p += sizeof t.code_bytes; + memcpy(&t.sd_scale, p, sizeof t.sd_scale); + if (!(t.sd_scale >= 1.0 && t.sd_scale < 1e6)) return RX_CM_ERR_FORMAT; + *m = t; + return RX_CM_OK; +} + +void rx_cm_digest(const RxCostModel *m, uint8_t out[32]) { + uint8_t buf[sizeof(Header) + sizeof(((RxCostModel *)0)->cell) + 4096]; + size_t n = pack(m, buf); + sha256_hash(buf, n, out); +} diff --git a/src/runtime/rx_costmodel.h b/src/runtime/rx_costmodel.h new file mode 100644 index 00000000..6e2c0db5 --- /dev/null +++ b/src/runtime/rx_costmodel.h @@ -0,0 +1,208 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * rx_costmodel.h -- Omega's empirical cost model (OMEGA_EMPIRICAL_OPTIMIZER). + * + * Omega picks a realization for an operation from what it has measured, not + * from a fixed rule. This is a calculator only: + * + * features of a call -> predicted cost of every eligible realization, + * with how sure the prediction is + * -> SELECT one, or MEASURE two when unsure + * + * It never runs code, never holds or reads authority, never writes a + * generation. Whoever calls it executes the realizations and reports what + * they cost. A learned model becomes durable only as the `model` blob of a + * generation promoted through the R9 barrier by a subject that holds the + * promotion right; the Makefile checks that rx_costmodel.o references none + * of those operations. + * + * This is not authority learning. No feature is derived from a capability, + * and a realization is eligible only if the caller says it was verified. + * + * Predictor: one Bayesian linear regression (normal-inverse-gamma, ridge + * prior) per (cell, arm), on log2 picoseconds per call. A cell is + * (operation class, core class, pressure bucket). Pressure cells hold a + * two-number correction over the quiet cell of the same core class. The basis is small and + * readable: 1, log2 M, log2 N, their product, (log2 N)^2, N <= 3, + * N % 4 != 0, and how far the working set overflows the core's cache, in + * doublings. Each (cell, arm) keeps only sufficient statistics, so the model + * serializes to a fixed-size blob. + */ +#ifndef RX_COSTMODEL_H +#define RX_COSTMODEL_H + +#include +#include + +#define RX_CM_ARMS 8u +#define RX_CM_OPS 1u /* matvec only today; widen with a second operation */ +#define RX_CM_CORES 3u +#define RX_CM_PRESSURE 3u +#define RX_CM_CELLS (RX_CM_OPS * RX_CM_CORES * RX_CM_PRESSURE) +#define RX_CM_D 8u /* basis size */ +#define RX_CM_TRI (RX_CM_D * (RX_CM_D + 1u) / 2u) +#define RX_CM_MIN_OBS 12u /* own-cell observations before a cell is used alone */ +#define RX_CM_RES_MIN_OBS 4u /* observations before a pressure correction is used */ +#define RX_CM_UNSEEN_SHIFT 1.0 /* prior sd, log2: an unseen pressure may halve or double cost */ + +#define RX_CM_OK 0 +#define RX_CM_ERR_ARG -1 +#define RX_CM_ERR_FORMAT -2 +#define RX_CM_ERR_DIGEST -3 +#define RX_CM_SKIPPED 1 /* observe: pressure data with no quiet model to correct */ +enum { RX_CM_OP_MATVEC = 0 }; +enum { RX_CM_CORE_X925 = 0, RX_CM_CORE_A725 = 1, RX_CM_CORE_OTHER = 2 }; + +/* What the caller knows about one call. Fields the matvec workload cannot + * vary are carried and recorded, never used to fit (see spec). */ +typedef struct { + uint32_t op; /* semantic operation class */ + uint32_t M, N; /* input shape; branch count is 0 for matvec */ + uint64_t state_bytes; /* working set: (M*N + N + M) * 8 */ + uint64_t cache_bytes; /* last private cache of this core class */ + uint32_t core; /* hardware state: core class */ + uint32_t pressure; /* resource pressure bucket, 0 = none */ + uint32_t thermal_c; /* thermal state, degrees C (recorded) */ + uint32_t current_arm; /* realization in use now */ + uint32_t eligible; /* bit per arm: verified and allowed here */ + uint32_t need_evidence; /* 1: only arms with a passing verdict (always 1 here) */ + uint32_t cognitive; /* cognitive requirement (recorded; 0 for matvec) */ + uint64_t latency_budget_ps; /* per call; 0 = none */ + uint64_t energy_budget_pj; /* per call; 0 = none */ +} RxCmFeatures; + +/* One measurement the caller made. */ +typedef struct { + uint64_t ps_per_call; /* latency */ + int failed; /* wrong result, crash or verification refusal */ +} RxCmObservation; + +typedef struct { + double mean_log2_ps; /* predicted log2 picoseconds per call */ + double sd_log2; /* predictive standard deviation, log2 units */ + double dof; /* degrees of freedom of the predictive t */ + uint32_t n; /* observations behind it */ + int known; /* its own cell has at least RX_CM_MIN_OBS observations */ + int borrowed; /* unseen pressure: the quiet prediction, widened */ + double energy_pj; /* predicted energy per call (0 if no power figure) */ + double fail_p; /* posterior mean failure probability */ +} RxCmPrediction; + +enum { RX_CM_SELECT = 1, RX_CM_MEASURE = 2, RX_CM_FALLBACK = 3 }; + +/* Why a MEASURE or FALLBACK was returned. */ +enum { + RX_CM_WHY_CONFIDENT = 0, + RX_CM_WHY_UNKNOWN_ARM, /* an eligible arm has too few observations here */ + RX_CM_WHY_WORTH_MEASURING, /* expected improvement from measuring exceeds ei_min */ + RX_CM_WHY_BUDGET_SPENT, /* would measure, but the exploration budget is spent */ + RX_CM_WHY_NO_ELIGIBLE, /* nothing but the fallback may run */ + RX_CM_WHY_OVER_BUDGET /* no arm is predicted to meet the latency/energy budget */ +}; + +typedef struct { + int action; + uint32_t arm; /* SELECT / FALLBACK: run this */ + uint32_t measure[2]; /* MEASURE: run both, keep the faster */ + uint32_t why; + double p_runner_up_better; /* P(competitor faster than the chosen arm) */ + double expected_improvement; /* E[max(0, log2 cost chosen - log2 cost competitor)] */ + RxCmPrediction pred[RX_CM_ARMS]; +} RxCmDecision; + +typedef struct { + /* Measure only when the expected improvement of the likeliest competitor + * over the chosen arm exceeds this, in log2 units (0.08 is about 6%). + * Two arms predicted equal and tight are not worth a run; an arm the + * model knows little about, or two arms it cannot separate, are. */ + double ei_min; + double max_fail_p; /* an arm that has failed and is above this is not selected */ + uint32_t fallback_arm; /* known-good: always verified, never removed */ + /* Exploration budget: extra time spent measuring may not exceed + * explore_frac of the time spent on chosen work plus explore_allow_ps. */ + double explore_frac; + uint64_t explore_allow_ps; +} RxCmPolicy; + +typedef struct { + double tri[RX_CM_TRI]; /* X'X + lambda I, upper triangle, row-major */ + double xty[RX_CM_D]; + double yty; + uint64_t n; + uint64_t trials, failures; /* failure probability: Beta(1 + f, 1 + t - f) */ +} RxCmArmStats; + +typedef struct { + uint32_t n_arms; + double lambda; /* ridge prior precision */ + RxCmArmStats cell[RX_CM_CELLS][RX_CM_ARMS]; + double power_mw[RX_CM_CORES][RX_CM_ARMS]; /* measured power while running, 0 = unknown */ + uint64_t verify_ns[RX_CM_ARMS]; /* verification cost */ + uint64_t synth_ns[RX_CM_ARMS]; /* synthesis cost; recompute = synth + verify */ + uint64_t code_bytes[RX_CM_ARMS]; /* memory held by the realization */ + /* Calibration: predictive spread is multiplied by this. Set from residuals + * on validation work measured apart from training (rx_cm_calibrate). */ + double sd_scale; + /* Runtime only; not serialized. */ + uint64_t explore_ps, work_ps; +} RxCostModel; + +void rx_cm_default_policy(RxCmPolicy *p); +void rx_cm_init(RxCostModel *m, uint32_t n_arms); + +/* Fold one measurement in. 0 on success; RX_CM_SKIPPED for a pressure + * measurement when there is no quiet model yet to correct. */ +int rx_cm_observe(RxCostModel *m, const RxCmFeatures *f, uint32_t arm, const RxCmObservation *o); + +/* Predict one arm. Needs RX_CM_MIN_OBS quiet observations for the core class. + * Under pressure, adds the learned correction once it has RX_CM_RES_MIN_OBS + * observations; before that, widens by RX_CM_UNSEEN_SHIFT and marks it + * borrowed. */ +void rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, RxCmPrediction *out); + +/* Decide. `allow_measure` 0 freezes the model's behaviour: it never asks to + * measure and picks the best prediction. The fallback arm is chosen when no + * other eligible arm has any prediction at all. */ +void rx_cm_decide(const RxCostModel *m, const RxCmPolicy *p, const RxCmFeatures *f, + int allow_measure, RxCmDecision *out); + +/* Split calibration. For n measurements taken apart from the training data, + * set sd_scale so that the central q interval covers a fraction q of them + * (never below 1). Returns the scale, or 0 if fewer than 10 were usable. */ +double rx_cm_calibrate(RxCostModel *m, const RxCmFeatures *f, const uint32_t *arm, + const uint64_t *ps, uint32_t n, double q); + +/* Account for time spent: chosen work, and extra time spent measuring. */ +void rx_cm_charge(RxCostModel *m, uint64_t work_ps, uint64_t explore_ps); + +/* Canonical bytes: header, the fixed-size body, SHA-256 of both. The size is + * rx_cm_blob_size(). Runtime counters are not included. */ +size_t rx_cm_blob_size(void); +int rx_cm_serialize(const RxCostModel *m, uint8_t *out, size_t cap, size_t *out_len); +int rx_cm_deserialize(RxCostModel *m, const uint8_t *in, size_t len); +void rx_cm_digest(const RxCostModel *m, uint8_t out[32]); + +/* Basis for these features, exposed so tests can see exactly what is fit. */ +void rx_cm_basis(const RxCmFeatures *f, double phi[RX_CM_D]); +uint32_t rx_cm_cell(const RxCmFeatures *f); + +/* Two-sided central interval half-width multiplier for coverage q with + * `dof` degrees of freedom (Student t, Cornish-Fisher approximation). */ +double rx_cm_t_quantile(double q, double dof); + +#endif /* RX_COSTMODEL_H */ diff --git a/tests/bw_warp_sim.h b/tests/bw_warp_sim.h index 8652710e..6d93828f 100644 --- a/tests/bw_warp_sim.h +++ b/tests/bw_warp_sim.h @@ -1,4 +1,19 @@ -/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). * * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an @@ -29,6 +44,8 @@ * an exit jumped past it, or a target was corrupted) * WS_E_NEVER_JOINED the warp finished with a barrier still armed * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS + * WS_E_IMADHI_RC IMAD.HI.U32 with a register Rc: the chip reads Rc as a 64-bit pair (omega #492, + * nvcc oracle, chip probe pending), so a non-RZ Rc is refused, not modelled * Every error names the pc. Memory: global addresses are host pointers (optionally range * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word * nobody wrote shows up in the oracle comparison. @@ -47,9 +64,7 @@ #include #define WS_WARP 32 -#ifndef WS_MAX_THREADS #define WS_MAX_THREADS 256 -#endif #define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP) #define WS_MAX_FRAGS 64 #define WS_NBAR BW_RECONV_MAX_BAR @@ -58,12 +73,12 @@ enum { WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, - WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS + WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS, WS_E_IMADHI_RC }; static const char *ws_err_name(int e) { static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", - "FRAGS", "BAR_TIMEOUT", "STEPS" }; + "FRAGS", "BAR_TIMEOUT", "STEPS", "IMADHI_RC" }; return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; } @@ -97,19 +112,11 @@ typedef struct { uint64_t splits; /* branches that split a fragment */ uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */ int max_frags, peak_bars; - /* developer bar D3 (docs/PAIN_POINTS.md): who faulted, and where. Set with err. */ - uint32_t cur_tid, cur_cx, cur_cy; /* the lane being executed */ - uint32_t err_tid, err_cx, err_cy; /* block (cx, cy) and thread of the first error */ - uint64_t err_addr; unsigned err_bytes; /* WS_E_MEM: the address and access size that missed */ - /* developer bar D12: the work done, counted per lane (accumulates across ws_run calls) */ - uint64_t gld_bytes, gst_bytes; /* global bytes loaded and stored */ - uint64_t fp_ops; /* FADD, FSUB, FMUL count 1; FFMA counts 2 */ } WsSim; static void ws_fail(WsSim *s, int err, int pc, const char *what) { if (s->err) return; s->err = err; s->err_pc = pc; - s->err_tid = s->cur_tid; s->err_cx = s->cur_cx; s->err_cy = s->cur_cy; snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : ""); } @@ -130,7 +137,6 @@ static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { if (s->nranges == 0) return 1; for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; - if (!s->err) { s->err_addr = a; s->err_bytes = bytes; } ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; } static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { @@ -142,7 +148,6 @@ static int ws_popc(uint32_t m) { return __builtin_popcount(m); } /* One lane, one non-control instruction. */ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { const BlackwellIRProgram *p = s->p; - s->cur_tid = tid; s->cur_cx = cx; s->cur_cy = cy; uint32_t a = ws_rds(l, in->src1_vreg, in->src1_subreg, p), b = ws_rds(l, in->src2_vreg, in->src2_subreg, p), c = ws_rds(l, in->src3_vreg, in->src3_subreg, p); switch (in->op) { case BW_IR_NOP: break; @@ -154,7 +159,9 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; case BW_IR_IMAD: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break; - case BW_IR_IMAD_HI_U32: ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IMAD_HI_U32: /* Rd = hi32(Ra * Rb) with Rc = RZ. The chip reads a register Rc as a 64-bit pair (omega #492): refused until a chip probe pins the model */ + if (in->src3_vreg >= 0) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with a register Rc (the chip reads Rc as a 64-bit pair, not chip-probed): use RZ, or SHF.R + IADD3"); return; } + ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32), p); break; case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break; case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wrs(l, in->dst_vreg, in->dst_subreg, a << b, p); break; @@ -162,17 +169,17 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_LOP3_XOR: ws_wrs(l, in->dst_vreg, in->dst_subreg, a ^ b, p); break; case BW_IR_LOP3_LUT: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; - case BW_IR_LDG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); s->gld_bytes += 4; break; } - case BW_IR_LDG_E_U16: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 2, pc)) return; uint16_t x; memcpy(&x, (const void *)(uintptr_t)ad, 2); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); s->gld_bytes += 2; break; } - case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); s->gst_bytes += 4; break; } - case BW_IR_LDG_E_64: case BW_IR_LDG_E_128: { unsigned n = in->op == BW_IR_LDG_E_64 ? 8u : 16u; uint64_t ad = ws_rd64(l, in->src1_vreg, p) + (uint64_t)(int64_t)(int32_t)in->imm; if (!ws_mem_ok(s, ad, n, pc)) return; if (ad & (n - 1)) { ws_fail(s, WS_E_MEM, pc, "wide global load not naturally aligned"); return; } uint32_t x[4]; memcpy(x, (const void *)(uintptr_t)ad, n); s->gld_bytes += n; for (unsigned i = 0; i < n / 4; i++) ws_wrs(l, in->dst_vreg, i, x[i], p); break; } + case BW_IR_LDG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; } + case BW_IR_LDG_E_U16: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 2, pc)) return; uint16_t x; memcpy(&x, (const void *)(uintptr_t)ad, 2); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; } + case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; } + case BW_IR_LDG_E_64: case BW_IR_LDG_E_128: { unsigned n = in->op == BW_IR_LDG_E_64 ? 8u : 16u; uint64_t ad = ws_rd64(l, in->src1_vreg, p) + (uint64_t)(int64_t)(int32_t)in->imm; if (!ws_mem_ok(s, ad, n, pc)) return; if (ad & (n - 1)) { ws_fail(s, WS_E_MEM, pc, "wide global load not naturally aligned"); return; } uint32_t x[4]; memcpy(x, (const void *)(uintptr_t)ad, n); for (unsigned i = 0; i < n / 4; i++) ws_wrs(l, in->dst_vreg, i, x[i], p); break; } case BW_IR_LDS128: { uint32_t ad = a + in->imm; if (ad & 15u) { ws_fail(s, WS_E_SMEM, pc, "LDS.128 address not 16-byte aligned"); return; } for (unsigned i = 0; i < 4; i++) { uint32_t *m = ws_smem(s, ad + 4u * i, pc); if (!m) return; ws_wrs(l, in->dst_vreg, i, *m, p); } break; } case BW_IR_LDS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; ws_wrs(l, in->dst_vreg, in->dst_subreg, *m, p); break; } case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } - case BW_IR_FADD: s->fp_ops++; ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; - case BW_IR_FSUB: s->fp_ops++; ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; - case BW_IR_FMUL: s->fp_ops++; ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; - case BW_IR_FFMA: s->fp_ops += 2; ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(fmaf(ws_u2f(a), ws_u2f(b), ws_u2f(c))), p); break; + case BW_IR_FADD: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; + case BW_IR_FSUB: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; + case BW_IR_FMUL: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; + case BW_IR_FFMA: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(fmaf(ws_u2f(a), ws_u2f(b), ws_u2f(c))), p); break; case BW_IR_FMNMX_MAX: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(fmaxf(ws_u2f(a), ws_u2f(b))), p); break; case BW_IR_FMNMX_MIN: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(fminf(ws_u2f(a), ws_u2f(b))), p); break; case BW_IR_MUFU_EX2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(exp2f(ws_u2f(a))), p); break; diff --git a/tests/bw_warp_sim_k.h b/tests/bw_warp_sim_k.h index 2b040ed7..99387564 100644 --- a/tests/bw_warp_sim_k.h +++ b/tests/bw_warp_sim_k.h @@ -1,4 +1,19 @@ -/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). * * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an @@ -29,6 +44,8 @@ * an exit jumped past it, or a target was corrupted) * WS_E_NEVER_JOINED the warp finished with a barrier still armed * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS + * WS_E_IMADHI_RC IMAD.HI.U32 with a register Rc: the chip reads Rc as a 64-bit pair (omega #492, + * nvcc oracle, chip probe pending), so a non-RZ Rc is refused, not modelled * Every error names the pc. Memory: global addresses are host pointers (optionally range * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word * nobody wrote shows up in the oracle comparison. @@ -62,12 +79,12 @@ enum { WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, - WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS + WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS, WS_E_IMADHI_RC }; static const char *ws_err_name(int e) { static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", - "FRAGS", "BAR_TIMEOUT", "STEPS" }; + "FRAGS", "BAR_TIMEOUT", "STEPS", "IMADHI_RC" }; return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; } @@ -149,7 +166,9 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; case BW_IR_IMAD: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break; - case BW_IR_IMAD_HI_U32: ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IMAD_HI_U32: /* Rd = hi32(Ra * Rb) with Rc = RZ. The chip reads a register Rc as a 64-bit pair (omega #492): refused until a chip probe pins the model */ + if (in->src3_vreg >= 0) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with a register Rc (the chip reads Rc as a 64-bit pair, not chip-probed): use RZ, or SHF.R + IADD3"); return; } + ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32), p); break; case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break; case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wrs(l, in->dst_vreg, in->dst_subreg, a << b, p); break; diff --git a/tests/bw_warp_sim_pa.h b/tests/bw_warp_sim_pa.h index cee1061b..157e396c 100644 --- a/tests/bw_warp_sim_pa.h +++ b/tests/bw_warp_sim_pa.h @@ -1,4 +1,19 @@ -/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). * * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an @@ -29,6 +44,8 @@ * an exit jumped past it, or a target was corrupted) * WS_E_NEVER_JOINED the warp finished with a barrier still armed * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS + * WS_E_IMADHI_RC IMAD.HI.U32 with a register Rc: the chip reads Rc as a 64-bit pair (omega #492, + * nvcc oracle, chip probe pending), so a non-RZ Rc is refused, not modelled * Every error names the pc. Memory: global addresses are host pointers (optionally range * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word * nobody wrote shows up in the oracle comparison. @@ -70,12 +87,12 @@ enum { WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, - WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS + WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS, WS_E_IMADHI_RC }; static const char *ws_err_name(int e) { static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", - "FRAGS", "BAR_TIMEOUT", "STEPS" }; + "FRAGS", "BAR_TIMEOUT", "STEPS", "IMADHI_RC" }; return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; } @@ -158,7 +175,9 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; case BW_IR_IMAD: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break; - case BW_IR_IMAD_HI_U32: ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IMAD_HI_U32: /* Rd = hi32(Ra * Rb) with Rc = RZ. The chip reads a register Rc as a 64-bit pair (omega #492): refused until a chip probe pins the model */ + if (in->src3_vreg >= 0) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with a register Rc (the chip reads Rc as a 64-bit pair, not chip-probed): use RZ, or SHF.R + IADD3"); return; } + ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32), p); break; case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break; case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wrs(l, in->dst_vreg, in->dst_subreg, a << b, p); break; diff --git a/tests/bw_warp_sim_tc.h b/tests/bw_warp_sim_tc.h index 16699a13..a08e38b6 100644 --- a/tests/bw_warp_sim_tc.h +++ b/tests/bw_warp_sim_tc.h @@ -1,4 +1,19 @@ -/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). * * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an @@ -29,6 +44,8 @@ * an exit jumped past it, or a target was corrupted) * WS_E_NEVER_JOINED the warp finished with a barrier still armed * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS + * WS_E_IMADHI_RC IMAD.HI.U32 with a register Rc: the chip reads Rc as a 64-bit pair (omega #492, + * nvcc oracle, chip probe pending), so a non-RZ Rc is refused, not modelled * Every error names the pc. Memory: global addresses are host pointers (optionally range * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word * nobody wrote shows up in the oracle comparison. @@ -71,12 +88,12 @@ static QmmaModel ws_qmma_model = { 32, 26, 0, 2, 0 }; /* chip-exact 262144/26214 enum { WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, - WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS + WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS, WS_E_IMADHI_RC }; static const char *ws_err_name(int e) { static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", - "FRAGS", "BAR_TIMEOUT", "STEPS" }; + "FRAGS", "BAR_TIMEOUT", "STEPS", "IMADHI_RC" }; return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; } @@ -167,7 +184,9 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; case BW_IR_IMAD: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break; - case BW_IR_IMAD_HI_U32: ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IMAD_HI_U32: /* Rd = hi32(Ra * Rb) with Rc = RZ. The chip reads a register Rc as a 64-bit pair (omega #492): refused until a chip probe pins the model */ + if (in->src3_vreg >= 0) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with a register Rc (the chip reads Rc as a 64-bit pair, not chip-probed): use RZ, or SHF.R + IADD3"); return; } + ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32), p); break; case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break; case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wrs(l, in->dst_vreg, in->dst_subreg, a << b, p); break; diff --git a/tests/dagsim/dagsim_unit.c b/tests/dagsim/dagsim_unit.c new file mode 100644 index 00000000..f9c6c089 --- /dev/null +++ b/tests/dagsim/dagsim_unit.c @@ -0,0 +1,502 @@ +/* + * dagsim_unit -- analytical tests for the offline resource-DAG evaluator + * (omega-mv#583 acceptance, first box). Any host, no GPU. + * + * Every case is a DAG whose answer is known by hand. The evaluator must + * produce exactly that number (to 1 ns), the same number twice, and never a + * makespan below its own bounds. + */ +#include "dagsim.h" +#include "runtime/rx_costmodel.h" + +#include +#include +#include +#include + +static int g_checks, g_fail; +#define CHECK(cond, ...) do { \ + g_checks++; \ + if (!(cond)) { \ + g_fail++; \ + fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); \ + fputc('\n', stderr); \ + } \ + } while (0) +#define NEAR(a, b, tol) (fabs((a) - (b)) <= (tol)) + +static const char *HDR = + "oev version=1\n" + "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" + "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" + "calibration id=c1 taken=2026-10-19 valid_days=30 load1=1.0 src=test\n" + "resource id=mem kind=capacity cap=1000 prov=specified src=test\n" + "energy instrument=none\n"; + +static int eval_text(const char *body, ds_model *m, ds_result *r, const ds_opts *o) { + char text[8192], err[256]; + snprintf(text, sizeof text, "%s%s", HDR, body); + int rc = ds_parse_text(text, m, err, sizeof err); + if (rc != DS_OK) { fprintf(stderr, " parse: %s\n", err); r->status = rc; return rc; } + ds_opts def; memset(&def, 0, sizeof def); + def.asof_day = ds_day_from_date("2026-10-20"); + def.interval_q = 0.9; + rc = ds_eval(m, o ? o : &def, r); + /* invariants every OK result must satisfy */ + if (rc == DS_OK) { + CHECK(r->makespan_ns + 1e-6 >= r->bound_ns, "makespan %.1f below bound %.1f", r->makespan_ns, r->bound_ns); + CHECK(r->makespan_ns + 1e-6 >= r->bound_critical_path_ns, "below critical path bound"); + double s = 0; for (int i = 0; i < DS_ATTR_N; i++) s += r->attr_ns[i]; + CHECK(NEAR(s, r->makespan_ns, 1e-6), "attribution %.3f does not sum to makespan %.3f", s, r->makespan_ns); + ds_result r2; ds_eval(m, o ? o : &def, &r2); + CHECK(memcmp(&r2, r, sizeof r2) == 0, "not deterministic"); + } + return rc; +} + +static void test_serial_chain(void) { + ds_model m; ds_result r; + int rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=20 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" + "dep A B\ndep B C\n", &m, &r, NULL); + CHECK(rc == DS_OK, "serial chain status %s: %s", ds_status_name(r.status), r.reason); + CHECK(NEAR(r.makespan_ns, 60, 1e-9), "serial makespan %.3f", r.makespan_ns); + CHECK(NEAR(r.bound_critical_path_ns, 60, 1e-9), "critical path bound %.3f", r.bound_critical_path_ns); + CHECK(r.n_critical == 3, "critical path length %d", r.n_critical); + CHECK(NEAR(r.attr_ns[DS_ATTR_COMPUTE], 60, 1e-9), "compute attribution %.3f", r.attr_ns[DS_ATTR_COMPUTE]); + CHECK(NEAR(r.attr_ns[DS_ATTR_RESIDUAL], 0, 1e-9), "residual %.3f", r.attr_ns[DS_ATTR_RESIDUAL]); + CHECK(NEAR(r.start_ns[2], 30, 1e-9) && NEAR(r.end_ns[2], 60, 1e-9), "C window"); + CHECK(r.prov_mask == (1u << DS_PROV_MEASURED | 1u << DS_PROV_SPECIFIED), "prov mask %x", r.prov_mask); + CHECK(r.n_ops == 3 && NEAR(r.total_busy_ns, 60, 1e-9), "conservation"); +} + +static void test_perfect_overlap(void) { + ds_model m; ds_result r; + int rc = eval_text( + "resource id=cpu kind=compute slots=4 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=20 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK, "overlap status %s", ds_status_name(r.status)); + CHECK(NEAR(r.makespan_ns, 30, 1e-9), "independent overlap makespan %.3f", r.makespan_ns); + CHECK(NEAR(r.attr_ns[DS_ATTR_SERIALIZATION], 0, 1e-9), "no serialization when slots suffice"); + /* same DAG, one slot: work bound 60 binds and the wait is serialization */ + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=20 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK, "one slot status"); + CHECK(NEAR(r.makespan_ns, 60, 1e-9), "one-slot makespan %.3f", r.makespan_ns); + CHECK(NEAR(r.bound_critical_path_ns, 30, 1e-9), "critical path stays 30"); + CHECK(NEAR(r.bound_ns, 60, 1e-9), "resource bound 60, got %.3f", r.bound_ns); + CHECK(NEAR(r.attr_ns[DS_ATTR_SERIALIZATION], 30, 1e-9), "serialization %.3f", r.attr_ns[DS_ATTR_SERIALIZATION]); +} + +static void test_shared_bandwidth(void) { + ds_model m; ds_result r; + /* two 1 GB copies on a 1 GB/s pipe, both slots free: each gets half, both end at 2 s */ + int rc = eval_text( + "resource id=cpu kind=compute slots=2 prov=specified src=test\n" + "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" + "op id=A kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" + "op id=B kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK, "bw status %s: %s", ds_status_name(r.status), r.reason); + CHECK(NEAR(r.makespan_ns, 2e9, 1e-3), "two concurrent full-bandwidth claims must not sum: %.0f", r.makespan_ns); + CHECK(NEAR(r.end_ns[0], 2e9, 1e-3) && NEAR(r.end_ns[1], 2e9, 1e-3), "both end at 2 s"); + CHECK(NEAR(r.op_contention_ns[0], 1e9, 1e-3), "A contention %.0f", r.op_contention_ns[0]); + CHECK(NEAR(r.bound_critical_path_ns, 1e9, 1e-3), "cp bound 1 s"); + CHECK(NEAR(r.bound_ns, 2e9, 1e-3), "pipe work bound 2 s, got %.0f", r.bound_ns); + CHECK(NEAR(r.attr_ns[DS_ATTR_BANDWIDTH] + r.attr_ns[DS_ATTR_CONTENTION], 2e9, 1e-3), "bw+contention = 2 s"); + CHECK(NEAR(r.attr_ns[DS_ATTR_CONTENTION], 1e9, 1e-3), "contention attributed 1 s, got %.0f", r.attr_ns[DS_ATTR_CONTENTION]); + /* unequal: B is half the size; it finishes at 1 s, A at 1.5 s */ + rc = eval_text( + "resource id=cpu kind=compute slots=2 prov=specified src=test\n" + "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" + "op id=A kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" + "op id=B kind=copy engine=cpu bytes=5e8 bw=pipe prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK, "bw2 status"); + CHECK(NEAR(r.end_ns[1], 1e9, 1e-3), "B ends at 1 s, got %.0f", r.end_ns[1]); + CHECK(NEAR(r.end_ns[0], 1.5e9, 1e-3), "A ends at 1.5 s, got %.0f", r.end_ns[0]); + /* compute and traffic inside one op overlap: floor is max(F/P, Q/B) */ + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" + "op id=A kind=compute engine=cpu t_ns=5e8 bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK && NEAR(r.makespan_ns, 1e9, 1e-3), "max(F/P,Q/B) floor, got %.0f", r.makespan_ns); + CHECK(NEAR(r.op_floor_ns[0], 1e9, 1e-3), "op floor"); + /* a compute-bound op streaming alone has no contention, whatever its busy time */ + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" + "op id=A kind=compute engine=cpu t_ns=4e9 bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK && NEAR(r.makespan_ns, 4e9, 1e-3) && NEAR(r.op_contention_ns[0], 0, 1e-3), + "compute-bound op alone: contention %.0f (want 0)", r.op_contention_ns[0]); + CHECK(NEAR(r.attr_ns[DS_ATTR_COMPUTE], 4e9, 1e-3), "attributed as compute"); + /* serialized sharing: one slot, so no contention and makespan = sum */ + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" + "op id=A kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" + "op id=B kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK && NEAR(r.makespan_ns, 2e9, 1e-3) && NEAR(r.attr_ns[DS_ATTR_CONTENTION], 0, 1e-3), + "serialized copies: no contention, makespan %.0f contention %.0f", r.makespan_ns, r.attr_ns[DS_ATTR_CONTENTION]); +} + +static void test_capacity_refusal(void) { + ds_model m; ds_result r; + int rc = eval_text( + "resource id=cpu kind=compute slots=2 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "buffer id=x bytes=600 cap=mem first=A last=A\n" + "buffer id=y bytes=600 cap=mem first=A last=A\n", &m, &r, NULL); + CHECK(rc == DS_REFUSED_CAPACITY, "1200 live bytes in 1000 must refuse, got %s", ds_status_name(rc)); + CHECK(strstr(r.reason, "x") && strstr(r.reason, "y"), "reason names the buffers: %s", r.reason); + /* same buffers, disjoint lifetimes: fits */ + rc = eval_text( + "resource id=cpu kind=compute slots=2 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "dep A B\n" + "buffer id=x bytes=600 cap=mem first=A last=A\n" + "buffer id=y bytes=600 cap=mem first=B last=B\n", &m, &r, NULL); + CHECK(rc == DS_OK, "disjoint lifetimes fit, got %s: %s", ds_status_name(rc), r.reason); + CHECK(NEAR(r.peak_live_bytes[0], 600, 1e-9), "peak 600, got %.0f", r.peak_live_bytes[0]); + /* a single buffer larger than the ledger refuses before any scheduling */ + rc = eval_text( + "resource id=cpu kind=compute slots=2 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "buffer id=big bytes=2000 cap=mem\n", &m, &r, NULL); + CHECK(rc == DS_REFUSED_CAPACITY, "oversized resident buffer refuses"); +} + +static void test_lifetimes(void) { + ds_model m; ds_result r; + int rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "dep A B\ndep B C\n" + "buffer id=resident bytes=100 cap=mem\n" + "buffer id=x bytes=300 cap=mem first=A last=B\n" + "buffer id=y bytes=400 cap=mem first=B last=C\n", &m, &r, NULL); + CHECK(rc == DS_OK, "lifetimes status %s: %s", ds_status_name(rc), r.reason); + CHECK(NEAR(r.peak_live_bytes[0], 800, 1e-9), "peak = resident + x + y during B, got %.0f", r.peak_live_bytes[0]); + CHECK(NEAR(r.peak_live_at_ns[0], 10, 1e-9), "peak reached when B starts, got %.1f", r.peak_live_at_ns[0]); +} + +static void test_sync(void) { + ds_model m; ds_result r; + int rc = eval_text( + "resource id=cpu kind=compute slots=2 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=30 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=5 prov=measured src=t\n" + "sync id=s ops=A,B\n" + "dep s C\n", &m, &r, NULL); + CHECK(rc == DS_OK, "sync status %s: %s", ds_status_name(rc), r.reason); + int c = ds_find_op(&m, "C"); + CHECK(c >= 0 && NEAR(r.start_ns[c], 30, 1e-9), "C waits for the slower of A,B: start %.1f", c >= 0 ? r.start_ns[c] : -1); + CHECK(NEAR(r.makespan_ns, 35, 1e-9), "sync makespan %.1f", r.makespan_ns); + /* a barrier with a declared cost is attributed as sync */ + rc = eval_text( + "resource id=cpu kind=compute slots=2 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=30 prov=measured src=t\n" + "op id=fence kind=barrier t_ns=7 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=5 prov=measured src=t\n" + "dep A fence\ndep B fence\ndep fence C\n", &m, &r, NULL); + CHECK(rc == DS_OK && NEAR(r.makespan_ns, 42, 1e-9), "costed barrier makespan %.1f", r.makespan_ns); + CHECK(NEAR(r.attr_ns[DS_ATTR_SYNC], 7, 1e-9), "sync attribution %.1f", r.attr_ns[DS_ATTR_SYNC]); +} + +static void test_launch(void) { + ds_model m; ds_result r; + /* two jobs, one launcher, one slot: launches serialize, 80 ms */ + int rc = eval_text( + "resource id=gpu kind=compute slots=1 prov=specified src=test\n" + "resource id=L kind=launch prov=measured src=t\n" + "op id=A kind=compute engine=gpu t_ns=0 launch_ns=40e6 launcher=L prov=measured src=t\n" + "op id=B kind=compute engine=gpu t_ns=0 launch_ns=40e6 launcher=L prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK && NEAR(r.makespan_ns, 80e6, 1e-3), "serialized launches add: %.0f", r.makespan_ns); + CHECK(NEAR(r.attr_ns[DS_ATTR_LAUNCH] + r.attr_ns[DS_ATTR_SERIALIZATION], 80e6, 1e-3), "launch+serialization"); + /* two engines, two launchers: launches overlap, 40 ms */ + rc = eval_text( + "resource id=gpu kind=compute slots=1 prov=specified src=test\n" + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "resource id=L1 kind=launch prov=measured src=t\n" + "resource id=L2 kind=launch prov=measured src=t\n" + "op id=A kind=compute engine=gpu t_ns=0 launch_ns=40e6 launcher=L1 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=0 launch_ns=40e6 launcher=L2 prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK && NEAR(r.makespan_ns, 40e6, 1e-3), "independent launches overlap: %.0f", r.makespan_ns); +} + +static void test_calibration(void) { + ds_model m; ds_result r; char text[4096], err[256]; + const char *body = + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n"; + /* stale: asof past taken + valid_days */ + ds_opts o; memset(&o, 0, sizeof o); o.interval_q = 0.9; + o.asof_day = ds_day_from_date("2026-11-19"); + int rc = eval_text(body, &m, &r, &o); + CHECK(rc == DS_REFUSED_STALE, "31 days after a 30-day calibration refuses, got %s", ds_status_name(rc)); + o.asof_day = ds_day_from_date("2026-11-18"); + rc = eval_text(body, &m, &r, &o); + CHECK(rc == DS_OK, "last valid day is fine, got %s", ds_status_name(rc)); + /* missing: measured value without a calibration record */ + snprintf(text, sizeof text, + "oev version=1\n" + "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" + "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" + "energy instrument=none\n%s", body); + rc = ds_parse_text(text, &m, err, sizeof err); + CHECK(rc == DS_OK, "parse without calibration: %s", err); + o.asof_day = ds_day_from_date("2026-10-20"); + rc = ds_eval(&m, &o, &r); + CHECK(rc == DS_REFUSED_NO_CALIBRATION, "measured without calibration refuses, got %s", ds_status_name(rc)); + /* unknown provenance never becomes a number */ + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu prov=unknown src=arch-244-not-run\n" + "dep A B\n", &m, &r, NULL); + CHECK(rc == DS_REFUSED_UNKNOWN, "unknown op refuses, got %s", ds_status_name(rc)); + CHECK(strstr(r.reason, "B") != NULL, "reason names the op: %s", r.reason); + CHECK(r.makespan_ns == 0, "no makespan on refusal"); + /* synthetic input marks the result */ + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=synthetic src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK && r.synthetic == 1, "synthetic flag"); +} + +static void test_intervals(void) { + ds_model m; ds_result r; double lo, hi; + char text[8192]; + snprintf(text, sizeof text, "%s%s", HDR, + "resource id=cpu kind=compute slots=2 prov=specified src=test\n" + "resource id=pipe kind=bandwidth cap=1e9 lo=0.5e9 hi=2e9 prov=measured src=t\n" + "op id=A kind=compute engine=cpu t_ns=100 lo=80 hi=150 prov=measured src=t\n" + "op id=B kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" + "dep A B\n"); + char err[256]; + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); + ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; + int rc = ds_eval_interval(&m, &o, &r, &lo, &hi); + CHECK(rc == DS_OK, "interval status"); + CHECK(NEAR(r.makespan_ns, 100 + 1e9, 1e-3), "nominal %.0f", r.makespan_ns); + CHECK(NEAR(lo, 80 + 0.5e9, 1e-3), "optimistic %.0f", lo); + CHECK(NEAR(hi, 150 + 2e9, 1e-3), "pessimistic %.0f", hi); +} + +static void test_energy(void) { + ds_model m; ds_result r; char text[4096], err[256]; + int rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=1e9 prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK && !r.energy_measured && !r.energy_synthetic, "instrument=none means UNMEASURED"); + snprintf(text, sizeof text, + "oev version=1\n" + "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" + "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" + "calibration id=c1 taken=2026-10-19 valid_days=30 load1=1.0 src=test\n" + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=1e9 prov=measured src=t\n" + "energy instrument=tdp-guess boundary=soc power_w=100 prov=synthetic\n"); + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); + ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; + rc = ds_eval(&m, &o, &r); + CHECK(rc == DS_OK && r.energy_synthetic == 1 && !r.energy_measured && NEAR(r.energy_j, 100.0, 1e-9) && r.synthetic, + "synthetic power gives a labelled E=P*T (100 J), got %.3f syn=%d", r.energy_j, r.energy_synthetic); + snprintf(text, sizeof text, + "oev version=1\n" + "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" + "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" + "calibration id=c1 taken=2026-10-19 valid_days=30 load1=1.0 src=test\n" + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=1e9 prov=measured src=t\n" + "energy instrument=cyberpower-ups-nut boundary=wall joules=42.5 prov=measured src=receipt.json\n"); + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); + rc = ds_eval(&m, &o, &r); + CHECK(rc == DS_OK && r.energy_measured == 1 && NEAR(r.energy_j, 42.5, 1e-9) && !r.synthetic, "measured joules pass through"); + /* a measured joules figure with no instrument name is a parse error */ + snprintf(text, sizeof text, + "oev version=1\n" + "machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test\n" + "build repo=omega-mv sha=0000000000000000000000000000000000000000\n" + "energy instrument=none joules=42.5 prov=measured src=x\n"); + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_PARSE_ERROR, "joules without an instrument is refused at parse"); +} + +static void test_parse_and_cycles(void) { + ds_model m; ds_result r; char text[4096], err[256]; + snprintf(text, sizeof text, "%s%s", HDR, "resource id=cpu kind=compute slots=1 prov=specified src=test\nbogus x=1\n"); + int rc = ds_parse_text(text, &m, err, sizeof err); + CHECK(rc == DS_PARSE_ERROR && strstr(err, "line 8"), "unknown keyword is an error with its line: %s", err); + snprintf(text, sizeof text, "%s%s", HDR, "resource id=cpu kind=compute slots=1 prov=specified src=test\nop id=A kind=compute engine=cpu t_ns=1 prov=measured src=t typo=3\n"); + rc = ds_parse_text(text, &m, err, sizeof err); + CHECK(rc == DS_PARSE_ERROR, "unknown field is an error: %s", err); + snprintf(text, sizeof text, "%s%s", HDR, "resource id=cpu kind=compute slots=1 prov=specified src=test\nop id=A kind=compute engine=cpu t_ns=1 src=t\n"); + rc = ds_parse_text(text, &m, err, sizeof err); + CHECK(rc == DS_PARSE_ERROR, "a number without prov is an error: %s", err); + rc = eval_text( + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=1 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=1 prov=measured src=t\n" + "dep A B\ndep B A\n", &m, &r, NULL); + CHECK(rc == DS_REFUSED_CYCLE, "cycle refuses, got %s", ds_status_name(rc)); + /* the pinned fixture parses and evaluates */ + rc = ds_parse_file("tests/dagsim/fixtures/m1_spark_subset.oev", &m, err, sizeof err); + CHECK(rc == DS_OK, "fixture parses: %s", err); + if (rc == DS_OK) { + ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; + rc = ds_eval(&m, &o, &r); + CHECK(rc == DS_OK, "fixture evaluates: %s", r.reason); + CHECK(NEAR(r.makespan_ns, 20000 + 40736700 + 111000000, 1e-3), "fixture serial makespan %.0f", r.makespan_ns); + CHECK(!r.energy_measured && !r.energy_synthetic, "fixture energy UNMEASURED"); + } +} + +/* rx_costmodel integration: a matvec op with a fitted blob gets its time from + * rx_cm_predict; an unknown cell refuses. Synthetic observations, so the blob is + * labelled fitted on synthetic data and the result is marked synthetic. */ +static void test_costmodel_reuse(void) { + RxCostModel cm; rx_cm_init(&cm, 2); + uint64_t rng = 0x9E3779B97F4A7C15ull; + for (int i = 0; i < 200; i++) { + rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; + uint32_t M = 8 + (uint32_t)(rng % 120), N = 4 + (uint32_t)((rng >> 20) % 60); + RxCmFeatures f; memset(&f, 0, sizeof f); + f.op = RX_CM_OP_MATVEC; f.M = M; f.N = N; f.state_bytes = ((uint64_t)M * N + M + N) * 8u; + f.cache_bytes = 1u << 20; f.core = RX_CM_CORE_X925; f.eligible = 3; + double l2 = 10.0 + 0.95 * (log2((double)M) + log2((double)N)); + RxCmObservation o; o.failed = 0; o.ps_per_call = (uint64_t)pow(2.0, l2); + rx_cm_observe(&cm, &f, 0, &o); + } + ds_model m; ds_result r; char text[4096], err[256]; + snprintf(text, sizeof text, "%s%s", HDR, + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=mv kind=matvec engine=cpu M=64 N=16 core=0 arm=0 prov=fitted src=rx_costmodel\n"); + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); + ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; + int rc = ds_eval(&m, &o, &r); + CHECK(rc == DS_REFUSED_UNKNOWN, "matvec with no model refuses, got %s", ds_status_name(rc)); + o.costmodel = &cm; + rc = ds_eval(&m, &o, &r); + CHECK(rc == DS_OK, "matvec with model: %s %s", ds_status_name(rc), r.reason); + RxCmFeatures f; memset(&f, 0, sizeof f); + f.op = RX_CM_OP_MATVEC; f.M = 64; f.N = 16; f.state_bytes = (64u * 16u + 64u + 16u) * 8u; f.cache_bytes = 1u << 20; f.core = 0; + RxCmPrediction p; rx_cm_predict(&cm, &f, 0, &p); + CHECK(p.known, "cell known"); + CHECK(NEAR(r.makespan_ns, pow(2.0, p.mean_log2_ps) / 1000.0, 1e-6), "time comes from rx_cm_predict: %.3f vs %.3f", r.makespan_ns, pow(2.0, p.mean_log2_ps) / 1000.0); + CHECK(r.prov_mask & (1u << DS_PROV_FITTED), "fitted provenance recorded"); + /* arm 1 was never observed: unknown, refuse */ + snprintf(text, sizeof text, "%s%s", HDR, + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=mv kind=matvec engine=cpu M=64 N=16 core=0 arm=1 prov=fitted src=rx_costmodel\n"); + CHECK(ds_parse_text(text, &m, err, sizeof err) == DS_OK, "parse: %s", err); + rc = ds_eval(&m, &o, &r); + CHECK(rc == DS_REFUSED_UNKNOWN, "unobserved arm refuses, got %s", ds_status_name(rc)); +} + +static void test_counterfactual(void) { + ds_model m; ds_result r; + int rc = eval_text( + "resource id=cpu kind=compute slots=2 prov=specified src=test\n" + "resource id=pipe kind=bandwidth cap=1e9 prov=measured src=t\n" + "op id=A kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n" + "op id=B kind=copy engine=cpu bytes=1e9 bw=pipe prov=measured src=t\n", &m, &r, NULL); + CHECK(rc == DS_OK, "cf status"); + ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; + double d = ds_counterfactual(&m, &o, ds_find_res(&m, "pipe")); + CHECK(NEAR(d, 1e9, 1e-3), "doubling the pipe halves the bandwidth-bound DAG: %.0f", d); + d = ds_counterfactual(&m, &o, ds_find_res(&m, "cpu")); + CHECK(NEAR(d, 2e9, 1e-3), "doubling slots changes nothing when the pipe binds: %.0f", d); +} + +static void test_minimum_work_diff(void) { + ds_model base, tr; ds_diff d; char text[8192], err[256]; + const char *base_body = + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B kind=compute engine=cpu t_ns=20 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" + "dep A B\ndep B C\n"; + snprintf(text, sizeof text, "%s%s", HDR, base_body); + CHECK(ds_parse_text(text, &base, err, sizeof err) == DS_OK, "base: %s", err); + ds_opts o; memset(&o, 0, sizeof o); o.asof_day = ds_day_from_date("2026-10-20"); o.interval_q = 0.9; + /* no change: identical transformed DAG adds no nodes and removes none */ + CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr: %s", err); + CHECK(ds_diff_models(&base, &tr, &o, &d) == DS_OK && d.n_removed == 0 && d.n_added == 0 && d.n_kept == 3, "no-change fixture: removed %d added %d kept %d", d.n_removed, d.n_added, d.n_kept); + /* B reused from a lookup: valid, 20 ns of work moves to the reused ledger */ + snprintf(text, sizeof text, "%s%s", HDR, + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=B_lookup kind=compute engine=cpu t_ns=2 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" + "dep A B_lookup\ndep B_lookup C\n" + "removed op=B reason=reused ref=B_lookup proof=rx_plan:digest\n"); + CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr2: %s", err); + int rc = ds_diff_models(&base, &tr, &o, &d); + CHECK(rc == DS_OK && NEAR(d.removed_ns[DS_RM_REUSED], 20, 1e-9) && d.n_added == 1, "reuse accounted: %s rc=%s", d.reason, ds_status_name(rc)); + /* eliminated with no proof while C still consumed it: required work cannot vanish by assertion */ + snprintf(text, sizeof text, "%s%s", HDR, + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" + "dep A C\n" + "removed op=B reason=eliminated\n"); + CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr3: %s", err); + rc = ds_diff_models(&base, &tr, &o, &d); + CHECK(rc == DS_REFUSED_REMOVED_REQUIRED && d.required_removed, "unproven elimination of consumed work refuses, got %s", ds_status_name(rc)); + /* unchanged_dep on A, but A changed: the consumer is invalidated */ + snprintf(text, sizeof text, "%s%s", HDR, + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" + "dep A C\n" + "removed op=B reason=unchanged_dep ref=A\n"); + CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr4: %s", err); + rc = ds_diff_models(&base, &tr, &o, &d); + CHECK(rc == DS_OK && NEAR(d.removed_ns[DS_RM_UNCHANGED_DEP], 20, 1e-9), "unchanged dependency allows skipping B"); + o.changed = "A"; + rc = ds_diff_models(&base, &tr, &o, &d); + CHECK(rc == DS_REFUSED_REMOVED_REQUIRED, "changed A invalidates the skip of B, got %s", ds_status_name(rc)); + o.changed = NULL; + /* shared work counted twice is detected */ + snprintf(text, sizeof text, "%s%s", HDR, + "resource id=cpu kind=compute slots=1 prov=specified src=test\n" + "op id=A kind=compute engine=cpu t_ns=10 prov=measured src=t\n" + "op id=C kind=compute engine=cpu t_ns=30 prov=measured src=t\n" + "dep A C\n" + "removed op=B reason=shared ref=A\n" + "removed op=B reason=shared ref=C\n"); + CHECK(ds_parse_text(text, &tr, err, sizeof err) == DS_OK, "tr5: %s", err); + rc = ds_diff_models(&base, &tr, &o, &d); + CHECK(rc != DS_OK && d.double_counted, "double-counted shared work detected"); +} + +int main(void) { + test_serial_chain(); + test_perfect_overlap(); + test_shared_bandwidth(); + test_capacity_refusal(); + test_lifetimes(); + test_sync(); + test_launch(); + test_calibration(); + test_intervals(); + test_energy(); + test_parse_and_cycles(); + test_costmodel_reuse(); + test_counterfactual(); + test_minimum_work_diff(); + printf("dagsim_unit: %d checks, %d failed\n", g_checks, g_fail); + return g_fail ? 1 : 0; +} diff --git a/tests/dagsim/fixtures/m1_spark_overlap.oev b/tests/dagsim/fixtures/m1_spark_overlap.oev new file mode 100644 index 00000000..249980f1 --- /dev/null +++ b/tests/dagsim/fixtures/m1_spark_overlap.oev @@ -0,0 +1,34 @@ +# OEV1 fixture: Spark M1 hardware passport subset (omega-mv#578), pinned 2026-10-19. +# Every number below is a loaded-machine measurement (load1 15 to 29) or a datasheet +# ceiling, and says which. Nothing here was produced by running a GPU in this harness. +oev version=1 +machine id=spark-b87b soc=GB10 cores=20 mem_bytes=137438953472 prov=specified src=nvidia-dgx-spark-datasheet +build repo=omega-mv sha=f4a9849f00000000000000000000000000000000 physics=6d7cf0d4 +calibration id=M1-20261019 taken=2026-10-19 valid_days=30 load1=24.2 src=~/yardsticks/aien-model/M1/ + +# Resources. One capacity ledger for CPU and GPU (unified memory). +resource id=cpu kind=compute slots=20 prov=specified src=nproc +resource id=gpu kind=compute slots=1 prov=specified src=one-job-at-a-time-engine +resource id=dram_spec kind=bandwidth cap=273e9 prov=specified src=nvidia-dgx-spark-datasheet +resource id=dram_gpu kind=bandwidth cap=218.6e9 lo=218.6e9 hi=219e9 prov=measured src=handoff-2026-10-19-qwen38-decode-speed.md:calc:e726ff77 +resource id=dram_cpu kind=bandwidth cap=65.4e9 lo=60.8e9 hi=69.8e9 prov=measured src=M1/block4.csv:gb=1,threads=8 +resource id=unified_mem kind=capacity cap=137438953472 prov=specified src=nvidia-dgx-spark-datasheet +resource id=gpu_launch kind=launch prov=measured src=M1/block2.csv + +# Ops. Same work as m1_spark_subset.oev, but the 1 GB copy is split into two +# concurrent halves on two CPU slots. They share dram_cpu (processor sharing). +# At two threads the per-thread busy time (55.5 ms) still binds and the shared +# pipe does not (2 GB of traffic at 65.4 GB/s is 30.6 ms), which is what block4 +# shows at 4 threads (50 to 61 GB/s). More halves would hit the pipe. +op id=realize kind=compute engine=cpu t_ns=20000 lo=19400 hi=22800 prov=measured src=M1/block1.csv:path_ns_per_call +op id=gpu_job kind=compute engine=gpu t_ns=0 prov=measured src=M1/block2.csv:vecadd_65 launch_ns=40736700 launcher=gpu_launch +op id=copyA kind=copy engine=cpu t_ns=55500000 lo=53950000 hi=58300000 prov=measured src=M1/block4.csv:gb=1,threads=1,half bytes=1000000000 bw=dram_cpu +op id=copyB kind=copy engine=cpu t_ns=55500000 lo=53950000 hi=58300000 prov=measured src=M1/block4.csv:gb=1,threads=1,half bytes=1000000000 bw=dram_cpu +dep realize gpu_job +dep gpu_job copyA +dep gpu_job copyB + +buffer id=src1g bytes=1000000000 cap=unified_mem first=copyA last=copyB +buffer id=dst1g bytes=1000000000 cap=unified_mem first=copyA last=copyB + +energy instrument=none diff --git a/tests/dagsim/fixtures/m1_spark_subset.oev b/tests/dagsim/fixtures/m1_spark_subset.oev new file mode 100644 index 00000000..34fdf736 --- /dev/null +++ b/tests/dagsim/fixtures/m1_spark_subset.oev @@ -0,0 +1,32 @@ +# OEV1 fixture: Spark M1 hardware passport subset (omega-mv#578), pinned 2026-10-19. +# Every number below is a loaded-machine measurement (load1 15 to 29) or a datasheet +# ceiling, and says which. Nothing here was produced by running a GPU in this harness. +oev version=1 +machine id=spark-b87b soc=GB10 cores=20 mem_bytes=137438953472 prov=specified src=nvidia-dgx-spark-datasheet +build repo=omega-mv sha=f4a9849f00000000000000000000000000000000 physics=6d7cf0d4 +calibration id=M1-20261019 taken=2026-10-19 valid_days=30 load1=24.2 src=~/yardsticks/aien-model/M1/ + +# Resources. One capacity ledger for CPU and GPU (unified memory). +resource id=cpu kind=compute slots=20 prov=specified src=nproc +resource id=gpu kind=compute slots=1 prov=specified src=one-job-at-a-time-engine +resource id=dram_spec kind=bandwidth cap=273e9 prov=specified src=nvidia-dgx-spark-datasheet +resource id=dram_gpu kind=bandwidth cap=218.6e9 lo=218.6e9 hi=219e9 prov=measured src=handoff-2026-10-19-qwen38-decode-speed.md:calc:e726ff77 +resource id=dram_cpu kind=bandwidth cap=65.4e9 lo=60.8e9 hi=69.8e9 prov=measured src=M1/block4.csv:gb=1,threads=8 +resource id=unified_mem kind=capacity cap=137438953472 prov=specified src=nvidia-dgx-spark-datasheet +resource id=gpu_launch kind=launch prov=measured src=M1/block2.csv + +# Ops. A CPU realization call (block1 path) feeding one GPU job (block2) whose +# fixed submit lifecycle dominates, then a CPU memcpy of 1 GB (block4, 1 thread). +op id=realize kind=compute engine=cpu t_ns=20000 lo=19400 hi=22800 prov=measured src=M1/block1.csv:path_ns_per_call +op id=gpu_job kind=compute engine=gpu t_ns=0 prov=measured src=M1/block2.csv:vecadd_65 launch_ns=40736700 launcher=gpu_launch +op id=copy1g kind=copy engine=cpu t_ns=111000000 lo=107900000 hi=116600000 prov=measured src=M1/block4.csv:gb=1,threads=1 bytes=2000000000 bw=dram_cpu +dep realize gpu_job +dep gpu_job copy1g + +# Buffers. +buffer id=src1g bytes=1000000000 cap=unified_mem first=copy1g last=copy1g +buffer id=dst1g bytes=1000000000 cap=unified_mem first=copy1g last=copy1g + +# Energy: no wall instrument on this box; nvidia-smi is GPU module only and was not +# integrated over this DAG. UNMEASURED. +energy instrument=none diff --git a/tests/dagsim/fixtures/m1_synthetic_power.oev b/tests/dagsim/fixtures/m1_synthetic_power.oev new file mode 100644 index 00000000..3087178e --- /dev/null +++ b/tests/dagsim/fixtures/m1_synthetic_power.oev @@ -0,0 +1,31 @@ +# OEV1 fixture: Spark M1 hardware passport subset (omega-mv#578), pinned 2026-10-19. +# Every number below is a loaded-machine measurement (load1 15 to 29) or a datasheet +# ceiling, and says which. Nothing here was produced by running a GPU in this harness. +oev version=1 +machine id=spark-b87b soc=GB10 cores=20 mem_bytes=137438953472 prov=specified src=nvidia-dgx-spark-datasheet +build repo=omega-mv sha=f4a9849f00000000000000000000000000000000 physics=6d7cf0d4 +calibration id=M1-20261019 taken=2026-10-19 valid_days=30 load1=24.2 src=~/yardsticks/aien-model/M1/ + +# Resources. One capacity ledger for CPU and GPU (unified memory). +resource id=cpu kind=compute slots=20 prov=specified src=nproc +resource id=gpu kind=compute slots=1 prov=specified src=one-job-at-a-time-engine +resource id=dram_spec kind=bandwidth cap=273e9 prov=specified src=nvidia-dgx-spark-datasheet +resource id=dram_gpu kind=bandwidth cap=218.6e9 lo=218.6e9 hi=219e9 prov=measured src=handoff-2026-10-19-qwen38-decode-speed.md:calc:e726ff77 +resource id=dram_cpu kind=bandwidth cap=65.4e9 lo=60.8e9 hi=69.8e9 prov=measured src=M1/block4.csv:gb=1,threads=8 +resource id=unified_mem kind=capacity cap=137438953472 prov=specified src=nvidia-dgx-spark-datasheet +resource id=gpu_launch kind=launch prov=measured src=M1/block2.csv + +# Ops. A CPU realization call (block1 path) feeding one GPU job (block2) whose +# fixed submit lifecycle dominates, then a CPU memcpy of 1 GB (block4, 1 thread). +op id=realize kind=compute engine=cpu t_ns=20000 lo=19400 hi=22800 prov=measured src=M1/block1.csv:path_ns_per_call +op id=gpu_job kind=compute engine=gpu t_ns=0 prov=measured src=M1/block2.csv:vecadd_65 launch_ns=40736700 launcher=gpu_launch +op id=copy1g kind=copy engine=cpu t_ns=111000000 lo=107900000 hi=116600000 prov=measured src=M1/block4.csv:gb=1,threads=1 bytes=2000000000 bw=dram_cpu +dep realize gpu_job +dep gpu_job copy1g + +# Buffers. +buffer id=src1g bytes=1000000000 cap=unified_mem first=copy1g last=copy1g +buffer id=dst1g bytes=1000000000 cap=unified_mem first=copy1g last=copy1g + +# Labelled sensitivity only: 100 W is a made-up figure, not an instrument. +energy instrument=what-if-100W boundary=wall power_w=100 prov=synthetic diff --git a/tests/dagsim/fixtures/mw_bad_elimination.oev b/tests/dagsim/fixtures/mw_bad_elimination.oev new file mode 100644 index 00000000..cc9e2a48 --- /dev/null +++ b/tests/dagsim/fixtures/mw_bad_elimination.oev @@ -0,0 +1,14 @@ +# Minimum-work accounting fixtures (parent #276 clarification). Synthetic +# numbers, labelled so; the point is the ledger, not the magnitudes. +oev version=1 +machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test +build repo=omega-mv sha=0000000000000000000000000000000000000000 +calibration id=mw taken=2026-10-19 valid_days=3650 load1=0 src=synthetic +resource id=cpu kind=compute slots=1 prov=specified src=test +resource id=mem kind=capacity cap=1000 prov=specified src=test +energy instrument=none +op id=parse kind=compute engine=cpu t_ns=10 prov=synthetic src=t +op id=execute kind=compute engine=cpu t_ns=50 prov=synthetic src=t +dep parse execute +removed op=search reason=eliminated +removed op=check reason=eliminated diff --git a/tests/dagsim/fixtures/mw_baseline.oev b/tests/dagsim/fixtures/mw_baseline.oev new file mode 100644 index 00000000..e2eaf36f --- /dev/null +++ b/tests/dagsim/fixtures/mw_baseline.oev @@ -0,0 +1,16 @@ +# Minimum-work accounting fixtures (parent #276 clarification). Synthetic +# numbers, labelled so; the point is the ledger, not the magnitudes. +oev version=1 +machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test +build repo=omega-mv sha=0000000000000000000000000000000000000000 +calibration id=mw taken=2026-10-19 valid_days=3650 load1=0 src=synthetic +resource id=cpu kind=compute slots=1 prov=specified src=test +resource id=mem kind=capacity cap=1000 prov=specified src=test +energy instrument=none +op id=parse kind=compute engine=cpu t_ns=10 prov=synthetic src=t +op id=search kind=compute engine=cpu t_ns=200 prov=synthetic src=t +op id=check kind=compute engine=cpu t_ns=20 prov=synthetic src=t +op id=execute kind=compute engine=cpu t_ns=50 prov=synthetic src=t +dep parse search +dep search check +dep check execute diff --git a/tests/dagsim/fixtures/mw_reuse.oev b/tests/dagsim/fixtures/mw_reuse.oev new file mode 100644 index 00000000..9e24750f --- /dev/null +++ b/tests/dagsim/fixtures/mw_reuse.oev @@ -0,0 +1,17 @@ +# Minimum-work accounting fixtures (parent #276 clarification). Synthetic +# numbers, labelled so; the point is the ledger, not the magnitudes. +oev version=1 +machine id=t soc=t cores=4 mem_bytes=1000 prov=specified src=test +build repo=omega-mv sha=0000000000000000000000000000000000000000 +calibration id=mw taken=2026-10-19 valid_days=3650 load1=0 src=synthetic +resource id=cpu kind=compute slots=1 prov=specified src=test +resource id=mem kind=capacity cap=1000 prov=specified src=test +energy instrument=none +op id=parse kind=compute engine=cpu t_ns=10 prov=synthetic src=t +op id=lookup kind=compute engine=cpu t_ns=5 prov=synthetic src=t +op id=check kind=compute engine=cpu t_ns=20 prov=synthetic src=t +op id=execute kind=compute engine=cpu t_ns=50 prov=synthetic src=t +dep parse lookup +dep lookup check +dep check execute +removed op=search reason=reused ref=lookup proof=rx_plan:verified-plan-digest diff --git a/tests/dagsim/fixtures/needs_arch244_trace.oev b/tests/dagsim/fixtures/needs_arch244_trace.oev new file mode 100644 index 00000000..67116507 --- /dev/null +++ b/tests/dagsim/fixtures/needs_arch244_trace.oev @@ -0,0 +1,24 @@ +# OEV1 fixture: Spark M1 hardware passport subset (omega-mv#578), pinned 2026-10-19. +# Every number below is a loaded-machine measurement (load1 15 to 29) or a datasheet +# ceiling, and says which. Nothing here was produced by running a GPU in this harness. +oev version=1 +machine id=spark-b87b soc=GB10 cores=20 mem_bytes=137438953472 prov=specified src=nvidia-dgx-spark-datasheet +build repo=omega-mv sha=f4a9849f00000000000000000000000000000000 physics=6d7cf0d4 +calibration id=M1-20261019 taken=2026-10-19 valid_days=30 load1=24.2 src=~/yardsticks/aien-model/M1/ + +# Resources. One capacity ledger for CPU and GPU (unified memory). +resource id=cpu kind=compute slots=20 prov=specified src=nproc +resource id=gpu kind=compute slots=1 prov=specified src=one-job-at-a-time-engine +resource id=dram_spec kind=bandwidth cap=273e9 prov=specified src=nvidia-dgx-spark-datasheet +resource id=dram_gpu kind=bandwidth cap=218.6e9 lo=218.6e9 hi=219e9 prov=measured src=handoff-2026-10-19-qwen38-decode-speed.md:calc:e726ff77 +resource id=dram_cpu kind=bandwidth cap=65.4e9 lo=60.8e9 hi=69.8e9 prov=measured src=M1/block4.csv:gb=1,threads=8 +resource id=unified_mem kind=capacity cap=137438953472 prov=specified src=nvidia-dgx-spark-datasheet +resource id=gpu_launch kind=launch prov=measured src=M1/block2.csv + +# Ops. The second op needs the aien-architecture#244 objective trace, which was +# NOT_RUN on 2026-10-19. Unknown stays unknown: this fixture must be refused. +op id=realize kind=compute engine=cpu t_ns=20000 lo=19400 hi=22800 prov=measured src=M1/block1.csv:path_ns_per_call +op id=objective_replay kind=compute engine=cpu prov=unknown src=aien-architecture#244:NOT_RUN +dep realize objective_replay + +energy instrument=none diff --git a/tests/dagsim/run_cli.sh b/tests/dagsim/run_cli.sh new file mode 100644 index 00000000..5b22297d --- /dev/null +++ b/tests/dagsim/run_cli.sh @@ -0,0 +1,61 @@ +#!/bin/sh +# run_cli.sh DAGSIM_BIN [OUT_DIR] +# Reproducible CLI example for omega-mv#583: pinned fixtures in, JSON and a +# Pareto table out, byte-identical on a second run, no GPU touched. +set -u +BIN=${1:?dagsim binary} +OUT=${2:-/tmp/dagsim-cli-$$} +F=tests/dagsim/fixtures +ASOF=2026-10-20 +mkdir -p "$OUT" +fail=0 +say() { echo "run_cli: $*"; } +check() { if [ "$1" -ne 0 ]; then say "FAIL $2"; fail=1; else say "ok $2"; fi; } + +"$BIN" eval $F/m1_spark_subset.oev --asof $ASOF > "$OUT/subset.json"; rc=$? +check $rc "eval exits 0 on the pinned M1 subset" +grep -q '"status": "OK"' "$OUT/subset.json"; check $? "status OK" +grep -q '"makespan_ns": {"nominal": 151756700,' "$OUT/subset.json"; check $? "serial makespan 20000 + 40736700 + 111000000 ns" +grep -q '"makespan_ge_bounds": true' "$OUT/subset.json"; check $? "makespan is above its bounds" +grep -q '"status": "UNMEASURED"' "$OUT/subset.json"; check $? "energy UNMEASURED without an instrument" +grep -q '"gpu_executed": false' "$OUT/subset.json"; check $? "offline: no GPU execution" +grep -q '"provenance": \["measured", "specified"\]' "$OUT/subset.json"; check $? "provenance list names measured and specified only" +"$BIN" eval $F/m1_spark_subset.oev --asof $ASOF > "$OUT/subset2.json" +cmp -s "$OUT/subset.json" "$OUT/subset2.json"; check $? "second run byte-identical" + +"$BIN" eval $F/needs_arch244_trace.oev --asof $ASOF > "$OUT/unknown.json"; rc=$? +[ $rc -eq 1 ] ; check $? "unknown-trace fixture exits 1" +grep -q '"status": "REFUSED_UNKNOWN"' "$OUT/unknown.json"; check $? "unknown stays unknown (REFUSED_UNKNOWN)" +grep -q 'makespan' "$OUT/unknown.json"; [ $? -ne 0 ]; check $? "no makespan printed on refusal" + +"$BIN" eval $F/m1_spark_subset.oev --asof 2026-12-01 > "$OUT/stale.json" +grep -q '"status": "REFUSED_STALE"' "$OUT/stale.json"; check $? "30-day calibration refused at 43 days" + +"$BIN" eval $F/m1_synthetic_power.oev --asof $ASOF > "$OUT/syn.json" +grep -q '"status": "SYNTHETIC_SENSITIVITY_ONLY"' "$OUT/syn.json"; check $? "synthetic power is labelled, not ranked" +grep -q '"ranking_valid": false' "$OUT/syn.json"; check $? "energy ranking invalid on synthetic power" + +"$BIN" eval $F/m1_spark_overlap.oev --asof $ASOF > "$OUT/overlap.json" +grep -q '"status": "OK"' "$OUT/overlap.json"; check $? "overlap fixture evaluates" +# two halves sharing a 65.4 GB/s pipe: 2 GB of traffic is 30.58 ms, each half's own busy +# time is 55.5 ms, so busy time binds and both halves end together at 40.7567 + 55.5 ms. +grep -q '"makespan_ns": {"nominal": 96256700,' "$OUT/overlap.json"; check $? "split copy: busy time binds, 40756700 + 55500000 ns" + +"$BIN" pareto $F/m1_spark_subset.oev $F/m1_spark_overlap.oev $F/m1_synthetic_power.oev $F/needs_arch244_trace.oev --asof $ASOF > "$OUT/pareto.txt"; rc=$? +check $rc "pareto exits 0" +grep -q 'needs_arch244_trace.oev *REFUSED_UNKNOWN' "$OUT/pareto.txt"; check $? "refused candidate shown as refused in the table" +grep -q 'm1_spark_overlap.oev *OK .* yes$' "$OUT/pareto.txt"; check $? "faster candidate is on the front" +grep -q 'm1_spark_subset.oev *OK .* no$' "$OUT/pareto.txt"; check $? "dominated candidate is off the front" +grep -q 'Energy not measured on every row' "$OUT/pareto.txt"; check $? "pareto says energy is not ranked" + +"$BIN" diff $F/mw_baseline.oev $F/mw_reuse.oev --asof $ASOF > "$OUT/diff_reuse.json"; rc=$? +check $rc "reuse diff OK" +grep -q '"reused": 200' "$OUT/diff_reuse.json"; check $? "200 ns of search moved to the reused ledger" +"$BIN" diff $F/mw_baseline.oev $F/mw_bad_elimination.oev --asof $ASOF > "$OUT/diff_bad.json"; rc=$? +[ $rc -eq 1 ]; check $? "unproven elimination exits 1" +grep -q '"status": "REFUSED_REMOVED_REQUIRED"' "$OUT/diff_bad.json"; check $? "unproven elimination refused" +"$BIN" diff $F/mw_baseline.oev $F/mw_baseline.oev --asof $ASOF > "$OUT/diff_same.json" +grep -q '"kept": 4, "added": 0, "removed": 0' "$OUT/diff_same.json"; check $? "no-change fixture adds no nodes" + +say "outputs in $OUT" +exit $fail diff --git a/tests/dagsim/sha256_host.c b/tests/dagsim/sha256_host.c new file mode 100644 index 00000000..64ffdf4e --- /dev/null +++ b/tests/dagsim/sha256_host.c @@ -0,0 +1,19 @@ +/* Link stub for the imported dagsim binary on hosts that are not AArch64. + * src/omega_om_units.c is AArch64 machine code. rx_costmodel.o references + * sha256_hash from serialize/digest only. The analytical dagsim tests do not + * call it. On AArch64 the Makefile links src/omega_om_units.c instead, and + * this file is not built. dagsim --model is NOT_RUN on this stub: a blob + * digest would abort rather than return a made-up hash. + */ +#if !defined(__aarch64__) +#include +#include +#include +#include + +void sha256_hash(const uint8_t *d, size_t n, uint8_t out[32]) { + (void)d; (void)n; (void)out; + fprintf(stderr, "sha256_hash: host stub; dagsim --model is NOT_RUN off AArch64\n"); + abort(); +} +#endif diff --git a/tests/gpu_imadhi_rc_test.c b/tests/gpu_imadhi_rc_test.c new file mode 100644 index 00000000..179596a2 --- /dev/null +++ b/tests/gpu_imadhi_rc_test.c @@ -0,0 +1,106 @@ +/* omega #492: IMAD.HI.U32 with a register Rc through one host warp simulator (host-only, no chip). + * + * The chip reads the Rc of IMAD.HI.U32 as a 64-bit register pair, not as one 32-bit word. + * Receipt (manufacturer's compiler, not a chip run): nvcc 13.0 for sm_121 emits, for + * hi32(a * b + c64) IMAD.HI.U32 R7, R7, R6, R2 with R2:R3 = c64 (no separate add), + * hi32(a * b + (u64)c32) UMOV UR5, URZ ; IMAD.HI.U32 R5, R7, R6, UR4 (upper half zeroed first), + * __umulhi(a, b) + c32 Rc pair = {lo 0, hi c32}, so the add lands in the high word. + * Listing and source: evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016/. The first version of the + * trainer GEMM (PR #487) used IMAD.HI.U32 with a register Rc, passed the simulators (32-bit Rc) + * and gave wrong odd columns on the chip. A chip probe of the exact pair semantics is still + * pending (lane E3), so the simulators refuse a non-RZ Rc instead of modelling it. + * + * This file is compiled once per simulator header: -DSIM_HEADER='"bw_warp_sim_k.h"' and so on + * (Makefile target test-gpu-imadhi-rc). The four headers share their static names, so one + * translation unit holds one simulator. + * + * Checks (each runs the one-instruction kernel on one warp and stores Rd): + * 1. Rc = RZ: the simulator runs and Rd = hi32(Ra * Rb) (control, the one live user today) + * 2. Rc = register: the encoder emits a non-RZ Rc (the chip would read a pair) and the + * simulator refuses with WS_E_IMADHI_RC naming the pc. Before the fix it returned + * hi32(Ra * Rb) + Rc.lo, the 32-bit model the chip does not implement. + */ +#include +#include +#include +#include +#include "omega_blackwell_codegen.h" +#ifndef SIM_HEADER +#error "compile with -DSIM_HEADER='\"bw_warp_sim.h\"' (or _tc, _k, _pa)" +#endif +#include SIM_HEADER + +enum { V_OUT = 0, V_A, V_B, V_C, V_D, NV }; +enum { CB_OUT = 0x380 }; + +static void ins(BlackwellIRProgram *p, BlackwellIROpcode op, int d, int a, int b, int c, uint32_t imm, uint8_t dsub) { + BlackwellIRInsn *i = &p->insns[p->count++]; + memset(i, 0, sizeof *i); + i->op = op; i->dst_vreg = d; i->src1_vreg = a; i->src2_vreg = b; i->src3_vreg = c; i->ureg = -1; i->imm = imm; i->dst_subreg = dsub; +} + +/* out[tid] = IMAD.HI.U32(a, b, rc ? {chi:clo} : RZ) */ +static void build(BlackwellIRProgram *p, uint32_t a, uint32_t b, uint32_t clo, uint32_t chi, int rc) { + memset(p, 0, sizeof *p); + ins(p, BW_IR_LDC64, V_OUT, -1, -1, -1, CB_OUT, 0); + ins(p, BW_IR_MOV_IMM, V_A, -1, -1, -1, a, 0); + ins(p, BW_IR_MOV_IMM, V_B, -1, -1, -1, b, 0); + ins(p, BW_IR_MOV_IMM, V_C, -1, -1, -1, clo, 0); + ins(p, BW_IR_MOV_IMM, V_C, -1, -1, -1, chi, 1); + ins(p, BW_IR_IMAD_HI_U32, V_D, V_A, V_B, rc ? V_C : -1, 0, 0); + ins(p, BW_IR_STG_E, -1, V_OUT, V_D, -1, 0, 0); + ins(p, BW_IR_EXIT, -1, -1, -1, -1, 0, 0); + /* physical registers: out pair R2:R3, a R4, b R5, c pair R6:R7 (even-aligned), d R8 */ + static const int phys[NV] = { 2, 4, 5, 6, 8 }; + p->regalloc.num_vregs = NV; + for (int v = 0; v < NV; v++) p->regalloc.vreg_to_phys[v] = phys[v]; +} + +static int run(const BlackwellIRProgram *p, uint32_t *out, char *msg, size_t msglen) { + WsSim s; memset(&s, 0, sizeof s); + static uint8_t smem[1024]; + s.p = p; s.threads = 32; s.smem = smem; s.smem_bytes = sizeof smem; + uint64_t oa = (uintptr_t)out; + s.cbank[CB_OUT / 4] = (uint32_t)oa; s.cbank[CB_OUT / 4 + 1] = (uint32_t)(oa >> 32); + s.ranges[0] = (WsRange){ (uintptr_t)oa, (uintptr_t)oa + 4 }; s.nranges = 1; + int err = ws_run(&s, 1, 1); + snprintf(msg, msglen, "%s", s.msg); + return err ? s.err : 0; +} + +int main(void) { + const uint32_t A = 0xdeadbeefu, B = 0x10000u, CLO = 0x1234u, CHI = 0x5678u; /* B = 2^16 is the tgemm pack */ + const uint32_t hi_ab = (uint32_t)(((uint64_t)A * B) >> 32); + const uint32_t pair_model = (uint32_t)((((uint64_t)A * B) + (((uint64_t)CHI << 32) | CLO)) >> 32); + int fails = 0; char msg[200]; uint32_t out; + BlackwellIRProgram *p = calloc(1, sizeof *p); + printf("sim %s: hi32(a*b) 0x%08x, nvcc pair model 0x%08x (chip probe pending), old 32-bit model 0x%08x\n", + SIM_HEADER, hi_ab, pair_model, hi_ab + CLO); + + /* 1. Rc = RZ: runs, Rd = hi32(a * b) */ + build(p, A, B, CLO, CHI, 0); out = 0xffbadbadu; + int err = run(p, &out, msg, sizeof msg); + if (err || out != hi_ab) { fails++; printf("FAIL rc=RZ: err %d (%s) out 0x%08x want 0x%08x\n", err, msg, out, hi_ab); } + else printf("PASS rc=RZ: Rd = hi32(a*b) = 0x%08x\n", out); + + /* 2. Rc = register pair: the word carries a non-RZ Rc, the simulator must refuse */ + build(p, A, B, CLO, CHI, 1); + BlackwellIRInsn e = p->insns[5]; e.dst_vreg = 8; e.src1_vreg = 4; e.src2_vreg = 5; e.src3_vreg = 6; /* physical numbers for the encoder */ + uint32_t w[4] = { 0, 0, 0, 0 }; + if (omega_blackwell_encode_one(&e, w) != 0 || (w[0] & 0xffffu) != 0x7227u || (w[2] & 0xffu) != 6u) { + fails++; printf("FAIL encode: IMAD.HI.U32 R8, R4, R5, R6 -> %08x %08x %08x %08x (want opcode 7227, Rc byte 06)\n", w[0], w[1], w[2], w[3]); + } else printf("PASS encode: IMAD.HI.U32 R8, R4, R5, R6 carries Rc = R6 (the chip reads R6:R7)\n"); + out = 0xffbadbadu; + err = run(p, &out, msg, sizeof msg); + if (err != WS_E_IMADHI_RC) { + fails++; + printf("FAIL rc=R6: simulator did not refuse (err %d%s%s), out 0x%08x: a 32-bit Rc model the chip does not implement\n", + err, err ? " " : "", err ? msg : "", out); + } else if (!strstr(msg, "IMAD.HI.U32") || !strstr(msg, "at pc 5")) { + fails++; printf("FAIL rc=R6: refused but the message does not name the instruction and pc: %s\n", msg); + } else printf("PASS rc=R6: refused: %s\n", msg); + + free(p); + printf("%s %s\n", fails ? "FAIL" : "PASS", SIM_HEADER); + return fails ? 1 : 0; +} diff --git a/tools/dagsim/dagsim.c b/tools/dagsim/dagsim.c new file mode 100644 index 00000000..b7187d44 --- /dev/null +++ b/tools/dagsim/dagsim.c @@ -0,0 +1,751 @@ +/* + * dagsim.c -- offline resource-DAG cost evaluator (omega-mv#583). + * + * See dagsim.h and spec/offline-evidence-v1.md. The scheduler is a + * deterministic fluid simulation: compute engines have slots, bandwidth + * resources are shared equally among the ops streaming on them at any + * instant, launches are serialized per launch resource, and an op finishes + * when both its own busy time and its traffic are done (perfect overlap + * inside one op, which is the issue's stated floor max(F/P, Q/B)). Ops are + * dispatched in file order, so the same file always gives the same bytes. + */ +#include "dagsim.h" +#include "runtime/rx_costmodel.h" + +#include +#include +#include +#include +#include +#include + +/* ---------------------------------------------------------------- names */ + +const char *ds_status_name(int s) { + switch (s) { + case DS_OK: return "OK"; + case DS_REFUSED_UNKNOWN: return "REFUSED_UNKNOWN"; + case DS_REFUSED_STALE: return "REFUSED_STALE"; + case DS_REFUSED_NO_CALIBRATION: return "REFUSED_NO_CALIBRATION"; + case DS_REFUSED_CAPACITY: return "REFUSED_CAPACITY"; + case DS_REFUSED_CYCLE: return "REFUSED_CYCLE"; + case DS_REFUSED_UNSUPPORTED: return "REFUSED_UNSUPPORTED"; + case DS_REFUSED_REMOVED_REQUIRED: return "REFUSED_REMOVED_REQUIRED"; + case DS_PARSE_ERROR: return "PARSE_ERROR"; + default: return "?"; + } +} + +const char *ds_prov_name(int p) { + switch (p) { + case DS_PROV_UNKNOWN: return "unknown"; + case DS_PROV_SYNTHETIC: return "synthetic"; + case DS_PROV_FITTED: return "fitted"; + case DS_PROV_MEASURED: return "measured"; + case DS_PROV_SPECIFIED: return "specified"; + default: return "?"; + } +} + +static int prov_from_name(const char *s) { + if (!strcmp(s, "unknown")) return DS_PROV_UNKNOWN; + if (!strcmp(s, "synthetic")) return DS_PROV_SYNTHETIC; + if (!strcmp(s, "fitted")) return DS_PROV_FITTED; + if (!strcmp(s, "measured")) return DS_PROV_MEASURED; + if (!strcmp(s, "specified")) return DS_PROV_SPECIFIED; + return -1; +} + +/* days since 1970-01-01 for a civil date (Howard Hinnant's days_from_civil) */ +int ds_day_from_date(const char *s) { + int y, m, d; + if (!s || sscanf(s, "%d-%d-%d", &y, &m, &d) != 3) return -1; + y -= m <= 2; + int era = (y >= 0 ? y : y - 399) / 400; + unsigned yoe = (unsigned)(y - era * 400); + unsigned doy = (153u * (unsigned)(m + (m > 2 ? -3 : 9)) + 2u) / 5u + (unsigned)d - 1u; + unsigned doe = yoe * 365u + yoe / 4u - yoe / 100u + doy; + return era * 146097 + (int)doe - 719468; +} + +int ds_find_op(const ds_model *m, const char *id) { + for (int i = 0; i < m->n_ops; i++) if (!strcmp(m->ops[i].id, id)) return i; + return -1; +} +int ds_find_res(const ds_model *m, const char *id) { + for (int i = 0; i < m->n_res; i++) if (!strcmp(m->res[i].id, id)) return i; + return -1; +} + +/* ---------------------------------------------------------------- parse */ +/* bounded string copy; ids and sources are truncated, never overflowed */ +static void sset(char *dst, size_t cap, const char *src) { + size_t n = strlen(src); + if (n >= cap) n = cap - 1; + memcpy(dst, src, n); dst[n] = 0; +} + + +typedef struct { char key[32]; char val[DS_SRC]; int used; } kv; + +static int split_kv(char *line, char *keyword, size_t kwcap, kv *kvs, int maxkv, char *positional[], int *npos) { + int n = 0; *npos = 0; keyword[0] = 0; + char *p = strchr(line, '#'); if (p) *p = 0; + char *save = NULL; + char *tok = strtok_r(line, " \t\r\n", &save); + if (!tok) return -1; /* empty */ + snprintf(keyword, kwcap, "%s", tok); + while ((tok = strtok_r(NULL, " \t\r\n", &save))) { + char *eq = strchr(tok, '='); + if (!eq) { if (*npos < 8) positional[(*npos)++] = tok; continue; } + if (n >= maxkv) return -2; + *eq = 0; + sset(kvs[n].key, sizeof kvs[n].key, tok); + sset(kvs[n].val, sizeof kvs[n].val, eq + 1); + kvs[n].used = 0; + n++; + } + return n; +} + +static const char *get(kv *kvs, int n, const char *key) { + for (int i = 0; i < n; i++) if (!strcmp(kvs[i].key, key)) { kvs[i].used = 1; return kvs[i].val; } + return NULL; +} + +static int unused_key(kv *kvs, int n, char *out, size_t cap) { + for (int i = 0; i < n; i++) if (!kvs[i].used) { snprintf(out, cap, "%s", kvs[i].key); return 1; } + return 0; +} + +static int parse_real(const char *s, double *out) { + if (!s) return 0; + char *end; double v = strtod(s, &end); + if (end == s || *end) return 0; + if (!(v == v) || v < 0) return 0; + *out = v; return 1; +} + +#define PERR(...) do { snprintf(err, errcap, "line %d: ", lineno); size_t _l = strlen(err); snprintf(err + _l, errcap - _l, __VA_ARGS__); return DS_PARSE_ERROR; } while (0) + +static int parse_prov_src(kv *kvs, int n, int *prov, char *src, size_t srccap, int required, char *err, size_t errcap, int lineno) { + const char *p = get(kvs, n, "prov"); + const char *s = get(kvs, n, "src"); + if (!p) { if (required) PERR("prov= is required wherever a number appears"); *prov = -1; return DS_OK; } + int v = prov_from_name(p); + if (v < 0) PERR("prov=%s is not one of specified|measured|fitted|synthetic|unknown", p); + *prov = v; + if ((v == DS_PROV_MEASURED || v == DS_PROV_FITTED) && !s) PERR("prov=%s needs src=", p); + snprintf(src, srccap, "%s", s ? s : ""); + return DS_OK; +} + +int ds_parse_text(const char *text, ds_model *m, char *err, size_t errcap) { + memset(m, 0, sizeof *m); + err[0] = 0; + int lineno = 0; + const char *cur = text; + int seen_oev = 0, seen_machine = 0, seen_build = 0; + /* two-pass is not needed: ops before deps/buffers is the convention; we + * resolve references at use and require definition before use. */ + while (*cur) { + const char *nl = strchr(cur, '\n'); + size_t len = nl ? (size_t)(nl - cur) : strlen(cur); + char line[1024]; + lineno++; + if (len >= sizeof line) PERR("line too long"); + memcpy(line, cur, len); line[len] = 0; + cur = nl ? nl + 1 : cur + len; + char keyword[32]; kv kvs[24]; char *pos[8]; int npos; + int n = split_kv(line, keyword, sizeof keyword, kvs, 24, pos, &npos); + if (n == -1) continue; + if (n == -2) PERR("too many fields"); + if (!strcmp(keyword, "oev")) { + const char *v = get(kvs, n, "version"); + if (!v || strcmp(v, "1")) PERR("oev version=1 required"); + m->version = 1; seen_oev = 1; + } else if (!strcmp(keyword, "machine")) { + const char *v; + if (!(v = get(kvs, n, "id"))) PERR("machine needs id="); + sset(m->machine_id, sizeof m->machine_id, v); + if ((v = get(kvs, n, "soc"))) sset(m->soc, sizeof m->soc, v); + if ((v = get(kvs, n, "cores"))) m->cores = atoi(v); + if ((v = get(kvs, n, "mem_bytes")) && !parse_real(v, &m->mem_bytes)) PERR("bad mem_bytes"); + int prov; char src[DS_SRC]; + int rc = parse_prov_src(kvs, n, &prov, src, sizeof src, 1, err, errcap, lineno); if (rc) return rc; + seen_machine = 1; + } else if (!strcmp(keyword, "build")) { + const char *v; + if ((v = get(kvs, n, "repo"))) sset(m->repo, sizeof m->repo, v); + if (!(v = get(kvs, n, "sha")) || strlen(v) != 40) PERR("build needs sha=<40 hex>"); + sset(m->build_sha, sizeof m->build_sha, v); + if ((v = get(kvs, n, "physics"))) sset(m->physics, sizeof m->physics, v); + seen_build = 1; + } else if (!strcmp(keyword, "calibration")) { + const char *v; + if (m->has_calibration) PERR("one calibration record only"); + if (!(v = get(kvs, n, "id"))) PERR("calibration needs id="); + sset(m->cal_id, sizeof m->cal_id, v); + if (!(v = get(kvs, n, "taken")) || (m->cal_taken_day = ds_day_from_date(v)) < 0) PERR("calibration needs taken=YYYY-MM-DD"); + if (!(v = get(kvs, n, "valid_days"))) PERR("calibration needs valid_days="); + m->cal_valid_days = atoi(v); + if (!(v = get(kvs, n, "load1")) || !parse_real(v, &m->cal_load1)) PERR("calibration needs load1="); + if (!(v = get(kvs, n, "src"))) PERR("calibration needs src="); + sset(m->cal_src, sizeof m->cal_src, v); + if ((v = get(kvs, n, "model"))) sset(m->cal_model, sizeof m->cal_model, v); + m->has_calibration = 1; + } else if (!strcmp(keyword, "resource")) { + if (m->n_res >= DS_MAX_RES) PERR("too many resources (max %d)", DS_MAX_RES); + ds_resource *r = &m->res[m->n_res]; + memset(r, 0, sizeof *r); + const char *v; + if (!(v = get(kvs, n, "id"))) PERR("resource needs id="); + if (ds_find_res(m, v) >= 0) PERR("duplicate resource %s", v); + sset(r->id, sizeof r->id, v); + if (!(v = get(kvs, n, "kind"))) PERR("resource needs kind="); + if (!strcmp(v, "compute")) { + r->kind = DS_RES_COMPUTE; + if (!(v = get(kvs, n, "slots")) || (r->slots = atoi(v)) < 1) PERR("compute resource needs slots>=1"); + } else if (!strcmp(v, "bandwidth")) { + r->kind = DS_RES_BANDWIDTH; + if (!parse_real(get(kvs, n, "cap"), &r->cap) || r->cap <= 0) PERR("bandwidth resource needs cap>0"); + r->lo = r->hi = r->cap; + if ((v = get(kvs, n, "lo")) && !parse_real(v, &r->lo)) PERR("bad lo"); + if ((v = get(kvs, n, "hi")) && !parse_real(v, &r->hi)) PERR("bad hi"); + if (r->lo > r->cap || r->hi < r->cap) PERR("need lo <= cap <= hi"); + } else if (!strcmp(v, "capacity")) { + r->kind = DS_RES_CAPACITY; + if (!parse_real(get(kvs, n, "cap"), &r->cap) || r->cap <= 0) PERR("capacity resource needs cap>0"); + } else if (!strcmp(v, "launch")) { + r->kind = DS_RES_LAUNCH; + } else PERR("resource kind=%s unknown", v); + int rc = parse_prov_src(kvs, n, &r->prov, r->src, sizeof r->src, 1, err, errcap, lineno); if (rc) return rc; + m->n_res++; + } else if (!strcmp(keyword, "op")) { + if (m->n_ops >= DS_MAX_OPS) PERR("too many ops (max %d)", DS_MAX_OPS); + ds_op *o = &m->ops[m->n_ops]; + memset(o, 0, sizeof *o); + o->engine = o->bw = o->launcher = -1; + const char *v; + if (!(v = get(kvs, n, "id"))) PERR("op needs id="); + if (ds_find_op(m, v) >= 0) PERR("duplicate op %s", v); + sset(o->id, sizeof o->id, v); + if (!(v = get(kvs, n, "kind"))) PERR("op needs kind="); + if (!strcmp(v, "compute")) o->kind = DS_OP_COMPUTE; + else if (!strcmp(v, "copy")) o->kind = DS_OP_COPY; + else if (!strcmp(v, "matvec")) o->kind = DS_OP_MATVEC; + else if (!strcmp(v, "barrier")) o->kind = DS_OP_BARRIER; + else if (!strcmp(v, "storage")) o->kind = DS_OP_STORAGE; + else if (!strcmp(v, "persist")) o->kind = DS_OP_PERSIST; + else PERR("op kind=%s unknown", v); + if ((v = get(kvs, n, "engine"))) { + o->engine = ds_find_res(m, v); + if (o->engine < 0 || m->res[o->engine].kind != DS_RES_COMPUTE) PERR("engine=%s is not a compute resource defined above", v); + } + if ((v = get(kvs, n, "t_ns"))) { if (!parse_real(v, &o->t_ns)) PERR("bad t_ns"); o->has_t = 1; o->lo = o->hi = o->t_ns; } + if ((v = get(kvs, n, "lo"))) { if (!o->has_t || !parse_real(v, &o->lo) || o->lo > o->t_ns) PERR("lo needs t_ns and lo <= t_ns"); } + if ((v = get(kvs, n, "hi"))) { if (!o->has_t || !parse_real(v, &o->hi) || o->hi < o->t_ns) PERR("hi needs t_ns and hi >= t_ns"); } + if ((v = get(kvs, n, "bytes"))) { + if (!parse_real(v, &o->bytes)) PERR("bad bytes"); + const char *b = get(kvs, n, "bw"); + if (!b || (o->bw = ds_find_res(m, b)) < 0 || m->res[o->bw].kind != DS_RES_BANDWIDTH) PERR("bytes= needs bw="); + } + if ((v = get(kvs, n, "launch_ns"))) { + if (!parse_real(v, &o->launch_ns)) PERR("bad launch_ns"); + const char *l = get(kvs, n, "launcher"); + if (!l || (o->launcher = ds_find_res(m, l)) < 0 || m->res[o->launcher].kind != DS_RES_LAUNCH) PERR("launch_ns= needs launcher="); + } + if ((v = get(kvs, n, "M"))) o->M = (uint32_t)strtoul(v, NULL, 10); + if ((v = get(kvs, n, "N"))) o->N = (uint32_t)strtoul(v, NULL, 10); + if ((v = get(kvs, n, "core"))) o->core = (uint32_t)strtoul(v, NULL, 10); + if ((v = get(kvs, n, "arm"))) o->arm = (uint32_t)strtoul(v, NULL, 10); + if (o->kind == DS_OP_MATVEC && !o->has_t && (o->M == 0 || o->N == 0)) PERR("matvec needs M= and N= (or t_ns=)"); + if (o->kind != DS_OP_BARRIER && o->engine < 0) PERR("op %s needs engine=", o->id); + int required = o->has_t || o->bytes > 0 || o->launch_ns > 0 || o->kind == DS_OP_MATVEC; + int rc = parse_prov_src(kvs, n, &o->t_prov, o->src, sizeof o->src, required, err, errcap, lineno); if (rc) return rc; + if (o->t_prov < 0) o->t_prov = DS_PROV_SPECIFIED; /* a costless barrier: ordering only */ + if (!o->has_t && o->kind != DS_OP_MATVEC && o->t_prov == DS_PROV_UNKNOWN) { /* fine: explicitly unknown */ } + m->n_ops++; + } else if (!strcmp(keyword, "dep")) { + if (npos != 2 || n != 0) PERR("dep "); + if (m->n_deps >= DS_MAX_DEPS) PERR("too many deps"); + int a = ds_find_op(m, pos[0]), b = ds_find_op(m, pos[1]); + if (a < 0 || b < 0) PERR("dep names an op not defined above: %s %s", pos[0], pos[1]); + m->deps[m->n_deps].from = a; m->deps[m->n_deps].to = b; m->n_deps++; + } else if (!strcmp(keyword, "buffer")) { + if (m->n_buf >= DS_MAX_BUF) PERR("too many buffers"); + ds_buffer *b = &m->buf[m->n_buf]; + memset(b, 0, sizeof *b); b->first = b->last = -1; + const char *v; + if (!(v = get(kvs, n, "id"))) PERR("buffer needs id="); + sset(b->id, sizeof b->id, v); + if (!parse_real(get(kvs, n, "bytes"), &b->bytes)) PERR("buffer needs bytes="); + if (!(v = get(kvs, n, "cap")) || (b->cap = ds_find_res(m, v)) < 0 || m->res[b->cap].kind != DS_RES_CAPACITY) PERR("buffer needs cap="); + if ((v = get(kvs, n, "first")) && (b->first = ds_find_op(m, v)) < 0) PERR("first=%s not an op", v); + if ((v = get(kvs, n, "last")) && (b->last = ds_find_op(m, v)) < 0) PERR("last=%s not an op", v); + m->n_buf++; + } else if (!strcmp(keyword, "sync")) { + /* a barrier op that depends on every listed op */ + if (m->n_ops >= DS_MAX_OPS) PERR("too many ops"); + const char *v, *list; + if (!(v = get(kvs, n, "id"))) PERR("sync needs id="); + if (ds_find_op(m, v) >= 0) PERR("duplicate op %s", v); + if (!(list = get(kvs, n, "ops"))) PERR("sync needs ops=a,b,..."); + ds_op *o = &m->ops[m->n_ops]; + memset(o, 0, sizeof *o); + o->engine = o->bw = o->launcher = -1; o->kind = DS_OP_BARRIER; o->t_prov = DS_PROV_SPECIFIED; + sset(o->id, sizeof o->id, v); + int self = m->n_ops++; + char tmp[DS_SRC]; sset(tmp, sizeof tmp, list); + char *save = NULL; + for (char *t = strtok_r(tmp, ",", &save); t; t = strtok_r(NULL, ",", &save)) { + int a = ds_find_op(m, t); + if (a < 0 || a == self) PERR("sync lists unknown op %s", t); + if (m->n_deps >= DS_MAX_DEPS) PERR("too many deps"); + m->deps[m->n_deps].from = a; m->deps[m->n_deps].to = self; m->n_deps++; + } + } else if (!strcmp(keyword, "energy")) { + const char *v; + if (m->has_energy) PERR("one energy record only"); + if (!(v = get(kvs, n, "instrument"))) PERR("energy needs instrument="); + sset(m->instrument, sizeof m->instrument, v); + if ((v = get(kvs, n, "boundary"))) sset(m->boundary, sizeof m->boundary, v); + const char *j = get(kvs, n, "joules"), *pw = get(kvs, n, "power_w"); + int prov = -1; char src[DS_SRC]; + int rc = parse_prov_src(kvs, n, &prov, src, sizeof src, j || pw ? 1 : 0, err, errcap, lineno); if (rc) return rc; + m->energy_prov = prov; + if (j) { + if (!strcmp(m->instrument, "none") || !m->boundary[0]) PERR("joules= needs a named instrument and boundary="); + if (prov != DS_PROV_MEASURED) PERR("joules= must be prov=measured with src="); + if (!parse_real(j, &m->joules)) PERR("bad joules"); + } + if (pw) { + if (prov != DS_PROV_SYNTHETIC) PERR("power_w= is allowed only as prov=synthetic (a sensitivity figure); measured power must be integrated into joules= by the instrument"); + if (!parse_real(pw, &m->power_w)) PERR("bad power_w"); + } + m->has_energy = 1; + } else if (!strcmp(keyword, "removed")) { + if (m->n_removed >= DS_MAX_REMOVED) PERR("too many removed records"); + ds_removed *r = &m->removed[m->n_removed]; + memset(r, 0, sizeof *r); + const char *v; + if (!(v = get(kvs, n, "op"))) PERR("removed needs op="); + sset(r->op, sizeof r->op, v); + if (ds_find_op(m, v) >= 0) PERR("removed op %s is still present in this DAG", v); + if (!(v = get(kvs, n, "reason"))) PERR("removed needs reason="); + if (!strcmp(v, "eliminated")) r->reason = DS_RM_ELIMINATED; + else if (!strcmp(v, "reused")) r->reason = DS_RM_REUSED; + else if (!strcmp(v, "unchanged_dep")) r->reason = DS_RM_UNCHANGED_DEP; + else if (!strcmp(v, "deferred")) r->reason = DS_RM_DEFERRED; + else if (!strcmp(v, "moved")) r->reason = DS_RM_MOVED; + else if (!strcmp(v, "shared")) r->reason = DS_RM_SHARED; + else if (!strcmp(v, "lookup")) r->reason = DS_RM_LOOKUP; + else PERR("removed reason=%s unknown", v); + if ((v = get(kvs, n, "ref"))) sset(r->ref, sizeof r->ref, v); + if ((v = get(kvs, n, "proof"))) sset(r->proof, sizeof r->proof, v); + m->n_removed++; + } else PERR("unknown record '%s'", keyword); + char bad[32]; + if (unused_key(kvs, n, bad, sizeof bad)) PERR("unknown field %s= on '%s'", bad, keyword); + } + lineno = 0; + if (!seen_oev) PERR("missing 'oev version=1'"); + if (!seen_machine) PERR("missing machine record"); + if (!seen_build) PERR("missing build record"); + if (!m->has_energy) PERR("missing energy record (use instrument=none for UNMEASURED)"); + return DS_OK; +} + +int ds_parse_file(const char *path, ds_model *m, char *err, size_t errcap) { + FILE *fp = fopen(path, "r"); + if (!fp) { snprintf(err, errcap, "cannot open %s", path); return DS_PARSE_ERROR; } + fseek(fp, 0, SEEK_END); long n = ftell(fp); fseek(fp, 0, SEEK_SET); + if (n < 0 || n > (1 << 20)) { fclose(fp); snprintf(err, errcap, "file size out of range"); return DS_PARSE_ERROR; } + char *buf = malloc((size_t)n + 1); + if (!buf) { fclose(fp); snprintf(err, errcap, "out of memory"); return DS_PARSE_ERROR; } + size_t got = fread(buf, 1, (size_t)n, fp); buf[got] = 0; fclose(fp); + int rc = ds_parse_text(buf, m, err, errcap); + free(buf); + return rc; +} + +/* ---------------------------------------------------------------- evaluate */ + +/* resolved per-op numbers for one evaluation mode */ +typedef struct { double t, launch, bytes, cap; int prov; } ds_num; /* cap in B/ns */ + +static double res_cap(const ds_resource *r, int mode) { + if (mode < 0) return r->lo; + if (mode > 0) return r->hi; + return r->cap; +} + +static int refuse(ds_result *r, int status, const char *fmt, const char *a, const char *b) { + memset(r, 0, sizeof *r); + r->status = status; + snprintf(r->reason, sizeof r->reason, fmt, a ? a : "", b ? b : ""); + return status; +} + +static int resolve(const ds_model *m, const ds_opts *o, ds_num *num, ds_result *r) { + int any_cal = 0; + for (int i = 0; i < m->n_res; i++) { + if (m->res[i].prov == DS_PROV_UNKNOWN) return refuse(r, DS_REFUSED_UNKNOWN, "resource %s has prov=unknown; unknown stays unknown%s", m->res[i].id, ""); + if (m->res[i].prov == DS_PROV_MEASURED || m->res[i].prov == DS_PROV_FITTED) any_cal = 1; + } + for (int i = 0; i < m->n_ops; i++) { + const ds_op *op = &m->ops[i]; + ds_num *x = &num[i]; + memset(x, 0, sizeof *x); + x->prov = op->t_prov; + x->launch = op->launch_ns; + x->bytes = op->bytes; + x->cap = op->bw >= 0 ? res_cap(&m->res[op->bw], o->mode) * 1e-9 : 0; /* B/s -> B/ns */ + if (op->kind == DS_OP_MATVEC && !op->has_t) { + const RxCostModel *cm = (const RxCostModel *)o->costmodel; + if (!cm) return refuse(r, DS_REFUSED_UNKNOWN, "op %s is a matvec with no rx_costmodel blob supplied (calibration model=)%s", op->id, ""); + RxCmFeatures f; memset(&f, 0, sizeof f); + f.op = RX_CM_OP_MATVEC; f.M = op->M; f.N = op->N; + f.state_bytes = ((uint64_t)op->M * op->N + op->M + op->N) * 8u; + f.cache_bytes = 1u << 20; f.core = op->core; f.eligible = 1u << op->arm; f.current_arm = op->arm; + RxCmPrediction p; rx_cm_predict(cm, &f, op->arm, &p); + if (!p.known || p.n == 0) return refuse(r, DS_REFUSED_UNKNOWN, "op %s: rx_costmodel has no known cell for this (op, core, pressure) and arm; measure it first%s", op->id, ""); + double q = o->interval_q > 0 && o->interval_q < 1 ? o->interval_q : 0.9; + double k = rx_cm_t_quantile(q, p.dof); + double l2 = p.mean_log2_ps + (o->mode < 0 ? k * p.sd_log2 : o->mode > 0 ? -k * p.sd_log2 : 0); + x->t = pow(2.0, l2) / 1000.0; /* ps -> ns */ + x->prov = DS_PROV_FITTED; + any_cal = 1; + } else { + if (op->t_prov == DS_PROV_UNKNOWN) return refuse(r, DS_REFUSED_UNKNOWN, "op %s has prov=unknown (%s); unknown stays unknown", op->id, op->src[0] ? op->src : "no source"); + x->t = o->mode < 0 ? op->hi : o->mode > 0 ? op->lo : op->t_ns; + if (op->t_prov == DS_PROV_MEASURED || op->t_prov == DS_PROV_FITTED) any_cal = 1; + } + if (op->engine < 0 && op->kind != DS_OP_BARRIER) return refuse(r, DS_REFUSED_UNSUPPORTED, "op %s has no engine%s", op->id, ""); + if (op->launcher >= 0 && op->engine < 0) return refuse(r, DS_REFUSED_UNSUPPORTED, "op %s: launch without an engine%s", op->id, ""); + } + if (any_cal) { + if (!m->has_calibration) return refuse(r, DS_REFUSED_NO_CALIBRATION, "measured or fitted values need a calibration record naming when and under what load they were taken%s%s", "", ""); + int asof = o->asof_day; + if (asof <= 0) asof = (int)(time(NULL) / 86400); + if (asof > m->cal_taken_day + m->cal_valid_days) { + char d[64]; snprintf(d, sizeof d, "asof is %d day(s) past the validity envelope", asof - m->cal_taken_day - m->cal_valid_days); + return refuse(r, DS_REFUSED_STALE, "calibration %s is stale: %s", m->cal_id, d); + } + } + return DS_OK; +} + +/* Kahn topological order; returns 0 on a cycle */ +static int topo(const ds_model *m, int *order) { + int indeg[DS_MAX_OPS] = {0}; + for (int i = 0; i < m->n_deps; i++) indeg[m->deps[i].to]++; + int n = 0, head = 0; + for (int i = 0; i < m->n_ops; i++) if (indeg[i] == 0) order[n++] = i; + while (head < n) { + int u = order[head++]; + for (int i = 0; i < m->n_deps; i++) if (m->deps[i].from == u && --indeg[m->deps[i].to] == 0) order[n++] = m->deps[i].to; + } + return n == m->n_ops; +} + +enum { ST_WAIT = 0, ST_READY, ST_LAUNCH, ST_RUN, ST_DONE }; +enum { BLK_NONE = 0, BLK_SLOT, BLK_LAUNCHER }; + +int ds_eval(const ds_model *m, const ds_opts *o, ds_result *r) { + memset(r, 0, sizeof *r); + if (m->n_ops == 0) return refuse(r, DS_REFUSED_UNSUPPORTED, "empty DAG%s%s", "", ""); + ds_num num[DS_MAX_OPS]; + int rc = resolve(m, o, num, r); + if (rc) return rc; + int order[DS_MAX_OPS]; + if (!topo(m, order)) return refuse(r, DS_REFUSED_CYCLE, "dependency cycle%s%s", "", ""); + /* a buffer that never fits */ + for (int i = 0; i < m->n_buf; i++) + if (m->buf[i].bytes > m->res[m->buf[i].cap].cap) + return refuse(r, DS_REFUSED_CAPACITY, "buffer %s alone exceeds capacity %s", m->buf[i].id, m->res[m->buf[i].cap].id); + + /* provenance mask */ + for (int i = 0; i < m->n_ops; i++) r->prov_mask |= 1u << num[i].prov; + for (int i = 0; i < m->n_res; i++) { + int used = 0; + for (int j = 0; j < m->n_ops; j++) if (m->ops[j].engine == i || m->ops[j].bw == i || m->ops[j].launcher == i) used = 1; + for (int j = 0; j < m->n_buf; j++) if (m->buf[j].cap == i) used = 1; + if (used) r->prov_mask |= 1u << m->res[i].prov; + } + + /* ---- fluid simulation ---- */ + int state[DS_MAX_OPS] = {0}, blocked[DS_MAX_OPS] = {0}, npred[DS_MAX_OPS] = {0}, done_pred[DS_MAX_OPS] = {0}; + double rem_t[DS_MAX_OPS], rem_b[DS_MAX_OPS], rem_l[DS_MAX_OPS], run_start[DS_MAX_OPS], bytes_end[DS_MAX_OPS]; + int free_slots[DS_MAX_RES], launcher_busy[DS_MAX_RES] = {0}; + for (int i = 0; i < m->n_res; i++) free_slots[i] = m->res[i].slots; + for (int i = 0; i < m->n_deps; i++) npred[m->deps[i].to]++; + for (int i = 0; i < m->n_ops; i++) { + rem_t[i] = num[i].t; rem_b[i] = num[i].bytes; rem_l[i] = num[i].launch; + run_start[i] = bytes_end[i] = -1; + r->n_ops++; + r->total_bytes += num[i].bytes; r->total_busy_ns += num[i].t; r->total_launch_ns += num[i].launch; + } + double t = 0; + int remaining = m->n_ops, guard = 0; + while (remaining > 0) { + if (++guard > 8 * DS_MAX_OPS + 16) return refuse(r, DS_REFUSED_UNSUPPORTED, "scheduler did not converge%s%s", "", ""); + /* deps satisfied -> ready */ + for (int i = 0; i < m->n_ops; i++) if (state[i] == ST_WAIT && done_pred[i] == npred[i]) { state[i] = ST_READY; r->ready_ns[i] = t; } + /* dispatch in file order */ + for (int i = 0; i < m->n_ops; i++) { + if (state[i] != ST_READY) continue; + const ds_op *op = &m->ops[i]; + if (op->engine >= 0 && free_slots[op->engine] <= 0) { blocked[i] = BLK_SLOT; continue; } + if (op->launcher >= 0 && rem_l[i] > 0 && launcher_busy[op->launcher]) { blocked[i] = BLK_LAUNCHER; continue; } + if (op->engine >= 0) free_slots[op->engine]--; + r->start_ns[i] = t; + if (op->launcher >= 0 && rem_l[i] > 0) { launcher_busy[op->launcher] = 1; state[i] = ST_LAUNCH; } + else { state[i] = ST_RUN; run_start[i] = t; } + } + /* next event */ + int active[DS_MAX_RES] = {0}; + for (int i = 0; i < m->n_ops; i++) if (state[i] == ST_RUN && rem_b[i] > 0) active[m->ops[i].bw]++; + double dt = -1; int any = 0; + for (int i = 0; i < m->n_ops; i++) { + if (state[i] == ST_LAUNCH) { any = 1; if (dt < 0 || rem_l[i] < dt) dt = rem_l[i]; } + else if (state[i] == ST_RUN) { + any = 1; + /* next event for this op: the earlier of its two progress bars */ + double d = -1; + if (rem_t[i] > 0) d = rem_t[i]; + if (rem_b[i] > 0) { + double share = num[i].cap / active[m->ops[i].bw]; + double db = rem_b[i] / share; + if (d < 0 || db < d) d = db; + } + if (d < 0) d = 0; + if (dt < 0 || d < dt) dt = d; + } + } + if (!any) return refuse(r, DS_REFUSED_UNSUPPORTED, "no op can make progress (slots=0 or all blocked)%s%s", "", ""); + if (dt < 0) dt = 0; + t += dt; + const double eps = 1e-9; + for (int i = 0; i < m->n_ops; i++) { + if (state[i] == ST_LAUNCH) { + rem_l[i] -= dt; + if (rem_l[i] <= eps) { rem_l[i] = 0; launcher_busy[m->ops[i].launcher] = 0; state[i] = ST_RUN; run_start[i] = t; } + } else if (state[i] == ST_RUN) { + if (rem_t[i] > 0) { rem_t[i] -= dt; if (rem_t[i] <= eps) rem_t[i] = 0; } + if (rem_b[i] > 0) { + double share = num[i].cap / active[m->ops[i].bw]; + rem_b[i] -= share * dt; + if (rem_b[i] <= eps * (num[i].bytes > 1 ? num[i].bytes : 1)) { rem_b[i] = 0; bytes_end[i] = t; } + } + if (rem_t[i] <= 0 && rem_b[i] <= 0) { + state[i] = ST_DONE; r->end_ns[i] = t; remaining--; + if (m->ops[i].engine >= 0) free_slots[m->ops[i].engine]++; + for (int d = 0; d < m->n_deps; d++) if (m->deps[d].from == i) done_pred[m->deps[d].to]++; + if (num[i].bytes > 0) { + double ideal = num[i].bytes / num[i].cap; + double actual = (bytes_end[i] >= 0 ? bytes_end[i] : t) - run_start[i]; + r->op_contention_ns[i] = actual - ideal > 0 ? actual - ideal : 0; + } + } + } + } + if (t > r->makespan_ns) r->makespan_ns = t; + } + + /* ---- bounds ---- */ + double cp[DS_MAX_OPS]; + for (int k = 0; k < m->n_ops; k++) { + int i = order[k]; + double floor_ = num[i].t; + if (num[i].bytes > 0 && num[i].bytes / num[i].cap > floor_) floor_ = num[i].bytes / num[i].cap; + floor_ += num[i].launch; + r->op_floor_ns[i] = floor_; + double best = 0; + for (int d = 0; d < m->n_deps; d++) if (m->deps[d].to == i && cp[m->deps[d].from] > best) best = cp[m->deps[d].from]; + cp[i] = best + floor_; + if (cp[i] > r->bound_critical_path_ns) r->bound_critical_path_ns = cp[i]; + } + r->bound_ns = r->bound_critical_path_ns; + for (int i = 0; i < m->n_res; i++) { + double w = 0; + for (int j = 0; j < m->n_ops; j++) { + if (m->res[i].kind == DS_RES_BANDWIDTH && m->ops[j].bw == i) w += num[j].bytes / num[j].cap; + if (m->res[i].kind == DS_RES_COMPUTE && m->ops[j].engine == i) w += (num[j].t + num[j].launch) / m->res[i].slots; + if (m->res[i].kind == DS_RES_LAUNCH && m->ops[j].launcher == i) w += num[j].launch; + } + r->bound_resource_ns[i] = w; + if (w > r->bound_ns) r->bound_ns = w; + } + + /* ---- capacity ledger: frees before allocations at the same instant ---- */ + for (int c = 0; c < m->n_res; c++) { + if (m->res[c].kind != DS_RES_CAPACITY) continue; + double live = 0, peak = 0, peak_at = 0; + /* event list */ + double et[2 * DS_MAX_BUF]; double ev[2 * DS_MAX_BUF]; int en = 0; + for (int b = 0; b < m->n_buf; b++) { + if (m->buf[b].cap != c) continue; + double s = m->buf[b].first >= 0 ? r->start_ns[m->buf[b].first] : 0; + double e = m->buf[b].last >= 0 ? r->end_ns[m->buf[b].last] : r->makespan_ns; + et[en] = s; ev[en] = m->buf[b].bytes; en++; + et[en] = e; ev[en] = -m->buf[b].bytes; en++; + } + /* insertion sort by time, frees (negative) first on ties; stable */ + for (int i = 1; i < en; i++) { + double kt = et[i], kv_ = ev[i]; int j = i - 1; + while (j >= 0 && (et[j] > kt || (et[j] == kt && ev[j] > 0 && kv_ < 0))) { et[j + 1] = et[j]; ev[j + 1] = ev[j]; j--; } + et[j + 1] = kt; ev[j + 1] = kv_; + } + for (int i = 0; i < en; i++) { live += ev[i]; if (live > peak) { peak = live; peak_at = et[i]; } } + r->peak_live_bytes[c] = peak; r->peak_live_at_ns[c] = peak_at; + if (peak > m->res[c].cap + 1e-9) { + char names[160] = ""; size_t L = 0; + for (int b = 0; b < m->n_buf && L < sizeof names - 8; b++) { + if (m->buf[b].cap != c) continue; + double s = m->buf[b].first >= 0 ? r->start_ns[m->buf[b].first] : 0; + double e = m->buf[b].last >= 0 ? r->end_ns[m->buf[b].last] : r->makespan_ns; + if (s <= peak_at && e > peak_at) { snprintf(names + L, sizeof names - L, "%s%s", L ? "," : "", m->buf[b].id); L = strlen(names); } + } + char what[200]; snprintf(what, sizeof what, "%.0f B live at t=%.0f ns (%s) exceeds", peak, peak_at, names); + int st = refuse(r, DS_REFUSED_CAPACITY, "%s %s", what, m->res[c].id); + return st; + } + } + + /* ---- attribution along the schedule's critical chain ---- */ + int cur = 0; + for (int i = 1; i < m->n_ops; i++) if (r->end_ns[i] > r->end_ns[cur]) cur = i; + double accounted = 0; + int chain[DS_MAX_OPS]; int nc = 0; + while (cur >= 0 && nc < DS_MAX_OPS) { + chain[nc++] = cur; + const ds_op *op = &m->ops[cur]; + double runtime = r->end_ns[cur] - (run_start[cur] >= 0 ? run_start[cur] : r->start_ns[cur]); + r->attr_ns[DS_ATTR_LAUNCH] += num[cur].launch; + accounted += num[cur].launch; + if (op->kind == DS_OP_BARRIER) r->attr_ns[DS_ATTR_SYNC] += runtime; + else if (num[cur].bytes > 0) { + double ideal = num[cur].bytes / num[cur].cap; + double actual = ideal + r->op_contention_ns[cur]; + if (num[cur].t >= actual) r->attr_ns[DS_ATTR_COMPUTE] += runtime; + else { r->attr_ns[DS_ATTR_BANDWIDTH] += ideal; r->attr_ns[DS_ATTR_CONTENTION] += r->op_contention_ns[cur]; if (runtime > actual) r->attr_ns[DS_ATTR_RESIDUAL] += runtime - actual; } + } else r->attr_ns[DS_ATTR_COMPUTE] += runtime; + accounted += runtime; + /* predecessor: the dep that finished last */ + int pred = -1; + for (int d = 0; d < m->n_deps; d++) if (m->deps[d].to == cur && (pred < 0 || r->end_ns[m->deps[d].from] > r->end_ns[pred])) pred = m->deps[d].from; + double gap = r->start_ns[cur] - (pred >= 0 ? r->end_ns[pred] : 0); + if (gap > 0) { + if (blocked[cur] == BLK_SLOT) r->attr_ns[DS_ATTR_SERIALIZATION] += gap; + else if (blocked[cur] == BLK_LAUNCHER) r->attr_ns[DS_ATTR_LAUNCH] += gap; + else r->attr_ns[DS_ATTR_RESIDUAL] += gap; + accounted += gap; + } + cur = pred; + } + for (int i = 0; i < nc; i++) r->critical_path[i] = chain[nc - 1 - i]; + r->n_critical = nc; + double leftover = r->makespan_ns - accounted; + if (fabs(leftover) > 1e-6) r->attr_ns[DS_ATTR_RESIDUAL] += leftover; + + /* ---- energy ---- */ + if (m->has_energy && m->joules > 0 && strcmp(m->instrument, "none")) { r->energy_measured = 1; r->energy_j = m->joules; } + else if (m->has_energy && m->power_w > 0 && m->energy_prov == DS_PROV_SYNTHETIC) { r->energy_synthetic = 1; r->energy_j = m->power_w * r->makespan_ns * 1e-9; r->prov_mask |= 1u << DS_PROV_SYNTHETIC; } + r->synthetic = (r->prov_mask & (1u << DS_PROV_SYNTHETIC)) ? 1 : 0; + r->status = DS_OK; + return DS_OK; +} + +int ds_eval_interval(const ds_model *m, const ds_opts *o, ds_result *nominal, double *lo_ns, double *hi_ns) { + ds_opts x = *o; x.mode = 0; + int rc = ds_eval(m, &x, nominal); + *lo_ns = *hi_ns = 0; + if (rc) return rc; + ds_result tmp; + x.mode = +1; if (ds_eval(m, &x, &tmp) == DS_OK) *lo_ns = tmp.makespan_ns; + x.mode = -1; if (ds_eval(m, &x, &tmp) == DS_OK) *hi_ns = tmp.makespan_ns; + return rc; +} + +double ds_counterfactual(const ds_model *m, const ds_opts *o, int res_index) { + if (res_index < 0 || res_index >= m->n_res) return -DS_REFUSED_UNSUPPORTED; + ds_model *c = malloc(sizeof *c); + if (!c) return -DS_REFUSED_UNSUPPORTED; + *c = *m; + ds_resource *r = &c->res[res_index]; + switch (r->kind) { + case DS_RES_BANDWIDTH: r->cap *= 2; r->lo *= 2; r->hi *= 2; break; + case DS_RES_COMPUTE: r->slots *= 2; break; + case DS_RES_LAUNCH: for (int i = 0; i < c->n_ops; i++) if (c->ops[i].launcher == res_index) c->ops[i].launch_ns = 0; break; + case DS_RES_CAPACITY: r->cap *= 2; break; + } + ds_result res; ds_opts x = *o; x.mode = 0; + int rc = ds_eval(c, &x, &res); + free(c); + return rc == DS_OK ? res.makespan_ns : -(double)rc; +} + +/* ---------------------------------------------------------------- diff */ + +static int in_changed(const char *list, const char *id) { + if (!list) return 0; + const char *p = list; size_t L = strlen(id); + while (*p) { + const char *q = strchr(p, ','); size_t n = q ? (size_t)(q - p) : strlen(p); + if (n == L && !strncmp(p, id, L)) return 1; + if (!q) break; + p = q + 1; + } + return 0; +} + +int ds_diff_models(const ds_model *b, const ds_model *t, const ds_opts *o, ds_diff *d) { + memset(d, 0, sizeof *d); + for (int i = 0; i < t->n_ops; i++) if (ds_find_op(b, t->ops[i].id) < 0) d->n_added++; + for (int i = 0; i < b->n_ops; i++) { + int j = ds_find_op(t, b->ops[i].id); + if (j >= 0) { + d->n_kept++; + if (b->ops[i].t_ns != t->ops[j].t_ns || b->ops[i].bytes != t->ops[j].bytes) + snprintf(d->reason, sizeof d->reason, "op %s kept but its demand changed; trace conservation broken", b->ops[i].id); + continue; + } + d->n_removed++; + /* find its removed record(s) */ + int found = 0; const ds_removed *rec = NULL; + for (int k = 0; k < t->n_removed; k++) if (!strcmp(t->removed[k].op, b->ops[i].id)) { found++; rec = &t->removed[k]; } + if (found == 0) { d->required_removed = 1; snprintf(d->reason, sizeof d->reason, "op %s vanished without a removed record", b->ops[i].id); return DS_REFUSED_REMOVED_REQUIRED; } + if (found > 1) { d->double_counted = 1; snprintf(d->reason, sizeof d->reason, "op %s removed %d times (double counted)", b->ops[i].id, found); return DS_REFUSED_REMOVED_REQUIRED; } + /* consumers of the removed op in the baseline that still exist */ + int has_consumer = 0; + for (int k = 0; k < b->n_deps; k++) if (b->deps[k].from == i && ds_find_op(t, b->ops[b->deps[k].to].id) >= 0) has_consumer = 1; + int ok = 1; + switch (rec->reason) { + case DS_RM_ELIMINATED: if (has_consumer && !rec->proof[0]) ok = 0; break; + case DS_RM_REUSED: case DS_RM_LOOKUP: case DS_RM_MOVED: if (!rec->ref[0] || ds_find_op(t, rec->ref) < 0) ok = 0; break; + case DS_RM_UNCHANGED_DEP: if (!rec->ref[0] || ds_find_op(b, rec->ref) < 0 || in_changed(o ? o->changed : NULL, rec->ref)) ok = 0; break; + case DS_RM_DEFERRED: if (has_consumer) ok = 0; break; + case DS_RM_SHARED: + if (!rec->ref[0] || ds_find_op(t, rec->ref) < 0) ok = 0; + for (int k = 0; k < t->n_removed; k++) if (!strcmp(t->removed[k].op, rec->ref)) { d->double_counted = 1; ok = 0; } + break; + } + if (!ok) { + d->required_removed = 1; + snprintf(d->reason, sizeof d->reason, "op %s: removal reason %d not supported by its evidence (ref=%s proof=%s changed=%s)", + b->ops[i].id, rec->reason, rec->ref, rec->proof, o && o->changed ? o->changed : ""); + return DS_REFUSED_REMOVED_REQUIRED; + } + double w = b->ops[i].t_ns + b->ops[i].launch_ns; + if (b->ops[i].bytes > 0 && b->ops[i].bw >= 0) { double tb = b->ops[i].bytes / (b->res[b->ops[i].bw].cap * 1e-9); if (tb > b->ops[i].t_ns) w = tb + b->ops[i].launch_ns; } + d->removed_ns[rec->reason] += w; + } + /* removed records that name ops never in the baseline are fabricated savings */ + for (int k = 0; k < t->n_removed; k++) if (ds_find_op(b, t->removed[k].op) < 0) { + d->double_counted = 1; + snprintf(d->reason, sizeof d->reason, "removed record for %s, which was never in the baseline", t->removed[k].op); + return DS_REFUSED_REMOVED_REQUIRED; + } + return DS_OK; +} diff --git a/tools/dagsim/dagsim.h b/tools/dagsim/dagsim.h new file mode 100644 index 00000000..a409b588 --- /dev/null +++ b/tools/dagsim/dagsim.h @@ -0,0 +1,166 @@ +/* + * dagsim.h -- offline resource-DAG cost evaluator (omega-mv#583). + * + * Reads an OEV1 file (spec/offline-evidence-v1.md) and composes the per-op + * costs it carries, or that rx_costmodel predicts for matvec ops, into a + * whole-DAG schedule under finite resources: compute slots, shared + * bandwidth (processor sharing), serialized launches, one capacity ledger, + * dependencies and barriers. It never executes anything. It refuses rather + * than guess: unknown provenance, stale calibration, capacity overflow, + * unsupported constructs and cycles are all refusals with a reason. + * + * Host only. No GPU, no authority, no generation store. + */ +#ifndef DAGSIM_H +#define DAGSIM_H + +#include +#include + +#define DS_MAX_OPS 256 +#define DS_MAX_RES 32 +#define DS_MAX_BUF 256 +#define DS_MAX_DEPS 1024 +#define DS_MAX_REMOVED 256 +#define DS_ID 40 +#define DS_SRC 128 + +enum ds_prov { DS_PROV_UNKNOWN = 0, DS_PROV_SYNTHETIC, DS_PROV_FITTED, DS_PROV_MEASURED, DS_PROV_SPECIFIED }; +enum ds_res_kind { DS_RES_COMPUTE = 1, DS_RES_BANDWIDTH, DS_RES_CAPACITY, DS_RES_LAUNCH }; +enum ds_op_kind { DS_OP_COMPUTE = 1, DS_OP_COPY, DS_OP_MATVEC, DS_OP_BARRIER, DS_OP_STORAGE, DS_OP_PERSIST }; +enum ds_removed_reason { DS_RM_ELIMINATED = 1, DS_RM_REUSED, DS_RM_UNCHANGED_DEP, DS_RM_DEFERRED, DS_RM_MOVED, DS_RM_SHARED, DS_RM_LOOKUP }; + +enum ds_status { + DS_OK = 0, + DS_REFUSED_UNKNOWN, /* an op or resource with prov=unknown feeds the result */ + DS_REFUSED_STALE, /* asof is past calibration taken + valid_days */ + DS_REFUSED_NO_CALIBRATION, /* measured/fitted values but no calibration record */ + DS_REFUSED_CAPACITY, /* peak live bytes exceed a capacity resource */ + DS_REFUSED_CYCLE, + DS_REFUSED_UNSUPPORTED, /* construct the evaluator does not model */ + DS_REFUSED_REMOVED_REQUIRED, /* a remaining op depends on removed work without a valid derivation */ + DS_PARSE_ERROR +}; + +typedef struct { + char id[DS_ID]; + int kind; + int slots; /* compute */ + double cap, lo, hi; /* bandwidth (B/s) or capacity (B) */ + int prov; + char src[DS_SRC]; +} ds_resource; + +typedef struct { + char id[DS_ID]; + int kind; + int engine; /* resource index or -1 */ + double t_ns, lo, hi; /* own busy time */ + int t_prov; + int has_t; + double bytes; + int bw; /* bandwidth resource index or -1 */ + double launch_ns; + int launcher; /* launch resource index or -1 */ + uint32_t M, N, core, arm; /* matvec */ + char src[DS_SRC]; +} ds_op; + +typedef struct { char id[DS_ID]; double bytes; int cap; int first, last; } ds_buffer; +typedef struct { int from, to; } ds_dep; +typedef struct { char op[DS_ID]; int reason; char ref[DS_ID]; char proof[DS_SRC]; } ds_removed; + +typedef struct { + int version; + char machine_id[DS_ID], soc[DS_ID]; + int cores; + double mem_bytes; + char build_sha[48], physics[48], repo[DS_ID]; + int has_calibration; + char cal_id[DS_ID]; + int cal_taken_day; /* days since 1970-01-01 */ + int cal_valid_days; + double cal_load1; + char cal_src[DS_SRC], cal_model[DS_SRC]; + int n_res, n_ops, n_buf, n_deps, n_removed; + ds_resource res[DS_MAX_RES]; + ds_op ops[DS_MAX_OPS]; + ds_buffer buf[DS_MAX_BUF]; + ds_dep deps[DS_MAX_DEPS]; + ds_removed removed[DS_MAX_REMOVED]; + /* energy */ + char instrument[DS_ID], boundary[DS_ID]; + int energy_prov; + double joules, power_w; + int has_energy; +} ds_model; + +typedef struct { + int asof_day; /* days since epoch; 0 = today */ + int mode; /* 0 nominal, -1 pessimistic (lo bandwidth, hi time), +1 optimistic */ + const void *costmodel; /* const RxCostModel* or NULL */ + double interval_q; /* for rx_costmodel intervals, e.g. 0.9 */ + const char *changed; /* diff only: comma list of baseline ops whose inputs changed */ +} ds_opts; + +enum { DS_ATTR_COMPUTE = 0, DS_ATTR_BANDWIDTH, DS_ATTR_CONTENTION, DS_ATTR_LAUNCH, DS_ATTR_SERIALIZATION, DS_ATTR_SYNC, DS_ATTR_RESIDUAL, DS_ATTR_N }; + +typedef struct { + int status; + char reason[256]; + double makespan_ns; + double start_ns[DS_MAX_OPS], end_ns[DS_MAX_OPS], ready_ns[DS_MAX_OPS]; + double op_contention_ns[DS_MAX_OPS]; /* extra traffic time due to sharing */ + double op_floor_ns[DS_MAX_OPS]; /* launch + max(t, bytes/cap) */ + double bound_critical_path_ns; + double bound_resource_ns[DS_MAX_RES]; /* per resource work bound */ + double bound_ns; /* max of all bounds */ + double peak_live_bytes[DS_MAX_RES]; /* per capacity resource */ + double peak_live_at_ns[DS_MAX_RES]; + double attr_ns[DS_ATTR_N]; + int critical_path[DS_MAX_OPS], n_critical; + uint32_t prov_mask; /* bit per ds_prov that fed the result */ + int synthetic; + /* conservation: what went in is what was scheduled */ + int n_ops; + double total_bytes, total_busy_ns, total_launch_ns; + /* energy */ + int energy_measured; /* 1: joules from a named instrument */ + int energy_synthetic; /* 1: E = P*T from a synthetic power figure */ + double energy_j; +} ds_result; + +/* Parse. Returns DS_OK or DS_PARSE_ERROR with the line and reason in `err`. */ +int ds_parse_file(const char *path, ds_model *m, char *err, size_t errcap); +int ds_parse_text(const char *text, ds_model *m, char *err, size_t errcap); + +/* Evaluate deterministically. Fills every field of `r` or sets a refusal. */ +int ds_eval(const ds_model *m, const ds_opts *o, ds_result *r); + +/* Three-point interval: nominal, lo (optimistic) and hi (pessimistic) makespan. + * Returns the nominal status; lo/hi are 0 when any point refused. */ +int ds_eval_interval(const ds_model *m, const ds_opts *o, ds_result *nominal, double *lo_ns, double *hi_ns); + +/* Counterfactual sensitivity: makespan with one resource doubled (bandwidth cap x2, + * compute slots x2, launch cost 0). Returns the new makespan or a negative status. */ +double ds_counterfactual(const ds_model *m, const ds_opts *o, int res_index); + +/* Minimum-work accounting: compare a baseline and a transformed model by stable op + * ids. Sums removed work by reason and detects double-counted shared work and removal + * of required work. */ +typedef struct { + double removed_ns[8]; /* by ds_removed_reason */ + int n_removed, n_added, n_kept; + int double_counted; /* an op removed with reason=shared appears twice or refs a missing op */ + int required_removed; /* a removed op without a valid derivation still has consumers */ + char reason[256]; +} ds_diff; +int ds_diff_models(const ds_model *baseline, const ds_model *transformed, const ds_opts *o, ds_diff *d); + +const char *ds_status_name(int s); +const char *ds_prov_name(int p); +int ds_day_from_date(const char *yyyy_mm_dd); +int ds_find_op(const ds_model *m, const char *id); +int ds_find_res(const ds_model *m, const char *id); + +#endif /* DAGSIM_H */ diff --git a/tools/dagsim/dagsim_cli.c b/tools/dagsim/dagsim_cli.c new file mode 100644 index 00000000..fb279f90 --- /dev/null +++ b/tools/dagsim/dagsim_cli.c @@ -0,0 +1,222 @@ +/* + * dagsim_cli -- reproducible command line for the offline resource-DAG + * evaluator (omega-mv#583). No GPU, no execution: it reads OEV1 files and + * prints JSON. Every number it prints names the provenance it came from. + * + * dagsim eval FILE [--asof YYYY-MM-DD] [--model BLOB] + * dagsim pareto FILE... [--asof YYYY-MM-DD] [--model BLOB] + * dagsim diff BASELINE TRANSFORMED [--changed a,b,...] + */ +#include "dagsim.h" +#include "runtime/rx_costmodel.h" + +#include +#include +#include +#include + +static void jstr(const char *s) { + putchar('"'); + for (; *s; s++) { + if (*s == '"' || *s == '\\') { putchar('\\'); putchar(*s); } + else if ((unsigned char)*s < 0x20) printf("\\u%04x", *s); + else putchar(*s); + } + putchar('"'); +} +static void jnum(double v) { if (v == floor(v) && fabs(v) < 1e15) printf("%.0f", v); else printf("%.6g", v); } + +static int load_model(const char *path, RxCostModel *cm) { + FILE *fp = fopen(path, "rb"); + if (!fp) return 0; + size_t cap = rx_cm_blob_size(); + uint8_t *buf = malloc(cap); + if (!buf) { fclose(fp); return 0; } + size_t got = fread(buf, 1, cap, fp); + fclose(fp); + int ok = rx_cm_deserialize(cm, buf, got) == RX_CM_OK; + free(buf); + return ok; +} + +static const char *attr_name[DS_ATTR_N] = { "compute", "bandwidth", "contention", "launch", "serialization", "sync", "residual" }; +static const char *res_kind_name(int k) { + switch (k) { case DS_RES_COMPUTE: return "compute"; case DS_RES_BANDWIDTH: return "bandwidth"; case DS_RES_CAPACITY: return "capacity"; case DS_RES_LAUNCH: return "launch"; default: return "?"; } +} + +static void emit_result_json(const char *path, const ds_model *m, const ds_opts *o, const ds_result *r, double lo, double hi) { + printf("{\n \"schema\": \"oev1\", \"tool\": \"dagsim\", \"file\": "); jstr(path); + printf(",\n \"machine\": "); jstr(m->machine_id); + printf(", \"build_sha\": "); jstr(m->build_sha); + printf(",\n \"calibration\": {\"id\": "); jstr(m->has_calibration ? m->cal_id : ""); + printf(", \"taken_day\": %d, \"valid_days\": %d, \"load1\": ", m->cal_taken_day, m->cal_valid_days); jnum(m->cal_load1); + printf(", \"src\": "); jstr(m->cal_src); printf("},\n \"asof_day\": %d,\n", o->asof_day); + printf(" \"status\": "); jstr(ds_status_name(r->status)); printf(", \"reason\": "); jstr(r->reason); + printf(",\n \"provenance\": ["); int first = 1; + for (int p = 0; p <= DS_PROV_SPECIFIED; p++) if (r->prov_mask & (1u << p)) { if (!first) printf(", "); jstr(ds_prov_name(p)); first = 0; } + printf("], \"synthetic\": %d, \"offline\": true, \"gpu_executed\": false", r->synthetic); + if (r->status != DS_OK) { printf("\n}\n"); return; } + printf(",\n \"makespan_ns\": {\"nominal\": "); jnum(r->makespan_ns); printf(", \"lo\": "); jnum(lo); printf(", \"hi\": "); jnum(hi); + printf(", \"interval_known\": %s}", (lo > 0 && hi > 0) ? "true" : "false"); + printf(",\n \"bounds_ns\": {\"critical_path\": "); jnum(r->bound_critical_path_ns); printf(", \"max\": "); jnum(r->bound_ns); printf(", \"resources\": {"); + for (int i = 0; i < m->n_res; i++) { if (i) printf(", "); jstr(m->res[i].id); printf(": "); jnum(r->bound_resource_ns[i]); } + printf("}, \"makespan_ge_bounds\": %s}", r->makespan_ns + 1e-6 >= r->bound_ns ? "true" : "false"); + printf(",\n \"ops\": ["); + for (int i = 0; i < m->n_ops; i++) { + printf("%s\n {\"id\": ", i ? "," : ""); jstr(m->ops[i].id); + printf(", \"start_ns\": "); jnum(r->start_ns[i]); printf(", \"end_ns\": "); jnum(r->end_ns[i]); + printf(", \"floor_ns\": "); jnum(r->op_floor_ns[i]); printf(", \"contention_ns\": "); jnum(r->op_contention_ns[i]); + printf(", \"prov\": "); jstr(ds_prov_name(m->ops[i].kind == DS_OP_MATVEC && !m->ops[i].has_t ? DS_PROV_FITTED : m->ops[i].t_prov)); + printf(", \"src\": "); jstr(m->ops[i].src); printf("}"); + } + printf("\n ],\n \"critical_path\": ["); + for (int i = 0; i < r->n_critical; i++) { if (i) printf(", "); jstr(m->ops[r->critical_path[i]].id); } + printf("],\n \"peak_live_bytes\": {"); + first = 1; + for (int i = 0; i < m->n_res; i++) if (m->res[i].kind == DS_RES_CAPACITY) { + if (!first) printf(", "); + first = 0; + jstr(m->res[i].id); printf(": {\"bytes\": "); jnum(r->peak_live_bytes[i]); printf(", \"at_ns\": "); jnum(r->peak_live_at_ns[i]); printf(", \"cap\": "); jnum(m->res[i].cap); printf("}"); + } + printf("},\n \"attribution_ns\": {"); + for (int i = 0; i < DS_ATTR_N; i++) { if (i) printf(", "); jstr(attr_name[i]); printf(": "); jnum(r->attr_ns[i]); } + printf("},\n \"energy\": {\"status\": "); + jstr(r->energy_measured ? "MEASURED" : r->energy_synthetic ? "SYNTHETIC_SENSITIVITY_ONLY" : "UNMEASURED"); + printf(", \"joules\": "); if (r->energy_measured || r->energy_synthetic) jnum(r->energy_j); else printf("null"); + printf(", \"instrument\": "); jstr(m->instrument); printf(", \"boundary\": "); jstr(m->boundary); + printf(", \"ranking_valid\": %s}", r->energy_measured ? "true" : "false"); + printf(",\n \"conservation\": {\"n_ops\": %d, \"total_bytes\": ", r->n_ops); jnum(r->total_bytes); + printf(", \"total_busy_ns\": "); jnum(r->total_busy_ns); printf(", \"total_launch_ns\": "); jnum(r->total_launch_ns); + printf(", \"instrument_overhead_ns\": null}"); + /* counterfactual sensitivities, ranked by how much they could move the makespan, + * weighted by how uncertain the resource's own figure is */ + printf(",\n \"sensitivities\": ["); + double score[DS_MAX_RES]; int idx[DS_MAX_RES]; double cf[DS_MAX_RES]; double width[DS_MAX_RES]; + for (int i = 0; i < m->n_res; i++) { + cf[i] = ds_counterfactual(m, o, i); + width[i] = m->res[i].kind == DS_RES_BANDWIDTH && m->res[i].cap > 0 ? (m->res[i].hi - m->res[i].lo) / m->res[i].cap : 0; + if (m->res[i].prov == DS_PROV_SPECIFIED) width[i] = 1.0; /* a datasheet number is not a measurement */ + score[i] = cf[i] >= 0 ? fabs(r->makespan_ns - cf[i]) * (width[i] > 0 ? width[i] : 0.05) : 0; + idx[i] = i; + } + for (int a = 1; a < m->n_res; a++) { + int k = idx[a]; int b = a - 1; + while (b >= 0 && score[idx[b]] < score[k]) { idx[b + 1] = idx[b]; b--; } + idx[b + 1] = k; + } + for (int a = 0; a < m->n_res; a++) { + int i = idx[a]; + printf("%s\n {\"resource\": ", a ? "," : ""); jstr(m->res[i].id); + printf(", \"kind\": "); jstr(res_kind_name(m->res[i].kind)); + printf(", \"makespan_if_doubled_ns\": "); if (cf[i] >= 0) jnum(cf[i]); else printf("null"); + printf(", \"delta_ns\": "); if (cf[i] >= 0) jnum(r->makespan_ns - cf[i]); else printf("null"); + printf(", \"interval_width_frac\": "); jnum(width[i]); + printf(", \"decision_score\": "); jnum(score[i]); + printf(", \"next_experiment\": "); + if (m->res[i].kind == DS_RES_BANDWIDTH) jstr(m->res[i].prov == DS_PROV_SPECIFIED ? "measure this bandwidth on this DAG's shapes (datasheet figure, never attained)" : "repeat the bandwidth receipt on a quiet box to tighten lo/hi"); + else if (m->res[i].kind == DS_RES_LAUNCH) jstr("measure launch with the device context held open (M1 block2 follow-up)"); + else if (m->res[i].kind == DS_RES_COMPUTE) jstr("measure the critical op at the next larger shape"); + else jstr("none: capacity is a ledger, not a rate"); + printf("}"); + } + printf("\n ],\n \"unresolved_residual_ns\": "); jnum(r->attr_ns[DS_ATTR_RESIDUAL]); + printf(",\n \"note\": \"loaded-box calibration (load1 above); bounds are floors, not predictions; no performance claim from this simulation alone\"\n}\n"); +} + +static int cmd_eval(const char *path, const ds_opts *o) { + ds_model *m = malloc(sizeof *m); char err[256]; + if (!m) return 2; + if (ds_parse_file(path, m, err, sizeof err) != DS_OK) { + printf("{\"schema\": \"oev1\", \"tool\": \"dagsim\", \"file\": "); jstr(path); printf(", \"status\": \"PARSE_ERROR\", \"reason\": "); jstr(err); printf("}\n"); + free(m); return 1; + } + ds_result *r = malloc(sizeof *r); double lo, hi; + if (!r) { free(m); return 2; } + int rc = ds_eval_interval(m, o, r, &lo, &hi); + emit_result_json(path, m, o, r, lo, hi); + free(r); free(m); + return rc == DS_OK ? 0 : 1; +} + +typedef struct { const char *path; int status; double t, lo, hi, peak, e; int e_meas, e_syn, front; char reason[256]; } prow; + +static int cmd_pareto(int n, const char **paths, const ds_opts *o) { + prow rows[64]; if (n > 64) n = 64; + int all_energy = 1; + for (int k = 0; k < n; k++) { + prow *p = &rows[k]; memset(p, 0, sizeof *p); p->path = paths[k]; + ds_model *m = malloc(sizeof *m); ds_result *r = malloc(sizeof *r); char err[256]; + if (!m || !r) return 2; + if (ds_parse_file(paths[k], m, err, sizeof err) != DS_OK) { p->status = DS_PARSE_ERROR; snprintf(p->reason, sizeof p->reason, "%s", err); free(m); free(r); all_energy = 0; continue; } + p->status = ds_eval_interval(m, o, r, &p->lo, &p->hi); + snprintf(p->reason, sizeof p->reason, "%s", r->reason); + if (p->status == DS_OK) { + p->t = r->makespan_ns; + for (int i = 0; i < m->n_res; i++) if (m->res[i].kind == DS_RES_CAPACITY && r->peak_live_bytes[i] > p->peak) p->peak = r->peak_live_bytes[i]; + p->e = r->energy_j; p->e_meas = r->energy_measured; p->e_syn = r->energy_synthetic; + if (!p->e_meas) all_energy = 0; + } else all_energy = 0; + free(m); free(r); + } + /* non-dominated on (time, peak bytes[, measured joules]) among OK rows */ + for (int a = 0; a < n; a++) { + if (rows[a].status != DS_OK) continue; + rows[a].front = 1; + for (int b = 0; b < n; b++) { + if (a == b || rows[b].status != DS_OK) continue; + int le = rows[b].t <= rows[a].t && rows[b].peak <= rows[a].peak && (!all_energy || rows[b].e <= rows[a].e); + int lt = rows[b].t < rows[a].t || rows[b].peak < rows[a].peak || (all_energy && rows[b].e < rows[a].e); + if (le && lt) { rows[a].front = 0; break; } + } + } + printf("# dagsim pareto: objectives = seconds, peak bytes%s. Energy %s.\n", all_energy ? ", joules" : "", + all_energy ? "measured on every row and part of the dominance test" : "not measured on every row: shown, never ranked"); + printf("# %-36s %-24s %14s %14s %14s %12s %-14s %s\n", "file", "status", "seconds", "lo_s", "hi_s", "peak_bytes", "energy", "front"); + for (int k = 0; k < n; k++) { + prow *p = &rows[k]; + char e[32]; + if (p->e_meas) snprintf(e, sizeof e, "%.3f J", p->e); else if (p->e_syn) snprintf(e, sizeof e, "syn %.3f J", p->e); else snprintf(e, sizeof e, "UNMEASURED"); + if (p->status == DS_OK) + printf(" %-36s %-24s %14.6f %14.6f %14.6f %12.0f %-14s %s\n", p->path, "OK", p->t * 1e-9, p->lo * 1e-9, p->hi * 1e-9, p->peak, e, p->front ? "yes" : "no"); + else + printf(" %-36s %-24s %14s %14s %14s %12s %-14s %s # %s\n", p->path, ds_status_name(p->status), "-", "-", "-", "-", "-", "refused", p->reason); + } + return 0; +} + +static int cmd_diff(const char *a, const char *b, const ds_opts *o) { + ds_model *ma = malloc(sizeof *ma), *mb = malloc(sizeof *mb); char err[256]; ds_diff d; + if (!ma || !mb) return 2; + if (ds_parse_file(a, ma, err, sizeof err) != DS_OK) { fprintf(stderr, "%s: %s\n", a, err); return 1; } + if (ds_parse_file(b, mb, err, sizeof err) != DS_OK) { fprintf(stderr, "%s: %s\n", b, err); return 1; } + int rc = ds_diff_models(ma, mb, o, &d); + static const char *rn[8] = { "", "eliminated", "reused", "unchanged_dep", "deferred", "moved", "shared", "lookup" }; + printf("{\"tool\": \"dagsim diff\", \"baseline\": "); jstr(a); printf(", \"transformed\": "); jstr(b); + printf(", \"status\": "); jstr(ds_status_name(rc)); printf(", \"reason\": "); jstr(d.reason); + printf(", \"kept\": %d, \"added\": %d, \"removed\": %d, \"double_counted\": %d, \"required_removed\": %d, \"removed_ns\": {", d.n_kept, d.n_added, d.n_removed, d.double_counted, d.required_removed); + for (int i = 1; i < 8; i++) { if (i > 1) printf(", "); jstr(rn[i]); printf(": "); jnum(d.removed_ns[i]); } + printf("}, \"changed\": "); jstr(o->changed ? o->changed : ""); printf("}\n"); + free(ma); free(mb); + return rc == DS_OK ? 0 : 1; +} + +int main(int argc, char **argv) { + if (argc < 3) { fprintf(stderr, "usage: dagsim eval FILE | pareto FILE... | diff BASE TRANSFORMED [--asof YYYY-MM-DD] [--model BLOB] [--changed a,b]\n"); return 2; } + ds_opts o; memset(&o, 0, sizeof o); o.interval_q = 0.9; + RxCostModel *cm = NULL; + const char *files[64]; int nf = 0; + for (int i = 2; i < argc; i++) { + if (!strcmp(argv[i], "--asof") && i + 1 < argc) { o.asof_day = ds_day_from_date(argv[++i]); if (o.asof_day < 0) { fprintf(stderr, "bad --asof\n"); return 2; } } + else if (!strcmp(argv[i], "--model") && i + 1 < argc) { cm = malloc(sizeof *cm); if (!cm || !load_model(argv[++i], cm)) { fprintf(stderr, "cannot load rx_costmodel blob %s\n", argv[i]); return 2; } o.costmodel = cm; } + else if (!strcmp(argv[i], "--changed") && i + 1 < argc) o.changed = argv[++i]; + else if (nf < 64) files[nf++] = argv[i]; + } + if (o.asof_day == 0) { fprintf(stderr, "dagsim: --asof YYYY-MM-DD is required so the run is reproducible (calibration validity is checked against it)\n"); return 2; } + int rc; + if (!strcmp(argv[1], "eval") && nf == 1) rc = cmd_eval(files[0], &o); + else if (!strcmp(argv[1], "pareto") && nf >= 1) rc = cmd_pareto(nf, files, &o); + else if (!strcmp(argv[1], "diff") && nf == 2) rc = cmd_diff(files[0], files[1], &o); + else { fprintf(stderr, "usage: dagsim eval FILE | pareto FILE... | diff BASE TRANSFORMED\n"); rc = 2; } + free(cm); + return rc; +} diff --git a/tools/extract/drift-baseline.tsv b/tools/extract/drift-baseline.tsv index b42f3c4e..e0d83f02 100644 --- a/tools/extract/drift-baseline.tsv +++ b/tools/extract/drift-baseline.tsv @@ -1,5 +1,7 @@ # omega_path omega_blob — omega-mv e5b32567a7aff3ce594e3254a622a846a6cd190b (cutover), GPU paths per map.sh against CUDA++ 008d9bd -Makefile 4e068c1de5026d2ac4f6c2463fbb4292e6c26ec0 +# Blobs omega changed through 62d1d0d668f897386a0e311374f8dbcc5c32dde8 (#492 IMAD.HI.U32 Rc refusal, #936 dagsim) are that commit's. +# Makefile still differs: CUDA++ keeps its own build. The other refreshed paths match that commit. +Makefile 6b53cd12431392798d60c2da26875a3d7c843595 evidence/C3-GEMM/01-host-only.log c581c5126718bc24ef10ed1b1637528018a260e5 evidence/C3-GEMM/02-sim.log fd97da9f29871abfc16fb2ed938c38a4d3c2314f evidence/C3-GEMM/README.md 0e730a9bf2072f34d59d9cdc1961f4112a7ff90e @@ -146,7 +148,7 @@ mk/omega-gpu-engine.mk ac62777ff5096d83d159b89aa27d0827a98ac0bb mk/omega-gpu-wait.mk 42e5ed25b232b7ae67f922e9a4a948e3ef942159 mk/sm121dis.mk d29fcba32384d80b1fd4897b4ed1fb2eb865d631 src/omega_blackwell_codegen.c debdea0eacfc7c5aed717ea52a7137b7e3902b33 -src/omega_blackwell_codegen.h 30ff3898369d226b6b68c0af2c0d435b4dc1db2c +src/omega_blackwell_codegen.h 1f0b70eecd3860da68bbe013860f27ca01da0d2f src/omega_blackwell_codegen_attn.c 060e25cff987ff43fdb234aee39497253cf25c3a src/omega_blackwell_codegen_attn.h 4f84bba33c4cf4dfca8834335b6900aba01b677b src/omega_blackwell_codegen_attn256.c ecb4c1dae47bfa0b7d25f90468b17de79247c040 @@ -272,19 +274,36 @@ src/omega_gpu_tern_api.c 36f052a92daa588b5032aca0476648de6103094e src/omega_gpu_tern_api.h 4e81e02c195418de8859415498a61108ea0ad23b src/omega_gpu_wait.c 57325f0fd0564a9e4e5815372e62c4810a076331 src/omega_gpu_wait.h f007dbca95527ef6266daf85397779ce6f8e84e2 +src/omega_numeric.c 271f0027b37069249bb79af99d139f1aee92c36c +src/omega_numeric.h b3548189a5d0101bfa1d3f9e83abb3595528004b +src/omega_numeric_lifecycle.h a90a359306f98420a3189a488fe413319b4fad38 +src/omega_numeric_reduce.c 3dffadfe031a774191d07bddacd203fadee6a4b2 +src/omega_numeric_reduce.h ac9cf9c9b636b292ab23d5a25309cf2669496a17 src/omega_sm121_disasm.c 796db0339247b998fa37c60a15aeac6105705774 src/omega_sm121_disasm.h aaf815d6387018ff0ac2598c23dac1398672cb60 src/omega_types.h 433c3e8113c154e2c12e1cb508841319d29d1b7d src/omega_vector.c a3add41a0d77a484d4357e913fb9e7c0c9b4cc23 src/omega_vector.h 348195ca448839cb191ea1c9d5da05b4e61bdda8 +src/runtime/rx_costmodel.c d8ddf956d88d4d9164682d2223a277cd04fc37dd +src/runtime/rx_costmodel.h 6e2c0db52fafe3548e9161bc96ea9c07292dcd36 src/sha256.h 7db16bcacfbe68623fc2a042dd4cca5a388e41f9 tests/bw_encode_control_test.c 46a67b11dfc65bf7d6fab711312b6368a8814f87 tests/bw_hmma_model.h f019912d90dd2f5b1b3634473abdc8f767c32c25 tests/bw_qmma_model.h 8429ad2cbf5031f435b13f3bcb332dbdbfac1d58 -tests/bw_warp_sim.h 41513c380f1b994dc14fd6a173e11d5c329ab0dd -tests/bw_warp_sim_k.h 9e39ed0bdd87a3aa4abb2dad20d67a3bfe83ec58 -tests/bw_warp_sim_pa.h 198c12f0514846e4127390ee2440d95b9fd88b79 -tests/bw_warp_sim_tc.h bf662ea6b469bd59353ef57417d3ed0d878aeddf +tests/bw_warp_sim.h 6d93828f3979b3a7223ebe0f1952b53e1d414764 +tests/bw_warp_sim_k.h 993875647985cc99ce27a78003df26df91fd18e1 +tests/bw_warp_sim_pa.h 157e396cde9d942cc0bfff02fd057ed393c52a0a +tests/bw_warp_sim_tc.h a08e38b6a05d815448fe8b4546d204aff6f3e166 +tests/dagsim/dagsim_unit.c f9c6c089ea9bbb8ab820e5070583bd6c1cf98804 +tests/dagsim/fixtures/m1_spark_overlap.oev 249980f17162a95e47917072fa842b37bca08b41 +tests/dagsim/fixtures/m1_spark_subset.oev 34fdf73644edbaad5ead8d444752430aefb451a0 +tests/dagsim/fixtures/m1_synthetic_power.oev 3087178eaea87fedcc9bde161717b4b8316b43af +tests/dagsim/fixtures/mw_bad_elimination.oev cc9e2a4803c6f98068812cc2da777a9a669f3a31 +tests/dagsim/fixtures/mw_baseline.oev e2eaf36f8104aae08df67774734d5d5d25dfbce2 +tests/dagsim/fixtures/mw_reuse.oev 9e24750f34439621be86961c4357ec09715b4d13 +tests/dagsim/fixtures/needs_arch244_trace.oev 6711650728006ad0feff66cb0768d9e3fc6b5b6f +tests/dagsim/run_cli.sh 5b22297de823d24af6c8f7e5f373b90f632aa461 +tests/dagsim/sha256_host.c 64ffdf4e5d6443ccf1272d43f6dba39589624c0a tests/fake_m16_native.c f9ce918fb205e2fc22fc491a44462de40d8bd0c9 tests/fake_m16_native.h cc5b4a6bcaa70eb696dc345c7087b757dcf79799 tests/gpu_attention_test.c b42cd14dc4c2759e275a6f35498ce08d5929077c @@ -302,6 +321,7 @@ tests/gpu_gdn_multi_test.c 108578a8fc7dbb8b95db323e5ea1752da767601b tests/gpu_gdn_test.c 655875a653860259e3d93f80fb54bded048b26af tests/gpu_gemm_test.c b60f70964f40113d59931993e1c9eaa046008cfa tests/gpu_gpr_boundary_test.c 052baca71823f2ad4941184785b24fa81713109a +tests/gpu_imadhi_rc_test.c 179596a27cd1c24e02ec8459a45f2e7216ca2424 tests/gpu_isolation_test.c 786629e31c02a1d5ee3de782761e47ff2647dfce tests/gpu_l2_reuse_test.c 169be18b7ba7c13fff48685e452d351663dc34be tests/gpu_l2_stale_test.c fa4ab3c8d0541b2bb469b821280f87c60e7b45b5 @@ -338,6 +358,9 @@ tests/test_omega_gpu_platform.c 38ff95b817b165611a4fadf036f37d7144d9bc2a tests/test_omega_gpu_platform_linux.c eb9b9be4cecb87d5f17cb8b79c283f26a5a805dd tests/test_omega_gpu_session_platform.c b02defe6fbfbe51494d25450809bffa65e8fda7b tests/test_omega_gpu_wait.c 0d4cc10d93af3dd9c7cd5590e934d133fe5a27b7 +tools/dagsim/dagsim.c b7187d4450ea026881516638e65e21db3f4e6a78 +tools/dagsim/dagsim.h a409b588ba3688ecf0318e10d25f3b1f3a911dbd +tools/dagsim/dagsim_cli.c fb279f9040cb33d051bebf8f9d1288b9be325230 tools/gpu_engine_backend_mutations.sh 443b3585a8748715ea63b9e2a3ed848aae8ecc89 tools/gpu_engine_mutations.sh a909493d166084c94546a182eddb0e6ed441caca tools/mutation_runner.sh ab60143c8fd705a6293d4c7f935d928554ed5d1f diff --git a/tools/extract/extra-paths.txt b/tools/extract/extra-paths.txt index cf74e2ef..7f32496c 100644 --- a/tools/extract/extra-paths.txt +++ b/tools/extract/extra-paths.txt @@ -6,6 +6,7 @@ mk/omega-gpu-code-alloc.mk mk/omega-gpu-wait.mk tools/gpu_engine_mutations.sh tools/gpu_engine_backend_mutations.sh +tools/dagsim evidence/E1-CLOSURE evidence/FB1-CUT5-HD128-7dd92dc evidence/OWN30B-FP8-GEMV-7ce5dd4b-20261013T1744Z