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 / tests / gpu_dattn256_multi_test.c

119 lines · 7299 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * gpu_dattn256_multi_test (chip): the multi-token decode attention (L369, omega_gpu_decode_attn256_multi_record) must be
4 * T decode steps (omega_gpu_decode_attn256 at positions pos0 .. pos0 + T - 1) bit for bit: every output row and every
5 * K/V row it appends. Qwen3.6-35B-A3B shape (16 q heads, 2 KV heads, 4 q heads per CTA row, rotary 64), two caches with
6 * the same random history, qkv rows 36864 bytes apart as the 35B graph lays them out (the attention row is the first
7 * 20480 bytes of the q|k|v mix row). Cases cross split boundaries, start mid-context and reach 8k (several attend groups).
8 * Controls that must differ: the prep launch dropped (test mutant 1) and a wrong row stride.
9 * Verdict line last: "gpu_dattn256_multi_test: N checks, F failed: PASS|FAIL".
10 */
11#include "omega_gpu_decode_attn256_api.h"
12#include "omega_gpu_session.h"
13#include "omega_gpu_cmdlist.h"
14#include <math.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#define QH 16u
20#define KVH 2u
21#define QPC 4u
22#define ROT 64u
23#define QKVW ((QH + 2u * KVH) * 256u) /* 5120 */
24#define OUTW (QH * 256u) /* 4096 */
25#define XROW 36864u /* the graph's ATT_MIX row: 9216 f32 */
26#define OROW (OUTW * 4u)
27#define MAXP 8448u
28static const float EPS = 1e-6f, OSCALE = 18446744073709551616.0f; /* 2^64, as the graph */
29
30static int g_checks, g_failed;
31#define CHECK(c, ...) do { g_checks++; if (!(c)) { g_failed++; printf(" FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
32
33static uint32_t rs = 12345u;
34static float frand(void) { rs = rs * 1664525u + 1013904223u; return ((float)(rs >> 8) / 16777216.0f) * 2.0f - 1.0f; }
35static uint16_t bf(float x) { uint32_t u; memcpy(&u, &x, 4); return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); }
36
37
38static int make_cache(OmegaGpuDecodeAttn256Cache **c, const float *qn, const float *kn, const uint16_t *hk, const uint16_t *hv, uint32_t hist) {
39 if (omega_gpu_decode_attn256_cache_alloc_shape(1, MAXP, 1e7, ROT, QH, KVH, QPC, c) != 0) return -1;
40 if (omega_gpu_decode_attn256_set_norms(*c, 0, qn, kn) != 0) return -1;
41 for (uint32_t h = 0; h < KVH && hist; h++)
42 if (omega_gpu_decode_attn256_cache_write(*c, 0, h, 0, hist, hk + (size_t)h * MAXP * 256, hv + (size_t)h * MAXP * 256) != 0) return -1;
43 return 0;
44}
45
46/* one multi record of T tokens from pos0; rows: T rows of XROW bytes */
47static int run_multi(OmegaGpuDecodeAttn256Cache *c, uint32_t pos0, uint32_t T, const uint8_t *rows, uint32_t xstride, float *out) {
48 if (omega_gpu_decode_attn256_multi_prepare(c) != 0) return -1;
49 OmegaGpuCmdList *cl = NULL;
50 if (omega_gpu_cmdlist_create(16, &cl) != 0) return -1;
51 omega_gpu_session_lock();
52 OmegaGpuMem m; memset(&m, 0, sizeof m);
53 const size_t s_rows = (size_t)T * XROW, s_out = (size_t)T * OROW;
54 int ok = omega_gpu_session_open() != NULL && omega_gpu_session_alloc(s_rows + s_out, &m) == 0;
55 if (ok) {
56 memcpy(m.cpu, rows, s_rows);
57 float *o = (float *)((uint8_t *)m.cpu + s_rows);
58 for (size_t i = 0; i < (size_t)T * OUTW; i++) o[i] = NAN;
59 ok = omega_gpu_session_sync_to_gpu(&m) == 0;
60 }
61 if (ok) ok = omega_gpu_decode_attn256_multi_record(cl, c, 0, pos0, T, m.va, xstride, EPS, OSCALE, m.va + s_rows, OROW) == 0;
62 OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts);
63 OmegaGpuCmdListStats st; memset(&st, 0, sizeof st);
64 if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &st) == 0 && st.sems_done == st.launches;
65 if (ok) ok = omega_gpu_session_sync_to_cpu(&m) == 0;
66 if (ok) memcpy(out, (uint8_t *)m.cpu + s_rows, s_out);
67 if (m.cpu) omega_gpu_session_free(&m);
68 omega_gpu_session_unlock();
69 omega_gpu_cmdlist_free(cl);
70 if (!ok) return -1;
71 return omega_gpu_decode_attn256_counters_check(c) == 0 ? 0 : -1;
72}
73
74static int one_case(uint32_t pos0, uint32_t T, int control) {
75 float qn[256], kn[256];
76 for (unsigned i = 0; i < 256; i++) { qn[i] = 1.0f + 0.2f * frand(); kn[i] = 1.0f + 0.2f * frand(); }
77 uint16_t *hk = calloc((size_t)KVH * MAXP * 256, 2), *hv = calloc((size_t)KVH * MAXP * 256, 2);
78 uint8_t *rows = calloc((size_t)T, XROW);
79 float *oa = malloc((size_t)T * OROW), *ob = malloc((size_t)T * OROW);
80 if (!hk || !hv || !rows || !oa || !ob) return -1;
81 for (uint32_t h = 0; h < KVH; h++) for (uint32_t p = 0; p < pos0; p++) for (unsigned d = 0; d < 256; d++) {
82 hk[((size_t)h * MAXP + p) * 256 + d] = bf(frand() * 3.0f); hv[((size_t)h * MAXP + p) * 256 + d] = bf(frand());
83 }
84 for (uint32_t t = 0; t < T; t++) { float *r = (float *)(rows + (size_t)t * XROW); for (unsigned i = 0; i < XROW / 4u; i++) r[i] = frand() * 2.0f; }
85 OmegaGpuDecodeAttn256Cache *ca = NULL, *cb = NULL;
86 int rc = make_cache(&ca, qn, kn, hk, hv, pos0) || make_cache(&cb, qn, kn, hk, hv, pos0) ? -1 : 0;
87 for (uint32_t t = 0; t < T && rc == 0; t++)
88 if (omega_gpu_decode_attn256(ca, 0, pos0 + t, (const float *)(rows + (size_t)t * XROW), EPS, OSCALE, oa + (size_t)t * OUTW, NULL) != 0) rc = -1;
89 if (rc == 0) {
90 if (control == 1) omega_gpu_decode_attn256_test_set_multi_mutant(1);
91 rc = run_multi(cb, pos0, T, rows, control == 2 ? QKVW * 4u : XROW, ob);
92 omega_gpu_decode_attn256_test_set_multi_mutant(0);
93 }
94 size_t odiff = 0, kdiff = 0;
95 if (rc == 0) {
96 odiff = 0; for (size_t i = 0; i < (size_t)T * OUTW; i++) { uint32_t a, b; memcpy(&a, &oa[i], 4); memcpy(&b, &ob[i], 4); if (a != b) odiff++; }
97 uint16_t ka[256], va[256], kb[256], vb[256];
98 for (uint32_t h = 0; h < KVH && rc == 0; h++) for (uint32_t t = 0; t < T; t++) {
99 if (omega_gpu_decode_attn256_cache_read(ca, 0, h, pos0 + t, ka, va) != 0 || omega_gpu_decode_attn256_cache_read(cb, 0, h, pos0 + t, kb, vb) != 0) { rc = -1; break; }
100 if (memcmp(ka, kb, sizeof ka) || memcmp(va, vb, sizeof va)) kdiff++;
101 }
102 }
103 const char *what = control == 1 ? "control prep dropped" : control == 2 ? "control row stride 20480" : "real";
104 if (rc != 0) { CHECK(0, "pos0 %u T %u %s: chip call failed (%s)", pos0, T, what, omega_gpu_decode_attn_last_error()); }
105 else if (control) { CHECK(odiff > 0, "pos0 %u T %u %s: NOT caught", pos0, T, what); printf("chip pos0 %u T %u %s: %zu output words differ, %zu K/V rows differ -> %s\n", pos0, T, what, odiff, kdiff, odiff ? "caught" : "MISSED"); }
106 else { CHECK(odiff == 0 && kdiff == 0, "pos0 %u T %u: %zu output words, %zu K/V rows differ", pos0, T, odiff, kdiff); printf("chip pos0 %u T %u: outputs %s, K/V rows %s\n", pos0, T, odiff ? "DIFFER" : "identical", kdiff ? "DIFFER" : "identical"); }
107 omega_gpu_decode_attn256_cache_free(ca); omega_gpu_decode_attn256_cache_free(cb);
108 free(hk); free(hv); free(rows); free(oa); free(ob);
109 return rc;
110}
111
112int main(void) {
113 static const uint32_t cases[][2] = { { 0, 1 }, { 0, 7 }, { 5, 128 }, { 250, 10 }, { 1000, 37 }, { 2040, 128 }, { 6000, 128 }, { 8191, 1 } };
114 for (unsigned i = 0; i < sizeof cases / sizeof cases[0]; i++) one_case(cases[i][0], cases[i][1], 0);
115 one_case(300, 16, 1);
116 one_case(300, 16, 2);
117 printf("gpu_dattn256_multi_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS");
118 return g_failed ? 1 : 0;
119}