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 / src / omega_gpu_fp8_slots.h
| 1 | #ifndef OMEGA_GPU_FP8_SLOTS_H |
| 2 | #define OMEGA_GPU_FP8_SLOTS_H |
| 3 | /* |
| 4 | * omega_gpu_fp8_slots: versioned, replaceable FP8 expert slots (Expert Ribbon, aienix#4 / omega#533). |
| 5 | * |
| 6 | * A pool is S fixed slots of one FP8 projection shape [n][k] (weights + block-128 scales), backed by ONE |
| 7 | * OmegaGpuFp8Tensor with experts = S, so every existing FP8 launch (grouped GEMV by slot index, the record path through |
| 8 | * a group table of omega_gpu_fp8_slots_entry addresses) reads a slot exactly as it reads a resident expert. Same kernel, |
| 9 | * same arithmetic: a slot holding expert E gives bit-identical y to a resident tensor holding E. |
| 10 | * |
| 11 | * Memory: GPU-UNCACHED always (nvos.h:1117-1118: GPU-cached system memory "will not be coherent with direct CPU |
| 12 | * mappings"; omega #457 gpu_l2_stale_test: a CPU rewrite of a cached weight buffer read stale lines, uncached FRESH). |
| 13 | * The AIEN_OMEGA_FP8_WEIGHT_CACHE / omega_gpu_fp8_set_weight_cache setting does not apply to pools. |
| 14 | * |
| 15 | * Protocol (state machine in omega_fp8_slot_table.h, host-tested): |
| 16 | * load(slot, key, w, scales) -> version |
| 17 | * refused BUSY while the slot is pinned or loading; the slot is hidden (LOADING) before the first byte is written, |
| 18 | * so no reader can pin the old or the half-written contents. NaN e4m3 codes refuse (NAN_WEIGHT), NaN/inf scales |
| 19 | * refuse (BAD_ARGS); a refused load leaves the slot EMPTY. Weights are copied byte for byte, scales packed as by |
| 20 | * omega_gpu_fp8_tensor_upload (f32 * 2^56); then the platform SYNC (CPU writes before device reads); only then is a |
| 21 | * new ring-wide version published. Versions never repeat, so (slot, version) names one load (no ABA). |
| 22 | * Does NOT take the session lock: a load may run while launches on other slots are in flight. |
| 23 | * pin(slot, version) / unpin(slot, version) |
| 24 | * a pin is the reader fence: hold it from before the launch is built until its completion marker is seen |
| 25 | * (synchronous APIs: until the call returns; a command list: until omega_gpu_cmdlist_submit returns). |
| 26 | * pin is refused STALE when the slot was reloaded, evicted or never loaded. |
| 27 | * entry(slot, version) -> {w_va, s_va}: only while pinned at that version (for a group table / router directory). |
| 28 | * evict(slot): BUSY while pinned. |
| 29 | * gemv_grouped_f32(slots, versions, ...): pins every slot, one grouped launch, unpins after the completion marker. |
| 30 | * |
| 31 | * Return codes: OMEGA_GPU_FP8_* (omega_gpu_fp8_api.h) plus BUSY / STALE below. |
| 32 | */ |
| 33 | #include "omega_gpu_fp8_api.h" |
| 34 | #include <stdint.h> |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
| 39 | #define OMEGA_GPU_FP8_SLOTS_BUSY (-6) |
| 40 | #define OMEGA_GPU_FP8_SLOTS_STALE (-7) |
| 41 | |
| 42 | typedef struct OmegaGpuFp8Slots OmegaGpuFp8Slots; |
| 43 | |
| 44 | typedef struct { |
| 45 | uint32_t state; /* 0 EMPTY, 1 LOADING, 2 READY */ |
| 46 | uint32_t pins; |
| 47 | uint64_t key; /* caller's expert identity of the READY contents */ |
| 48 | uint64_t version; /* 0 = never published */ |
| 49 | } OmegaGpuFp8SlotInfo; |
| 50 | |
| 51 | typedef struct { |
| 52 | uint32_t slots, n, k; |
| 53 | uint64_t device_bytes; /* weights + packed scales of the whole pool */ |
| 54 | uint64_t loads, load_failures, busy_refusals, stale_refusals, evictions, pins; |
| 55 | uint32_t pinned_now, pinned_high; |
| 56 | uint64_t bytes_loaded; /* weight + scale bytes copied by successful loads */ |
| 57 | uint64_t load_ns; /* summed wall time of successful loads (scan + copy + pack + sync) */ |
| 58 | } OmegaGpuFp8SlotsStats; |
| 59 | |
| 60 | int omega_gpu_fp8_slots_alloc(uint32_t slots, uint32_t n, uint32_t k, OmegaGpuFp8Slots **out); |
| 61 | /* BUSY (nothing freed) while any slot is pinned or loading. */ |
| 62 | int omega_gpu_fp8_slots_free(OmegaGpuFp8Slots *p); |
| 63 | /* The backing tensor (experts = slots): pass slot indices as expert ids to the FP8 launches. */ |
| 64 | const OmegaGpuFp8Tensor *omega_gpu_fp8_slots_tensor(const OmegaGpuFp8Slots *p); |
| 65 | int omega_gpu_fp8_slots_load(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t key, const uint8_t *w, const uint16_t *scale_inv_bf16, |
| 66 | uint64_t *version); |
| 67 | int omega_gpu_fp8_slots_pin(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t version); |
| 68 | int omega_gpu_fp8_slots_unpin(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t version); |
| 69 | int omega_gpu_fp8_slots_evict(OmegaGpuFp8Slots *p, uint32_t slot); |
| 70 | int omega_gpu_fp8_slots_entry(OmegaGpuFp8Slots *p, uint32_t slot, uint64_t version, uint64_t *w_va, uint64_t *s_va); |
| 71 | int omega_gpu_fp8_slots_query(OmegaGpuFp8Slots *p, uint32_t slot, OmegaGpuFp8SlotInfo *out); |
| 72 | void omega_gpu_fp8_slots_stats(OmegaGpuFp8Slots *p, OmegaGpuFp8SlotsStats *out); |
| 73 | /* Pins every (slot, version) (STALE: nothing launched, nothing left pinned), one grouped launch on the pool tensor |
| 74 | * (omega_gpu_fp8_gemv_grouped_f32 semantics: x_rows 1 or groups), unpins after it returned. */ |
| 75 | int omega_gpu_fp8_slots_gemv_grouped_f32(OmegaGpuFp8Slots *p, uint32_t groups, const uint32_t *slots, const uint64_t *versions, |
| 76 | const float *x, uint32_t x_rows, float *y, OmegaGpuFp8Info *info); |
| 77 | const char *omega_gpu_fp8_slots_rc_name(int rc); |
| 78 | /* TEST ONLY: 1 = load ignores pins (negative control: a reader must then see replaced weights). */ |
| 79 | void omega_gpu_fp8_slots_test_set_mutant(OmegaGpuFp8Slots *p, int mutant); |
| 80 | |
| 81 | #ifdef __cplusplus |
| 82 | } |
| 83 | #endif |
| 84 | #endif |