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/omega-mv/blob/main/src/omega_gpu_gdn_api.h # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_gpu_gdn_api.h` 137 lines ยท 10080 bytes Raw Blame History 1 #ifndef OMEGA_GPU_GDN_API_H 2 #define OMEGA_GPU_GDN_API_H 3 /* 4 * omega_gpu_gdn_api: the Gated DeltaNet decode step of Qwen3.8-27B in one launch per layer (omega#542, 2026-10-18). 5 * The kernel is described in omega_blackwell_codegen_gdn.h; the maths is sc qwen38.rs `delta_net`. 6 * 7 * int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out); 8 * Device-resident per-layer parameters, conv tails and recurrent states (zeroed), plus the launch buffer. 9 * The tails and states are GPU-cached (2026-10-19; AIEN_OMEGA_GDN_STATE_CACHED=0 keeps them uncached): the host 10 * never touches them directly, reset zeroes them with a chip launch and set_state / get_state copy one layer through 11 * an uncached staging buffer with chip launches (omega_blackwell_codegen_gdn.h, state copy / zero). 12 * int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, const float *a_log, 13 * const float *dt_bias, const float *norm_w); 14 * conv_w: linear_attn.conv1d.weight [10240][1][4] widened to f32 ([10240][4]); a_log, dt_bias: [48]; 15 * norm_w: linear_attn.norm.weight [128]. negA = -exp(a_log) is computed here (double, libm-free omega_hostmath_exp, rounded to float). 16 * int omega_gpu_gdn_set_state / omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state); 17 * The reference layouts: tail [3][10240] (oldest first, qwen38.rs conv_tail), state [48][128 k][128 v]. 18 * get_state refuses (CHIP_FAIL) when the three per-head copies of a key head's q/k tail disagree. 19 * int omega_gpu_gdn_reset(OmegaGpuGdn *g); every layer's tail and state to zero (a new sequence) 20 * int omega_gpu_gdn_step(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, 21 * float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, 22 * OmegaGpuGdnInfo *info); 23 * qkv [10240] and z [6144] are the in_proj outputs (times 1 / in_scale: the kernel multiplies them by in_scale); 24 * ab [48][2] = {a[h], b[h]}: the in_proj_a and in_proj_b rows stacked interleaved (row 2h = a_h, 2h + 1 = b_h) 25 * give it from one bf16 GEMV. out [6144] = the out_proj input times out_scale. Updates the layer's tail and 26 * state on the device. eps_l2 is 1e-6 in the reference; eps_norm is rms_norm_eps. 27 * int omega_gpu_gdn_record(cl, g, layer, qkv_va, z_va, ab_va, eps_l2, eps_norm, in_scale, out_scale, &out_va); 28 * The same launch appended to a command list (caller holds omega_gpu_session_lock()); inputs are device 29 * addresses, out lands in the launch buffer (out_va). A kernel cache miss that would evict is refused. 30 * 31 * Every buffer is GPU-uncached host-visible session memory (nvos.h:1117 rule), launched through 32 * omega_gpu_session_launch like the router. The output is poisoned before the launch; a surviving poison word is 33 * CHIP_FAIL. 34 */ 35 #include "omega_blackwell_codegen_gdn.h" 36 #include 37 #include 38 39 enum { 40 OMEGA_GPU_GDN_OK = 0, 41 OMEGA_GPU_GDN_BAD_ARGS = -1, 42 OMEGA_GPU_GDN_TOO_LARGE = -2, 43 OMEGA_GPU_GDN_CODEGEN_FAIL = -3, 44 OMEGA_GPU_GDN_CHIP_FAIL = -4 45 }; 46 47 #define OMEGA_GPU_GDN_MAX_LAYERS 64u 48 #define OMEGA_GPU_GDN_TAIL_FLOATS (3u * OMEGA_GDN_CONV_DIM) /* reference tail */ 49 #define OMEGA_GPU_GDN_STATE_FLOATS (OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_HEAD_DIM * OMEGA_GDN_HEAD_DIM) /* 786432 */ 50 51 typedef struct OmegaGpuGdn OmegaGpuGdn; 52 53 typedef struct { 54 uint32_t layer, threads, grid_x, grid_y, insns, gprs; 55 uint64_t state_bytes; /* state bytes read and written: 2 * 48 * 128 * 128 * 4 */ 56 uint64_t elapsed_ns; /* marker-to-marker chip time */ 57 uint64_t call_ns; /* whole call */ 58 uint32_t completion_marker; 59 int kernel_cache_hit; 60 } OmegaGpuGdnInfo; 61 62 int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out); 63 /* Q36 (sc L306): the same with value heads per key head `ratio` (OmegaGdnSpec.ratio: 0 = 3, Qwen3.8-27B, the plain alloc; 64 * 2 = Qwen3.6-35B-A3B). Every size the other calls use (conv dim, value width, tail, state, parameter block, prefill 65 * rows) follows the ratio; the comments elsewhere in this header give the ratio-3 numbers. */ 66 int omega_gpu_gdn_alloc_ratio(uint32_t layers, uint32_t ratio, OmegaGpuGdn **out); 67 void omega_gpu_gdn_free(OmegaGpuGdn *g); 68 int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, const float *a_log, const float *dt_bias, 69 const float *norm_w); 70 int omega_gpu_gdn_reset(OmegaGpuGdn *g); 71 int omega_gpu_gdn_set_state(OmegaGpuGdn *g, uint32_t layer, const float *tail, const float *state); 72 int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state); 73 int omega_gpu_gdn_step(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2, 74 float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnInfo *info); 75 struct OmegaGpuCmdList; 76 int omega_gpu_gdn_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint64_t qkv_va, uint64_t z_va, 77 uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale, uint64_t *out_va); 78 /* Test helper: the out row the last launch wrote to the launch buffer (after a record + submit, for example). */ 79 int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out); 80 const char *omega_gpu_gdn_last_error(void); 81 const char *omega_gpu_gdn_rc_name(int rc); 82 83 /* ---- multi-token decode (L369, 2026-10-21): T decode steps of layer `layer` in ONE launch, bit for bit the decode step 84 * T times (omega_blackwell_codegen_gdn.h, multi-token decode). Token t reads qkv_va + t * xstride, z_va + t * xstride, 85 * ab_va + t * abstride and writes its [vdim] output row at out_va + t * ostride (bytes; 16-byte aligned rows, 8 for ab). 86 * int omega_gpu_gdn_multi_prepare(g); builds the kernel now (the record path refuses a kernel cache miss) 87 * int omega_gpu_gdn_multi_record(cl, g, layer, T, qkv_va, z_va, xstride, ab_va, abstride, out_va, ostride, eps_l2, 88 * eps_norm, in_scale, out_scale); appended to cl; the caller holds the session lock */ 89 int omega_gpu_gdn_multi_prepare(OmegaGpuGdn *g); 90 int omega_gpu_gdn_multi_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va, 91 uint64_t z_va, uint32_t xstride, uint64_t ab_va, uint32_t abstride, uint64_t out_va, uint32_t ostride, 92 float eps_l2, float eps_norm, float in_scale, float out_scale); 93 94 /* ---- prefill (omega#543, 2026-10-18): T >= 3 new tokens of one layer in three launches (prep, scan, post; see 95 * omega_blackwell_codegen_gdn.h), continuing from the layer's tail and state exactly as T decode steps would, and leaving 96 * them where the next prefill chunk or decode step picks them up. A long prompt is prefilled in chunks. 97 * int omega_gpu_gdn_prefill(g, layer, T, qkv [T][10240], z [T][6144], ab [T][48][2], eps_l2, eps_norm, in_scale, 98 * out_scale, out [T][6144], info); the decode step's row layouts, one row per token 99 * int omega_gpu_gdn_prefill_reserve(g, max_tokens); sizes the prefill scratch now and pins it (the record path needs 100 * it: a recorded launch must never see its scratch move); a later prefill of more tokens is refused 101 * int omega_gpu_gdn_prefill_record(cl, g, layer, T, qkv_va, z_va, ab_va, eps_l2, eps_norm, in_scale, out_scale, &out_va); 102 * the three launches appended to a command list (caller holds the lock); out_va = the [T][6144] rows in the scratch 103 * Scratch bytes for T tokens: T * (40960 + 3 * 24576 + 384) + ceil8(T) * 74496 (the per-token records dominate). */ 104 #define OMEGA_GPU_GDN_PF_MAX_T 65536u 105 106 typedef struct { 107 uint32_t layer, tokens; 108 uint32_t insns[3], gprs[3]; /* prep, scan, post */ 109 uint64_t elapsed_ns[3]; /* marker-to-marker chip time per launch (host path) */ 110 uint64_t call_ns; 111 uint32_t kernel_cache_hits; /* 0..3 */ 112 } OmegaGpuGdnPfInfo; 113 114 int omega_gpu_gdn_prefill(OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, const float *qkv, const float *z, const float *ab, 115 float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info); 116 int omega_gpu_gdn_prefill_reserve(OmegaGpuGdn *g, uint32_t max_tokens); 117 int omega_gpu_gdn_prefill_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va, 118 uint64_t z_va, uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale, 119 uint64_t *out_va); 120 /* Test helper: the out rows the last prefill wrote (after a record + submit). */ 121 int omega_gpu_gdn_prefill_read_out(OmegaGpuGdn *g, uint32_t tokens, float *out); 122 123 /* Host layout helpers (no device; the test's simulator path uses them too). */ 124 /* The parameter block (OMEGA_GDN_PAR_BYTES) from the checkpoint tensors (see set_layer). -1 on a NULL pointer. */ 125 int omega_gdn_pack_params(const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block); 126 /* reference tail [3][10240] <-> device tail [48][384][4] (unpack: the q/k channels from head 3 * hk; -1 when the 127 * three copies of a key head disagree bit for bit) */ 128 void omega_gdn_pack_tail(const float *tail, float *dev); 129 int omega_gdn_unpack_tail(const float *dev, float *tail); 130 /* The same for a ratio (see omega_gpu_gdn_alloc_ratio); -1 on an unsupported ratio. */ 131 int omega_gdn_pack_params_r(uint32_t ratio, const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block); 132 int omega_gdn_pack_tail_r(uint32_t ratio, const float *tail, float *dev); 133 int omega_gdn_unpack_tail_r(uint32_t ratio, const float *dev, float *tail); 134 135 void omega_gpu_gdn_test_set_mutant(int mutant); /* TEST ONLY: OmegaGdnSpec.mutant; clears the kernel cache */ 136 137 #endif