CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / runtime / cm_decision_diff.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Link the unchanged rx_costmodel_unit through --wrap: every public test input |
| 3 | * reaches both implementations. Numeric distance is measured, not substituted |
| 4 | * for exact agreement of refusal, eligibility, ranking, actions or reasons. */ |
| 5 | #include "runtime/rx_costmodel.h" |
| 6 | #include <assert.h> |
| 7 | #include <math.h> |
| 8 | #include <stdint.h> |
| 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <unistd.h> |
| 14 | #ifdef NDEBUG |
| 15 | #error "cm_decision_diff requires enabled assertions" |
| 16 | #endif |
| 17 | struct DiffCase { |
| 18 | RxCostModel model; |
| 19 | RxCmFeatures features; |
| 20 | RxCmPolicy policy; |
| 21 | uint32_t kind, arm; |
| 22 | int allow; |
| 23 | }; |
| 24 | static struct DiffCase worst; |
| 25 | static const RxCostModel *active_model; |
| 26 | static const RxCmFeatures *active_features; |
| 27 | static const RxCmPolicy *active_policy; |
| 28 | static uint32_t active_kind, active_arm; |
| 29 | static int active_allow; |
| 30 | static const char *worst_field = "none"; |
| 31 | static double worst_a, worst_b; |
| 32 | void __real_rx_cm_decide(const RxCostModel *, const RxCmPolicy *, const RxCmFeatures *, int, RxCmDecision *); |
| 33 | void oracle_cm_decide(const RxCostModel *, const RxCmPolicy *, const RxCmFeatures *, int, RxCmDecision *); |
| 34 | int __real_rx_cm_predict(const RxCostModel *, const RxCmFeatures *, uint32_t, RxCmPrediction *); |
| 35 | int oracle_cm_predict(const RxCostModel *, const RxCmFeatures *, uint32_t, RxCmPrediction *); |
| 36 | static unsigned decisions, predictions; |
| 37 | static uint64_t max_ulp; |
| 38 | static unsigned initializations, observations, calibrations, charges; |
| 39 | void __real_rx_cm_init(RxCostModel *, uint32_t); |
| 40 | int __real_rx_cm_observe(RxCostModel *, const RxCmFeatures *, uint32_t, const RxCmObservation *); |
| 41 | double __real_rx_cm_calibrate(RxCostModel *, const RxCmFeatures *, const uint32_t *, const uint64_t *, uint32_t, double); |
| 42 | void __real_rx_cm_charge(RxCostModel *, uint64_t, uint64_t); |
| 43 | void oracle_cm_init(RxCostModel *, uint32_t); |
| 44 | int oracle_cm_observe(RxCostModel *, const RxCmFeatures *, uint32_t, const RxCmObservation *); |
| 45 | double oracle_cm_calibrate(RxCostModel *, const RxCmFeatures *, const uint32_t *, const uint64_t *, uint32_t, double); |
| 46 | void oracle_cm_charge(RxCostModel *, uint64_t, uint64_t); |
| 47 | static void numeric(const char *field, double a, double b) { |
| 48 | if (isnan(a) && isnan(b)) return; /* Payloads are not part of the numeric contract. */ |
| 49 | uint64_t x, y; |
| 50 | memcpy(&x, &a, 8); memcpy(&y, &b, 8); |
| 51 | x = x >> 63 ? ~x : x | UINT64_C(0x8000000000000000); |
| 52 | y = y >> 63 ? ~y : y | UINT64_C(0x8000000000000000); |
| 53 | uint64_t distance = x > y ? x-y : y-x; |
| 54 | if (distance > max_ulp) { |
| 55 | max_ulp = distance; |
| 56 | worst_field = field; worst_a = a; worst_b = b; |
| 57 | memset(&worst,0,sizeof worst); |
| 58 | if (active_model) worst.model = *active_model; |
| 59 | if (active_features) worst.features = *active_features; |
| 60 | if (active_policy) worst.policy = *active_policy; |
| 61 | worst.kind = active_kind; worst.arm = active_arm; worst.allow = active_allow; |
| 62 | } |
| 63 | if (distance != 0) { |
| 64 | int fd=open("build/cm_decision_max.case",O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC,0600); |
| 65 | FILE *output=fd<0?NULL:fdopen(fd,"wb"); |
| 66 | if(!output&&fd>=0)close(fd); |
| 67 | assert(output && fwrite(&worst,sizeof worst,1,output)==1); |
| 68 | assert(fclose(output)==0); |
| 69 | fprintf(stderr,"CM DECISION floating differential FAIL field=%s candidate=%a oracle=%a ulp=%llu\n", |
| 70 | field,a,b,(unsigned long long)distance); |
| 71 | abort(); |
| 72 | } |
| 73 | } |
| 74 | #ifndef CM_DIFF_OUTPUT_ONLY |
| 75 | static void state_equal(const char *operation, const RxCostModel *a, const RxCostModel *b) { |
| 76 | const unsigned char *actual=(const unsigned char *)a, *expected=(const unsigned char *)b; |
| 77 | for (size_t i=0;i<sizeof *a;++i) if (actual[i]!=expected[i]) { |
| 78 | fprintf(stderr,"CM STATE differential FAIL operation=%s byte=%zu candidate=%02x oracle=%02x\n", |
| 79 | operation,i,actual[i],expected[i]); |
| 80 | abort(); |
| 81 | } |
| 82 | } |
| 83 | void __wrap_rx_cm_init(RxCostModel *m, uint32_t n) { |
| 84 | RxCostModel expected; |
| 85 | oracle_cm_init(m ? &expected : NULL,n); |
| 86 | __real_rx_cm_init(m,n); |
| 87 | if (m) state_equal("init",m,&expected); |
| 88 | ++initializations; |
| 89 | } |
| 90 | int __wrap_rx_cm_observe(RxCostModel *m, const RxCmFeatures *f, uint32_t arm, const RxCmObservation *o) { |
| 91 | RxCostModel expected; |
| 92 | if (m) memcpy(&expected,m,sizeof expected); |
| 93 | int reference=oracle_cm_observe(m ? &expected : NULL,f,arm,o); |
| 94 | int actual=__real_rx_cm_observe(m,f,arm,o); |
| 95 | assert(actual==reference); |
| 96 | if (m) state_equal("observe",m,&expected); |
| 97 | ++observations; |
| 98 | return actual; |
| 99 | } |
| 100 | double __wrap_rx_cm_calibrate(RxCostModel *m, const RxCmFeatures *f, const uint32_t *arm, |
| 101 | const uint64_t *ps, uint32_t n, double q) { |
| 102 | RxCostModel expected; |
| 103 | if (m) memcpy(&expected,m,sizeof expected); |
| 104 | double reference=oracle_cm_calibrate(m ? &expected : NULL,f,arm,ps,n,q); |
| 105 | double actual=__real_rx_cm_calibrate(m,f,arm,ps,n,q); |
| 106 | numeric("calibration",actual,reference); |
| 107 | if (m) state_equal("calibrate",m,&expected); |
| 108 | ++calibrations; |
| 109 | return actual; |
| 110 | } |
| 111 | void __wrap_rx_cm_charge(RxCostModel *m, uint64_t work, uint64_t explore) { |
| 112 | RxCostModel expected; |
| 113 | if (m) memcpy(&expected,m,sizeof expected); |
| 114 | oracle_cm_charge(m ? &expected : NULL,work,explore); |
| 115 | __real_rx_cm_charge(m,work,explore); |
| 116 | if (m) state_equal("charge",m,&expected); |
| 117 | ++charges; |
| 118 | } |
| 119 | #endif |
| 120 | static void prediction(const RxCmPrediction *a, const RxCmPrediction *b) { |
| 121 | assert(a->n == b->n && a->known == b->known && a->borrowed == b->borrowed && a->refused == b->refused); |
| 122 | numeric("mean_log2_ps",a->mean_log2_ps,b->mean_log2_ps); numeric("sd_log2",a->sd_log2,b->sd_log2); |
| 123 | numeric("dof",a->dof,b->dof); numeric("energy_pj",a->energy_pj,b->energy_pj); numeric("fail_p",a->fail_p,b->fail_p); |
| 124 | } |
| 125 | void __wrap_rx_cm_decide(const RxCostModel *m, const RxCmPolicy *p, const RxCmFeatures *f, int allow, RxCmDecision *out) { |
| 126 | RxCmDecision expected; |
| 127 | active_model=m; active_features=f; active_policy=p; active_kind=1; active_allow=allow; |
| 128 | __real_rx_cm_decide(m,p,f,allow,out); |
| 129 | oracle_cm_decide(m,p,f,allow,&expected); |
| 130 | assert(out->action == expected.action && out->arm == expected.arm && out->why == expected.why && |
| 131 | out->measure[0] == expected.measure[0] && out->measure[1] == expected.measure[1]); |
| 132 | numeric("p_runner_up_better",out->p_runner_up_better, expected.p_runner_up_better); |
| 133 | numeric("expected_improvement",out->expected_improvement, expected.expected_improvement); |
| 134 | for (unsigned i=0;i<RX_CM_ARMS;++i) prediction(out->pred+i,expected.pred+i); |
| 135 | ++decisions; |
| 136 | } |
| 137 | int __wrap_rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, RxCmPrediction *out) { |
| 138 | RxCmPrediction expected = {0}; |
| 139 | active_model=m; active_features=f; active_policy=NULL; active_kind=2; active_arm=arm; |
| 140 | int a = __real_rx_cm_predict(m,f,arm,out); |
| 141 | int b = oracle_cm_predict(m,f,arm,out ? &expected : NULL); |
| 142 | assert(a == b); |
| 143 | if (out) prediction(out,&expected); |
| 144 | ++predictions; |
| 145 | return a; |
| 146 | } |
| 147 | static uint64_t seed = 1; |
| 148 | static uint32_t random_word(void) { |
| 149 | seed = seed * UINT64_C(6364136223846793005) + 1; |
| 150 | return (uint32_t)(seed >> 32); |
| 151 | } |
| 152 | __attribute__((constructor)) static void random_cases(void) { |
| 153 | numeric("NaN class",nan("1"),nan("2")); |
| 154 | const char *replay = getenv("CM_DIFF_REPLAY_FD"); |
| 155 | if (replay) { |
| 156 | struct DiffCase c; |
| 157 | assert(strcmp(replay,"3")==0); |
| 158 | int fd=dup(3); |
| 159 | FILE *input=fd<0?NULL:fdopen(fd,"rb"); |
| 160 | if(!input&&fd>=0)close(fd); |
| 161 | assert(input && fread(&c,sizeof c,1,input)==1); |
| 162 | assert(fclose(input)==0); |
| 163 | if (c.kind==1) { |
| 164 | RxCmDecision d; |
| 165 | if (getenv("CM_DIFF_THRESHOLD_PROBE")) { |
| 166 | RxCmDecision candidate, reference; |
| 167 | __real_rx_cm_decide(&c.model,&c.policy,&c.features,c.allow,&candidate); |
| 168 | oracle_cm_decide(&c.model,&c.policy,&c.features,c.allow,&reference); |
| 169 | fprintf(stderr,"CM DECISION threshold probe EI candidate=%a oracle=%a\n", |
| 170 | candidate.expected_improvement,reference.expected_improvement); |
| 171 | if (candidate.expected_improvement >= 0 && reference.expected_improvement >= 0) { |
| 172 | c.policy.ei_min=fmin(candidate.expected_improvement,reference.expected_improvement); |
| 173 | c.policy.explore_frac=1; |
| 174 | c.policy.explore_allow_ps=UINT64_MAX; |
| 175 | c.model.work_ps=UINT64_MAX; c.model.explore_ps=0; c.allow=1; |
| 176 | } |
| 177 | } |
| 178 | __wrap_rx_cm_decide(&c.model,&c.policy,&c.features,c.allow,&d); |
| 179 | } else { |
| 180 | RxCmPrediction p; |
| 181 | __wrap_rx_cm_predict(&c.model,&c.features,c.arm,&p); |
| 182 | } |
| 183 | return; |
| 184 | } |
| 185 | RxCostModel m; |
| 186 | RxCmPolicy p; |
| 187 | rx_cm_init(&m,RX_CM_ARMS); |
| 188 | rx_cm_default_policy(&p); |
| 189 | RxCmFeatures f = {.M=32,.N=4,.cache_bytes=4096,.eligible=255}; |
| 190 | for (unsigned arm=0;arm<RX_CM_ARMS;++arm) for (unsigned i=0;i<128;++i) { |
| 191 | f.M=1+random_word()%4096; f.N=1+random_word()%32; |
| 192 | f.state_bytes=random_word(); |
| 193 | RxCmObservation o = {.ps_per_call=1+random_word(),.failed=(i==0 && arm==1)}; |
| 194 | int status=rx_cm_observe(&m,&f,arm,&o); |
| 195 | assert(status == RX_CM_OK); |
| 196 | } |
| 197 | for (unsigned i=0;i<1024;++i) { |
| 198 | f.op=i%17 ? 0 : UINT32_MAX; |
| 199 | f.M=random_word(); f.N=random_word(); f.state_bytes=random_word(); |
| 200 | f.cache_bytes=i%5 ? random_word() : 0; |
| 201 | f.core=random_word()%5; f.pressure=random_word()%5; |
| 202 | f.eligible=random_word(); f.latency_budget_ps=i%3 ? 0 : random_word(); |
| 203 | f.energy_budget_pj=i%7 ? 0 : random_word(); |
| 204 | p.fallback_arm=random_word()%11; |
| 205 | p.explore_allow_ps=i%2 ? 0 : UINT64_MAX; |
| 206 | m.work_ps=random_word(); m.explore_ps=random_word(); |
| 207 | RxCmDecision d; |
| 208 | __wrap_rx_cm_decide(&m,&p,&f,(i/2)%2,&d); |
| 209 | } |
| 210 | RxCmPrediction poisoned; |
| 211 | memset(&poisoned,0xa5,sizeof poisoned); |
| 212 | assert(__wrap_rx_cm_predict(NULL,&f,0,&poisoned)==RX_CM_ERR_ARG); |
| 213 | memset(&poisoned,0xa5,sizeof poisoned); |
| 214 | assert(__wrap_rx_cm_predict(&m,NULL,0,&poisoned)==RX_CM_ERR_ARG); |
| 215 | memset(&poisoned,0xa5,sizeof poisoned); |
| 216 | assert(__wrap_rx_cm_predict(&m,&f,RX_CM_ARMS,&poisoned)==RX_CM_ERR_ARG); |
| 217 | assert(__wrap_rx_cm_predict(&m,&f,0,NULL)==RX_CM_ERR_ARG); |
| 218 | } |
| 219 | __attribute__((destructor)) static void report(void) { |
| 220 | printf("CM DECISION differential PASS decisions=%u predictions=%u max_numeric_ulp=%llu field=%s candidate=%a oracle=%a\n", |
| 221 | decisions,predictions,(unsigned long long)max_ulp,worst_field,worst_a,worst_b); |
| 222 | printf("CM STATE differential PASS initializations=%u observations=%u calibrations=%u charges=%u\n", |
| 223 | initializations,observations,calibrations,charges); |
| 224 | } |
| 225 | #ifdef CM_DIFF_REPLAY_MAIN |
| 226 | int main(void) { return 0; } |
| 227 | #endif |