interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_gpu_prefill_attn_api.h

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