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_native_runtime.h

196 lines · 10244 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#ifndef OMEGA_GPU_NATIVE_RUNTIME_H
3#define OMEGA_GPU_NATIVE_RUNTIME_H
4/* Private caller glue. Policy and ownership decisions are generated from .om.
5 * Device allocations remain in the existing FP8/GEMM/TC cache slots. Callers
6 * hold the existing session lock; no artifact/qualification authority lives here. */
7#include "cudapp_native.h"
8#include "omega_gpu_session.h"
9#include "omega_gpu_code_alloc.h"
10#include "omega_native_artifact_snapshot.h"
11#include <string.h>
12#include <stdint.h>
13
14/* Advances only after successful device teardown, including normal close. */
15uint64_t omega_gpu_session_epoch(void);
16typedef struct {
17 uint64_t pool[528];
18} OmegaGpuNativeCache;
19typedef struct {
20 OmegaGpuNativeCache *cache;
21 uint64_t slot, revision, epoch;
22} OmegaGpuNativeLease;
23
24static inline int64_t omega_native_cache_call(OmegaGpuNativeCache *c, uint64_t op,
25 const uint64_t req[16], const uint64_t tok[16]) {
26 uint64_t bits = 0;
27 uint32_t rc = omega_native_cache(&bits, c->pool, 528, req, 16, tok, 16, op);
28 bits = omega_bw_value(7, bits, rc, 0, 0, 0);
29 int64_t result;
30 memcpy(&result, &bits, sizeof result);
31 return result;
32}
33static inline void omega_native_context(uint64_t req[16]) {
34 memset(req, 0, sizeof(uint64_t) * 16);
35 req[0] = omega_gpu_session_epoch();
36 req[1] = 121;
37}
38/* A changed epoch means the old device handles are already dead. The physical
39 * cache owner must then discard stale host descriptors without dma_free. */
40static inline int omega_native_cache_refresh(OmegaGpuNativeCache *c, unsigned slots) {
41 uint64_t req[16], tok[16] = {0};
42 omega_native_context(req);
43 req[2] = UINT64_MAX; req[7] = slots;
44 return (int)omega_native_cache_call(c, 17, req, tok);
45}
46static inline int omega_native_shape_key(const void *shape, size_t bytes, uint8_t key[32]) {
47 uint8_t data[4096] = {0};
48 uint64_t result = 0;
49 if (!shape || !omega_bw_value(15, bytes, 0, 0, 0, 0)) return -1;
50 memcpy(data, shape, bytes);
51 uint32_t rc = omega_native_key(&result, data, sizeof data, bytes, key, 32);
52 return (int)(int64_t)omega_bw_value(8, rc, result, 0, 0, 0);
53}
54static inline int omega_native_cache_reserve(OmegaGpuNativeCache *c, const uint8_t key[32],
55 size_t charged, uint64_t req[16]) {
56 uint64_t tok[16] = {0};
57 omega_native_context(req);
58 req[2] = charged;
59 memcpy(req + 3, key, 32);
60 int64_t slot = omega_native_cache_call(c, 18, req, tok);
61 req[7] = c->pool[12]; /* accepted authoritative reservation owner */
62 return (int)(int64_t)omega_bw_value(9, (uint64_t)slot, 0, 0, 0, 0);
63}
64static inline int omega_native_cache_finish(OmegaGpuNativeCache *c, unsigned slot,
65 const uint64_t req[16], int commit) {
66 uint64_t tok[16] = {0};
67 tok[0] = slot; tok[2] = req[7];
68 return (int)omega_native_cache_call(c, omega_bw_value(10, (uint64_t)commit, 0, 0, 0, 0), req, tok);
69}
70static inline int omega_native_cache_victim(OmegaGpuNativeCache *c, unsigned slot, int erase) {
71 uint64_t req[16], tok[16] = {0};
72 omega_native_context(req); tok[0] = slot;
73 return (int)omega_native_cache_call(c, omega_bw_value(11, (uint64_t)erase, 0, 0, 0, 0), req, tok);
74}
75static inline OmegaGpuNativeLease omega_native_cache_lease(OmegaGpuNativeCache *c, unsigned slot) {
76 uint64_t revision = 0, epoch = omega_gpu_session_epoch();
77 uint32_t rc = omega_native_slotrev(&revision, c->pool, 528, slot, epoch);
78 OmegaGpuNativeLease lease = { c, slot, omega_bw_value(12, revision, rc, 0, 0, 0), epoch };
79 return lease;
80}
81static inline int64_t omega_native_live_call(const OmegaGpuNativeLease *lease, unsigned op,
82 const uint64_t req[16], int on) {
83 uint64_t tok[16] = {0}, bits = 0;
84 int64_t result;
85 if (!lease || !lease->cache) return -1;
86 tok[0] = lease->slot; tok[1] = lease->revision;
87 tok[3] = omega_bw_value(13, (uint64_t)on, 0, 0, 0, 0); tok[4] = lease->epoch;
88 uint32_t rc = omega_native_lease(&bits, lease->cache->pool, 528, req, 16, tok, 16, op);
89 bits = omega_bw_value(7, bits, rc, 0, 0, 0);
90 memcpy(&result, &bits, sizeof result);
91 return result;
92}
93static inline int omega_native_lease_op(const OmegaGpuNativeLease *lease, unsigned op, int on) {
94 uint64_t req[16];
95 omega_native_context(req);
96 return (int)omega_native_live_call(lease, op, req, on);
97}
98typedef struct {
99 uint8_t canonical[4096], code[16384], launch[4096];
100 uint64_t sizes[4];
101 uint8_t realization[32], manifest[32];
102 const OmegaNativeArtifactSnapshot *admission;
103} OmegaGpuNativeArtifact;
104/* The manifest ID remains the portable artifact authority. This object is an
105 * immutable payload snapshot, not an admission or qualification assertion. */
106static inline int omega_native_payload(const OmegaGpuNativeArtifact *a, uint8_t key[32]) {
107 uint64_t result = 0;
108 if (!a) return -1;
109 uint32_t rc = omega_native_validate(&result, a->canonical, 4096, a->sizes, 4,
110 a->code, 16384, a->launch, 4096, a->realization, 32, key, 32);
111 return (int)(int64_t)omega_bw_value(8, rc, result, 0, 0, 0);
112}
113/* Narrow custody projection into generated predicates. Expected manifest and
114 * launch remain selected immutable inputs; none is obtained from the snapshot.
115 * Fixed-size padding is required by the bounded native ABI, not a second parser. */
116static inline int omega_native_admitted(const OmegaGpuNativeArtifact *a, uint8_t key[32]) {
117 const OmegaNativeArtifactSnapshot *s = a ? a->admission : NULL;
118 uint8_t cubin[32768] = {0}, observed[2048] = {0}, text[16384] = {0};
119 uint64_t sizes[8] = {0}, payload_sizes[4], result = 0;
120 uint64_t name = 0;
121 if (!s || omega_native_payload(a, key) != 0) return -1;
122#if defined(OMEGA_GPU_NO_POSIX) || (defined(CUDAPP_MIGRATION_PENDING_BW_NAME) && CUDAPP_MIGRATION_PENDING_BW_NAME)
123 /* Freestanding checked-view exception pending omega-bridge#72. */
124 name = strnlen(s->kernel_function, sizeof s->kernel_function);
125 if (name >= sizeof s->kernel_function) return -1;
126#else
127 {
128 uint32_t rc = omega_bw_name(&result, s->kernel_function, sizeof s->kernel_function, &name, 1);
129 if (omega_bw_value(8, rc, result, 0, 0, 0) != 0) return -1;
130 }
131#endif
132 if (!omega_bw_value(14, s->sizes[OMEGA_SNAPSHOT_CUBIN], s->sizes[OMEGA_SNAPSHOT_TEXT],
133 s->sizes[OMEGA_SNAPSHOT_QMD0], s->sizes[OMEGA_SNAPSHOT_QMD1],
134 s->sizes[OMEGA_SNAPSHOT_CBANK])) return -1;
135 memcpy(cubin, s->bytes[OMEGA_SNAPSHOT_CUBIN], s->sizes[OMEGA_SNAPSHOT_CUBIN]);
136 memcpy(text, s->bytes[OMEGA_SNAPSHOT_TEXT], s->sizes[OMEGA_SNAPSHOT_TEXT]);
137 memcpy(observed, s->bytes[OMEGA_SNAPSHOT_QMD0], 384);
138 memcpy(observed + 384, s->bytes[OMEGA_SNAPSHOT_QMD1], 384);
139 memcpy(observed + 768, s->bytes[OMEGA_SNAPSHOT_CBANK], s->sizes[OMEGA_SNAPSHOT_CBANK]);
140 memcpy(observed + 896, s->kernel_function, name);
141 memcpy(observed + 960, a->manifest, 32);
142 memcpy(observed + 1024, s->id, 64);
143 sizes[0] = s->sizes[OMEGA_SNAPSHOT_CUBIN];
144 sizes[1] = s->sizes[OMEGA_SNAPSHOT_CBANK]; sizes[2] = name;
145 uint32_t rc = omega_native_templates(&result, a->launch, 4096, cubin, 32768,
146 observed, 2048, sizes, 8);
147 if (omega_bw_value(8, rc, result, 0, 0, 0) != 0) return -1;
148 memcpy(payload_sizes, a->sizes, sizeof payload_sizes);
149 payload_sizes[1] = s->sizes[OMEGA_SNAPSHOT_TEXT];
150 rc = omega_native_validate(&result, a->canonical, 4096, payload_sizes, 4,
151 text, 16384, a->launch, 4096, a->realization, 32, key, 32);
152 return (int)(int64_t)omega_bw_value(8, rc, result, 0, 0, 0);
153}
154static inline int omega_native_launch(const OmegaGpuNativeArtifact *a, const uint64_t call[12], uint64_t launch[32]) {
155 uint64_t result = 0;
156 uint32_t rc = omega_native_binding(&result, a->canonical, 4096, a->launch, 4096,
157 call, 12, launch, 32);
158 return (int)(int64_t)omega_bw_value(8, rc, result, 0, 0, 0);
159}
160static inline int omega_native_bound(const OmegaGpuNativeArtifact *a, const OmegaGpuNativeLease *lease) {
161 uint8_t key[32]; uint64_t req[16];
162 if (!lease || !lease->cache || omega_native_payload(a, key) != 0) return -1;
163 omega_native_context(req);
164 req[2] = omega_gpu_code_alloc_bytes((size_t)a->sizes[1]);
165 memcpy(req + 3, key, 32);
166 return (int)omega_native_live_call(lease, 15, req, 0);
167}
168struct OmegaGpuFp8Tensor;
169typedef struct {
170 uint32_t experts, n, k;
171 uint64_t allocation_epoch, owner_generation;
172 OmegaGpuMem weights, scales;
173} OmegaGpuNativeTensorObservation;
174/* The existing session lock must be held. Copies owner descriptors only;
175 * generation, membership and alias predicates remain generated native code. */
176int omega_gpu_fp8_native_observe(const struct OmegaGpuFp8Tensor *,
177 OmegaGpuNativeTensorObservation *);
178int omega_gpu_fp8_prepare_native(const OmegaGpuNativeArtifact *a, OmegaGpuNativeLease *lease);
179int omega_gpu_fp8gemm_prepare_native(const OmegaGpuNativeArtifact *a, OmegaGpuNativeLease *lease);
180struct OmegaGpuCmdList;
181/* Appends a launch and atomically retains its exact resident code lease. */
182int omega_gpu_cmdlist_add_native(struct OmegaGpuCmdList *cl, const OmegaGpuLaunch *launch,
183 const OmegaGpuNativeLease *lease);
184OmegaGpuNativeCache *omega_gpu_fp8_native_cache(void);
185OmegaGpuNativeCache *omega_gpu_fp8gemm_native_cache(void);
186/* Existing session lock held; only a current generated-validated retained lease
187 * can project the exact cache slot's typed code allocation. */
188int omega_gpu_fp8gemm_native_code_observe(const OmegaGpuNativeLease *, OmegaGpuMem *);
189int omega_gpu_fp8_record_native(struct OmegaGpuCmdList *, const struct OmegaGpuFp8Tensor *,
190 const OmegaGpuNativeArtifact *, const OmegaGpuNativeLease *,
191 uint32_t groups, uint64_t table, uint64_t xp, uint32_t xrows, uint64_t y);
192int omega_gpu_fp8gemm_record_native(struct OmegaGpuCmdList *, const struct OmegaGpuFp8Tensor *,
193 const OmegaGpuNativeArtifact *, const OmegaGpuNativeLease *,
194 uint32_t rows, uint32_t segs, uint64_t table, uint64_t xp, uint64_t y,
195 uint64_t xmap, uint64_t ymap);
196#endif