From e2b52f53bc62d5e7d503546fdf3c4dbb5520f335 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 31 Oct 2026 08:53:13 -0500 Subject: [PATCH 1/2] DRA-86: omega_gpu_gdn_state_view + omega_gpu_gdn_dev_copy (device addresses of GDN par/tail/state; one KK_COPY launch VA->VA) --- src/omega_gpu_gdn_api.c | 18 ++++++++++++++++++ src/omega_gpu_gdn_api.h | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/omega_gpu_gdn_api.c b/src/omega_gpu_gdn_api.c index c8fd2be4..aa4930d4 100644 --- a/src/omega_gpu_gdn_api.c +++ b/src/omega_gpu_gdn_api.c @@ -360,6 +360,24 @@ static int zero_ts_locked(OmegaGpuGdn *g) { int rc = move_locked(KK_ZERO, 0, g->tail.va, tb); return rc == OMEGA_GPU_GDN_OK ? move_locked(KK_ZERO, 0, g->state.va, sb) : rc; } + +int omega_gpu_gdn_state_view(const OmegaGpuGdn *g, OmegaGpuGdnStateView *v) { + if (!g || !v) return OMEGA_GPU_GDN_BAD_ARGS; + memset(v, 0, sizeof *v); + v->layers = g->layers; v->ratio = g->ratio; v->cached = g->cached; v->state_bf16 = g->bf16; + v->par_va = g->par.va; v->tail_va = g->tail.va; v->state_va = g->state.va; + v->par_bytes = g->sh.par_bytes; v->tail_bytes = g->sh.tail_bytes; v->state_bytes = g->sh.state_bytes; + return OMEGA_GPU_GDN_OK; +} + +int omega_gpu_gdn_dev_copy(uint64_t src_va, uint64_t dst_va, size_t bytes) { + if (!src_va || !dst_va) return OMEGA_GPU_GDN_BAD_ARGS; + omega_gpu_session_lock(); + int rc = dev_open_locked() ? move_locked(KK_COPY, src_va, dst_va, bytes) : OMEGA_GPU_GDN_CHIP_FAIL; + omega_gpu_session_unlock(); + return rc; +} + /* rec non-NULL (record path, caller holds the session lock): the inputs are the device rows dev_*, the launch is * appended to rec instead of submitted, nothing is staged, poisoned or read back. */ static int gdn_core(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2, diff --git a/src/omega_gpu_gdn_api.h b/src/omega_gpu_gdn_api.h index a2300ebc..086cebc1 100644 --- a/src/omega_gpu_gdn_api.h +++ b/src/omega_gpu_gdn_api.h @@ -71,6 +71,20 @@ int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, int omega_gpu_gdn_reset(OmegaGpuGdn *g); int omega_gpu_gdn_set_state(OmegaGpuGdn *g, uint32_t layer, const float *tail, const float *state); int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state); +/* DRA-86 (prefix cache) / DRA-84 (chunked prefill): the device addresses of the per-layer blocks, for VA -> VA work + * outside this file. Layer l: par_va + l * par_bytes (omega_gdn_pack_params_r), tail_va + l * tail_bytes + * (omega_gdn_pack_tail_r layout), state_va + l * state_bytes ([value heads][128 k][128 v], f32, or bf16 when + * state_bf16). cached: the tail and state are GPU-cached, so only chip launches may read or write them. */ +typedef struct { + uint32_t layers, ratio; + int cached, state_bf16; + uint64_t par_va, tail_va, state_va; + uint32_t par_bytes, tail_bytes, state_bytes; +} OmegaGpuGdnStateView; +int omega_gpu_gdn_state_view(const OmegaGpuGdn *g, OmegaGpuGdnStateView *v); +/* One chip copy dst[i] = src[i] (the state copy kernel above, synchronous); bytes > 0 and a multiple of + * OMEGA_GDN_COPY_WORDS_PER_CTA * 4 = 4096, else BAD_ARGS. Takes the session lock. */ +int omega_gpu_gdn_dev_copy(uint64_t src_va, uint64_t dst_va, size_t bytes); int omega_gpu_gdn_step(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnInfo *info); struct OmegaGpuCmdList; From fa8226b8465af3ece1b419b4638cb707b7e33892 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 31 Oct 2026 09:36:07 -0500 Subject: [PATCH 2/2] DRA-86: q36 prefix snapshot module (KV + DeltaNet state/tail) with pluggable copy/alloc, refusals, budget, LRU src/omega_gpu_q36_prefix.{c,h}: save/restore a conversation prefix of the hybrid Qwen3.6-35B (10 attention layers K/V + 30 DeltaNet state and conv tail) through caller copy/alloc callbacks (chip: omega_gpu_gdn_dev_copy). Restore checks before any write and refuses stale model/lib tag, other tokens, length, shape and an over-budget save; LRU eviction; resident bytes per conversation. Host test make test-gpu-q36-prefix: 56 checks PASS; AIEN_OMEGA_Q36_PF_MUTANT=10 (restore one block off) and --negative-control FAIL as required. gdn_api: omega_gpu_gdn_dev_copy refuses a NULL address, 0 bytes or a length off the 4096 grain before the device opens; gpu_gdn_test --host-only checks state_view/dev_copy refusals (74 checks). Linear DRA-86, refs #239. Chip: NOT_RUN. --- mk/q36-prefix.mk | 14 ++ src/omega_gpu_gdn_api.c | 4 +- src/omega_gpu_q36_prefix.c | 195 ++++++++++++++++++++++++++ src/omega_gpu_q36_prefix.h | 110 +++++++++++++++ tests/gpu_gdn_test.c | 10 ++ tests/gpu_q36_prefix_test.c | 269 ++++++++++++++++++++++++++++++++++++ 6 files changed, 601 insertions(+), 1 deletion(-) create mode 100644 mk/q36-prefix.mk create mode 100644 src/omega_gpu_q36_prefix.c create mode 100644 src/omega_gpu_q36_prefix.h create mode 100644 tests/gpu_q36_prefix_test.c diff --git a/mk/q36-prefix.mk b/mk/q36-prefix.mk new file mode 100644 index 00000000..c1ce4eb0 --- /dev/null +++ b/mk/q36-prefix.mk @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# DRA-86: device-resident prefix snapshots of the hybrid Qwen3.6-35B (src/omega_gpu_q36_prefix.{c,h}). Host only: the +# test's copy / alloc / free callbacks work on a host arena. The off-by-one restore mutant (AIEN_OMEGA_Q36_PF_MUTANT=10) +# and the negative control (refusal checks removed) must each make the binary FAIL. +.PHONY: test-gpu-q36-prefix +GPU_Q36_PREFIX_TEST = $(OUT_DIR)/gpu_q36_prefix_test +$(GPU_Q36_PREFIX_TEST): tests/gpu_q36_prefix_test.c src/omega_gpu_q36_prefix.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_q36_prefix_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-q36-prefix: $(GPU_Q36_PREFIX_TEST) + ./$(GPU_Q36_PREFIX_TEST) --host-only + ! AIEN_OMEGA_Q36_PF_MUTANT=10 ./$(GPU_Q36_PREFIX_TEST) > $(OUT_DIR)/gpu_q36_prefix_mutant10.txt + ! ./$(GPU_Q36_PREFIX_TEST) --negative-control > $(OUT_DIR)/gpu_q36_prefix_negative.txt + @echo "test-gpu-q36-prefix: mutant 10 and the negative control FAIL as required" +CARRIED_HOST_TESTS += test-gpu-q36-prefix diff --git a/src/omega_gpu_gdn_api.c b/src/omega_gpu_gdn_api.c index aa4930d4..0ea4827c 100644 --- a/src/omega_gpu_gdn_api.c +++ b/src/omega_gpu_gdn_api.c @@ -371,7 +371,9 @@ int omega_gpu_gdn_state_view(const OmegaGpuGdn *g, OmegaGpuGdnStateView *v) { } int omega_gpu_gdn_dev_copy(uint64_t src_va, uint64_t dst_va, size_t bytes) { - if (!src_va || !dst_va) return OMEGA_GPU_GDN_BAD_ARGS; + const size_t per = (size_t)OMEGA_GDN_COPY_WORDS_PER_CTA * 4u; + /* refused before the device opens: a bad call never touches the chip */ + if (!src_va || !dst_va || bytes == 0 || bytes % per || bytes / per > 0x7fffffffu) return OMEGA_GPU_GDN_BAD_ARGS; omega_gpu_session_lock(); int rc = dev_open_locked() ? move_locked(KK_COPY, src_va, dst_va, bytes) : OMEGA_GPU_GDN_CHIP_FAIL; omega_gpu_session_unlock(); diff --git a/src/omega_gpu_q36_prefix.c b/src/omega_gpu_q36_prefix.c new file mode 100644 index 00000000..b8ba7199 --- /dev/null +++ b/src/omega_gpu_q36_prefix.c @@ -0,0 +1,195 @@ +/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* Device-resident prefix snapshots of the hybrid Qwen3.6-35B (DRA-86); the layout and refusals are in the header. */ +#include "omega_gpu_q36_prefix.h" + +#include +#include +#include + +static int g_mutant; + +void omega_q36_prefix_test_set_mutant(int mutant) { g_mutant = mutant; } + +int omega_q36_prefix_budget_from_env(uint64_t *bytes) { + if (!bytes) return OMEGA_Q36_PREFIX_BAD_ARGS; + const char *v = getenv(OMEGA_Q36_PREFIX_POOL_ENV); + double gb = OMEGA_Q36_PREFIX_DEFAULT_GB; + if (v && *v) { + char *end = NULL; + gb = strtod(v, &end); + if (!end || *end || !isfinite(gb) || gb < 0.0 || gb > 1048576.0) return OMEGA_Q36_PREFIX_BAD_ARGS; + } + *bytes = (uint64_t)(gb * 1073741824.0); + return OMEGA_Q36_PREFIX_OK; +} + +void omega_q36_prefix_pool_init(OmegaQ36PrefixPool *p, uint64_t budget) { + if (!p) return; + memset(p, 0, sizeof *p); + p->budget = budget; +} + +int omega_q36_prefix_live_from_views(OmegaQ36PrefixLive *live, const OmegaGpuDecodeAttn256CacheView *attn, uint32_t attn_layers, + const OmegaGpuGdnStateView *gdn) { + if (!live || !attn || !gdn || !attn_layers || attn_layers > OMEGA_Q36_PREFIX_MAX_ATTN) return OMEGA_Q36_PREFIX_BAD_ARGS; + memset(live, 0, sizeof *live); + live->attn_layers = attn_layers; + live->kv_heads = attn[0].kv_heads; + live->max_pos = attn[0].max_pos; + for (uint32_t l = 0; l < attn_layers; l++) { + if (attn[l].kv_heads != live->kv_heads || attn[l].max_pos != live->max_pos) return OMEGA_Q36_PREFIX_SHAPE_MISMATCH; + if (!attn[l].k_va || !attn[l].v_va) return OMEGA_Q36_PREFIX_BAD_ARGS; + live->k_va[l] = attn[l].k_va; + live->v_va[l] = attn[l].v_va; + } + live->gdn_layers = gdn->layers; + live->tail_bytes = gdn->tail_bytes; + live->state_bytes = gdn->state_bytes; + live->tail_va = gdn->tail_va; + live->state_va = gdn->state_va; + return OMEGA_Q36_PREFIX_OK; +} + +static size_t gdn_bytes(const OmegaQ36PrefixLive *live, uint32_t per_layer) { return (size_t)live->gdn_layers * per_layer; } + +size_t omega_q36_prefix_blob_bytes(const OmegaQ36PrefixLive *live, uint32_t pos) { + if (!live || !pos || pos % OMEGA_Q36_PREFIX_POS_ALIGN || pos > live->max_pos || !live->kv_heads) return 0; + if (!live->attn_layers || live->attn_layers > OMEGA_Q36_PREFIX_MAX_ATTN) return 0; + size_t st = gdn_bytes(live, live->state_bytes), tl = gdn_bytes(live, live->tail_bytes); + if (st % OMEGA_Q36_PREFIX_COPY_ALIGN || tl % OMEGA_Q36_PREFIX_COPY_ALIGN) return 0; + return st + tl + (size_t)live->attn_layers * 2u * live->kv_heads * pos * OMEGA_Q36_PREFIX_ROW_BYTES; +} + +/* Every copy of the layout, live <-> blob; to_blob picks the direction. rows: positions copied per head. */ +static int copy_all(const OmegaQ36PrefixOps *ops, const OmegaQ36PrefixLive *live, uint64_t blob, uint32_t pos, uint32_t rows, + int to_blob) { +#define MOVE(live_va, blob_va, n) \ + do { \ + if ((n) && ops->copy(ops->ctx, to_blob ? (live_va) : (blob_va), to_blob ? (blob_va) : (live_va), (n))) \ + return OMEGA_Q36_PREFIX_COPY_FAILED; \ + } while (0) + size_t st = gdn_bytes(live, live->state_bytes), tl = gdn_bytes(live, live->tail_bytes); + MOVE(live->state_va, blob, st); + MOVE(live->tail_va, blob + st, tl); + uint64_t at = blob + st + tl; + size_t head = (size_t)pos * OMEGA_Q36_PREFIX_ROW_BYTES; + size_t moved = (size_t)rows * OMEGA_Q36_PREFIX_ROW_BYTES; + for (uint32_t l = 0; l < live->attn_layers; l++) + for (uint32_t plane = 0; plane < 2; plane++) { + uint64_t base = plane ? live->v_va[l] : live->k_va[l]; + for (uint32_t h = 0; h < live->kv_heads; h++, at += head) + MOVE(base + (uint64_t)h * live->max_pos * OMEGA_Q36_PREFIX_ROW_BYTES, at, moved); + } +#undef MOVE + return OMEGA_Q36_PREFIX_OK; +} + +static int same_shape(const OmegaQ36PrefixSnap *s, const OmegaQ36PrefixLive *live) { + return s->attn_layers == live->attn_layers && s->kv_heads == live->kv_heads && s->gdn_layers == live->gdn_layers && + s->tail_bytes == live->tail_bytes && s->state_bytes == live->state_bytes; +} + +int omega_q36_prefix_save(OmegaQ36PrefixPool *p, const OmegaQ36PrefixOps *ops, const OmegaQ36PrefixLive *live, + const uint8_t tag[OMEGA_Q36_PREFIX_TAG_BYTES], const uint8_t digest[OMEGA_Q36_PREFIX_TAG_BYTES], + uint32_t pos, OmegaQ36PrefixSnap *out) { + if (!p || !ops || !ops->alloc || !ops->free || !ops->copy || !live || !tag || !digest || !out || out->blob_va) + return OMEGA_Q36_PREFIX_BAD_ARGS; + size_t bytes = omega_q36_prefix_blob_bytes(live, pos); + if (!bytes) return OMEGA_Q36_PREFIX_BAD_ARGS; + if (bytes > p->budget || p->resident > p->budget - bytes) return OMEGA_Q36_PREFIX_OVER_BUDGET; + uint64_t va = 0; + if (ops->alloc(ops->ctx, bytes, &va) || !va) return OMEGA_Q36_PREFIX_NO_MEMORY; + int rc = copy_all(ops, live, va, pos, pos, 1); + if (rc) { + ops->free(ops->ctx, va, bytes); + return rc; + } + memcpy(out->model_tag, tag, OMEGA_Q36_PREFIX_TAG_BYTES); + memcpy(out->token_digest, digest, OMEGA_Q36_PREFIX_TAG_BYTES); + out->pos = pos; + out->attn_layers = live->attn_layers; + out->kv_heads = live->kv_heads; + out->gdn_layers = live->gdn_layers; + out->tail_bytes = live->tail_bytes; + out->state_bytes = live->state_bytes; + out->blob_va = va; + out->blob_bytes = bytes; + out->last_use = ++p->tick; + p->resident += bytes; + p->snaps++; + p->saved++; + return OMEGA_Q36_PREFIX_OK; +} + +static int check(const OmegaQ36PrefixSnap *s, const OmegaQ36PrefixLive *live, const uint8_t *tag, const uint8_t *digest, + uint32_t pos) { + if (memcmp(s->model_tag, tag, OMEGA_Q36_PREFIX_TAG_BYTES)) return OMEGA_Q36_PREFIX_STALE_MODEL; + if (memcmp(s->token_digest, digest, OMEGA_Q36_PREFIX_TAG_BYTES)) return OMEGA_Q36_PREFIX_TOKEN_MISMATCH; + if (!same_shape(s, live)) return OMEGA_Q36_PREFIX_SHAPE_MISMATCH; + if (pos != s->pos || pos > live->max_pos || s->blob_bytes != omega_q36_prefix_blob_bytes(live, pos)) + return OMEGA_Q36_PREFIX_LENGTH_MISMATCH; + return OMEGA_Q36_PREFIX_OK; +} + +int omega_q36_prefix_restore(OmegaQ36PrefixPool *p, const OmegaQ36PrefixOps *ops, OmegaQ36PrefixSnap *s, + const OmegaQ36PrefixLive *live, const uint8_t tag[OMEGA_Q36_PREFIX_TAG_BYTES], + const uint8_t digest[OMEGA_Q36_PREFIX_TAG_BYTES], uint32_t pos) { + if (!p || !ops || !ops->copy || !s || !s->blob_va || !live || !tag || !digest) return OMEGA_Q36_PREFIX_BAD_ARGS; + int rc = g_mutant == 2 ? OMEGA_Q36_PREFIX_OK : check(s, live, tag, digest, pos); + if (rc) { + p->refused++; + return rc; + } + if (g_mutant == 2) pos = s->pos < live->max_pos ? s->pos : live->max_pos; + uint32_t rows = g_mutant == 1 ? pos - OMEGA_Q36_PREFIX_POS_ALIGN : pos; + rc = copy_all(ops, live, s->blob_va, s->pos, rows, 0); + if (rc) return rc; + s->last_use = ++p->tick; + p->restored++; + return OMEGA_Q36_PREFIX_OK; +} + +void omega_q36_prefix_drop(OmegaQ36PrefixPool *p, const OmegaQ36PrefixOps *ops, OmegaQ36PrefixSnap *s) { + if (!p || !ops || !ops->free || !s || !s->blob_va) return; + ops->free(ops->ctx, s->blob_va, s->blob_bytes); + p->resident -= s->blob_bytes; + p->snaps--; + memset(s, 0, sizeof *s); +} + +int omega_q36_prefix_make_room(OmegaQ36PrefixPool *p, const OmegaQ36PrefixOps *ops, OmegaQ36PrefixSnap *snaps, uint32_t n, + size_t need) { + if (!p || !ops || !ops->free || (n && !snaps)) return OMEGA_Q36_PREFIX_BAD_ARGS; + if (need > p->budget) return OMEGA_Q36_PREFIX_OVER_BUDGET; + uint64_t listed = 0; + for (uint32_t i = 0; i < n; i++) + if (snaps[i].blob_va) listed += snaps[i].blob_bytes; + uint64_t room = p->budget - need; + if (p->resident > room && p->resident - room > listed) return OMEGA_Q36_PREFIX_OVER_BUDGET; + int dropped = 0; + while (p->resident > room) { + OmegaQ36PrefixSnap *old = NULL; + for (uint32_t i = 0; i < n; i++) + if (snaps[i].blob_va && (!old || snaps[i].last_use < old->last_use)) old = &snaps[i]; + if (!old) return OMEGA_Q36_PREFIX_OVER_BUDGET; /* unreachable: listed covers the gap */ + omega_q36_prefix_drop(p, ops, old); + p->evicted++; + dropped++; + } + return dropped; +} + +const char *omega_q36_prefix_rc_name(int rc) { + switch (rc) { + case OMEGA_Q36_PREFIX_OK: return "OK"; + case OMEGA_Q36_PREFIX_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_Q36_PREFIX_STALE_MODEL: return "STALE_MODEL"; + case OMEGA_Q36_PREFIX_TOKEN_MISMATCH: return "TOKEN_MISMATCH"; + case OMEGA_Q36_PREFIX_LENGTH_MISMATCH: return "LENGTH_MISMATCH"; + case OMEGA_Q36_PREFIX_SHAPE_MISMATCH: return "SHAPE_MISMATCH"; + case OMEGA_Q36_PREFIX_OVER_BUDGET: return "OVER_BUDGET"; + case OMEGA_Q36_PREFIX_NO_MEMORY: return "NO_MEMORY"; + case OMEGA_Q36_PREFIX_COPY_FAILED: return "COPY_FAILED"; + default: return rc > 0 ? "DROPPED" : "UNKNOWN"; + } +} diff --git a/src/omega_gpu_q36_prefix.h b/src/omega_gpu_q36_prefix.h new file mode 100644 index 00000000..48516896 --- /dev/null +++ b/src/omega_gpu_q36_prefix.h @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +#ifndef OMEGA_GPU_Q36_PREFIX_H +#define OMEGA_GPU_Q36_PREFIX_H +/* + * omega_gpu_q36_prefix: device-resident prefix snapshots of the hybrid Qwen3.6-35B (DRA-86, 2026-10-31). + * + * A snapshot is the whole per-conversation state after `pos` prompt tokens, copied VA -> VA into one blob: + * [gdn_layers][state_bytes] the DeltaNet recurrent states (omega_gpu_gdn_state_view state_va) + * [gdn_layers][tail_bytes] the conv tails (tail_va) + * [attn_layers][K, V][kv_heads][pos][512 B] the attention K/V rows 0 .. pos - 1 of every head + * The live K/V row (layer, head, p) is at k_va/v_va[layer] + (head * max_pos + p) * 512 + * (omega_gpu_decode_attn256_cache_view), so each (layer, plane, head) is one contiguous copy of pos * 512 bytes. + * + * The module owns no memory and opens no device: the caller passes OmegaQ36PrefixOps (alloc / free / copy). On the + * chip, copy is omega_gpu_gdn_dev_copy (one KK_COPY launch; every length a multiple of 4096 bytes, which is why pos + * must be a multiple of OMEGA_Q36_PREFIX_POS_ALIGN = 8 and every GDN block a multiple of 4096); the host test passes + * an arena. + * + * Restore refuses before any copy, so a refused restore leaves the live state untouched (the caller reads cold): + * STALE_MODEL the snapshot's model tag (caller-made: lib sha + weights + numeric env) is not the live one + * TOKEN_MISMATCH the token digest (caller-made over the pos prompt tokens) is not the prompt's + * LENGTH_MISMATCH pos is not the snapshot's (off by a turn), pos > max_pos, or the blob size is not the layout's + * SHAPE_MISMATCH layers, heads, tail or state bytes differ from the live state + * Save refuses OVER_BUDGET when resident + blob > budget; omega_q36_prefix_make_room evicts least-recently-used + * snapshots first. The budget defaults to 16 GiB (AIEN_Q36_PREFIX_POOL_GB, the sovereign door's knob). + * + * Nothing calls this module on a serving path yet: the sovereign door is switched by AIEN_Q36_PREFIX=1 (default off) + * and restores through host copies until omega.lock moves to an omega-mv commit carrying this file. + */ +#include +#include + +#include "omega_gpu_decode_attn256_api.h" +#include "omega_gpu_gdn_api.h" + +#define OMEGA_Q36_PREFIX_TAG_BYTES 32u +#define OMEGA_Q36_PREFIX_POS_ALIGN 8u +#define OMEGA_Q36_PREFIX_ROW_BYTES 512u /* one bf16 K or V row, head_dim 256 */ +#define OMEGA_Q36_PREFIX_COPY_ALIGN 4096u /* omega_gpu_gdn_dev_copy */ +#define OMEGA_Q36_PREFIX_MAX_ATTN 64u +#define OMEGA_Q36_PREFIX_DEFAULT_GB 16.0 +#define OMEGA_Q36_PREFIX_POOL_ENV "AIEN_Q36_PREFIX_POOL_GB" + +enum { + OMEGA_Q36_PREFIX_OK = 0, + OMEGA_Q36_PREFIX_BAD_ARGS = -1, + OMEGA_Q36_PREFIX_STALE_MODEL = -2, + OMEGA_Q36_PREFIX_TOKEN_MISMATCH = -3, + OMEGA_Q36_PREFIX_LENGTH_MISMATCH = -4, + OMEGA_Q36_PREFIX_SHAPE_MISMATCH = -5, + OMEGA_Q36_PREFIX_OVER_BUDGET = -6, + OMEGA_Q36_PREFIX_NO_MEMORY = -7, + OMEGA_Q36_PREFIX_COPY_FAILED = -8, +}; + +/* The live per-conversation state: device addresses only. */ +typedef struct { + uint32_t attn_layers, kv_heads, max_pos; + uint64_t k_va[OMEGA_Q36_PREFIX_MAX_ATTN], v_va[OMEGA_Q36_PREFIX_MAX_ATTN]; + uint32_t gdn_layers, tail_bytes, state_bytes; + uint64_t tail_va, state_va; +} OmegaQ36PrefixLive; + +typedef struct { + int (*alloc)(void *ctx, size_t bytes, uint64_t *va); /* 0 = ok */ + void (*free)(void *ctx, uint64_t va, size_t bytes); + int (*copy)(void *ctx, uint64_t src_va, uint64_t dst_va, size_t bytes); /* 0 = ok */ + void *ctx; +} OmegaQ36PrefixOps; + +typedef struct { + uint8_t model_tag[OMEGA_Q36_PREFIX_TAG_BYTES]; + uint8_t token_digest[OMEGA_Q36_PREFIX_TAG_BYTES]; + uint32_t pos; + uint32_t attn_layers, kv_heads, gdn_layers, tail_bytes, state_bytes; + uint64_t blob_va; /* 0 = empty slot */ + size_t blob_bytes; + uint64_t last_use; /* pool tick of the last save or restore */ +} OmegaQ36PrefixSnap; + +typedef struct { + uint64_t budget, resident; /* bytes */ + uint32_t snaps; + uint64_t tick, saved, restored, refused, evicted; +} OmegaQ36PrefixPool; + +/* AIEN_Q36_PREFIX_POOL_GB (unset or empty: 16) as bytes; a negative, non-finite or unparsable value: BAD_ARGS. */ +int omega_q36_prefix_budget_from_env(uint64_t *bytes); +void omega_q36_prefix_pool_init(OmegaQ36PrefixPool *p, uint64_t budget); +/* The live state from the attention cache views (one per attention layer) and the GDN state view. */ +int omega_q36_prefix_live_from_views(OmegaQ36PrefixLive *live, const OmegaGpuDecodeAttn256CacheView *attn, uint32_t attn_layers, + const OmegaGpuGdnStateView *gdn); +/* Blob bytes for `pos` tokens of this live shape; 0 when pos or a block breaks the copy alignment. */ +size_t omega_q36_prefix_blob_bytes(const OmegaQ36PrefixLive *live, uint32_t pos); +int omega_q36_prefix_save(OmegaQ36PrefixPool *p, const OmegaQ36PrefixOps *ops, const OmegaQ36PrefixLive *live, + const uint8_t tag[OMEGA_Q36_PREFIX_TAG_BYTES], const uint8_t digest[OMEGA_Q36_PREFIX_TAG_BYTES], + uint32_t pos, OmegaQ36PrefixSnap *out); +int omega_q36_prefix_restore(OmegaQ36PrefixPool *p, const OmegaQ36PrefixOps *ops, OmegaQ36PrefixSnap *s, + const OmegaQ36PrefixLive *live, const uint8_t tag[OMEGA_Q36_PREFIX_TAG_BYTES], + const uint8_t digest[OMEGA_Q36_PREFIX_TAG_BYTES], uint32_t pos); +void omega_q36_prefix_drop(OmegaQ36PrefixPool *p, const OmegaQ36PrefixOps *ops, OmegaQ36PrefixSnap *s); +/* Drop least-recently-used snapshots of snaps[0..n) until need more bytes fit; the count dropped, or OVER_BUDGET + * (nothing dropped) when need alone exceeds the budget or the listed snapshots cannot free enough. */ +int omega_q36_prefix_make_room(OmegaQ36PrefixPool *p, const OmegaQ36PrefixOps *ops, OmegaQ36PrefixSnap *snaps, uint32_t n, + size_t need); +const char *omega_q36_prefix_rc_name(int rc); +/* TEST ONLY: 1 = restore one 8-position block short at the boundary, 2 = restore skips the refusal checks. */ +void omega_q36_prefix_test_set_mutant(int mutant); + +#endif diff --git a/tests/gpu_gdn_test.c b/tests/gpu_gdn_test.c index e0dc71fc..b58dbf4c 100644 --- a/tests/gpu_gdn_test.c +++ b/tests/gpu_gdn_test.c @@ -156,6 +156,16 @@ static void host_only(void) { dev[(1u * OMEGA_GDN_SLOTS + 7u) * 4u + 1u] += 1.0f; /* head 1 (a second copy of key head 0), q slot 7 */ CHECK(omega_gdn_unpack_tail_r(GDN_TEST_RATIO, dev, t2) == -1, "unpack refuses disagreeing copies"); free(t); free(t2); free(dev); + /* DRA-86 state view and device copy: every bad call is refused before the device opens */ + { + OmegaGpuGdnStateView v; + CHECK(omega_gpu_gdn_state_view(NULL, &v) == OMEGA_GPU_GDN_BAD_ARGS, "state view refuses a NULL handle"); + CHECK(omega_gpu_gdn_state_view((const OmegaGpuGdn *)&v, NULL) == OMEGA_GPU_GDN_BAD_ARGS, "state view refuses a NULL view"); + CHECK(omega_gpu_gdn_dev_copy(0, 4096, 4096) == OMEGA_GPU_GDN_BAD_ARGS, "dev copy refuses a NULL source"); + CHECK(omega_gpu_gdn_dev_copy(4096, 0, 4096) == OMEGA_GPU_GDN_BAD_ARGS, "dev copy refuses a NULL destination"); + CHECK(omega_gpu_gdn_dev_copy(4096, 8192, 0) == OMEGA_GPU_GDN_BAD_ARGS, "dev copy refuses 0 bytes"); + CHECK(omega_gpu_gdn_dev_copy(4096, 8192, 4096 + 4) == OMEGA_GPU_GDN_BAD_ARGS, "dev copy refuses a length off the 4096 grain"); + } /* parameter block */ Layer L; layer_make(&L, 9); uint8_t *blk = malloc(T_PAR_BYTES); diff --git a/tests/gpu_q36_prefix_test.c b/tests/gpu_q36_prefix_test.c new file mode 100644 index 00000000..4e4b72b0 --- /dev/null +++ b/tests/gpu_q36_prefix_test.c @@ -0,0 +1,269 @@ +/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* + * gpu_q36_prefix_test: host test of src/omega_gpu_q36_prefix.{c,h} (DRA-86). No device: the copy / alloc / free + * callbacks work on a host arena standing in for device memory, with the chip copy's rule (every length a multiple + * of 4096 bytes) enforced. + * identical bytes after a save / scribble / restore round trip; rows past pos and other conversations untouched + * refusals: stale model tag, token digest, pos off by one turn (8 positions either way), blob length, KV and GDN + * shape; a refused restore copies nothing + * budget: OVER_BUDGET at save, least-recently-used eviction by make_room, AIEN_Q36_PREFIX_POOL_GB parsing + * the 35B layout: 739,901,440 bytes for 32k positions (Wolfram, in the DRA-86 PR) + * AIEN_OMEGA_Q36_PF_MUTANT=10 (the hive's DRA-86 number: restore one 8-position block short at the boundary) must make + * it FAIL. --negative-control runs the same checks with the refusal checks removed (the unmodified base had no + * restore checks at all), which must FAIL too. + * Verdict line last: "gpu_q36_prefix_test: N checks, F failed: PASS|FAIL". + */ +#include "omega_gpu_q36_prefix.h" + +#include +#include +#include + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +/* ---- the fake device: one arena, VA = BASE + offset ---- */ +#define BASE 0x100000ull +#define ARENA (8u << 20) +static uint8_t *g_mem; +static size_t g_top; /* bump pointer for blobs */ +static int g_allocs; /* live blob allocations */ +static int g_copies, g_unaligned, g_fail_copy_at = -1; + +static uint8_t *at(uint64_t va, size_t n) { + if (va < BASE || va - BASE > ARENA || n > ARENA - (va - BASE)) return NULL; + return g_mem + (va - BASE); +} +static int f_alloc(void *ctx, size_t bytes, uint64_t *va) { + (void)ctx; + size_t n = (bytes + 4095u) & ~(size_t)4095u; + if (n > ARENA - g_top) return -1; + *va = BASE + g_top; + g_top += n; + g_allocs++; + return 0; +} +static void f_free(void *ctx, uint64_t va, size_t bytes) { (void)ctx; (void)va; (void)bytes; g_allocs--; } +static int f_copy(void *ctx, uint64_t src, uint64_t dst, size_t bytes) { + (void)ctx; + if (g_fail_copy_at >= 0 && g_copies == g_fail_copy_at) return -1; + g_copies++; + if (bytes % OMEGA_Q36_PREFIX_COPY_ALIGN) g_unaligned++; + uint8_t *s = at(src, bytes), *d = at(dst, bytes); + if (!s || !d) return -1; + memmove(d, s, bytes); + return 0; +} +static const OmegaQ36PrefixOps OPS = {f_alloc, f_free, f_copy, NULL}; + +/* ---- the live state: 2 attention layers x 2 KV heads x 64 positions, 3 GDN layers of 4096 + 4096 bytes ---- */ +#define ATTN 2u +#define HEADS 2u +#define MAXPOS 64u +#define GDN 3u +#define SB 4096u +#define TB 4096u +#define PLANE ((size_t)HEADS * MAXPOS * OMEGA_Q36_PREFIX_ROW_BYTES) +#define LIVE_BYTES ((size_t)GDN * (SB + TB) + (size_t)ATTN * 2u * PLANE) + +static OmegaQ36PrefixLive live_at(uint64_t off) { + OmegaQ36PrefixLive l; + memset(&l, 0, sizeof l); + l.attn_layers = ATTN; l.kv_heads = HEADS; l.max_pos = MAXPOS; + l.gdn_layers = GDN; l.state_bytes = SB; l.tail_bytes = TB; + l.state_va = BASE + off; + l.tail_va = l.state_va + (size_t)GDN * SB; + for (uint32_t i = 0; i < ATTN; i++) { + l.k_va[i] = l.tail_va + (size_t)GDN * TB + (size_t)i * 2u * PLANE; + l.v_va[i] = l.k_va[i] + PLANE; + } + return l; +} +static void fill(uint64_t off, uint32_t seed) { + uint32_t x = seed * 2654435761u + 1u; + for (size_t i = 0; i < LIVE_BYTES; i++) { x ^= x << 13; x ^= x >> 17; x ^= x << 5; g_mem[off + i] = (uint8_t)x; } +} +/* bytes of the live block at off that differ from ref, split into rows < pos and the rest */ +static void diff(uint64_t off, const uint8_t *ref, uint32_t pos, size_t *gdn, size_t *below, size_t *above) { + *gdn = *below = *above = 0; + const uint8_t *m = g_mem + off; + size_t g = (size_t)GDN * (SB + TB); + for (size_t i = 0; i < g; i++) *gdn += m[i] != ref[i]; + for (size_t i = g; i < LIVE_BYTES; i++) { + size_t row = ((i - g) % PLANE) / OMEGA_Q36_PREFIX_ROW_BYTES % MAXPOS; + if (m[i] != ref[i]) { if (row < pos) (*below)++; else (*above)++; } + } +} + +static void digest_of(uint8_t d[32], uint32_t salt, uint32_t pos) { + for (int i = 0; i < 32; i++) d[i] = (uint8_t)(salt * 31u + pos * 7u + (uint32_t)i); +} + +int main(int argc, char **argv) { + /* --host-only: the only tier */ + const char *mv = getenv("AIEN_OMEGA_Q36_PF_MUTANT"); + int mutant = mv && atoi(mv) == 10 ? 1 : 0; + for (int i = 1; i < argc; i++) + if (!strcmp(argv[i], "--negative-control")) mutant = 2; + omega_q36_prefix_test_set_mutant(mutant); + if (mutant) printf("gpu_q36_prefix_test: %s\n", mutant == 1 ? "mutant 10 (restore off by one block)" : "negative control (no refusal checks)"); + g_mem = calloc(1, ARENA); + if (!g_mem) return 2; + const uint64_t L1 = 0, L2 = LIVE_BYTES; /* two conversations' live states */ + g_top = 2u * LIVE_BYTES; + uint8_t *h1 = malloc(LIVE_BYTES), *h2 = malloc(LIVE_BYTES), *hc = malloc(LIVE_BYTES); + if (!h1 || !h2 || !hc) return 2; + OmegaQ36PrefixLive live = live_at(L1); + uint8_t tag[32], tag2[32], dg[32], dg_other[32]; + memset(tag, 0xa5, 32); memset(tag2, 0xa5, 32); tag2[31] ^= 1; + digest_of(dg, 1, 48); digest_of(dg_other, 2, 48); + size_t g, lo, hi; + + /* 1. layout */ + size_t blob = omega_q36_prefix_blob_bytes(&live, 48); + CHECK(blob == (size_t)GDN * (SB + TB) + (size_t)ATTN * 2u * HEADS * 48u * 512u, "blob bytes %zu", blob); + CHECK(omega_q36_prefix_blob_bytes(&live, 44) == 0, "pos 44 is not 8-aligned"); + CHECK(omega_q36_prefix_blob_bytes(&live, MAXPOS + 8) == 0, "pos past max_pos"); + OmegaQ36PrefixLive big; + memset(&big, 0, sizeof big); + big.attn_layers = 10; big.kv_heads = 2; big.max_pos = 33792; big.gdn_layers = 30; + big.state_bytes = 32u * 128u * 128u * 4u; big.tail_bytes = 32u * 6144u; + CHECK(omega_q36_prefix_blob_bytes(&big, 32768) == 739901440u, "35B 32k blob %zu", omega_q36_prefix_blob_bytes(&big, 32768)); + CHECK(omega_q36_prefix_blob_bytes(&big, 32767) == 0, "35B pos 32767 refused"); + big.state_bytes = 32u * 128u * 128u * 2u; /* bf16 state */ + CHECK(omega_q36_prefix_blob_bytes(&big, 32768) == 739901440u - 31457280u, "35B bf16 32k blob"); + + /* 2. live from the real views */ + OmegaGpuDecodeAttn256CacheView v[ATTN]; + memset(v, 0, sizeof v); + for (uint32_t i = 0; i < ATTN; i++) { v[i].kv_heads = HEADS; v[i].max_pos = MAXPOS; v[i].k_va = live.k_va[i]; v[i].v_va = live.v_va[i]; } + OmegaGpuGdnStateView gv; + memset(&gv, 0, sizeof gv); + gv.layers = GDN; gv.state_bytes = SB; gv.tail_bytes = TB; gv.state_va = live.state_va; gv.tail_va = live.tail_va; + OmegaQ36PrefixLive lv; + CHECK(omega_q36_prefix_live_from_views(&lv, v, ATTN, &gv) == 0 && !memcmp(&lv, &live, sizeof lv), "live from views"); + v[1].kv_heads = 4; + CHECK(omega_q36_prefix_live_from_views(&lv, v, ATTN, &gv) == OMEGA_Q36_PREFIX_SHAPE_MISMATCH, "mixed head counts refused"); + + /* 3. round trip */ + OmegaQ36PrefixPool pool; + omega_q36_prefix_pool_init(&pool, 1u << 20); + fill(L1, 1); fill(L2, 9); + memcpy(h1, g_mem + L1, LIVE_BYTES); + memcpy(hc, g_mem + L2, LIVE_BYTES); + OmegaQ36PrefixSnap s; + memset(&s, 0, sizeof s); + int rc = omega_q36_prefix_save(&pool, &OPS, &live, tag, dg, 48, &s); + CHECK(rc == 0 && s.blob_bytes == blob && pool.resident == blob && pool.snaps == 1, "save: %s", omega_q36_prefix_rc_name(rc)); + CHECK(!memcmp(g_mem + L1, h1, LIVE_BYTES), "save leaves the live state alone"); + fill(L1, 2); + memcpy(h2, g_mem + L1, LIVE_BYTES); + diff(L1, h1, 48, &g, &lo, &hi); + CHECK(g > 0 && lo > 0, "control: the scribbled state differs from the saved one (%zu, %zu)", g, lo); + rc = omega_q36_prefix_restore(&pool, &OPS, &s, &live, tag, dg, 48); + CHECK(rc == 0, "restore: %s", omega_q36_prefix_rc_name(rc)); + diff(L1, h1, 48, &g, &lo, &hi); + CHECK(g == 0, "GDN state and tail identical after restore (%zu bytes differ)", g); + CHECK(lo == 0, "K/V rows 0..47 identical after restore (%zu bytes differ)", lo); + diff(L1, h2, 48, &g, &lo, &hi); + CHECK(hi == 0, "K/V rows 48..63 untouched by the restore (%zu bytes differ)", hi); + CHECK(!memcmp(g_mem + L2, hc, LIVE_BYTES), "the other conversation is untouched"); + CHECK(g_unaligned == 0, "every copy a multiple of 4096 bytes (%d not)", g_unaligned); + CHECK(g_copies == 2 * (2 + (int)(ATTN * 2u * HEADS)), "copies %d", g_copies); + /* restore into the other conversation's live state: same bytes there */ + OmegaQ36PrefixLive live2 = live_at(L2); + CHECK(omega_q36_prefix_restore(&pool, &OPS, &s, &live2, tag, dg, 48) == 0, "restore into conversation 2"); + diff(L2, h1, 48, &g, &lo, &hi); + CHECK(g == 0 && lo == 0, "conversation 2 holds the saved prefix"); + diff(L2, hc, 48, &g, &lo, &hi); + CHECK(hi == 0, "conversation 2 rows past pos keep their own bytes"); + + /* 4. refusals: each copies nothing and leaves the live state as it was */ + struct { const char *what; const uint8_t *tag, *dg; uint32_t pos; int want; } R[] = { + {"stale model tag", tag2, dg, 48, OMEGA_Q36_PREFIX_STALE_MODEL}, + {"other tokens", tag, dg_other, 48, OMEGA_Q36_PREFIX_TOKEN_MISMATCH}, + {"one turn short (pos 40)", tag, dg, 40, OMEGA_Q36_PREFIX_LENGTH_MISMATCH}, + {"one turn long (pos 56)", tag, dg, 56, OMEGA_Q36_PREFIX_LENGTH_MISMATCH}, + }; + uint64_t refused0 = pool.refused; + for (size_t i = 0; i < sizeof R / sizeof R[0]; i++) { + fill(L1, 3u + (uint32_t)i); + memcpy(h2, g_mem + L1, LIVE_BYTES); + int c0 = g_copies; + rc = omega_q36_prefix_restore(&pool, &OPS, &s, &live, R[i].tag, R[i].dg, R[i].pos); + CHECK(rc == R[i].want, "%s: %s, want %s", R[i].what, omega_q36_prefix_rc_name(rc), omega_q36_prefix_rc_name(R[i].want)); + CHECK(g_copies == c0 && !memcmp(g_mem + L1, h2, LIVE_BYTES), "%s: the live state was touched", R[i].what); + } + OmegaQ36PrefixLive bad = live; + bad.kv_heads = 1; + rc = omega_q36_prefix_restore(&pool, &OPS, &s, &bad, tag, dg, 48); + CHECK(rc == OMEGA_Q36_PREFIX_SHAPE_MISMATCH, "KV head count: %s", omega_q36_prefix_rc_name(rc)); + bad = live; + bad.state_bytes = 2u * SB; + rc = omega_q36_prefix_restore(&pool, &OPS, &s, &bad, tag, dg, 48); + CHECK(rc == OMEGA_Q36_PREFIX_SHAPE_MISMATCH, "GDN state bytes: %s", omega_q36_prefix_rc_name(rc)); + bad = live; + bad.max_pos = 40; + rc = omega_q36_prefix_restore(&pool, &OPS, &s, &bad, tag, dg, 48); + CHECK(rc == OMEGA_Q36_PREFIX_LENGTH_MISMATCH, "pos past the live max_pos: %s", omega_q36_prefix_rc_name(rc)); + OmegaQ36PrefixSnap torn = s; + torn.blob_bytes -= 4096; + rc = omega_q36_prefix_restore(&pool, &OPS, &torn, &live, tag, dg, 48); + CHECK(rc == OMEGA_Q36_PREFIX_LENGTH_MISMATCH, "blob length: %s", omega_q36_prefix_rc_name(rc)); + CHECK(pool.refused == refused0 + 8, "refused counter %llu", (unsigned long long)(pool.refused - refused0)); + + /* 5. save refusals and a failed copy */ + OmegaQ36PrefixSnap t; + memset(&t, 0, sizeof t); + CHECK(omega_q36_prefix_save(&pool, &OPS, &live, tag, dg, 44, &t) == OMEGA_Q36_PREFIX_BAD_ARGS, "save at pos 44"); + CHECK(omega_q36_prefix_save(&pool, &OPS, &live, tag, dg, 72, &t) == OMEGA_Q36_PREFIX_BAD_ARGS, "save past max_pos"); + CHECK(omega_q36_prefix_save(&pool, &OPS, &live, tag, dg, 48, &s) == OMEGA_Q36_PREFIX_BAD_ARGS, "save over a held snapshot"); + int a0 = g_allocs; + g_fail_copy_at = g_copies + 3; + rc = omega_q36_prefix_save(&pool, &OPS, &live, tag, dg, 48, &t); + g_fail_copy_at = -1; + CHECK(rc == OMEGA_Q36_PREFIX_COPY_FAILED && g_allocs == a0 && pool.resident == blob && !t.blob_va, "failed copy: %s", omega_q36_prefix_rc_name(rc)); + + /* 6. budget and least-recently-used eviction */ + OmegaQ36PrefixSnap S[3]; + memset(S, 0, sizeof S); + omega_q36_prefix_drop(&pool, &OPS, &s); + CHECK(pool.resident == 0 && pool.snaps == 0 && g_allocs == 0, "drop frees the blob"); + omega_q36_prefix_pool_init(&pool, 2u * blob + 4095u); + uint8_t d0[32], d1[32], d2[32]; + digest_of(d0, 10, 48); digest_of(d1, 11, 48); digest_of(d2, 12, 48); + CHECK(omega_q36_prefix_save(&pool, &OPS, &live, tag, d0, 48, &S[0]) == 0, "save A"); + CHECK(omega_q36_prefix_save(&pool, &OPS, &live, tag, d1, 48, &S[1]) == 0, "save B"); + a0 = g_allocs; + rc = omega_q36_prefix_save(&pool, &OPS, &live, tag, d2, 48, &S[2]); + CHECK(rc == OMEGA_Q36_PREFIX_OVER_BUDGET && g_allocs == a0 && pool.resident == 2u * blob, "save C over budget: %s", omega_q36_prefix_rc_name(rc)); + CHECK(omega_q36_prefix_restore(&pool, &OPS, &S[0], &live, tag, d0, 48) == 0, "A used again"); + rc = omega_q36_prefix_make_room(&pool, &OPS, S, 3, blob); + CHECK(rc == 1 && S[0].blob_va && !S[1].blob_va, "make_room drops B (least recently used): %d", rc); + CHECK(omega_q36_prefix_save(&pool, &OPS, &live, tag, d2, 48, &S[2]) == 0, "save C after eviction"); + CHECK(pool.resident == 2u * blob && pool.resident <= pool.budget && pool.evicted == 1 && pool.snaps == 2, "resident %llu", + (unsigned long long)pool.resident); + CHECK(omega_q36_prefix_make_room(&pool, &OPS, S, 3, 3u * blob) == OMEGA_Q36_PREFIX_OVER_BUDGET && pool.snaps == 2, + "a need over the whole budget drops nothing"); + CHECK(omega_q36_prefix_make_room(&pool, &OPS, S, 1, 2u * blob) == OMEGA_Q36_PREFIX_OVER_BUDGET && pool.snaps == 2, + "listed snapshots too small to free enough drop nothing"); + + /* 7. the budget knob */ + uint64_t b = 0; + unsetenv(OMEGA_Q36_PREFIX_POOL_ENV); + CHECK(omega_q36_prefix_budget_from_env(&b) == 0 && b == 17179869184ull, "default 16 GiB: %llu", (unsigned long long)b); + setenv(OMEGA_Q36_PREFIX_POOL_ENV, "0.5", 1); + CHECK(omega_q36_prefix_budget_from_env(&b) == 0 && b == 536870912ull, "0.5 GiB"); + setenv(OMEGA_Q36_PREFIX_POOL_ENV, "0", 1); + CHECK(omega_q36_prefix_budget_from_env(&b) == 0 && b == 0, "0: nothing resident"); + const char *badv[] = {"-1", "x", "16G", "nan", "inf"}; + for (size_t i = 0; i < sizeof badv / sizeof badv[0]; i++) { + setenv(OMEGA_Q36_PREFIX_POOL_ENV, badv[i], 1); + CHECK(omega_q36_prefix_budget_from_env(&b) == OMEGA_Q36_PREFIX_BAD_ARGS, "%s refused", badv[i]); + } + unsetenv(OMEGA_Q36_PREFIX_POOL_ENV); + + printf("gpu_q36_prefix_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); + free(h1); free(h2); free(hc); free(g_mem); + return g_failed ? 1 : 0; +}