interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_gpu_prefill_attn_api.h

94 lines · 6947 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef OMEGA_GPU_PREFILL_ATTN_API_H
18#define OMEGA_GPU_PREFILL_ATTN_API_H
19/*
20 * omega_gpu_prefill_attn_api: causal prefill attention on tensor cores for Qwen3-30B-A3B (OWN-30B L1 attention,
21 * 2026-10-13). The API of ~/handoffs/own30b-attn-api.md; the kernel is described in omega_blackwell_codegen_pattn.h.
22 *
23 * int omega_gpu_prefill_attn(c, layer, pos0, m, qkv, eps, out_scale, out, info)
24 * c: the decode attention's cache (shared KV cache; positions 0 .. pos0-1 of `layer` already in it).
25 * qkv: host f32 [m][5120] (q 32 x 128 | k 4 x 128 | v 4 x 128, raw), tokens at positions pos0 .. pos0+m-1.
26 * Appends K = bf16(RoPE(RMSNorm(k) * k_norm)) and V = bf16(v) for those positions (the decode kernel's operation
27 * order, so the rows equal m decode steps' rows on the same chip), and writes out: host f32 [m][32 * 128] (token-
28 * major, head h at h * 128) multiplied by out_scale. Staged in passes of up to 1024 tokens, each one APPEND + one ATTEND launch (cut 3; set_fused(1): 64-token fused chunks). pos0 + m <= max_pos.
29 * Returns OMEGA_GPU_DATTN_OK or an OMEGA_GPU_DATTN_* code; the text is in omega_gpu_decode_attn_last_error().
30 *
31 * Every buffer is GPU-uncached host-visible session memory (nvos.h:1117 rule), launched through
32 * omega_gpu_session_launch. The chunk staging buffers (qkv 64 x 5120 f32, out 64 x 4096 f32) belong to this module
33 * and are released with omega_gpu_prefill_attn_release() or when the session closes.
34 */
35#include "omega_gpu_decode_attn_api.h"
36#include "omega_blackwell_codegen_pattn.h"
37#include <stdint.h>
38
39typedef struct {
40 uint32_t layer, pos0, m, chunks; /* chunks = launches made (2 per pass, or ceil(m / 64) fused) */
41 uint32_t grid_x, grid_y, threads, splits, per_split; /* of the first launch; splits = 1 (no key split), per_split = keys seen by that launch's last CTA */
42 uint64_t elapsed_ns; /* chip time, summed over the launches */
43 uint64_t call_ns;
44 int kernel_cache_hit;
45} OmegaGpuPrefillAttnInfo;
46
47int omega_gpu_prefill_attn(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos0, uint32_t m,
48 const float *qkv, float eps, float out_scale, float *out,
49 OmegaGpuPrefillAttnInfo *info);
50void omega_gpu_prefill_attn_release(void);
51
52/* Device-resident entry (cut 3), for the one-pass prefill pipeline: the same work with no host copies.
53 * qkv_va: f32 [m][5120] rows (20480-byte stride), out_va: f32 [m][4096] rows (16384-byte stride), both 16-byte aligned
54 * GPU VAs in the shared session (GPU-cached device buffers from omega_gpu_pf_alloc_dev, or session buffers).
55 * The kernels only read qkv and only write out; no poison check is possible here (the host never reads device
56 * buffers), the caller's download path is the check. Two launches (APPEND then ATTEND over all m tokens), or
57 * 64-token fused chunks after omega_gpu_prefill_attn_set_fused(1). Same return codes as omega_gpu_prefill_attn. */
58int omega_gpu_prefill_attn_dev(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos0, uint32_t m,
59 uint64_t qkv_va, float eps, float out_scale, uint64_t out_va,
60 OmegaGpuPrefillAttnInfo *info);
61
62/* ---- measurement knobs (tests; each clears the kernel cache) ---- */
63void omega_gpu_prefill_attn_set_warps(uint32_t warps); /* 0 = default (OMEGA_GPU_PATTN_WARPS); 4 or 8 */
64void omega_gpu_prefill_attn_test_set_mutant(int mutant); /* TEST ONLY: OmegaPrefillAttnSpec.mutant */
65#define OMEGA_GPU_PATTN_WARPS 8u
66void omega_gpu_prefill_attn_set_fused(int fused); /* 1: cut-1 path (64-token fused chunks); 0 (default): APPEND + ATTEND */
67/* ATTEND reads a GPU-cached mirror of the layer's K/V (history copied in, new rows written by APPEND to both places).
68 * Default on when built with omega_gpu_prefill.h (kv_mirror_available() == 1); a no-op otherwise. */
69void omega_gpu_prefill_attn_set_kv_mirror(int on);
70/* Persistent mirror (cut 5): one per (cache, layer), kept between calls, in the decode cache's EXACT layout
71 * ([K plane | V plane], plane = view.v_va - view.k_va, row (kv_head, pos) at plane + (kv_head * max_pos + pos) * 256, bf16)
72 * but in GPU-cached device memory (omega_gpu_pf_alloc_dev), so the decode kernel can later be pointed at it unchanged.
73 * Rows 0 .. valid-1 equal the cache. A call copies only cache rows valid .. pos0-1 (rows decode appended since) and
74 * writes rows pos0 .. pos0+m-1 to both. CONTRACT for token identity (the mirror cannot see other writers):
75 * - anyone who rewrites cache rows below a mirror's `valid` other than through this API (a new sequence decoded into a
76 * used cache, omega_gpu_decode_attn_cache_write, ...) calls omega_gpu_prefill_attn_kv_mirror_invalidate(c, layer
77 * or UINT32_MAX for all, from_pos) first; appending at positions >= valid (normal decode) needs nothing;
78 * - before omega_gpu_decode_attn_cache_free(c), call omega_gpu_prefill_attn_kv_mirror_forget(c) (mirrors are keyed by
79 * the layer's K plane VA; a later cache at the same VA would otherwise inherit stale rows). */
80#define OMEGA_GPU_PATTN_MAX_MIRRORS 128u
81typedef struct { uint64_t k_va, v_va; uint32_t kv_stride, max_pos, valid; } OmegaGpuPrefillAttnMirrorView;
82int omega_gpu_prefill_attn_kv_mirror_view(const OmegaGpuDecodeAttnCache *c, uint32_t layer, OmegaGpuPrefillAttnMirrorView *out);
83void omega_gpu_prefill_attn_kv_mirror_invalidate(const OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t from_pos);
84void omega_gpu_prefill_attn_kv_mirror_forget(const OmegaGpuDecodeAttnCache *c);
85void omega_gpu_prefill_attn_test_set_mirror_per_call(int on); /* TEST: forget mirrored rows before every call (cut 4 cost) */
86int omega_gpu_prefill_attn_kv_mirror_available(void);
87/* launches of the last call (return value) and the chip time of each (first `cap` of at most 256 logged) */
88uint32_t omega_gpu_prefill_attn_test_launch_ns(uint64_t *ns, uint32_t cap);
89#define OMEGA_GPU_PATTN_HOST_PASS 1024u /* host entry: tokens per staged pass in split mode */
90
91/* host: build + encode the kernel for a spec into code (cap bytes); returns the code size or 0 (tests) */
92size_t omega_gpu_prefill_attn_encode(const OmegaPrefillAttnSpec *spec, uint8_t *code, size_t cap, uint32_t *gprs);
93
94#endif