diff --git a/tests/r4d_emu/emu/hip/hip_runtime.h b/tests/r4d_emu/emu/hip/hip_runtime.h index 36647374..31d786d9 100644 --- a/tests/r4d_emu/emu/hip/hip_runtime.h +++ b/tests/r4d_emu/emu/hip/hip_runtime.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -99,6 +100,17 @@ 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 */ +/* Blocks run one after another on one OS thread, so a fence orders nothing that is not already in + * order and an atomic is a plain read-modify-write. Enough for the split-K arrival pattern + * (r4d_lora_bf16.hip): the last block of a group in launch order is the one that reduces. */ +inline void __threadfence() {} +#define __HIP_MEMORY_SCOPE_AGENT 3 +#define __hip_atomic_fetch_add(p, v, order, scope) emu_atomic_fetch_add((p), (v)) +template inline T emu_atomic_fetch_add(T* p, T v) { const T o = *p; *p = o + v; return o; } +/* The device allocations a unit makes for itself (counter blocks): host memory here. */ +inline hipError_t hipMalloc(void** p, size_t n) { *p = std::calloc(n ? n : 1, 1); return *p ? 0 : 2; } +inline hipError_t hipMemsetAsync(void* p, int v, size_t n, hipStream_t) { std::memset(p, v, n); return 0; } + struct alignas(8) uint2 { unsigned x, y; }; struct alignas(16) uint4 { unsigned x, y, z, w; }; struct alignas(16) int4 { int x, y, z, w; }; diff --git a/tests/r4d_emu/lora_cases.h b/tests/r4d_emu/lora_cases.h index 5c993474..bf3ef2df 100644 --- a/tests/r4d_emu/lora_cases.h +++ b/tests/r4d_emu/lora_cases.h @@ -23,6 +23,10 @@ * half an ulp of y's dtype relative (2^-8 bf16, 2^-11 f16, 0 f32). * * An implementation marked `ref_bytes` must also produce, for expand, exactly the bytes of the * first implementation in the list -- the claim libr4d's expand makes about libref's order. + * + * A shrink row with a scratch hook (libr4d's WMMA row keeps its K split's partials there) is handed + * the scratch the hook asks for, filled with a non-zero pattern, as the core would hand it a slice of + * its arena. */ #pragma once @@ -39,13 +43,28 @@ namespace lora_cases { typedef int (*LaunchFn)(const RadArgs*, RadStream); +typedef int64_t (*ScratchFn)(const RadArgs*); struct Impl { const char* name; LaunchFn shrink, expand; bool ref_bytes; /* expand must match the first implementation bit for bit */ + ScratchFn shrink_scratch; /* the row's scratch hook, or null: none */ }; +/* The scratch a launch is handed, sized by the row's own hook from the call's parameters -- what + * the core does at declare with the band's M. Host memory: every implementation here runs on the + * host. */ +static inline void with_scratch(RadArgs& a, ScratchFn fn, std::vector& keep) { + a.scratch = nullptr; + a.scratch_bytes = 0; + const int64_t need = fn ? fn(&a) : 0; + if (need <= 0) return; + keep.assign((size_t)need, 0xA5); /* not zeros: a split must write its partials */ + a.scratch = keep.data(); + a.scratch_bytes = need; +} + struct Tally { int cases = 0, fails = 0; double worst_shrink = 0, worst_expand = 0; /* the largest |err| / tolerance seen */ @@ -173,6 +192,8 @@ static inline void shrink_case(Tally& T, const std::vector& impls, std::mt for (auto& x : tv) std::memcpy(&x, &kSentinel, 4); t[4] = mk(tv.data(), RAD_F32, {M, R}, {R, 1}); ++T.cases; + std::vector scr; + with_scratch(a, im.shrink_scratch, scr); const int rc = im.shrink(&a, nullptr); if (rc != RAD_OK) { fail(T, im, "shrink returned an error", M, R, K, adt, rc, -1); diff --git a/tests/r4d_emu/test_lora.cpp b/tests/r4d_emu/test_lora.cpp index d868f865..594cbafc 100644 --- a/tests/r4d_emu/test_lora.cpp +++ b/tests/r4d_emu/test_lora.cpp @@ -2,11 +2,27 @@ * * The case matrix and every check are lora_cases.h's, shared with tests/lora_ops_test.cpp (libref and * libavx): base rows untouched by bytes, adapter rows against a double-precision sum within the - * bounds stated there. Here the implementations are the emulated kernels, ref_host.cpp's - * restatement of libref's arithmetic, and libref itself when $RADIANCE_HOME holds one (ctest sets - * it) -- and the emulated EXPAND is held to the first of those bit for bit, because its - * j-ascending sum and its roundings are libref's. Shrink sums k in 32 lane strides and a fixed - * butterfly, so it is held to the bound rather than to libref's bytes. + * bounds stated there. Here the implementations are the emulated kernels -- the v1 scalar rows and + * the tiled rows (WMMA shrink, LDS-staged expand) -- ref_host.cpp's restatement of libref's + * arithmetic, and libref itself when $RADIANCE_HOME holds one (ctest sets it); every emulated + * EXPAND is held to the first of those bit for bit, because its j-ascending sum and its roundings + * are libref's. Shrink is held to the bound rather than to libref's bytes: v1 sums k in 32 lane + * strides and a butterfly, the WMMA row in 32-wide steps over waves and a K split. + * + * Then what the tiled rows promise beyond the matrix, each proven here on the emulator: + * + * shapes the real ones -- K 2048 / 5120 / 6144 / 17408, R 16 / 48 / 64 / 192, N 256 .. + * 12288, odd M, S 1..4, bf16 and E4M3 with a scale every 128 -- shrink to the f64 + * bound (and its worst error printed against f64), expand to libref's bytes. + * batch a row computed alone (M = 1) and as row p of M in {7, 16, 17, 37, 300}, the others + * random slots and base rows: its t and y bits are the same. Every K split, z fan-out + * and pass split the tiled rows can take differs between those launches. + * layout the same rows through aligned (16-byte loads) and unaligned (element loads) + * buffers: the same bits. + * base rows y filled with -0, NaN payloads and random values, t with a sentinel: a base row's + * bytes are untouched, and a launch of base rows only touches nothing. + * decode every fp8 E4M3 / E5M2 / i8 code through a one-hot pool: t is the code times its + * group's scale exactly, so the WMMA row's code -> bf16 widening is the ABI's. */ #include "rad_abi.h" #include "rad_plugin.h" @@ -14,15 +30,21 @@ #include "ref_host.h" #include "lora_cases.h" +#include +#include +#include #include #include #include #include +#include #include #include using lora_cases::Impl; using lora_cases::LaunchFn; +using lora_cases::mk; +using lora_cases::Params; static int host_shrink(const RadArgs* a, RadStream) { return host_lora_shrink(a); } static int host_expand(const RadArgs* a, RadStream) { return host_lora_expand(a); } @@ -48,18 +70,468 @@ static void load_libref(LaunchFn* shrink, LaunchFn* expand) { } } +static int g_fails = 0; +static void fail(const char* what, const std::string& detail) { + ++g_fails; + if (g_fails <= 40) std::printf("FAIL %s: %s\n", what, detail.c_str()); +} +static std::string fmt(const char* f, ...) __attribute__((format(printf, 1, 2))); +static std::string fmt(const char* f, ...) { + char b[512]; + va_list ap; + va_start(ap, f); + std::vsnprintf(b, sizeof b, f, ap); + va_end(ap); + return b; +} + +static const uint32_t kSent = 0x7fc0deadu; + +/* ------------------------------------------------------------------ one shrink problem + * x rows at row stride `lda` (from element `off` of the buffer), the scale stream [M, groups] at + * `lds`, dense pools, t dense. Built once a test and launched through any shrink row. */ +struct Shrink { + int M = 0, K = 0, R = 0, S = 0, group = 128, lda = 0, off = 0, lds = 0; + uint32_t adt = RAD_BF16; + std::vector abuf; /* (off + M * lda) elements of adt; 16-byte aligned base */ + std::vector sbuf; + std::vector slot; + std::vector la; +}; + +static size_t esz(uint32_t dt) { return (size_t)rad_dtype_bits(dt) / 8; } + +/* The x element of row m, column k, as the contract defines it (code times scale, one rounding). */ +static float x_of(const Shrink& P, int m, int k) { + float x = lora_cases::get(P.adt, P.abuf.data(), (size_t)(P.off + (size_t)m * P.lda + k)); + if (lora_cases::coded(P.adt)) x = x * P.sbuf[(size_t)m * P.lds + k / P.group]; + return x; +} + +/* Run `fn` (with its scratch hook) over P; t comes back [M, R], base rows holding kSent. */ +static int run_shrink(const Shrink& P, LaunchFn fn, lora_cases::ScratchFn hook, + std::vector* t) { + const bool sc = lora_cases::coded(P.adt); + const int groups = (P.K + P.group - 1) / P.group; + RadTensor ts[5]{}; + ts[0] = mk((void*)(P.abuf.data() + (size_t)P.off * esz(P.adt)), P.adt, {P.M, P.K}, {P.lda, 1}); + if (sc) ts[1] = mk((void*)P.sbuf.data(), RAD_F32, {P.M, groups}, {P.lds, 1}); + ts[2] = mk((void*)P.slot.data(), RAD_I32, {P.M}, {1}); + ts[3] = mk((void*)P.la.data(), RAD_BF16, {P.S, P.R, P.K}, {(int64_t)P.R * P.K, P.K, 1}); + t->assign((size_t)P.M * P.R, 0.0f); + for (auto& v : *t) std::memcpy(&v, &kSent, 4); + ts[4] = mk(t->data(), RAD_F32, {P.M, P.R}, {P.R, 1}); + Params Q; + Q.i("M", P.M); Q.i("K", P.K); Q.i("R", P.R); Q.i("S", P.S); Q.i("group", sc ? P.group : 0); + Q.s("dtype", rad_dtype_name(P.adt)); + RadArgs a{}; + a.t = ts; a.n_t = 5; a.p = Q.p.data(); a.n_p = (int)Q.p.size(); a.world_size = 1; + std::vector scr; + lora_cases::with_scratch(a, hook, scr); + return fn(&a, nullptr); +} + +/* Random x, scales, pools; slots as given. `aligned`: row stride a multiple of 16 elements and no + * offset, so the tiled row's 16-byte loads are taken; else K + 7 and 3. */ +static Shrink make_shrink(std::mt19937& rng, int M, int K, int R, int S, uint32_t adt, + const std::vector& slot, bool aligned) { + std::normal_distribution nd(0.f, 1.f); + std::uniform_real_distribution ud(0.01f, 0.05f); + Shrink P; + P.M = M; P.K = K; P.R = R; P.S = S; P.adt = adt; + P.lda = aligned ? (K + 15) / 16 * 16 + 16 : K + 7; + P.off = aligned ? 0 : 3; + const int groups = (K + P.group - 1) / P.group; + P.lds = groups + 3; + const float amp = adt == RAD_I8 ? 40.0f : (lora_cases::coded(adt) ? 4.0f : 1.0f); + P.abuf.assign(((size_t)P.off + (size_t)M * P.lda) * esz(adt) + 64, 0); + for (size_t i = 0; i < (size_t)P.off + (size_t)M * P.lda; ++i) + lora_cases::put(adt, P.abuf.data(), i, amp * nd(rng)); + P.sbuf.resize((size_t)M * P.lds); + for (auto& v : P.sbuf) v = ud(rng); + P.slot = slot; + P.la.resize((size_t)S * R * K); + for (auto& v : P.la) v = rad_f32_to_bf16(0.05f * nd(rng)); + return P; +} + +/* Copy row `src` of Q (x and scales) into row `dst` of P, which must have Q's K and dtype. */ +static void copy_row(Shrink& P, int dst, const Shrink& Q, int src) { + const size_t e = esz(P.adt); + std::memcpy(P.abuf.data() + ((size_t)P.off + (size_t)dst * P.lda) * e, + Q.abuf.data() + ((size_t)Q.off + (size_t)src * Q.lda) * e, (size_t)P.K * e); + const int groups = (P.K + P.group - 1) / P.group; + std::memcpy(&P.sbuf[(size_t)dst * P.lds], &Q.sbuf[(size_t)src * Q.lds], (size_t)groups * 4); +} + +/* t against the f64 sum: base rows keep the sentinel, adapter rows within (K + 2) 2^-24 sum|terms|. + * Returns the worst |err| / bound and the worst |err| / sum|terms| (the error the report quotes). */ +static void check_shrink(const char* who, const Shrink& P, const std::vector& t, + double* worst_bound, double* worst_rel) { + for (int m = 0; m < P.M; ++m) { + const int32_t s = P.slot[(size_t)m]; + const bool base = s < 0 || s >= P.S; + for (int j = 0; j < P.R; ++j) { + const float v = t[(size_t)m * P.R + j]; + if (base) { + if (std::memcmp(&v, &kSent, 4)) + fail(who, fmt("shrink wrote base row %d (M %d K %d R %d)", m, P.M, P.K, P.R)); + continue; + } + double sum = 0, mag = 0; + const uint16_t* ar = &P.la[((size_t)s * P.R + j) * P.K]; + for (int k = 0; k < P.K; ++k) { + const double term = (double)x_of(P, m, k) * rad_bf16_to_f32(ar[k]); + sum += term; mag += std::fabs(term); + } + const double tol = (P.K + 2) * std::ldexp(1.0, -24) * mag + 1e-30; + const double e = std::fabs((double)v - sum); + *worst_bound = std::max(*worst_bound, e / tol); + if (mag > 0) *worst_rel = std::max(*worst_rel, e / mag); + if (!(e <= tol)) + fail(who, fmt("shrink value M %d K %d R %d %s row %d col %d: %g vs %g (tol %g)", + P.M, P.K, P.R, rad_dtype_name(P.adt), m, j, (double)v, sum, tol)); + } + } +} + +/* ------------------------------------------------------------------ one expand problem */ +struct Expand { + int M = 0, N = 0, R = 0, S = 0, ldy = 0, yoff = 0; + uint32_t ydt = RAD_BF16; + std::vector t; /* [M, R] */ + std::vector slot; + std::vector lb; /* [S, N, R] */ + std::vector sc; /* [S, N] */ + std::vector y0; /* (yoff + M * ldy) elements */ +}; + +static Expand make_expand(std::mt19937& rng, int M, int N, int R, int S, uint32_t ydt, + const std::vector& slot, bool aligned) { + std::normal_distribution nd(0.f, 1.f); + std::uniform_real_distribution ud(0.5f, 2.0f); + std::uniform_int_distribution pick(0, 5); + Expand P; + P.M = M; P.N = N; P.R = R; P.S = S; P.ydt = ydt; P.slot = slot; + P.ldy = aligned ? N : N + 5; + P.yoff = aligned ? 0 : 2; + P.t.resize((size_t)M * R); + for (auto& v : P.t) v = 0.5f * nd(rng); + P.lb.resize((size_t)S * N * R); + for (auto& v : P.lb) v = rad_f32_to_bf16(0.05f * nd(rng)); + P.sc.resize((size_t)S * N); + for (auto& v : P.sc) v = ud(rng); + const size_t ny = (size_t)P.yoff + (size_t)M * P.ldy, e = esz(ydt); + P.y0.resize(ny * e); + /* -0, NaNs with payloads (quiet and signalling) and ordinary values: a base row must keep + * every one of them, bit for bit. */ + for (size_t i = 0; i < ny; ++i) { + const int k = pick(rng); + uint8_t* p = &P.y0[i * e]; + if (k == 0) { + lora_cases::put(ydt, P.y0.data(), i, -0.0f); + } else if (k == 1 && ydt == RAD_F32) { + const uint32_t u = 0x7fa00001u + (uint32_t)(i & 0xff); + std::memcpy(p, &u, 4); + } else if (k == 1) { + const uint16_t u = ydt == RAD_BF16 ? (uint16_t)(0xffa1u + (i & 7)) : (uint16_t)0x7d01u; + std::memcpy(p, &u, 2); + } else { + lora_cases::put(ydt, P.y0.data(), i, nd(rng)); + } + } + return P; +} + +static int run_expand(const Expand& P, LaunchFn fn, std::vector* y) { + *y = P.y0; + const size_t e = esz(P.ydt); + RadTensor ts[5]{}; + ts[0] = mk((void*)P.t.data(), RAD_F32, {P.M, P.R}, {P.R, 1}); + ts[1] = mk((void*)P.slot.data(), RAD_I32, {P.M}, {1}); + ts[2] = mk((void*)P.lb.data(), RAD_BF16, {P.S, P.N, P.R}, {(int64_t)P.N * P.R, P.R, 1}); + ts[3] = mk((void*)P.sc.data(), RAD_F32, {P.S, P.N}, {P.N, 1}); + ts[4] = mk(y->data() + (size_t)P.yoff * e, P.ydt, {P.M, P.N}, {P.ldy, 1}); + Params Q; + Q.i("M", P.M); Q.i("N", P.N); Q.i("R", P.R); Q.i("S", P.S); + Q.s("dtype", rad_dtype_name(P.ydt)); + RadArgs a{}; + a.t = ts; a.n_t = 5; a.p = Q.p.data(); a.n_p = (int)Q.p.size(); a.world_size = 1; + return fn(&a, nullptr); +} + +/* y against reference bytes `want` (an implementation of libref's order on the same operands), + * and base rows plus every element outside the slice against the input. */ +static void check_expand(const char* who, const Expand& P, const std::vector& y, + const std::vector& want) { + const size_t e = esz(P.ydt), ny = (size_t)P.yoff + (size_t)P.M * P.ldy; + int64_t bad_base = 0, bad_val = 0; + for (size_t i = 0; i < ny; ++i) { + const long long q = (long long)i - P.yoff; + const int m = q < 0 ? -1 : (int)(q / P.ldy), n = q < 0 ? -1 : (int)(q % P.ldy); + const bool inside = m >= 0 && m < P.M && n < P.N && P.slot[(size_t)m] >= 0 && + P.slot[(size_t)m] < P.S; + if (!inside) bad_base += std::memcmp(&y[i * e], &P.y0[i * e], e) != 0; + else bad_val += std::memcmp(&y[i * e], &want[i * e], e) != 0; + } + if (bad_base) + fail(who, fmt("expand touched %lld base-row / outside bytes (M %d N %d R %d %s)", + (long long)bad_base, P.M, P.N, P.R, rad_dtype_name(P.ydt))); + if (bad_val) + fail(who, fmt("expand: %lld outputs not libref's bytes (M %d N %d R %d %s)", + (long long)bad_val, P.M, P.N, P.R, rad_dtype_name(P.ydt))); +} + +/* ------------------------------------------------------------------ the tests */ + +struct Rows { + LaunchFn ref_shrink, ref_expand; /* libref or ref_host: the bytes expand is held to */ +}; + +/* Slots for M rows over S adapters, with base rows (-1 and S) mixed in. */ +static std::vector rand_slots(std::mt19937& rng, int M, int S, int base_pct) { + std::uniform_int_distribution u(0, 99), s(0, S - 1), b(0, 2); + std::vector v((size_t)M); + for (auto& x : v) { + if (u(rng) < base_pct) { + const int k = b(rng); + x = k == 0 ? -1 : (k == 1 ? S : -7); + } else { + x = s(rng); + } + } + return v; +} + +/* REAL SHAPES. Each pair is shrink -> expand, as the engine issues it: expand reads the t the + * tiled shrink wrote, and is held to libref's bytes over that same t. */ +static void test_shapes(const Rows& R) { + std::mt19937 rng(23); + struct Sh { int M, K, R, N, S; uint32_t adt, ydt; bool al; }; + const Sh shapes[] = { + { 9, 2048, 48, 2560, 4, RAD_BF16, RAD_BF16, true }, /* qkv, decode-ish */ + { 37, 2048, 32, 12288, 3, RAD_BF16, RAD_BF16, true }, /* gate_up */ + { 17, 6144, 16, 2048, 4, RAD_BF16, RAD_BF16, false }, /* down, unaligned */ + { 5, 17408, 16, 256, 2, RAD_BF16, RAD_F32, true }, /* the deepest K */ + { 7, 5120, 48, 1024, 4, RAD_F8E4M3, RAD_BF16, true }, /* fp8, group 128 */ + { 33, 5120, 64, 640, 1, RAD_F8E4M3, RAD_F16, false }, /* S = 1 */ + { 3, 2048, 192, 384, 4, RAD_BF16, RAD_BF16, true }, /* 3 modules x 64 */ + { 300, 2048, 48, 256, 2, RAD_BF16, RAD_BF16, true }, /* prefill-ish */ + { 131, 1000, 64, 1000, 3, RAD_I8, RAD_F32, false }, /* partial group, odd */ + { 21, 6144, 48, 300, 4, RAD_F8E5M2, RAD_BF16, true }, + }; + double wb = 0, wr = 0; + for (const Sh& sh : shapes) { + const std::vector slot = rand_slots(rng, sh.M, sh.S, 25); + const Shrink P = make_shrink(rng, sh.M, sh.K, sh.R, sh.S, sh.adt, slot, sh.al); + std::vector t; + const int rc = run_shrink(P, r4d_rad_lora_shrink_wmma, r4d_rad_lora_shrink_wmma_scratch, &t); + if (rc != RAD_OK) { fail("shapes", fmt("shrink_wmma rc %d", rc)); continue; } + check_shrink("shapes/shrink_wmma", P, t, &wb, &wr); + /* expand over the t just computed; base rows' t is the sentinel, never read */ + Expand E = make_expand(rng, sh.M, sh.N, sh.R, sh.S, sh.ydt, slot, sh.al); + E.t = t; + std::vector want, got; + if (run_expand(E, R.ref_expand, &want) != RAD_OK) { fail("shapes", "reference expand"); continue; } + if (run_expand(E, r4d_rad_lora_expand_tiled, &got) != RAD_OK) { + fail("shapes", "expand_tiled rc"); + continue; + } + check_expand("shapes/expand_tiled", E, got, want); + } + std::printf("lora shapes: %zu shrink -> expand pairs; tiled shrink worst |err|/bound %.4f, " + "worst |err| / sum|terms| %.3g (2^-24 = %.3g)\n", + sizeof(shapes) / sizeof(shapes[0]), wb, wr, std::ldexp(1.0, -24)); +} + +/* BATCH INVARIANCE: a row alone and inside batches of other slots and base rows. */ +static void test_invariance() { + std::mt19937 rng(41); + struct Sh { int K, R, N, S; uint32_t adt, ydt; }; + const Sh shapes[] = { + { 2048, 48, 2560, 4, RAD_BF16, RAD_BF16 }, /* 4 K splits x 3 j tiles */ + { 6144, 16, 640, 3, RAD_BF16, RAD_F32 }, /* 12 K splits */ + { 1000, 64, 300, 4, RAD_F8E4M3, RAD_BF16 }, /* coded, partial group, 2 splits */ + { 300, 20, 130, 2, RAD_BF16, RAD_F16 }, /* no split, ragged R and N */ + }; + const int Ms[] = { 7, 16, 17, 37, 300 }; + int compared = 0; + for (const Sh& sh : shapes) { + for (int al = 0; al < 2; ++al) { + /* The target row: one per slot, alone. */ + for (int s = 0; s < sh.S; ++s) { + const std::vector one = { s }; + const Shrink P1 = make_shrink(rng, 1, sh.K, sh.R, sh.S, sh.adt, one, al); + std::vector t1, t1s; + if (run_shrink(P1, r4d_rad_lora_shrink_wmma, r4d_rad_lora_shrink_wmma_scratch, + &t1) != RAD_OK || + run_shrink(P1, r4d_rad_lora_shrink, nullptr, &t1s) != RAD_OK) { + fail("invariance", "M=1 launch"); + continue; + } + Expand E1 = make_expand(rng, 1, sh.N, sh.R, sh.S, sh.ydt, one, al); + std::vector y1, y1s; + run_expand(E1, r4d_rad_lora_expand_tiled, &y1); + run_expand(E1, r4d_rad_lora_expand, &y1s); + if (y1 != y1s) fail("invariance", "tiled and scalar expand differ at M=1"); + for (int M : Ms) { + const std::vector slot0 = rand_slots(rng, M, sh.S, 30); + for (int p : { 0, M / 2, M - 1 }) { + std::vector slot = slot0; + slot[(size_t)p] = s; + /* the same pools; x and t of row p are the solo row's */ + Shrink PB = make_shrink(rng, M, sh.K, sh.R, sh.S, sh.adt, slot, al != 0); + PB.la = P1.la; + copy_row(PB, p, P1, 0); + std::vector tb; + if (run_shrink(PB, r4d_rad_lora_shrink_wmma, + r4d_rad_lora_shrink_wmma_scratch, &tb) != RAD_OK) { + fail("invariance", "batch launch"); + continue; + } + if (std::memcmp(&tb[(size_t)p * sh.R], t1.data(), (size_t)sh.R * 4)) + fail("invariance", fmt("shrink_wmma: row %d of M %d (K %d R %d %s, " + "%s) is not its M=1 bits", p, M, sh.K, sh.R, + rad_dtype_name(sh.adt), + al ? "aligned" : "unaligned")); + double wb = 0, wr = 0; + check_shrink("invariance/shrink_wmma", PB, tb, &wb, &wr); + Expand EB = make_expand(rng, M, sh.N, sh.R, sh.S, sh.ydt, slot, al != 0); + EB.lb = E1.lb; + EB.sc = E1.sc; + std::memcpy(&EB.t[(size_t)p * sh.R], E1.t.data(), (size_t)sh.R * 4); + const size_t e = esz(sh.ydt); + std::memcpy(&EB.y0[((size_t)EB.yoff + (size_t)p * EB.ldy) * e], + &E1.y0[(size_t)E1.yoff * e], (size_t)sh.N * e); + std::vector yb, ys; + run_expand(EB, r4d_rad_lora_expand_tiled, &yb); + run_expand(EB, r4d_rad_lora_expand, &ys); + if (std::memcmp(&yb[((size_t)EB.yoff + (size_t)p * EB.ldy) * e], + &y1[(size_t)E1.yoff * e], (size_t)sh.N * e)) + fail("invariance", fmt("expand_tiled: row %d of M %d (N %d R %d) is " + "not its M=1 bits", p, M, sh.N, sh.R)); + check_expand("invariance/expand_tiled", EB, yb, ys); + compared += 2; + } + } + } + } + } + /* The layouts against each other: one problem through aligned and unaligned buffers. */ + for (const Sh& sh : shapes) { + const std::vector slot = rand_slots(rng, 37, sh.S, 20); + const Shrink A = make_shrink(rng, 37, sh.K, sh.R, sh.S, sh.adt, slot, true); + Shrink U = make_shrink(rng, 37, sh.K, sh.R, sh.S, sh.adt, slot, false); + U.la = A.la; + for (int m = 0; m < 37; ++m) copy_row(U, m, A, m); + std::vector ta, tu; + run_shrink(A, r4d_rad_lora_shrink_wmma, r4d_rad_lora_shrink_wmma_scratch, &ta); + run_shrink(U, r4d_rad_lora_shrink_wmma, r4d_rad_lora_shrink_wmma_scratch, &tu); + if (std::memcmp(ta.data(), tu.data(), ta.size() * 4)) + fail("layout", fmt("shrink_wmma: aligned and unaligned x differ (K %d R %d)", sh.K, sh.R)); + ++compared; + } + std::printf("lora batch invariance: %d row comparisons (M=1 vs M in {7,16,17,37,300}, three " + "positions, aligned and unaligned)\n", compared); +} + +/* BASE ROWS ONLY: a prefill-sized launch of nothing but base rows touches no byte of t or y. */ +static void test_all_base() { + std::mt19937 rng(5); + std::vector slot((size_t)2048); + for (size_t i = 0; i < slot.size(); ++i) slot[i] = (i % 3 == 0) ? -1 : (i % 3 == 1 ? 4 : -100); + const Shrink P = make_shrink(rng, 2048, 256, 48, 4, RAD_BF16, slot, true); + std::vector t; + double wb = 0, wr = 0; + if (run_shrink(P, r4d_rad_lora_shrink_wmma, r4d_rad_lora_shrink_wmma_scratch, &t) != RAD_OK) + fail("all-base", "shrink rc"); + check_shrink("all-base/shrink_wmma", P, t, &wb, &wr); + const Expand E = make_expand(rng, 2048, 640, 48, 4, RAD_BF16, slot, false); + std::vector y; + if (run_expand(E, r4d_rad_lora_expand_tiled, &y) != RAD_OK) fail("all-base", "expand rc"); + if (y != E.y0) fail("all-base", "expand_tiled touched a base-only launch"); + std::printf("lora base rows only: M 2048, t and y untouched\n"); +} + +/* EVERY CODE through a one-hot pool: A[s, j, k] = (k == j), so t[m, j] = x(m, j) = code * scale + * exactly -- one exact product, the rest +-0 -- and the WMMA row's code widening is checked code + * by code. Codes whose value is NaN or Inf would turn every j's zero products into NaN, so they + * get rows of their own, where every t must be NaN as in the f64 sum. */ +static void test_codes() { + int checked = 0; + for (uint32_t adt : { (uint32_t)RAD_F8E4M3, (uint32_t)RAD_F8E5M2, (uint32_t)RAD_I8 }) { + const int K = 256, R = 256, S = 1; + std::vector special; + std::vector row(K); + for (int c = 0; c < 256; ++c) { + uint8_t b = (uint8_t)c; + const float v = lora_cases::get(adt, &b, 0); + if (std::isfinite(v)) row[(size_t)c] = b; + else { row[(size_t)c] = 0; special.push_back(c); } + } + const int M = 1 + (int)special.size(); + Shrink P; + P.M = M; P.K = K; P.R = R; P.S = S; P.adt = adt; P.lda = K; P.off = 0; + P.lds = 2; + P.abuf.assign((size_t)M * K + 64, 0); + std::memcpy(P.abuf.data(), row.data(), (size_t)K); + for (size_t i = 0; i < special.size(); ++i) + P.abuf[(size_t)(1 + i) * K + (size_t)(i * 37 % K)] = (uint8_t)special[i]; + P.sbuf.resize((size_t)M * 2); + for (int m = 0; m < M; ++m) { P.sbuf[(size_t)m * 2] = 0.0371f; P.sbuf[(size_t)m * 2 + 1] = 1.75f; } + P.slot.assign((size_t)M, 0); + P.la.assign((size_t)R * K, 0); + for (int j = 0; j < R; ++j) P.la[(size_t)j * K + j] = rad_f32_to_bf16(1.0f); + std::vector t; + if (run_shrink(P, r4d_rad_lora_shrink_wmma, r4d_rad_lora_shrink_wmma_scratch, &t) != RAD_OK) { + fail("codes", "rc"); + continue; + } + for (int j = 0; j < R; ++j) { + const float want = x_of(P, 0, j); + if (!(t[(size_t)j] == want)) + fail("codes", fmt("%s code 0x%02x: t %.9g, want %.9g", rad_dtype_name(adt), + (unsigned)row[(size_t)j], (double)t[(size_t)j], (double)want)); + ++checked; + } + for (int m = 1; m < M; ++m) { + const int pos = (int)((size_t)(m - 1) * 37 % K); + for (int j = 0; j < R; ++j) { + /* j == pos: the code's own value times 1 plus zeros (an Inf stays Inf); every + * other j: the code times 0, which is NaN for both NaN and Inf. */ + const float v = t[(size_t)m * R + j], want = x_of(P, m, pos); + const bool okv = (j == pos && !std::isnan(want)) ? v == want : std::isnan(v); + if (!okv) + fail("codes", fmt("%s code 0x%02x: t[%d] = %g", rad_dtype_name(adt), + (unsigned)special[(size_t)m - 1], j, (double)v)); + } + } + } + std::printf("lora codes: %d codes widened exactly (E4M3, E5M2, i8), NaN / Inf rows NaN\n", + checked); +} + int main() { LaunchFn rs = nullptr, re = nullptr; load_libref(&rs, &re); std::printf("lora: libref %s\n", rs && re ? "loaded: the emulated kernels are held to it as well" : "not found ($RADIANCE_HOME): held to ref_host.cpp and the double sums only"); - std::vector impls = { { "host", host_shrink, host_expand, true } }; - if (rs && re) impls.push_back({ "libref", rs, re, true }); - impls.push_back({ "r4d-emu", r4d_rad_lora_shrink, r4d_rad_lora_expand, true }); + std::vector impls = { { "host", host_shrink, host_expand, true, nullptr } }; + if (rs && re) impls.push_back({ "libref", rs, re, true, nullptr }); + impls.push_back({ "r4d-emu", r4d_rad_lora_shrink, r4d_rad_lora_expand, true, nullptr }); + impls.push_back({ "r4d-tile", r4d_rad_lora_shrink_wmma, r4d_rad_lora_expand_tiled, true, + r4d_rad_lora_shrink_wmma_scratch }); lora_cases::Tally T; lora_cases::run_matrix(T, impls, 11); std::printf("lora: %d implementation-cases, %d failure(s); worst |err|/tol shrink %.3f, " "expand %.3f\n", T.cases, T.fails, T.worst_shrink, T.worst_expand); - return T.fails != 0; + + Rows R{ rs ? rs : host_shrink, re ? re : host_expand }; + test_shapes(R); + test_invariance(); + test_all_base(); + test_codes(); + std::printf("lora tiled rows: %d failure(s)\n", g_fails); + return (T.fails != 0 || g_fails != 0) ? 1 : 0; }