This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/tests/g4k_emu/emu/hip/hip_runtime.h
T
AshlynandClaude Opus 5.5 846e52148a tests: g4k_emu -- gemma4k's kernel source on the host, every GPU thread a fiber
gemma4k builds for gfx12 only and its two joins have no libref oracle, so nothing on a machine
without the card, and nothing in rad-kbench, would ever run its kernels. g4k_emu compiles the
.hip sources as host C++ against a HIP shim: every GPU thread a fiber, __syncthreads and every
cross-lane op a barrier (one not reached uniformly is reported as a deadlock), the WMMA operand
layouts gfx12's. Checked against a port of libref's ref_attn_paged (attention, fixed cases and a
random sweep over every compiled geometry), against the host rows (pair_argmax), and against the
unfused chain bit for bit (the joins).

Registered in CTest under the label g4k_emu, on x86-64 with a clang: the kernels use
ext_vector_type and the fiber switch is SysV inline assembly. Under a clang C++ compiler they are
ordinary targets. The docker image compiles host C++ with g++, so there g4k_emu/run.sh builds the
same tests with the HIP compiler, ROCm's own clang++, and runs them as one test -- so wherever
gemma4k is built, this runs, the image's host-test stage included. About 40 s at the default
40-case sweep (50 s through run.sh, which compiles too); `g4k_emu_attn sweep 200` is the long run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:40:04 -05:00

72 lines
3.3 KiB
C++

/* tests/g4k_emu: SIMT EMULATOR SHIM -- runs gemma4k's kernel source on the host. Every GPU thread is a fiber on
* one OS thread; __syncthreads and every cross-lane op are barriers the scheduler enforces, and a
* barrier some threads never reach is reported as a deadlock rather than hung. Test-only. */
#pragma once
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <vector>
#define __global__
#define __device__
#define __host__
#define __shared__ static
#define __forceinline__ inline
#define __launch_bounds__(n)
struct dim3 {
unsigned x, y, z;
constexpr dim3(unsigned a = 1, unsigned b = 1, unsigned c = 1) : x(a), y(b), z(c) {}
};
typedef void* hipStream_t;
typedef int hipError_t;
enum { hipSuccess = 0 };
inline hipError_t hipGetLastError() { return 0; }
struct EmuIdx { unsigned x, y, z; };
EmuIdx& emu_thread_idx();
EmuIdx& emu_block_idx();
#define threadIdx (emu_thread_idx())
#define blockIdx (emu_block_idx())
void __syncthreads();
int emu_bpermute(int addr, int v);
int emu_readlane(int v, int lane);
#define __builtin_amdgcn_ds_bpermute(a, v) emu_bpermute((a), (v))
#define __builtin_amdgcn_readlane(v, l) emu_readlane((v), (l))
unsigned emu_ballot(bool p);
#define __builtin_amdgcn_ballot_w32(p) emu_ballot((p))
/* v_wmma_f32_16x16x16_bf16 (gfx12, wave32): A[M][K] lane M + 16 * (K / 8) element K % 8; B[K][N]
* lane N + 16 * (K / 8) element K % 8; D[M][N] lane N + 16 * (M / 8) element M % 8 */
typedef short emu_v8s __attribute__((ext_vector_type(8)));
typedef float emu_v8f __attribute__((ext_vector_type(8)));
emu_v8f emu_wmma_bf16(emu_v8s a, emu_v8s b, emu_v8f c);
#define __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12(a, b, c) emu_wmma_bf16((a), (b), (c))
typedef _Float16 emu_v8h __attribute__((ext_vector_type(8)));
emu_v8f emu_wmma_f16(emu_v8h a, emu_v8h b, emu_v8f c);
#define __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a, b, c) emu_wmma_f16((a), (b), (c))
extern int emu_last_block; /* threads a block of the last grid had: which kernel ran */
struct alignas(8) uint2 { unsigned x, y; };
struct alignas(16) uint4 { unsigned x, y, z, w; };
struct alignas(16) float4 { float x, y, z, w; };
inline uint4 make_uint4(unsigned a, unsigned b, unsigned c, unsigned d) { return uint4{a, b, c, d}; }
inline uint2 make_uint2(unsigned a, unsigned b) { return uint2{a, b}; }
inline float4 make_float4(float a, float b, float c, float d) { return float4{a, b, c, d}; }
inline float __uint_as_float(unsigned u) { float f; std::memcpy(&f, &u, 4); return f; }
inline float rsqrtf(float x) { return 1.0f / std::sqrt(x); } /* v_rsq_f32 is within an ulp of it */
inline unsigned __float_as_uint(float f) { unsigned u; std::memcpy(&u, &f, 4); return u; }
inline float __int_as_float(int u) { float f; std::memcpy(&f, &u, 4); return f; }
inline int __float_as_int(float f) { int u; std::memcpy(&u, &f, 4); return u; }
/* one block at a time, every thread a fiber */
void emu_run_grid(dim3 grid, dim3 block, void (*body)(void*), void* ctx);
template <typename K, typename A>
struct EmuCall { K k; A a; static void run(void* p) { auto* c = (EmuCall*)p; c->k(c->a); } };
template <typename A>
void emu_launch(void (*k)(A), dim3 g, dim3 b, A a) {
EmuCall<void (*)(A), A> c{k, a};
emu_run_grid(g, b, &EmuCall<void (*)(A), A>::run, &c);
}
#define G4K_LAUNCH(kernel, grid, block, stream, ...) emu_launch(&kernel, grid, block, __VA_ARGS__)