Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/runtime/cm_decision_diff.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / runtime / cm_decision_diff.c` 227 lines · 10856 bytes Raw Blame History 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 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 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;in == 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;ipred+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