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 / evidence / FB1-CUT4B-0396844 / omega_gpu_session.h

125 lines · 6112 bytes Raw Blame History
1#ifndef OMEGA_GPU_SESSION_H
2#define OMEGA_GPU_SESSION_H
3/*
4 * omega_gpu_session: the ONE persistent device for the native GPU APIs
5 * (FB-1 cut 4b, 2026-10-04). Shared by omega_gpu_matmul_api,
6 * omega_gpu_elementwise_api and omega_gpu_attention_api.
7 *
8 * Before this cut the matmul API kept its own device open for the process while
9 * the elementwise and attention APIs opened a client, device, VA space and
10 * channel for EVERY call and closed them again (~40 ms per call against ~0.1 ms
11 * of chip time; TinyLlama decode makes ~3500 elementwise calls per 32 tokens,
12 * sovereign-core cut 3c: 0.207 tok/s). Now:
13 * - one RM client / device / channel per process, opened on the first launch of
14 * any API and kept until omega_gpu_session_close();
15 * - one pushbuffer, constant bank, marker page and QMD page, reused per launch;
16 * - staging buffers owned by the callers grow to a high-water mark and are reused
17 * (omega_gpu_session_scratch);
18 * - every buffer is GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE, read
19 * 2026-10-04: "For system memory this will not be coherent with direct CPU
20 * mappings"; the default for system memory is uncached), because the host
21 * writes inputs the chip then reads, and polls the markers the chip writes;
22 * - every launch keeps the chip-proven sequence: shader-cache invalidate
23 * (clcec0.h NVCEC0_INVALIDATE_SHADER_CACHES, cut 1b finding), constant bank
24 * upload, QMD0 / QMD1, first marker, L2_FLUSH_DIRTY, second marker on the
25 * uncached page (C3 tail, omega#225/#230), GPFIFO retire after completion.
26 * - an uncertain completion (marker wait timed out) latches the process: nothing
27 * is freed or closed (the chip may still be running) and every later launch
28 * of every API is refused, as the engine does (omega_gpu_engine.h).
29 *
30 * Threading: one process-wide mutex. A caller takes omega_gpu_session_lock(),
31 * does its whole call (kernel lookup, staging, launches, readback) and unlocks.
32 * Calls from several threads therefore serialise on the device; that is the
33 * documented model (one channel, one GPFIFO).
34 *
35 * Diagnostics (cut 4b flake investigation): every failure names its stage
36 * (omega_gpu_session_last_error) and an open failure carries the driver text
37 * (Nvrm.err: RM class, ioctl errno, RM status) so a first-call failure is never
38 * reported as a bare CHIP_FAIL again.
39 */
40#include <stdbool.h>
41#include <stddef.h>
42#include <stdint.h>
43#include "m16_native.h"
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49typedef struct {
50 M16NativeContext ctx;
51 NvrmMem pb, cbank, marker, qmd; /* per-launch scratch, owned by the session */
52 uint32_t opens; /* device opens in this process */
53 uint64_t last_open_ns; /* wall time of the last successful open */
54} OmegaGpuSession;
55
56/* A caller-owned staging buffer that grows to its high-water mark. */
57typedef struct {
58 NvrmMem mem;
59 size_t cap; /* usable bytes */
60 size_t high_water; /* largest request so far */
61} OmegaGpuScratch;
62
63/* One launch: a kernel already resident at code_va, the argument words for
64 * constant bank 0 at 0x380 (buffer addresses then parameters), the grid. */
65typedef struct {
66 uint64_t code_va;
67 uint32_t gpr_count; /* registers the QMD declares (multiple of 8, >= 64 in practice) */
68 uint32_t threads_x, threads_y;
69 uint32_t grid_x, grid_y;
70 size_t num_elements; /* QMD config num_elements (kept per API for identical words) */
71 const uint32_t *args; /* n_args words written at cbank + 0x380 */
72 uint32_t n_args; /* <= OMEGA_GPU_SESSION_MAX_ARGS */
73 uint64_t timeout_ms; /* marker wait; past it the process latches */
74} OmegaGpuLaunch;
75#define OMEGA_GPU_SESSION_MAX_ARGS 32u
76
77/* Take the session lock. Never fails; does not touch the device. */
78void omega_gpu_session_lock(void);
79void omega_gpu_session_unlock(void);
80
81/* Caller holds the lock. Opens the device on first use. NULL when the process is
82 * latched or the open failed (stage in omega_gpu_session_last_error). */
83OmegaGpuSession *omega_gpu_session_open(void);
84bool omega_gpu_session_is_open(void);
85
86/* 1 once an uncertain completion has latched this process. */
87int omega_gpu_session_is_blocked(void);
88
89/* Caller holds the lock, device open. GPU-uncached allocation, page rounded. */
90int omega_gpu_session_alloc(size_t bytes, NvrmMem *out);
91void omega_gpu_session_free(NvrmMem *m); /* no-op when latched or closed; zeroes *m */
92
93/* Caller holds the lock, device open. Makes s->mem at least `bytes` long
94 * (reallocated when it grows; contents are not kept). */
95int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes);
96void omega_gpu_session_scratch_free(OmegaGpuScratch *s);
97
98/* Caller holds the lock, device open, not latched. Submits one launch and waits
99 * for both markers. 0 on success; -1 with the stage named on failure. A marker
100 * timeout latches the process. elapsed_ns (submit to second marker) and the
101 * observed first marker word are added / stored when the pointers are given. */
102int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out);
103
104/* Stage of the most recent failure ("" if none). Shared by every API. */
105const char *omega_gpu_session_last_error(void);
106void omega_gpu_session_set_error(const char *stage);
107
108/* Caller holds the lock. Register a function the session calls (lock held, device
109 * still open) right before it closes, so an API can drop its resident kernels and
110 * scratch. Up to 8; the same function is registered once. (Taking the lock inside
111 * deadlocked the first call of every API on 2026-10-04: the APIs register from
112 * their open path, which already holds it.) */
113int omega_gpu_session_on_close(void (*hook)(void));
114
115/* Close the device and free every session buffer. Takes the lock itself.
116 * Refused (no-op) after an uncertain completion. The next open reopens. */
117void omega_gpu_session_close(void);
118
119/* Device opens so far in this process (diagnostics; a persistent session shows 1). */
120uint32_t omega_gpu_session_open_count(void);
121
122#ifdef __cplusplus
123}
124#endif
125#endif