omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_gpu_fp8_slots.c
| 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 <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | struct OmegaGpuFp8Slots { |
| 11 | uint32_t slots, n, k; |
| 12 | OmegaGpuFp8Tensor *t; |
| 13 | OmegaFp8SlotTable tab; |
| 14 | pthread_mutex_t mu; /* bytes_loaded / load_ns */ |
| 15 | uint64_t bytes_loaded, load_ns; |
| 16 | }; |
| 17 | |
| 18 | const char *omega_gpu_fp8_slots_rc_name(int rc) { |
| 19 | switch (rc) { |
| 20 | case OMEGA_GPU_FP8_SLOTS_BUSY: return "BUSY"; |
| 21 | case OMEGA_GPU_FP8_SLOTS_STALE: return "STALE"; |
| 22 | default: return omega_gpu_fp8_rc_name(rc); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /* The table's codes are the pool's codes (OK 0, BAD_ARGS -1, BUSY -6, STALE -7). */ |
| 27 | _Static_assert(OMEGA_FP8_SLOT_BUSY == OMEGA_GPU_FP8_SLOTS_BUSY && OMEGA_FP8_SLOT_STALE == OMEGA_GPU_FP8_SLOTS_STALE && |
| 28 | (int)OMEGA_FP8_SLOT_BAD_ARGS == (int)OMEGA_GPU_FP8_BAD_ARGS, "slot table codes"); |
| 29 | |
| 30 | int omega_gpu_fp8_slots_alloc(uint32_t slots, uint32_t n, uint32_t k, OmegaGpuFp8Slots **out) { |
| 31 | if (out) *out = NULL; |
| 32 | if (!out || slots == 0) return OMEGA_GPU_FP8_BAD_ARGS; |
| 33 | OmegaGpuFp8Slots *p = calloc(1, sizeof *p); |
| 34 | if (!p) return OMEGA_GPU_FP8_BAD_ARGS; |
| 35 | p->slots = slots; p->n = n; p->k = k; |
| 36 | int rc = omega_gpu_fp8_tensor_alloc_uncached(slots, n, k, &p->t); |
| 37 | if (rc != OMEGA_GPU_FP8_OK) { free(p); return rc; } |
| 38 | if (omega_fp8_slot_table_init(&p->tab, slots) != 0 || pthread_mutex_init(&p->mu, NULL) != 0) { |
| 39 | omega_gpu_fp8_tensor_free(p->t); omega_fp8_slot_table_fini(&p->tab); free(p); return OMEGA_GPU_FP8_BAD_ARGS; |
| 40 | } |
| 41 | *out = p; |
| 42 | return OMEGA_GPU_FP8_OK; |
| 43 | } |
| 44 | |
| 45 | int omega_gpu_fp8_slots_free(OmegaGpuFp8Slots *p) { |
| 46 | if (!p) return OMEGA_GPU_FP8_OK; |
| 47 | if (!omega_fp8_slot_table_idle(&p->tab)) return OMEGA_GPU_FP8_SLOTS_BUSY; |
| 48 | omega_gpu_fp8_tensor_free(p->t); |
| 49 | omega_fp8_slot_table_fini(&p->tab); |
| 50 | pthread_mutex_destroy(&p->mu); |
| 51 | free(p); |
| 52 | return OMEGA_GPU_FP8_OK; |
| 53 | } |
| 54 | |
| 55 | const OmegaGpuFp8Tensor *omega_gpu_fp8_slots_tensor(const OmegaGpuFp8Slots *p) { return p ? p->t : NULL; } |
| 56 | |
| 57 | int omega_gpu_fp8_slots_load(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t key, const uint8_t *w, const uint16_t *scale, |
| 58 | uint64_t *version) { |
| 59 | if (version) *version = 0; |
| 60 | if (!p || !w || !scale || !version) return OMEGA_GPU_FP8_BAD_ARGS; |
| 61 | if (omega_gpu_session_is_blocked()) return OMEGA_GPU_FP8_CHIP_FAIL; |
| 62 | const uint64_t t0 = omega_gpu_session_now_ns(); |
| 63 | int rc = omega_fp8_slot_begin_load(&p->tab, slot); |
| 64 | if (rc != OMEGA_FP8_SLOT_OK) return rc; |
| 65 | /* From here the slot is LOADING: invisible to pin, so the bytes below are never read half-written. */ |
| 66 | const size_t wbytes = (size_t)p->n * p->k, scount = (size_t)(p->n / OMEGA_FP8_BLOCK) * (p->k / OMEGA_FP8_BLOCK); |
| 67 | uint8_t *wd = NULL; float *sd = NULL; |
| 68 | size_t first = 0; |
| 69 | if (omega_gpu_fp8_tensor_cpu_view(p->t, slot, &wd, &sd) != 0) rc = OMEGA_GPU_FP8_BAD_ARGS; |
| 70 | else if (omega_fp8_pack_scan_nan(w, wbytes, &first)) { |
| 71 | char e[120]; omega_gpu_fmt(e, sizeof e, "fp8 slot load: e4m3 NaN code at byte %zu", first); |
| 72 | omega_gpu_session_set_error(e); |
| 73 | rc = OMEGA_GPU_FP8_NAN_WEIGHT; |
| 74 | } else if (omega_fp8_pack_scales(scale, scount, sd, 0) != 0) { |
| 75 | omega_gpu_session_set_error("fp8 slot load: NaN, inf or overflowing scale"); |
| 76 | rc = OMEGA_GPU_FP8_BAD_ARGS; |
| 77 | } else { |
| 78 | memcpy(wd, w, wbytes); |
| 79 | if (omega_gpu_fp8_tensor_sync_to_gpu(p->t) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 80 | } |
| 81 | int frc = omega_fp8_slot_finish_load(&p->tab, slot, key, rc == OMEGA_GPU_FP8_OK, version); |
| 82 | if (rc == OMEGA_GPU_FP8_OK && frc != OMEGA_FP8_SLOT_OK) rc = frc; |
| 83 | if (rc == OMEGA_GPU_FP8_OK) { |
| 84 | pthread_mutex_lock(&p->mu); |
| 85 | p->bytes_loaded += wbytes + scount * 4u; |
| 86 | p->load_ns += omega_gpu_session_now_ns() - t0; |
| 87 | pthread_mutex_unlock(&p->mu); |
| 88 | } |
| 89 | return rc; |
| 90 | } |
| 91 | |
| 92 | int omega_gpu_fp8_slots_pin(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t version) { |
| 93 | return p ? omega_fp8_slot_pin(&p->tab, slot, version) : OMEGA_GPU_FP8_BAD_ARGS; |
| 94 | } |
| 95 | int omega_gpu_fp8_slots_unpin(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t version) { |
| 96 | return p ? omega_fp8_slot_unpin(&p->tab, slot, version) : OMEGA_GPU_FP8_BAD_ARGS; |
| 97 | } |
| 98 | int omega_gpu_fp8_slots_evict(OmegaGpuFp8Slots *p, uint32_t slot) { |
| 99 | return p ? omega_fp8_slot_evict(&p->tab, slot) : OMEGA_GPU_FP8_BAD_ARGS; |
| 100 | } |
| 101 | |
| 102 | int omega_gpu_fp8_slots_entry(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t version, uint64_t *w_va, uint64_t *s_va) { |
| 103 | if (!p || !w_va || !s_va) return OMEGA_GPU_FP8_BAD_ARGS; |
| 104 | OmegaFp8SlotRec r; |
| 105 | int rc = omega_fp8_slot_query(&p->tab, slot, &r); |
| 106 | if (rc != OMEGA_FP8_SLOT_OK) return rc; |
| 107 | if (r.state != OMEGA_FP8_SLOT_READY || r.version != version || version == 0) return OMEGA_GPU_FP8_SLOTS_STALE; |
| 108 | if (r.pins == 0) { omega_gpu_session_set_error("fp8 slot entry: slot not pinned (pin before taking its address)"); return OMEGA_GPU_FP8_BAD_ARGS; } |
| 109 | return omega_gpu_fp8_tensor_entry(p->t, slot, w_va, s_va); |
| 110 | } |
| 111 | |
| 112 | int omega_gpu_fp8_slots_query(OmegaGpuFp8Slots *p, uint32_t slot, OmegaGpuFp8SlotInfo *out) { |
| 113 | if (!p || !out) return OMEGA_GPU_FP8_BAD_ARGS; |
| 114 | OmegaFp8SlotRec r; |
| 115 | int rc = omega_fp8_slot_query(&p->tab, slot, &r); |
| 116 | if (rc != OMEGA_FP8_SLOT_OK) return rc; |
| 117 | out->state = r.state; out->pins = r.pins; out->key = r.key; out->version = r.version; |
| 118 | return OMEGA_GPU_FP8_OK; |
| 119 | } |
| 120 | |
| 121 | void omega_gpu_fp8_slots_stats(OmegaGpuFp8Slots *p, OmegaGpuFp8SlotsStats *out) { |
| 122 | if (!out) return; |
| 123 | memset(out, 0, sizeof *out); |
| 124 | if (!p) return; |
| 125 | OmegaFp8SlotStats s; |
| 126 | omega_fp8_slot_table_stats(&p->tab, &s); |
| 127 | out->slots = p->slots; out->n = p->n; out->k = p->k; |
| 128 | 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); |
| 129 | out->loads = s.loads; out->load_failures = s.load_failures; out->busy_refusals = s.busy_refusals; |
| 130 | out->stale_refusals = s.stale_refusals; out->evictions = s.evictions; out->pins = s.pins; |
| 131 | out->pinned_now = s.pinned_now; out->pinned_high = s.pinned_high; |
| 132 | pthread_mutex_lock(&p->mu); |
| 133 | out->bytes_loaded = p->bytes_loaded; out->load_ns = p->load_ns; |
| 134 | pthread_mutex_unlock(&p->mu); |
| 135 | } |
| 136 | |
| 137 | int omega_gpu_fp8_slots_gemv_grouped_f32(OmegaGpuFp8Slots *p, uint32_t groups, const uint32_t *slots, const uint64_t *versions, |
| 138 | const float *x, uint32_t x_rows, float *y, OmegaGpuFp8Info *info) { |
| 139 | if (!p || !slots || !versions || groups == 0) return OMEGA_GPU_FP8_BAD_ARGS; |
| 140 | if (groups > OMEGA_GPU_FP8_MAX_GROUPS) return OMEGA_GPU_FP8_TOO_LARGE; |
| 141 | uint32_t pinned = 0; |
| 142 | int rc = OMEGA_GPU_FP8_OK; |
| 143 | for (; pinned < groups; pinned++) { |
| 144 | rc = omega_fp8_slot_pin(&p->tab, slots[pinned], versions[pinned]); |
| 145 | if (rc != OMEGA_FP8_SLOT_OK) break; |
| 146 | } |
| 147 | if (rc == OMEGA_GPU_FP8_OK) rc = omega_gpu_fp8_gemv_grouped_f32(p->t, groups, slots, x, x_rows, y, info); |
| 148 | /* The call returned after the launch's completion marker (or failed): no reader of these slots remains. */ |
| 149 | for (uint32_t i = 0; i < pinned; i++) (void)omega_fp8_slot_unpin(&p->tab, slots[i], versions[i]); |
| 150 | return rc; |
| 151 | } |
| 152 | |
| 153 | void omega_gpu_fp8_slots_test_set_mutant(OmegaGpuFp8Slots *p, int mutant) { |
| 154 | if (p) omega_fp8_slot_table_set_mutant(&p->tab, mutant); |
| 155 | } |