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/r4d_emu/test_lora.cpp
T
baKon 51293b269c tests: lora_test and r4d_emu_lora over one shared case matrix
lora_cases.h holds the matrix (M 1/3/17/64/130, R 16/32/48, S 4, K and N 64/256/1000, a bf16 or
E4M3 with a 128-column scale, y bf16/f32, f16/f32/E5M2/i8 once; a, a_scale and y as column
slices; slots -1, -5, 0..3 and S) and the checks: base rows untouched by bytes (a sentinel NaN in
t, -0 in y), adapter rows against an f64 sum within (K + 2) 2^-24 sum|terms| (shrink) and half an
ulp of y plus 4 (R + 2) 2^-24 (|y| + |scale| sum|terms|) (expand).

lora_test runs libref and libavx at every ISA level through rad_plugin_open (libavx builds its fp8
decode table there). r4d_emu_lora runs libr4d's source on the SIMT emulator beside ref_host.cpp
and libref, expand bit for bit.
2026-10-16 20:52:09 -05:00

66 lines
2.9 KiB
C++

/* tests/r4d_emu: lora_shrink / lora_expand (r4d_lora_bf16.hip) on the host SIMT emulator.
*
* The case matrix and every check are lora_cases.h's, shared with tests/lora_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.
*/
#include "rad_abi.h"
#include "rad_plugin.h"
#include "r4d_plugin.h"
#include "ref_host.h"
#include "lora_cases.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dlfcn.h>
#include <string>
#include <vector>
using lora_cases::Impl;
using lora_cases::LaunchFn;
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); }
/* libref's two rows, through the plugin's own exports, or nulls. */
static void load_libref(LaunchFn* shrink, LaunchFn* expand) {
*shrink = *expand = nullptr;
const char* home = std::getenv("RADIANCE_HOME");
if (!home || !*home) return;
const std::string p = std::string(home) + "/kernels/libref.so";
void* h = dlopen(p.c_str(), RTLD_NOW | RTLD_LOCAL);
if (!h) return;
auto open = (int (*)(void))dlsym(h, "rad_plugin_open"); /* as the loader does */
if (open && open() < 0) return;
auto cnt = (int (*)(void))dlsym(h, "rad_kernel_count");
auto at = (const RadKernelInfo* (*)(int))dlsym(h, "rad_kernel_at");
if (!cnt || !at) return;
for (int i = 0; i < cnt(); ++i) {
const RadKernelInfo* k = at(i);
if (!k || !k->op) continue;
if (!std::strcmp(k->op, "lora_shrink")) *shrink = k->launch;
if (!std::strcmp(k->op, "lora_expand")) *expand = k->launch;
}
}
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<Impl> 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 });
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;
}