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/src/omega_gpu_fp8_slots.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 / src / omega_gpu_fp8_slots.c` 190 lines · 9029 bytes Raw Blame History 1 /* omega_gpu_fp8_slots.c: versioned replaceable FP8 expert slots (see the header). */ 2 #include "omega_gpu_fp8_slots.h" 3 #include "omega_gpu_fp8_internal.h" 4 #include "omega_fp8_slot_table.h" 5 #include "omega_gpu_session.h" 6 #include "omega_gpu_fmt.h" 7 #include 8 #include 9 #include "omega_api_shells.h" 10 #include "omega_api_views_rest.h" 11 12 struct OmegaGpuFp8Slots { 13 uint32_t slots, n, k; 14 OmegaGpuFp8Tensor *t; 15 OmegaFp8SlotTable tab; 16 pthread_mutex_t mu; /* bytes_loaded / load_ns */ 17 uint64_t bytes_loaded, load_ns; 18 }; 19 20 const char *omega_gpu_fp8_slots_rc_name(int rc) { 21 switch (rc) { 22 case OMEGA_GPU_FP8_SLOTS_BUSY: return "BUSY"; 23 case OMEGA_GPU_FP8_SLOTS_STALE: return "STALE"; 24 default: return omega_gpu_fp8_rc_name(rc); 25 } 26 } 27 28 /* The table's codes are the pool's codes (OK 0, BAD_ARGS -1, BUSY -6, STALE -7). */ 29 _Static_assert(OMEGA_FP8_SLOT_BUSY == OMEGA_GPU_FP8_SLOTS_BUSY && OMEGA_FP8_SLOT_STALE == OMEGA_GPU_FP8_SLOTS_STALE && 30 (int)OMEGA_FP8_SLOT_BAD_ARGS == (int)OMEGA_GPU_FP8_BAD_ARGS, "slot table codes"); 31 32 int omega_gpu_fp8_slots_alloc(uint32_t slots, uint32_t n, uint32_t k, OmegaGpuFp8Slots **out) { 33 if (out) *out = NULL; 34 if (!out || slots == 0) return OMEGA_GPU_FP8_BAD_ARGS; 35 OmegaGpuFp8Slots *p = calloc(1, sizeof *p); 36 if (!p) return OMEGA_GPU_FP8_BAD_ARGS; 37 const uint32_t shape[3] = {slots, n, k}; 38 ar_words(p, 3, shape, 3); 39 int rc = omega_gpu_fp8_tensor_alloc_uncached(slots, n, k, &p->t); 40 if (rc != OMEGA_GPU_FP8_OK) { free(p); return rc; } 41 if (omega_fp8_slot_table_init(&p->tab, slots) != 0 || pthread_mutex_init(&p->mu, NULL) != 0) { 42 omega_gpu_fp8_tensor_free(p->t); omega_fp8_slot_table_fini(&p->tab); free(p); return OMEGA_GPU_FP8_BAD_ARGS; 43 } 44 *out = p; 45 return OMEGA_GPU_FP8_OK; 46 } 47 48 int omega_gpu_fp8_slots_free(OmegaGpuFp8Slots *p) { 49 if (!p) return OMEGA_GPU_FP8_OK; 50 if (!omega_fp8_slot_table_idle(&p->tab)) return OMEGA_GPU_FP8_SLOTS_BUSY; 51 omega_gpu_fp8_tensor_free(p->t); 52 omega_fp8_slot_table_fini(&p->tab); 53 pthread_mutex_destroy(&p->mu); 54 free(p); 55 return OMEGA_GPU_FP8_OK; 56 } 57 58 const OmegaGpuFp8Tensor *omega_gpu_fp8_slots_tensor(const OmegaGpuFp8Slots *p) { return p ? p->t : NULL; } 59 60 int omega_gpu_fp8_slots_load(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t key, const uint8_t *w, const uint16_t *scale, 61 uint64_t *version) { 62 if (version) *version = 0; 63 if (!p || !w || !scale || !version) return OMEGA_GPU_FP8_BAD_ARGS; 64 if (omega_gpu_session_is_blocked()) return OMEGA_GPU_FP8_CHIP_FAIL; 65 const uint64_t t0 = omega_gpu_session_now_ns(); 66 int rc = omega_fp8_slot_begin_load(&p->tab, slot); 67 if (rc != OMEGA_FP8_SLOT_OK) return rc; 68 /* From here the slot is LOADING: invisible to pin, so the bytes below are never read half-written. */ 69 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 70 const size_t wbytes = (size_t)p->n * p->k, scount = (size_t)(p->n / OMEGA_FP8_BLOCK) * (p->k / OMEGA_FP8_BLOCK); 71 #else 72 uint64_t plan[32] = {3, p->slots, p->n, p->k}; 73 (void)ar_plan(plan); 74 const size_t wbytes = plan[17], scount = plan[16]; 75 #endif 76 uint8_t *wd = NULL; float *sd = NULL; 77 size_t first = 0; 78 if (omega_gpu_fp8_tensor_cpu_view(p->t, slot, &wd, &sd) != 0) rc = OMEGA_GPU_FP8_BAD_ARGS; 79 else if (omega_fp8_pack_scan_nan(w, wbytes, &first)) { 80 char e[120]; omega_gpu_fmt(e, sizeof e, "fp8 slot load: e4m3 NaN code at byte %zu", first); 81 omega_gpu_session_set_error(e); 82 rc = OMEGA_GPU_FP8_NAN_WEIGHT; 83 } else if (omega_fp8_pack_scales(scale, scount, sd, 0) != 0) { 84 omega_gpu_session_set_error("fp8 slot load: NaN, inf or overflowing scale"); 85 rc = OMEGA_GPU_FP8_BAD_ARGS; 86 } else { 87 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 88 memcpy(wd, w, wbytes); 89 #else 90 ar_copy(wd, w, wbytes); 91 #endif 92 if (omega_gpu_fp8_tensor_sync_to_gpu(p->t) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; 93 } 94 int frc = omega_fp8_slot_finish_load(&p->tab, slot, key, rc == OMEGA_GPU_FP8_OK, version); 95 if (rc == OMEGA_GPU_FP8_OK && frc != OMEGA_FP8_SLOT_OK) rc = frc; 96 if (rc == OMEGA_GPU_FP8_OK) { 97 pthread_mutex_lock(&p->mu); 98 p->bytes_loaded += wbytes + scount * 4u; 99 p->load_ns += omega_gpu_session_now_ns() - t0; 100 pthread_mutex_unlock(&p->mu); 101 } 102 return rc; 103 } 104 105 int omega_gpu_fp8_slots_pin(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t version) { 106 return p ? omega_fp8_slot_pin(&p->tab, slot, version) : OMEGA_GPU_FP8_BAD_ARGS; 107 } 108 int omega_gpu_fp8_slots_unpin(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t version) { 109 return p ? omega_fp8_slot_unpin(&p->tab, slot, version) : OMEGA_GPU_FP8_BAD_ARGS; 110 } 111 int omega_gpu_fp8_slots_evict(OmegaGpuFp8Slots *p, uint32_t slot) { 112 return p ? omega_fp8_slot_evict(&p->tab, slot) : OMEGA_GPU_FP8_BAD_ARGS; 113 } 114 115 int omega_gpu_fp8_slots_entry(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t version, uint64_t *w_va, uint64_t *s_va) { 116 if (!p || !w_va || !s_va) return OMEGA_GPU_FP8_BAD_ARGS; 117 OmegaFp8SlotRec r; 118 int rc = omega_fp8_slot_query(&p->tab, slot, &r); 119 if (rc != OMEGA_FP8_SLOT_OK) return rc; 120 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 121 uint64_t gate[16] = { r.state, r.pins, r.key, r.version }; 122 int gate_rc = omega_api_slot(7, version, gate); 123 #else 124 uint64_t result = 0; 125 ar_check(omega_arsgate(&result, version, &r, 3)); 126 int gate_rc = (int)(int64_t)result; 127 #endif 128 if (gate_rc == OMEGA_GPU_FP8_SLOTS_STALE) return gate_rc; 129 if (gate_rc != 0) { omega_gpu_session_set_error("fp8 slot entry: slot not pinned (pin before taking its address)"); return gate_rc; } 130 return omega_gpu_fp8_tensor_entry(p->t, slot, w_va, s_va); 131 } 132 133 int omega_gpu_fp8_slots_query(OmegaGpuFp8Slots *p, uint32_t slot, OmegaGpuFp8SlotInfo *out) { 134 if (!p || !out) return OMEGA_GPU_FP8_BAD_ARGS; 135 OmegaFp8SlotRec r; 136 int rc = omega_fp8_slot_query(&p->tab, slot, &r); 137 if (rc != OMEGA_FP8_SLOT_OK) return rc; 138 _Static_assert(sizeof *out == sizeof r, "slot query view"); 139 _Static_assert(offsetof(OmegaGpuFp8SlotInfo, key) == offsetof(OmegaFp8SlotRec, key), "slot query key view"); 140 _Static_assert(offsetof(OmegaGpuFp8SlotInfo, version) == offsetof(OmegaFp8SlotRec, version), "slot query version view"); 141 ar_copy(out, &r, sizeof r); 142 return OMEGA_GPU_FP8_OK; 143 } 144 145 void omega_gpu_fp8_slots_stats(OmegaGpuFp8Slots *p, OmegaGpuFp8SlotsStats *out) { 146 if (!out) return; 147 ar_words(out, sizeof *out / sizeof(uint32_t), NULL, 0); 148 if (!p) return; 149 OmegaFp8SlotStats s; 150 omega_fp8_slot_table_stats(&p->tab, &s); 151 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 152 out->slots = p->slots; out->n = p->n; out->k = p->k; 153 out->device_bytes = (uint64_t)p->slots * ((uint64_t)p->n * p->k + (uint64_t)(p->n / OMEGA_FP8_BLOCK) * (p->k / OMEGA_FP8_BLOCK) * 4u); 154 out->loads = s.loads; out->load_failures = s.load_failures; out->busy_refusals = s.busy_refusals; 155 out->stale_refusals = s.stale_refusals; out->evictions = s.evictions; out->pins = s.pins; 156 out->pinned_now = s.pinned_now; out->pinned_high = s.pinned_high; 157 #else 158 _Static_assert(sizeof *out == 12 * sizeof(uint64_t), "slot statistics descriptor extent"); 159 _Static_assert(offsetof(OmegaGpuFp8SlotsStats, device_bytes) == 16, "slot statistics device bytes"); 160 _Static_assert(offsetof(OmegaGpuFp8SlotsStats, loads) == 24, "slot statistics counters"); 161 _Static_assert(offsetof(OmegaGpuFp8SlotsStats, pinned_now) == 72, "slot statistics pin counts"); 162 _Static_assert(offsetof(OmegaGpuFp8SlotsStats, bytes_loaded) == 80, "slot statistics load counters"); 163 const uint64_t cfg[8] = {p->slots, p->n, p->k}; 164 uint64_t result = 0; 165 ar_check(omega_arsstats(&result, cfg, 8, &s, 7, out, 12)); 166 #endif 167 pthread_mutex_lock(&p->mu); 168 ar_copy(&out->bytes_loaded, &p->bytes_loaded, sizeof out->bytes_loaded + sizeof out->load_ns); 169 pthread_mutex_unlock(&p->mu); 170 } 171 172 int omega_gpu_fp8_slots_gemv_grouped_f32(OmegaGpuFp8Slots *p, uint32_t groups, const uint32_t *slots, const uint64_t *versions, 173 const float *x, uint32_t x_rows, float *y, OmegaGpuFp8Info *info) { 174 if (!p || !slots || !versions || groups == 0) return OMEGA_GPU_FP8_BAD_ARGS; 175 if (groups > OMEGA_GPU_FP8_MAX_GROUPS) return OMEGA_GPU_FP8_TOO_LARGE; 176 uint32_t pinned = 0; 177 int rc = OMEGA_GPU_FP8_OK; 178 for (; pinned < groups; pinned++) { 179 rc = omega_fp8_slot_pin(&p->tab, slots[pinned], versions[pinned]); 180 if (rc != OMEGA_FP8_SLOT_OK) break; 181 } 182 if (rc == OMEGA_GPU_FP8_OK) rc = omega_gpu_fp8_gemv_grouped_f32(p->t, groups, slots, x, x_rows, y, info); 183 /* The call returned after the launch's completion marker (or failed): no reader of these slots remains. */ 184 for (uint32_t i = 0; i < pinned; i++) (void)omega_fp8_slot_unpin(&p->tab, slots[i], versions[i]); 185 return rc; 186 } 187 188 void omega_gpu_fp8_slots_test_set_mutant(OmegaGpuFp8Slots *p, int mutant) { 189 if (p) omega_fp8_slot_table_set_mutant(&p->tab, mutant); 190 }