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 / fake_m16_native.h

172 lines · 8083 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * Simulated GB10 driver for the host test of the Blackwell GPU engine backend
4 * (tests/test_omega_blackwell_engine.c). NOT_RUN: written in cut A3b2 and never
5 * compiled or executed by its author.
6 *
7 * It replaces seven symbols of the real driver stack, so the REAL backend
8 * (src/omega_blackwell_engine.c) and the REAL vector wrapper
9 * (src/omega_blackwell_submit.c) run unchanged on a machine with no GPU:
10 * m16_native_open, m16_native_create_channel, m16_native_submit_methods,
11 * m16_native_wait_marker, m16_native_close, nvrm_alloc, nvrm_free.
12 * The signatures come from physics/m16/m16_native.h and physics/nvrm/nvrm.h at
13 * the physics pin; the fake file includes those headers, so a mismatch is a
14 * compile error.
15 *
16 * What it models (every point is a MODEL, UNVERIFIED on the chip, confidence
17 * about 70 percent that it matches the real driver):
18 * - "device" memory is host memory, GPU address equals CPU address, dirty
19 * (0x5a fill) so a missing zeroing step shows up;
20 * - the submitted pushbuffer is parsed and executed in order: DMA uploads,
21 * the two inline QMDs, the semaphore initial value, the release packets and
22 * the L2 flush memory operation (method 0x28, operation 0x10 << 27);
23 * - the vector kernel is a simple loop (c[i] = a[i] + b[i] for the threads the
24 * QMD asks for) and only runs if the program bytes in device memory are the
25 * bytes the test expects;
26 * - the second marker release is only written if an L2 flush operation
27 * appears between the two releases (modelling choice, see the doc);
28 * - m16_native_wait_marker is the real ">=" serial compare with no loop; a
29 * timeout advances the fake clock by the timeout so waited_ms is exact;
30 * - nvrm_free of a zeroed NvrmMem is a no-op that counts nothing, a real free
31 * zeroes the NvrmMem and clobbers errno (like a munmap or ioctl might).
32 */
33#ifndef FAKE_M16_NATIVE_H
34#define FAKE_M16_NATIVE_H
35
36#include <stddef.h>
37#include <stdint.h>
38
39#define FK_TEXT "fake driver says no"
40#define FK_EV_MAX 512
41#define FK_SNAP_WORDS 256
42#define FK_PB_MAX 16384 /* the whole 64 KiB session ring (omega_gpu_cmdlist.c RING_WORDS), so a near-full command list fits */
43
44/* Event kinds in the log (fk.ev). */
45enum {
46 FK_EV_OPEN = 1,
47 FK_EV_CHANNEL,
48 FK_EV_ALLOC, /* a = bytes asked */
49 FK_EV_FREE, /* a = handle */
50 FK_EV_SUBMIT, /* a = word count */
51 FK_EV_WAIT, /* a = FK_W_*, b = expected value, c = timeout ms */
52 FK_EV_CLOSE, /* a = live allocations at the moment of close */
53 FK_EV_PB_CBANK, /* a = destination, b = words (224) */
54 FK_EV_PB_ARGS, /* a = destination, b = words (7) */
55 FK_EV_PB_SEM_INIT, /* a = destination, b = value */
56 FK_EV_PB_QMD, /* a = QMD address, b = 1 if it is the kernel QMD */
57 FK_EV_PB_RELEASE, /* a = address, b = payload, c = flags word */
58 FK_EV_PB_FLUSH, /* a = operation word (last word of the packet) */
59 FK_EV_KERNEL /* a = threads, b = element count */
60};
61
62/* Which coherent word a wait looked at. */
63enum { FK_W_UNKNOWN = 0, FK_W_MARKER1, FK_W_MARKER2, FK_W_SEM, FK_W_COUNT };
64
65enum { FK_ALLOC_FAIL = 0, FK_ALLOC_NO_CPU, FK_ALLOC_SHORT, FK_ALLOC_MISALIGNED };
66enum { FK_SYNC_NORMAL = 0, FK_SYNC_NEVER, FK_SYNC_OVERSHOOT };
67/* FK_KERNEL_SKIP: run no kernel at all and only release the completion semaphore (for tests that count
68 * driver calls with kernels the fake does not model, such as the matmul and attention launches). */
69enum { FK_KERNEL_OK = 0, FK_KERNEL_NO_WRITE, FK_KERNEL_HALF, FK_KERNEL_WRONG, FK_KERNEL_SKIP };
70
71#define FK_SIZE_LOG 8192
72#define FK_LIVE_MAX 128 /* live driver buffers the fake can track at once */
73
74typedef struct {
75 int kind;
76 uint64_t a;
77 uint64_t b;
78 uint64_t c;
79} FakeEvent;
80
81typedef struct {
82 /* ---- faults and behaviour, set by the test after fake_reset ---- */
83 int open_fail;
84 int channel_fail;
85 int submit_fail;
86 int submit_faults_device; /* with submit_fail: the failed submit also sets rm.faulted, as a channel error does */
87 int close_fail;
88 int extra_channels; /* L136: nvrm_channel_n / nvrm_chan_submit succeed (default: refused, one channel) */
89 uint64_t alloc_max; /* largest nvrm_alloc the fake grants, 0 = the default 1 MiB */
90 int alloc_fail_nth; /* 1-based nvrm_alloc call that misbehaves, 0 = none */
91 int alloc_mode; /* FK_ALLOC_* for that call */
92 int free_fail_nth; /* 1-based real nvrm_free call that fails, 0 = none */
93 int marker_mode; /* FK_SYNC_*: first marker release */
94 int marker2_mode; /* FK_SYNC_*: second marker release */
95 int sem_mode; /* FK_SYNC_*: kernel completion semaphore */
96 int kernel_mode; /* FK_KERNEL_* */
97 const void *prog_expect; /* program bytes the device memory must hold, NULL = do not check */
98 size_t prog_len;
99
100 /* ---- observations ---- */
101 uint64_t clock_ms; /* the fake millisecond clock (fake_now_ms) */
102 int open_calls;
103 int channel_calls;
104 int alloc_calls;
105 int uncached_calls; /* allocations made through nvrm_alloc_gpu_uncached (also counted in alloc_calls) */
106 int free_calls; /* real frees only (a zeroed NvrmMem is not counted) */
107 int close_calls;
108 int submit_calls;
109 int chan_submit_calls; /* nvrm_chan_submit on an extra channel (extra_channels set) */
110 int chan_retire_calls;
111 int wait_calls;
112 int double_free; /* frees of a handle that was not live */
113 int live; /* live backend allocations right now (not the channel's own 4 KiB buffer) */
114 int live_at_close; /* live allocations when m16_native_close was called */
115 int device_open;
116 int channel_up;
117 uint64_t pb_mem_size; /* ctx->pb_mem.size when the pushbuffer was submitted */
118 int pb_parse_error; /* the pushbuffer had a packet the fake could not parse */
119 uint32_t pb[FK_PB_MAX];
120 size_t pb_len;
121
122 uint64_t marker1_va; /* first release address, from the pushbuffer */
123 uint64_t marker2_va; /* second release address, 0 with NO_C3 */
124 uint64_t sem_va; /* destination of the semaphore initial value DMA */
125 uint32_t sem_init_value;
126 uint32_t pre_marker1; /* values the GPU would see at the doorbell */
127 uint32_t pre_marker2;
128 uint32_t pre_sem;
129 int pre_valid; /* the three pre_* values were captured */
130 int kernel_ran;
131 int prog_matched;
132 uint64_t kernel_threads;
133 uint64_t kernel_n;
134 uint32_t out_before[FK_SNAP_WORDS]; /* device output words just before the kernel ran */
135 size_t snap_n;
136 int poison_collisions; /* snapshot words equal to the correct sum (a bad poison word) */
137 uint32_t release_flags[2];
138 uint64_t alloc_va[16]; /* GPU address of the nth nvrm_alloc (1-based nth stored at [nth - 1]) */
139 uint64_t arg_a; /* kernel argument addresses the device saw in the constant bank */
140 uint64_t arg_b;
141 uint64_t arg_c;
142 uint64_t prog_va_seen; /* program address from QMD1 */
143 uint64_t cbank_va_seen;/* constant bank address from QMD1 */
144
145 int wait_n[FK_W_COUNT];
146 uint64_t wait_ms[FK_W_COUNT];
147 uint32_t wait_expected[FK_W_COUNT];
148
149 uint64_t alloc_size_log[FK_SIZE_LOG]; /* size asked by the nth granted nvrm_alloc (0-based), first FK_SIZE_LOG only */
150 int alloc_size_n;
151 uint64_t free_size_log[FK_SIZE_LOG]; /* size of the nth real nvrm_free */
152 int free_size_n;
153
154 FakeEvent ev[FK_EV_MAX];
155 int nev;
156 int ev_overflow;
157} FakeDriver;
158
159extern FakeDriver fk;
160
161/* Frees every host allocation the fake still holds (the test's own teardown, not a driver call). */
162void fake_release_all(void);
163/* fake_release_all, then zero the whole state. Call at the start of every case. */
164void fake_reset(void);
165/* Millisecond clock for omega_gpu_engine_test_set_clock. */
166uint64_t fake_now_ms(void);
167/* Number of events of this kind in the log. */
168int fake_count(int kind);
169/* Index of the nth (1-based) event of this kind, or -1. */
170int fake_nth(int kind, int nth);
171
172#endif