physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / forge / analog-sim / forge_analog_sim.c
| 1 | /* |
| 2 | * forge/analog-sim/forge_analog_sim.c -- AR2 simulated analog provider. |
| 3 | * See forge_analog_sim.h. Every result here is SIMULATED_DEVELOPMENT. |
| 4 | */ |
| 5 | #include "forge_analog_sim.h" |
| 6 | |
| 7 | #include <string.h> |
| 8 | |
| 9 | #define ASIM_MAGNITUDE_LIMIT (1ll << 32) /* model offsets beyond this are refused; keeps every sum inside int64 */ |
| 10 | |
| 11 | static const char *const ORACLE_LABEL_EXACT_U64 = |
| 12 | "FORGE-ASIM/oracle/omega_matvec_reference/src/omega_matvec.h:74/u64-wrapping"; |
| 13 | static const char *const ORACLE_LABEL_I32 = |
| 14 | "FORGE-ASIM/oracle/omega_matmul_cpu_oracle_i32/src/omega_blackwell_matmul.h:50/u32-wrapping/n=1"; |
| 15 | static const char *const SUBSTRATE_LABEL = "FORGE-ASIM/substrate/analog-in-memory/simulated/v1"; |
| 16 | static const char *const DIGITAL_LABEL = "FORGE-ASIM/substrate/digital-cpu/v1"; |
| 17 | static const char *const ANALOG_CODE_LABEL = "FORGE-ASIM/code/analog-matvec-simulation/v1"; |
| 18 | static const char *const DIGITAL_CODE_LABEL = "FORGE-ASIM/code/digital-matvec-oracle/v1"; |
| 19 | |
| 20 | static void label_digest(const char *label, uint8_t out[32]) |
| 21 | { |
| 22 | forge_v2_sha256((const uint8_t *)label, strlen(label), out); |
| 23 | } |
| 24 | |
| 25 | /* ---- oracles ---- */ |
| 26 | |
| 27 | /* Restatement of omega_matvec_reference (omega src/omega_matvec.c:265). */ |
| 28 | void forge_asim_oracle_exact_u64(const uint64_t *A, const uint64_t *x, uint64_t *y, uint32_t M, uint32_t N) |
| 29 | { |
| 30 | if (!A || !x || !y) return; |
| 31 | for (uint32_t i = 0; i < M; ++i) { |
| 32 | uint64_t acc = 0; |
| 33 | for (uint32_t j = 0; j < N; ++j) acc += A[(size_t)i * N + j] * x[j]; |
| 34 | y[i] = acc; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /* Restatement of omega_matmul_cpu_oracle_i32 (omega src/omega_blackwell_matmul.c:41) |
| 39 | * with n = 1: c[i] = sum_p a[i*k + p] * b[p], wrapping uint32. */ |
| 40 | int forge_asim_oracle_i32(const uint32_t *a, const uint32_t *x, uint32_t *y, uint32_t M, uint32_t N) |
| 41 | { |
| 42 | if (!a || !x || !y || M == 0 || N == 0) return -1; |
| 43 | for (uint32_t i = 0; i < M; i++) { |
| 44 | uint32_t acc = 0; |
| 45 | for (uint32_t p = 0; p < N; p++) acc += a[i * N + p] * x[p]; |
| 46 | y[i] = acc; |
| 47 | } |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | int forge_asim_oracle_digest(uint32_t oracle, uint8_t out[32]) |
| 52 | { |
| 53 | if (!out) return FORGE_V2_ERR_ARG; |
| 54 | switch (oracle) { |
| 55 | case FORGE_ASIM_ORACLE_EXACT_U64: label_digest(ORACLE_LABEL_EXACT_U64, out); return FORGE_V2_OK; |
| 56 | case FORGE_ASIM_ORACLE_I32: label_digest(ORACLE_LABEL_I32, out); return FORGE_V2_OK; |
| 57 | default: return FORGE_V2_ERR_ARG; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static uint32_t oracle_of(const uint8_t digest[32]) |
| 62 | { |
| 63 | uint8_t d[32]; |
| 64 | forge_asim_oracle_digest(FORGE_ASIM_ORACLE_EXACT_U64, d); |
| 65 | if (memcmp(d, digest, 32) == 0) return FORGE_ASIM_ORACLE_EXACT_U64; |
| 66 | forge_asim_oracle_digest(FORGE_ASIM_ORACLE_I32, d); |
| 67 | if (memcmp(d, digest, 32) == 0) return FORGE_ASIM_ORACLE_I32; |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /* ---- error model ---- */ |
| 72 | |
| 73 | static uint64_t mag(int64_t v) { return v < 0 ? (uint64_t)0 - (uint64_t)v : (uint64_t)v; } |
| 74 | |
| 75 | static uint64_t sat_add(uint64_t a, uint64_t b) { return a > UINT64_MAX - b ? UINT64_MAX : a + b; } |
| 76 | |
| 77 | /* ceil(a * b / d) with a 128-bit product, saturating at UINT64_MAX only |
| 78 | * after the division; d > 0 */ |
| 79 | static uint64_t mul_div_ceil(uint64_t a, uint64_t b, uint64_t d) |
| 80 | { |
| 81 | unsigned __int128 p = (unsigned __int128)a * b; |
| 82 | unsigned __int128 q = p / d + (p % d != 0); |
| 83 | return q > UINT64_MAX ? UINT64_MAX : (uint64_t)q; |
| 84 | } |
| 85 | |
| 86 | /* signed a * b / d truncated toward zero, 128-bit product, saturating at |
| 87 | * +/-2^61 after the division (three such terms still sum inside int64) */ |
| 88 | static int64_t mul_div_trunc(int64_t a, int64_t b, int64_t d) |
| 89 | { |
| 90 | __int128 q = ((__int128)a * b) / d; |
| 91 | const __int128 lim = (__int128)1 << 61; |
| 92 | return (int64_t)(q > lim ? lim : (q < -lim ? -lim : q)); |
| 93 | } |
| 94 | |
| 95 | static int model_ok(const ForgeAnalogSimModel *m) |
| 96 | { |
| 97 | return mag(m->bias_lsb) <= (uint64_t)ASIM_MAGNITUDE_LIMIT && |
| 98 | mag(m->drift_lsb_per_hour) <= (uint64_t)ASIM_MAGNITUDE_LIMIT && |
| 99 | mag(m->temp_lsb_per_k) <= (uint64_t)ASIM_MAGNITUDE_LIMIT; |
| 100 | } |
| 101 | |
| 102 | static int model_ideal(const ForgeAnalogSimModel *m) |
| 103 | { |
| 104 | return m->quant_step_lsb <= 1 && m->bias_lsb == 0 && m->noise_peak_lsb == 0 && m->drift_lsb_per_hour == 0 && |
| 105 | m->temp_lsb_per_k == 0 && m->saturation_max == 0; |
| 106 | } |
| 107 | |
| 108 | uint64_t forge_asim_model_bound(const ForgeAnalogSimModel *m, uint64_t cal_age_s, uint32_t temp_delta_mc) |
| 109 | { |
| 110 | uint64_t b = 0; |
| 111 | if (!m) return UINT64_MAX; |
| 112 | if (m->quant_step_lsb > 1) b = m->quant_step_lsb / 2; |
| 113 | b = sat_add(b, mag(m->bias_lsb)); |
| 114 | b = sat_add(b, m->noise_peak_lsb); |
| 115 | b = sat_add(b, mul_div_ceil(mag(m->drift_lsb_per_hour), cal_age_s, 3600)); |
| 116 | b = sat_add(b, mul_div_ceil(mag(m->temp_lsb_per_k), temp_delta_mc, 1000)); |
| 117 | return b; |
| 118 | } |
| 119 | |
| 120 | /* ---- descriptors ---- */ |
| 121 | |
| 122 | void forge_asim_substrate_identity(uint8_t out[32]) { label_digest(SUBSTRATE_LABEL, out); } |
| 123 | |
| 124 | int forge_asim_describe(const ForgeAnalogSimModel *declared, const ForgeCalibrationArtifact *cal, |
| 125 | ForgeSubstrateDescriptor *s) |
| 126 | { |
| 127 | if (!declared || !cal || !s || !model_ok(declared)) return FORGE_V2_ERR_ARG; |
| 128 | if (cal->env_temp_max_mc < cal->env_temp_min_mc || cal->validity_window_s == 0) return FORGE_V2_ERR_ARG; |
| 129 | memset(s, 0, sizeof *s); |
| 130 | forge_asim_substrate_identity(s->substrate_identity); |
| 131 | s->substrate_class = FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY; |
| 132 | s->operation_families = FORGE_V2_OP_MATVEC; |
| 133 | s->input_repr = FORGE_V2_REPR_ANALOG_VOLTAGE; s->input_bits = FORGE_ASIM_INPUT_BITS; |
| 134 | s->output_repr = FORGE_V2_REPR_ANALOG_CURRENT; s->output_bits = FORGE_ASIM_OUTPUT_BITS; |
| 135 | s->state_model = FORGE_V2_STATE_PERSISTENT_WEIGHTS; |
| 136 | s->sync_model = FORGE_V2_SYNC_BATCH_BARRIER; |
| 137 | s->memory_model = FORGE_V2_MEM_IN_MEMORY_COMPUTE; |
| 138 | s->memory_capacity_bytes = (uint64_t)FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS; |
| 139 | s->numerical.dynamic_range_mdb = 144000; /* 24 bits */ |
| 140 | s->numerical.resolution_bits = FORGE_ASIM_OUTPUT_BITS; |
| 141 | s->numerical.settling_ns = declared->latency_ns; |
| 142 | s->numerical.calibration_validity_s = cal->validity_window_s; |
| 143 | s->calibration_required = 1; |
| 144 | s->calibration_method_mask = 1u << FORGE_V2_CAL_SIMULATED_MODEL; |
| 145 | s->calibration_max_window_s = cal->validity_window_s; |
| 146 | s->calibration_min_fixtures = 4; |
| 147 | /* worst case over the whole calibration window and envelope */ |
| 148 | uint32_t half_mc = (uint32_t)(((int64_t)cal->env_temp_max_mc - cal->env_temp_min_mc + 1) / 2); |
| 149 | uint64_t bound = forge_asim_model_bound(declared, cal->validity_window_s, half_mc); |
| 150 | if (model_ideal(declared)) { |
| 151 | s->error_kind = FORGE_V2_ERROR_EXACT; |
| 152 | } else { |
| 153 | s->error_kind = declared->noise_peak_lsb ? FORGE_V2_ERROR_BOUNDED_STOCHASTIC |
| 154 | : FORGE_V2_ERROR_BOUNDED_DETERMINISTIC; |
| 155 | s->error_abs = bound ? bound : 1; /* a non-exact substrate never declares zero error */ |
| 156 | } |
| 157 | s->fault_detect_mask = 0; |
| 158 | s->timing_min_latency_ns = declared->latency_ns; |
| 159 | s->timing_conversion_ns = declared->latency_ns; |
| 160 | s->timing_deterministic = declared->noise_peak_lsb ? 0 : 1; |
| 161 | s->limit_max_rows = FORGE_ASIM_MAX_ROWS; |
| 162 | s->limit_max_cols = FORGE_ASIM_MAX_COLS; |
| 163 | s->limit_max_state_bytes = (uint64_t)FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS; |
| 164 | s->limit_max_concurrent = 1; |
| 165 | s->evidence_capabilities = FORGE_V2_EVCAP_DURATION | FORGE_V2_EVCAP_TEMPERATURE | |
| 166 | FORGE_V2_EVCAP_MEASURED_ERROR | FORGE_V2_EVCAP_REPEATS; |
| 167 | return FORGE_V2_OK; |
| 168 | } |
| 169 | |
| 170 | void forge_asim_digital_descriptor(ForgeSubstrateDescriptor *s) |
| 171 | { |
| 172 | memset(s, 0, sizeof *s); |
| 173 | label_digest(DIGITAL_LABEL, s->substrate_identity); |
| 174 | s->substrate_class = FORGE_V2_SUBSTRATE_DIGITAL_CPU; |
| 175 | s->operation_families = FORGE_V2_OP_MATVEC | FORGE_V2_OP_MATMUL; |
| 176 | s->input_repr = FORGE_V2_REPR_DIGITAL_INT; s->input_bits = 64; |
| 177 | s->output_repr = FORGE_V2_REPR_DIGITAL_INT; s->output_bits = 64; |
| 178 | s->state_model = FORGE_V2_STATE_STATELESS; |
| 179 | s->sync_model = FORGE_V2_SYNC_CLOCKED; |
| 180 | s->memory_model = FORGE_V2_MEM_SHARED_COHERENT; |
| 181 | s->numerical.resolution_bits = 64; |
| 182 | s->error_kind = FORGE_V2_ERROR_EXACT; |
| 183 | s->timing_deterministic = 1; |
| 184 | s->limit_max_rows = FORGE_ASIM_MAX_ROWS; |
| 185 | s->limit_max_cols = FORGE_ASIM_MAX_COLS; |
| 186 | s->limit_max_concurrent = 1; |
| 187 | s->evidence_capabilities = FORGE_V2_EVCAP_REPEATS; |
| 188 | } |
| 189 | |
| 190 | /* ---- simulation ---- */ |
| 191 | |
| 192 | static uint64_t splitmix64(uint64_t z) |
| 193 | { |
| 194 | z += 0x9E3779B97F4A7C15ull; |
| 195 | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; |
| 196 | z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; |
| 197 | return z ^ (z >> 31); |
| 198 | } |
| 199 | |
| 200 | static int shape_ok(const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N) |
| 201 | { |
| 202 | const uint64_t lim = 1ull << FORGE_ASIM_INPUT_BITS; |
| 203 | if (!A || !x || M == 0 || N == 0 || M > FORGE_ASIM_MAX_ROWS || N > FORGE_ASIM_MAX_COLS) return 0; |
| 204 | for (size_t k = 0; k < (size_t)M * N; k++) if (A[k] >= lim) return 0; |
| 205 | for (uint32_t j = 0; j < N; j++) if (x[j] >= lim) return 0; |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | int forge_asim_realize(const ForgeAnalogSimModel *m, uint32_t wrong_row, int64_t wrong_delta, |
| 210 | const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N, |
| 211 | uint32_t run_index, uint64_t cal_age_s, int32_t temp_delta_mc, |
| 212 | uint64_t *y, uint64_t *latency_ns) |
| 213 | { |
| 214 | if (!m || !y || !latency_ns || !model_ok(m) || mag(wrong_delta) > (uint64_t)ASIM_MAGNITUDE_LIMIT) |
| 215 | return FORGE_ASIM_ERR_ARG; |
| 216 | if (!shape_ok(A, x, M, N)) return FORGE_ASIM_ERR_INPUT_RANGE; |
| 217 | const uint64_t age_cap = 1ull << 40; |
| 218 | int64_t age = (int64_t)(cal_age_s > age_cap ? age_cap : cal_age_s); |
| 219 | int64_t offset = m->bias_lsb + mul_div_trunc(m->drift_lsb_per_hour, age, 3600) + |
| 220 | mul_div_trunc(m->temp_lsb_per_k, temp_delta_mc, 1000); |
| 221 | for (uint32_t i = 0; i < M; i++) { |
| 222 | /* the ideal crossbar column current: exact for inputs within limits */ |
| 223 | int64_t v = 0; |
| 224 | for (uint32_t j = 0; j < N; j++) v += (int64_t)(A[(size_t)i * N + j] * x[j]); |
| 225 | v += offset; |
| 226 | if (m->noise_peak_lsb) { |
| 227 | uint64_t r = splitmix64(m->noise_seed ^ splitmix64(((uint64_t)run_index << 32) | i)); |
| 228 | v += (int64_t)(r % (2ull * m->noise_peak_lsb + 1)) - (int64_t)m->noise_peak_lsb; |
| 229 | } |
| 230 | if (v < 0) v = 0; /* no negative output current */ |
| 231 | if (m->quant_step_lsb > 1) { |
| 232 | int64_t q = m->quant_step_lsb; |
| 233 | v = ((v + q / 2) / q) * q; |
| 234 | } |
| 235 | if (m->saturation_max && (uint64_t)v > m->saturation_max) v = (int64_t)m->saturation_max; |
| 236 | if (wrong_row && i == wrong_row - 1) { |
| 237 | v += wrong_delta; |
| 238 | if (v < 0) v = 0; |
| 239 | } |
| 240 | y[i] = (uint64_t)v; |
| 241 | } |
| 242 | *latency_ns = m->latency_ns; |
| 243 | return FORGE_ASIM_PUBLISHED_ANALOG; |
| 244 | } |
| 245 | |
| 246 | /* ---- evidence ---- */ |
| 247 | |
| 248 | uint32_t forge_asim_provenance(void) { return FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT; } |
| 249 | |
| 250 | const char *forge_asim_refuse_name(uint32_t r) |
| 251 | { |
| 252 | switch (r) { |
| 253 | case FORGE_ASIM_REFUSE_NONE: return "none"; |
| 254 | case FORGE_ASIM_REFUSE_INELIGIBLE: return "ineligible"; |
| 255 | case FORGE_ASIM_REFUSE_LATENCY_DECLARED: return "latency_declared_over_budget"; |
| 256 | case FORGE_ASIM_REFUSE_EXACT_MISMATCH: return "exact_digest_mismatch"; |
| 257 | case FORGE_ASIM_REFUSE_ERROR_BOUND: return "error_over_bound"; |
| 258 | case FORGE_ASIM_REFUSE_CONFIDENCE: return "confidence_not_met"; |
| 259 | case FORGE_ASIM_REFUSE_NONDETERMINISM: return "nondeterministic_runs"; |
| 260 | case FORGE_ASIM_REFUSE_LATENCY_ACTUAL: return "latency_actual_over_budget"; |
| 261 | case FORGE_ASIM_REFUSE_UNSUPPORTED: return "unsupported_contract"; |
| 262 | default: return "unknown"; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static size_t put_u64(uint8_t *p, uint64_t v) |
| 267 | { |
| 268 | for (int b = 0; b < 8; b++) p[b] = (uint8_t)(v >> (8 * b)); |
| 269 | return 8; |
| 270 | } |
| 271 | |
| 272 | static void vec_digest(const char *domain, const uint64_t *v, size_t n, uint8_t out[32]) |
| 273 | { |
| 274 | uint8_t buf[64 + 8 * FORGE_ASIM_MAX_ROWS]; |
| 275 | size_t len = strlen(domain), off; |
| 276 | memcpy(buf, domain, len); |
| 277 | off = len; |
| 278 | off += put_u64(buf + off, n); |
| 279 | for (size_t k = 0; k < n; k++) off += put_u64(buf + off, v[k]); |
| 280 | forge_v2_sha256(buf, off, out); |
| 281 | } |
| 282 | |
| 283 | static void input_digest(const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N, uint8_t out[32]) |
| 284 | { |
| 285 | static const char dom[] = "FORGE-ASIM/input/matvec/v1"; |
| 286 | uint8_t buf[sizeof dom + 16 + 8 * (FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS + FORGE_ASIM_MAX_COLS)]; |
| 287 | size_t off = sizeof dom - 1; |
| 288 | memcpy(buf, dom, off); |
| 289 | off += put_u64(buf + off, M); |
| 290 | off += put_u64(buf + off, N); |
| 291 | for (size_t k = 0; k < (size_t)M * N; k++) off += put_u64(buf + off, A[k]); |
| 292 | for (uint32_t j = 0; j < N; j++) off += put_u64(buf + off, x[j]); |
| 293 | forge_v2_sha256(buf, off, out); |
| 294 | } |
| 295 | |
| 296 | /* Fill the fields every record of this provider shares, then validate and |
| 297 | * digest it through the V2 encoder. Provenance is fixed here and nowhere |
| 298 | * else. */ |
| 299 | static int seal_evidence(ForgeExecutionEvidenceV2 *e, uint8_t digest[32]) |
| 300 | { |
| 301 | uint8_t buf[4096]; |
| 302 | size_t n; |
| 303 | e->provenance_class = forge_asim_provenance(); |
| 304 | e->energy_source = FORGE_V2_ENERGY_NOT_MEASURED; |
| 305 | e->energy_nj = 0; |
| 306 | if (forge_v2_encode_evidence(e, buf, sizeof buf, &n) != FORGE_V2_OK) return FORGE_ASIM_ERR_EVIDENCE; |
| 307 | forge_v2_sha256(buf, n, digest); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | /* ---- the whole path ---- */ |
| 312 | |
| 313 | static void compute_ref(uint32_t oracle, const ForgeAsimRequest *rq, uint64_t *ref) |
| 314 | { |
| 315 | if (oracle == FORGE_ASIM_ORACLE_EXACT_U64) { |
| 316 | forge_asim_oracle_exact_u64(rq->A, rq->x, ref, rq->M, rq->N); |
| 317 | } else { |
| 318 | uint32_t a32[FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS], x32[FORGE_ASIM_MAX_COLS], y32[FORGE_ASIM_MAX_ROWS]; |
| 319 | for (size_t k = 0; k < (size_t)rq->M * rq->N; k++) a32[k] = (uint32_t)rq->A[k]; |
| 320 | for (uint32_t j = 0; j < rq->N; j++) x32[j] = (uint32_t)rq->x[j]; |
| 321 | forge_asim_oracle_i32(a32, x32, y32, rq->M, rq->N); |
| 322 | for (uint32_t i = 0; i < rq->M; i++) ref[i] = y32[i]; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* The digital realization: the contract's oracle on the CPU, repeated for |
| 327 | * the contract's min_samples so its evidence meets the same contract. */ |
| 328 | static int run_digital(const ForgeAsimSetup *st, const ForgeAsimRequest *rq, uint32_t oracle, |
| 329 | const uint8_t contract_digest[32], const uint8_t input_id[32], const uint64_t *ref, |
| 330 | ForgeAsimOutcome *out) |
| 331 | { |
| 332 | ForgeSubstrateDescriptor d; |
| 333 | uint8_t sd[32], code[32]; |
| 334 | uint64_t again[FORGE_ASIM_MAX_ROWS]; |
| 335 | uint32_t runs = rq->contract.min_samples, same = 0; |
| 336 | ForgeExecutionEvidenceV2 *e = &out->final_evidence; |
| 337 | for (uint32_t r = 0; r < runs; r++) { |
| 338 | compute_ref(oracle, rq, again); |
| 339 | if (memcmp(again, ref, (size_t)rq->M * sizeof ref[0]) == 0) same++; |
| 340 | } |
| 341 | if (same != runs) return FORGE_ASIM_ERR_EVIDENCE; /* a digital oracle that disagrees with itself */ |
| 342 | forge_asim_digital_descriptor(&d); |
| 343 | forge_v2_digest_substrate(&d, sd); |
| 344 | label_digest(DIGITAL_CODE_LABEL, code); |
| 345 | memset(e, 0, sizeof *e); |
| 346 | forge_v2_realization_identity(contract_digest, st->machine_identity, sd, code, NULL, e->realization_identity); |
| 347 | memcpy(e->machine_identity, st->machine_identity, 32); |
| 348 | memcpy(e->substrate_identity, d.substrate_identity, 32); |
| 349 | memcpy(e->semantic_contract_digest, contract_digest, 32); |
| 350 | memcpy(e->input_identity, input_id, 32); |
| 351 | vec_digest("FORGE-ASIM/output/matvec/v1", ref, rq->M, e->output_identity); |
| 352 | e->execution_epoch_s = rq->now.execution_epoch_s; |
| 353 | e->env_temperature_mc = rq->now.temperature_mc; |
| 354 | e->env_supply_uv = rq->now.supply_uv; |
| 355 | e->confidence_ppm = 1000000; |
| 356 | e->repeat_runs = runs; |
| 357 | e->repeat_passes = same; |
| 358 | e->fault_state = FORGE_V2_FAULT_NONE; |
| 359 | if (seal_evidence(e, out->final_evidence_digest) != 0) return FORGE_ASIM_ERR_EVIDENCE; |
| 360 | memcpy(out->y, ref, (size_t)rq->M * sizeof ref[0]); |
| 361 | out->published = 1; |
| 362 | return FORGE_ASIM_PUBLISHED_FALLBACK; |
| 363 | } |
| 364 | |
| 365 | int forge_asim_execute(const ForgeAsimSetup *st, const ForgeAsimRequest *rq, ForgeAsimOutcome *out) |
| 366 | { |
| 367 | ForgeSubstrateDescriptor analog, digital; |
| 368 | ForgeV2Candidate set[2]; |
| 369 | size_t nset = 0; |
| 370 | uint8_t contract_digest[32], analog_digest[32], cal_digest[32], input_id[32], ref_id[32]; |
| 371 | uint64_t ref[FORGE_ASIM_MAX_ROWS]; |
| 372 | uint64_t first[FORGE_ASIM_MAX_ROWS], run_y[FORGE_ASIM_MAX_ROWS], chosen[FORGE_ASIM_MAX_ROWS]; |
| 373 | int rc; |
| 374 | |
| 375 | if (!out) return FORGE_ASIM_ERR_ARG; |
| 376 | memset(out, 0, sizeof *out); |
| 377 | out->status = FORGE_ASIM_ERR_ARG; |
| 378 | if (!st || !rq) return out->status; |
| 379 | const ForgeSemanticResultContract *c = &rq->contract; |
| 380 | if (c->operation_family != FORGE_V2_OP_MATVEC || c->shape_rank != 2 || c->shape_dim[0] != rq->M || |
| 381 | c->shape_dim[1] != rq->N) |
| 382 | return out->status; |
| 383 | uint32_t oracle = oracle_of(c->reference_oracle_digest); |
| 384 | if (!oracle || c->min_samples == 0 || c->min_samples > FORGE_ASIM_MAX_SAMPLES) return out->status; |
| 385 | if (!shape_ok(rq->A, rq->x, rq->M, rq->N)) return out->status = FORGE_ASIM_ERR_INPUT_RANGE; |
| 386 | if (forge_v2_digest_contract(c, contract_digest) != FORGE_V2_OK) return out->status; |
| 387 | if (forge_asim_describe(&st->declared, &st->cal, &analog) != FORGE_V2_OK) return out->status; |
| 388 | if (!model_ok(&st->actual)) return out->status; |
| 389 | forge_v2_digest_substrate(&analog, analog_digest); |
| 390 | memcpy(out->analog_substrate_digest, analog_digest, 32); |
| 391 | if (forge_v2_digest_calibration(&st->cal, cal_digest) != FORGE_V2_OK) return out->status; |
| 392 | |
| 393 | /* the reference answer, from the oracle the contract names */ |
| 394 | compute_ref(oracle, rq, ref); |
| 395 | input_digest(rq->A, rq->x, rq->M, rq->N, input_id); |
| 396 | vec_digest("FORGE-ASIM/output/matvec/v1", ref, rq->M, ref_id); |
| 397 | |
| 398 | /* eligibility of the simulated analog realization */ |
| 399 | out->eligibility_rc = forge_v2_substrate_eligible(&analog, c, &st->cal, FORGE_V2_FAULT_NONE, &rq->now); |
| 400 | if (out->eligibility_rc != FORGE_V2_OK) out->refuse_reason = FORGE_ASIM_REFUSE_INELIGIBLE; |
| 401 | else if (rq->latency_budget_ns && st->declared.latency_ns > rq->latency_budget_ns) |
| 402 | out->refuse_reason = FORGE_ASIM_REFUSE_LATENCY_DECLARED; |
| 403 | else if (c->error_norm != FORGE_V2_NORM_ELEMENTWISE_MAX || c->error_kind == FORGE_V2_ERROR_MEASURED_DISTRIBUTION) |
| 404 | out->refuse_reason = FORGE_ASIM_REFUSE_UNSUPPORTED; |
| 405 | if (out->refuse_reason == FORGE_ASIM_REFUSE_NONE) { |
| 406 | memcpy(set[nset].contract_digest, contract_digest, 32); |
| 407 | memcpy(set[nset].machine_identity, st->machine_identity, 32); |
| 408 | memcpy(set[nset].substrate_digest, analog_digest, 32); |
| 409 | set[nset].substrate_class = analog.substrate_class; |
| 410 | nset++; |
| 411 | } |
| 412 | forge_asim_digital_descriptor(&digital); |
| 413 | if (st->include_digital && |
| 414 | forge_v2_substrate_eligible(&digital, c, NULL, FORGE_V2_FAULT_NONE, &rq->now) == FORGE_V2_OK) { |
| 415 | memcpy(set[nset].contract_digest, contract_digest, 32); |
| 416 | memcpy(set[nset].machine_identity, st->machine_identity, 32); |
| 417 | forge_v2_digest_substrate(&digital, set[nset].substrate_digest); |
| 418 | set[nset].substrate_class = digital.substrate_class; |
| 419 | nset++; |
| 420 | } |
| 421 | /* admission: the eligible set must hold a digital realization */ |
| 422 | if (forge_v2_admit_plan(set, nset, contract_digest) != FORGE_V2_OK) |
| 423 | return out->status = FORGE_ASIM_ERR_NO_FALLBACK; |
| 424 | |
| 425 | if (out->refuse_reason == FORGE_ASIM_REFUSE_NONE) { |
| 426 | uint32_t runs = c->min_samples; |
| 427 | uint64_t cal_age = rq->now.execution_epoch_s - st->cal.epoch_s; /* eligible => not negative */ |
| 428 | int32_t mid = (int32_t)(((int64_t)st->cal.env_temp_min_mc + st->cal.env_temp_max_mc) / 2); |
| 429 | int32_t dtemp = rq->now.temperature_mc - mid; |
| 430 | uint64_t worst = 0, worst_rel = 0, lat = 0, lat_total = 0; |
| 431 | uint32_t within_runs = 0, have_chosen = 0, differs = 0, exact_bad = 0, late = 0; |
| 432 | for (uint32_t r = 0; r < runs; r++) { |
| 433 | rc = forge_asim_realize(&st->actual, st->wrong_answer_row, st->wrong_answer_delta, rq->A, rq->x, |
| 434 | rq->M, rq->N, r, cal_age, dtemp, run_y, &lat); |
| 435 | if (rc != FORGE_ASIM_PUBLISHED_ANALOG) return out->status = rc; |
| 436 | lat_total = sat_add(lat_total, lat); |
| 437 | if (rq->latency_budget_ns && lat > rq->latency_budget_ns) late = 1; |
| 438 | if (r == 0) memcpy(first, run_y, sizeof first); |
| 439 | else if (memcmp(first, run_y, (size_t)rq->M * sizeof run_y[0]) != 0) differs = 1; |
| 440 | if (c->error_kind == FORGE_V2_ERROR_EXACT) { |
| 441 | uint8_t od[32]; |
| 442 | vec_digest("FORGE-ASIM/output/matvec/v1", run_y, rq->M, od); |
| 443 | if (memcmp(od, ref_id, 32) != 0) exact_bad = 1; |
| 444 | } |
| 445 | uint32_t ok = 1; |
| 446 | for (uint32_t i = 0; i < rq->M; i++) { |
| 447 | uint64_t err = run_y[i] > ref[i] ? run_y[i] - ref[i] : ref[i] - run_y[i]; |
| 448 | uint64_t allow = sat_add(c->error_abs, (uint64_t)c->error_rel_ppb * ref[i] / 1000000000ull); |
| 449 | if (c->error_kind == FORGE_V2_ERROR_EXACT) allow = 0; |
| 450 | if (err > allow) ok = 0; |
| 451 | if (err > worst) worst = err; |
| 452 | uint64_t rel = ref[i] ? mul_div_ceil(err, 1000000000ull, ref[i]) : (err ? UINT32_MAX : 0); |
| 453 | if (rel > worst_rel) worst_rel = rel; |
| 454 | } |
| 455 | if (ok) { |
| 456 | within_runs++; |
| 457 | if (!have_chosen) { memcpy(chosen, run_y, sizeof chosen); have_chosen = 1; } |
| 458 | } |
| 459 | } |
| 460 | out->analog_ran = 1; |
| 461 | out->analog_error_abs = worst; |
| 462 | out->analog_runs = runs; |
| 463 | out->analog_runs_within_bound = within_runs; |
| 464 | |
| 465 | if (late) out->refuse_reason = FORGE_ASIM_REFUSE_LATENCY_ACTUAL; |
| 466 | /* EXACT is never satisfied once any non-ideality is injected, even |
| 467 | * when the outputs happen to land on the oracle's values */ |
| 468 | else if (c->error_kind == FORGE_V2_ERROR_EXACT && |
| 469 | (exact_bad || !model_ideal(&st->actual) || st->wrong_answer_row != 0)) |
| 470 | out->refuse_reason = FORGE_ASIM_REFUSE_EXACT_MISMATCH; |
| 471 | else if (c->error_kind != FORGE_V2_ERROR_BOUNDED_STOCHASTIC && differs) |
| 472 | out->refuse_reason = FORGE_ASIM_REFUSE_NONDETERMINISM; |
| 473 | else if (c->error_kind != FORGE_V2_ERROR_BOUNDED_STOCHASTIC && within_runs != runs) |
| 474 | out->refuse_reason = FORGE_ASIM_REFUSE_ERROR_BOUND; |
| 475 | else if (c->error_kind == FORGE_V2_ERROR_BOUNDED_STOCHASTIC && |
| 476 | (uint64_t)within_runs * 1000000ull < (uint64_t)c->confidence_ppm * runs) |
| 477 | out->refuse_reason = FORGE_ASIM_REFUSE_CONFIDENCE; |
| 478 | |
| 479 | /* evidence of the simulated analog realization, refused or not */ |
| 480 | ForgeExecutionEvidenceV2 *e = &out->analog_evidence; |
| 481 | uint8_t code[32]; |
| 482 | label_digest(ANALOG_CODE_LABEL, code); |
| 483 | forge_v2_realization_identity(contract_digest, st->machine_identity, analog_digest, code, cal_digest, |
| 484 | e->realization_identity); |
| 485 | memcpy(e->machine_identity, st->machine_identity, 32); |
| 486 | memcpy(e->substrate_identity, analog.substrate_identity, 32); |
| 487 | memcpy(e->semantic_contract_digest, contract_digest, 32); |
| 488 | e->has_calibration = 1; |
| 489 | memcpy(e->calibration_digest, cal_digest, 32); |
| 490 | memcpy(e->input_identity, input_id, 32); |
| 491 | vec_digest("FORGE-ASIM/output/matvec/v1", have_chosen ? chosen : first, rq->M, e->output_identity); |
| 492 | e->execution_epoch_s = rq->now.execution_epoch_s; |
| 493 | e->execution_duration_ns = lat_total; |
| 494 | e->conversion_duration_ns = lat; |
| 495 | e->env_temperature_mc = rq->now.temperature_mc; |
| 496 | e->env_supply_uv = rq->now.supply_uv; |
| 497 | e->measured_error_abs = worst; /* V2 field name; the value is a simulated error vs the oracle */ |
| 498 | e->measured_error_rel_ppb = worst_rel > UINT32_MAX ? UINT32_MAX : (uint32_t)worst_rel; |
| 499 | e->confidence_ppm = (uint32_t)((uint64_t)within_runs * 1000000ull / runs); |
| 500 | e->repeat_runs = runs; |
| 501 | e->repeat_passes = within_runs; |
| 502 | e->repeat_mean_ns = runs ? lat_total / runs : 0; |
| 503 | e->fault_state = FORGE_V2_FAULT_NONE; |
| 504 | if (seal_evidence(e, out->analog_evidence_digest) != 0) return out->status = FORGE_ASIM_ERR_EVIDENCE; |
| 505 | |
| 506 | if (out->refuse_reason == FORGE_ASIM_REFUSE_NONE) { |
| 507 | out->final_evidence = *e; |
| 508 | memcpy(out->final_evidence_digest, out->analog_evidence_digest, 32); |
| 509 | memcpy(out->y, chosen, (size_t)rq->M * sizeof chosen[0]); |
| 510 | out->published = 1; |
| 511 | return out->status = FORGE_ASIM_PUBLISHED_ANALOG; |
| 512 | } |
| 513 | } |
| 514 | /* refused: nothing of the analog run is published; the digital |
| 515 | * realization answers under the same contract */ |
| 516 | if (!st->include_digital) return out->status = FORGE_ASIM_ERR_NO_FALLBACK; |
| 517 | return out->status = run_digital(st, rq, oracle, contract_digest, input_id, ref, out); |
| 518 | } |