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.h
| 1 | /* |
| 2 | * forge/analog-sim/forge_analog_sim.h -- AR2: simulated analog realization |
| 3 | * provider with digital oracle parity (ARCH-0018 section 12, gate AR2). |
| 4 | * |
| 5 | * A software model of an analog in-memory matvec substrate. Every result it |
| 6 | * produces is a SIMULATED_DEVELOPMENT result: no device acts, nothing here is |
| 7 | * a physical result, and there is no API to label it otherwise. |
| 8 | * |
| 9 | * Shape of the path (one call, forge_asim_execute): |
| 10 | * 1. describe the provider states its error model as a V2 substrate |
| 11 | * descriptor (declared model -> error_kind + error_abs); |
| 12 | * 2. eligible forge_v2_substrate_eligible against the contract, the |
| 13 | * calibration artifact and the execution conditions, plus |
| 14 | * the request's latency budget; |
| 15 | * 3. admit forge_v2_admit_plan: the eligible set must hold a digital |
| 16 | * realization of the same contract, otherwise nothing runs; |
| 17 | * 4. realize the simulation injects the ACTUAL non-idealities (which a |
| 18 | * test may make worse than the declared ones) for |
| 19 | * contract.min_samples runs; it never sees the oracle; |
| 20 | * 5. check outputs are compared to the reference oracle named by the |
| 21 | * contract; EXACT needs bit equality, bounded kinds need |
| 22 | * |err| <= error_abs + error_rel_ppb * |ref| / 1e9 per element; |
| 23 | * 6. publish only a passing result is copied out; a refused result |
| 24 | * still yields evidence, and the digital realization then |
| 25 | * produces the answer under the same contract. |
| 26 | * |
| 27 | * Reference oracles (restated, not linked; the gate script cross-checks them |
| 28 | * against the omega sources when an omega checkout is present): |
| 29 | * EXACT_U64 omega_matvec_reference omega src/omega_matvec.h:74 |
| 30 | * y = A * x, wrapping uint64 arithmetic. |
| 31 | * I32 omega_matmul_cpu_oracle_i32 omega src/omega_blackwell_matmul.h:50 |
| 32 | * n = 1 column, wrapping uint32 arithmetic. |
| 33 | * The f16 / bf16 oracles are not covered here (deferred). |
| 34 | * |
| 35 | * Integers only: bounds are in LSB units of the output, as spec section 6.4 |
| 36 | * requires. No heap, no hardware, no I/O. |
| 37 | */ |
| 38 | #ifndef FORGE_ANALOG_SIM_H |
| 39 | #define FORGE_ANALOG_SIM_H |
| 40 | |
| 41 | #include <stddef.h> |
| 42 | #include <stdint.h> |
| 43 | |
| 44 | #include "forge_substrate_v2.h" |
| 45 | |
| 46 | #define FORGE_ASIM_MAX_ROWS 64u |
| 47 | #define FORGE_ASIM_MAX_COLS 64u |
| 48 | #define FORGE_ASIM_MAX_SAMPLES 64u |
| 49 | #define FORGE_ASIM_INPUT_BITS 8u /* every A and x element must fit */ |
| 50 | #define FORGE_ASIM_OUTPUT_BITS 24u /* 8 + 8 + log2(64) */ |
| 51 | |
| 52 | /* Reference oracles, identified by the contract's reference_oracle_digest. */ |
| 53 | enum { |
| 54 | FORGE_ASIM_ORACLE_EXACT_U64 = 1, |
| 55 | FORGE_ASIM_ORACLE_I32 = 2 |
| 56 | }; |
| 57 | |
| 58 | /* Outcome of forge_asim_execute. Values >= 0 are decisions; < 0 are errors. */ |
| 59 | enum { |
| 60 | FORGE_ASIM_PUBLISHED_ANALOG = 0, /* simulated analog result passed the contract */ |
| 61 | FORGE_ASIM_PUBLISHED_FALLBACK = 1, /* analog refused; digital result published */ |
| 62 | FORGE_ASIM_ERR_ARG = -1, |
| 63 | FORGE_ASIM_ERR_INPUT_RANGE = -2, /* shape or element outside the declared limits */ |
| 64 | FORGE_ASIM_ERR_NO_FALLBACK = -3, /* plan not admitted: no digital realization */ |
| 65 | FORGE_ASIM_ERR_EVIDENCE = -4 /* evidence record failed V2 validation */ |
| 66 | }; |
| 67 | |
| 68 | /* Why the simulated analog realization was refused (0 = not refused). */ |
| 69 | enum { |
| 70 | FORGE_ASIM_REFUSE_NONE = 0, |
| 71 | FORGE_ASIM_REFUSE_INELIGIBLE = 1, /* forge_v2_substrate_eligible said no */ |
| 72 | FORGE_ASIM_REFUSE_LATENCY_DECLARED = 2,/* declared latency exceeds the request budget */ |
| 73 | FORGE_ASIM_REFUSE_EXACT_MISMATCH = 3, /* EXACT contract, output digest != oracle digest */ |
| 74 | FORGE_ASIM_REFUSE_ERROR_BOUND = 4, /* deterministic kind, some element out of bound */ |
| 75 | FORGE_ASIM_REFUSE_CONFIDENCE = 5, /* stochastic kind, too few runs within bound */ |
| 76 | FORGE_ASIM_REFUSE_NONDETERMINISM = 6, /* non-stochastic kind, runs disagree */ |
| 77 | FORGE_ASIM_REFUSE_LATENCY_ACTUAL = 7, /* simulated latency exceeds the request budget */ |
| 78 | FORGE_ASIM_REFUSE_UNSUPPORTED = 8 /* oracle or norm this provider cannot check */ |
| 79 | }; |
| 80 | |
| 81 | /* One error model. All magnitudes in output LSB units unless stated. |
| 82 | * Zero everywhere means an ideal (exact) simulated substrate. */ |
| 83 | typedef struct { |
| 84 | uint32_t quant_step_lsb; /* output quantization step; 0 or 1 = off */ |
| 85 | int64_t bias_lsb; /* constant offset */ |
| 86 | uint32_t noise_peak_lsb; /* uniform noise in [-peak, +peak]; 0 = off */ |
| 87 | uint64_t noise_seed; /* deterministic noise stream */ |
| 88 | int64_t drift_lsb_per_hour; /* offset growth with calibration age */ |
| 89 | int64_t temp_lsb_per_k; /* offset per kelvin away from the calibration temperature */ |
| 90 | uint64_t saturation_max; /* outputs clamp to [0, saturation_max]; 0 = off */ |
| 91 | uint64_t latency_ns; /* settling + conversion per run; 0 = none */ |
| 92 | } ForgeAnalogSimModel; |
| 93 | |
| 94 | /* The simulated provider and its world. */ |
| 95 | typedef struct { |
| 96 | ForgeAnalogSimModel declared; /* what the provider claims (descriptor, eligibility) */ |
| 97 | ForgeAnalogSimModel actual; /* what the simulation injects */ |
| 98 | uint32_t wrong_answer_row; /* fault injection: row + 1 gets wrong_answer_delta; 0 = off */ |
| 99 | int64_t wrong_answer_delta; |
| 100 | ForgeCalibrationArtifact cal; /* SIMULATED_MODEL calibration for this substrate */ |
| 101 | uint8_t machine_identity[32]; |
| 102 | uint32_t include_digital; /* 1: a digital CPU realization is in the eligible set */ |
| 103 | } ForgeAsimSetup; |
| 104 | |
| 105 | typedef struct { |
| 106 | ForgeSemanticResultContract contract; /* operation MATVEC, shape {M, N} */ |
| 107 | const uint64_t *A; /* M x N, row-major */ |
| 108 | const uint64_t *x; /* N */ |
| 109 | uint32_t M, N; |
| 110 | ForgeV2Conditions now; |
| 111 | uint64_t latency_budget_ns; /* 0 = no budget */ |
| 112 | } ForgeAsimRequest; |
| 113 | |
| 114 | typedef struct { |
| 115 | int status; /* FORGE_ASIM_PUBLISHED_* or FORGE_ASIM_ERR_* */ |
| 116 | uint32_t refuse_reason; /* FORGE_ASIM_REFUSE_* for the analog realization */ |
| 117 | int eligibility_rc; /* forge_v2_substrate_eligible result */ |
| 118 | uint32_t published; /* 1 when y holds an accepted result */ |
| 119 | uint64_t y[FORGE_ASIM_MAX_ROWS];/* zeroed unless published */ |
| 120 | uint32_t analog_ran; /* 1 when the simulation executed */ |
| 121 | uint64_t analog_error_abs; /* worst element error vs oracle over all runs */ |
| 122 | uint32_t analog_runs, analog_runs_within_bound; |
| 123 | uint8_t analog_substrate_digest[32]; |
| 124 | ForgeExecutionEvidenceV2 analog_evidence; /* valid when analog_ran */ |
| 125 | uint8_t analog_evidence_digest[32]; |
| 126 | ForgeExecutionEvidenceV2 final_evidence; /* evidence of the published result */ |
| 127 | uint8_t final_evidence_digest[32]; |
| 128 | } ForgeAsimOutcome; |
| 129 | |
| 130 | /* Oracles (restatements of the omega anchors above). */ |
| 131 | void forge_asim_oracle_exact_u64(const uint64_t *A, const uint64_t *x, uint64_t *y, uint32_t M, uint32_t N); |
| 132 | int forge_asim_oracle_i32(const uint32_t *a, const uint32_t *x, uint32_t *y, uint32_t M, uint32_t N); |
| 133 | /* Stable identity of an oracle; returns FORGE_V2_OK or FORGE_V2_ERR_ARG. */ |
| 134 | int forge_asim_oracle_digest(uint32_t oracle, uint8_t out[32]); |
| 135 | |
| 136 | /* Error bound (LSB) that a model guarantees at a calibration age and a |
| 137 | * temperature distance from the calibration point. Saturation is not part |
| 138 | * of the bound: clipping is a range limit and is caught by the check. */ |
| 139 | uint64_t forge_asim_model_bound(const ForgeAnalogSimModel *m, uint64_t cal_age_s, uint32_t temp_delta_mc); |
| 140 | |
| 141 | /* The V2 substrate descriptor this provider declares for a declared model |
| 142 | * and calibration: worst-case bound over the full calibration window and |
| 143 | * envelope. error_kind is EXACT only when the declared model is ideal. */ |
| 144 | int forge_asim_describe(const ForgeAnalogSimModel *declared, const ForgeCalibrationArtifact *cal, |
| 145 | ForgeSubstrateDescriptor *out); |
| 146 | |
| 147 | /* Identity of the simulated substrate instance (calibrations must name it). */ |
| 148 | void forge_asim_substrate_identity(uint8_t out[32]); |
| 149 | |
| 150 | /* The digital CPU realization used as the fallback. */ |
| 151 | void forge_asim_digital_descriptor(ForgeSubstrateDescriptor *out); |
| 152 | |
| 153 | /* One simulated run. Never consults an oracle. Returns the simulated |
| 154 | * latency in *latency_ns. */ |
| 155 | int forge_asim_realize(const ForgeAnalogSimModel *m, uint32_t wrong_row, int64_t wrong_delta, |
| 156 | const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N, |
| 157 | uint32_t run_index, uint64_t cal_age_s, int32_t temp_delta_mc, |
| 158 | uint64_t *y, uint64_t *latency_ns); |
| 159 | |
| 160 | /* The whole path: describe, eligibility, admission, realize, check, |
| 161 | * publish or fall back. Every evidence record it writes carries |
| 162 | * provenance_class = SIMULATED_DEVELOPMENT. */ |
| 163 | int forge_asim_execute(const ForgeAsimSetup *setup, const ForgeAsimRequest *req, ForgeAsimOutcome *out); |
| 164 | |
| 165 | /* Provenance used by every evidence record of this provider. Fixed. */ |
| 166 | uint32_t forge_asim_provenance(void); |
| 167 | |
| 168 | const char *forge_asim_refuse_name(uint32_t reason); |
| 169 | |
| 170 | #endif |