Archived
The 26B-A4B's router reads rmsnorm(x) * w_r * hidden^-0.5 and its experts rmsnorm(x) * w_e, quantised to E4M3 for the fp8 gate_up: an `rmsnorm` and an `rmsnorm_quant_fp8` a layer, each reading back the x norm_add_norm has just written and taking the sum of squares it has just taken. At decode that is 60 launches a step (8.3 and 7.8 us under --profile-ops on 4x R9700); at a prefill, eleven passes of each a layer per 20K-token request. norm_add_norm_route is norm_add_norm, then from the norm factor it already holds the router's row by rmsnorm's expression, ((x * s) * w_r) * mult, and the experts' codes as rmsnorm_quant_fp8 writes them: the bf16-rounded norm, a scale of amax * (1 / 448) a 128 columns (1 for an all-zero group), the value times its reciprocal clamped to +-448, one v_cvt_pk_fp8_f32. Element i is thread i % 256 at slot i / 256, so a group is four waves at one slot -- a wave's max, then the four through LDS. Its own name rather than optional operands on norm_add_norm: the codes need whole 128-column groups, which the plain join does not, and the arch falls back cleanly where it does not resolve. A device row and a host arm (one helper with norm_add_norm's), the schema, OPS.md. arch/gemma4 asks for it on every layer whose gate_up is block fp8 -- the user's fp8gu container, every layer -- and then issues neither op_rnorm nor op_enorm; it declares both still, and norm_add_norm and the chain behind it, for where it does not resolve. The routed arm's two norm gains are declared ahead of the attention join now, which names them. The overlap takes either join. A startup note counts the layers that take it. The same rounding as the chain, but for the order a sum of squares is added in, as every join here: g4k_emu holds the kernel to a chain summed in its block order bit for bit -- x, y, the router's row, every code and scale -- at the 26B-A4B's 2816 at four heights, at 128, 3072, 3200 and 6144, and with an all-zero group; against the host arm, no code more than a step off. The emulator gains v_cvt_pk_fp8_f32, through the project's saturating encoder. arch_test: with fp8 gate_up experts the route op resolves on all six layers, a decode step issues it and no rmsnorm_quant_fp8 and one rmsnorm fewer a layer; refused, norm_add_norm and both launches; a tp-4 prefill through the overlap issues it a slice and keeps every reduction ordered. Not yet run on the cards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
3.8 KiB
C++
77 lines
3.8 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))
|
|
/* v_cvt_pk_fp8_f32 (gfx12, OCP E4M3): a and b as two codes into the low half of `old` (hi false)
|
|
* or its high half, the other half kept; round to nearest even. The project's saturating encoder,
|
|
* rad_f32_to_fp8e4m3, is the same function on the +-448 a kernel clamps to first. */
|
|
int emu_cvt_pk_fp8(float a, float b, int old, bool hi);
|
|
#define __builtin_amdgcn_cvt_pk_fp8_f32(a, b, old, hi) emu_cvt_pk_fp8((a), (b), (old), (hi))
|
|
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__)
|