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.
This commit is contained in:
baKon committed 2026-10-16 20:52:09 -05:00
1 parent 979b54e091
commit 51293b269c
6 files changed
+567 -14

No files matched your search

+86 -1
View File
@@ -1,5 +1,5 @@
/* ref_host.cpp -- ref_host.h's functions: the rules and roundings of r4d_pair_argmax_f32.hip,
* r4d_norm_join_bf16.hip and r4d_expert_scale_bf16.hip, one element at a time. */
* r4d_norm_join_bf16.hip, r4d_expert_scale_bf16.hip and r4d_lora_bf16.hip, one element at a time. */
#include "ref_host.h"
#include "rad_plugin.h"
@@ -200,3 +200,88 @@ int host_expert_scale(const RadArgs* a) {
}
return RAD_OK;
}
/* lora_shrink / lora_expand: libref's arithmetic (libref/ref_lora.cpp) over the dense-last-axis
* operands the test hands over -- k ascending and j ascending, one f32 accumulator, every product
* and sum rounded. Expand's kernel sums in the same order, so its bytes are these; shrink's sums
* k in 32 lane strides and a butterfly, so its are these to a tolerance. A row whose slot is
* outside [0, S) is not touched. */
static float host_act(uint32_t dt, const void* p, int64_t i) {
switch (dt) {
case RAD_BF16: return rad_bf16_to_f32(((const uint16_t*)p)[i]);
case RAD_F16: return rad_f16_to_f32(((const uint16_t*)p)[i]);
case RAD_F32: return ((const float*)p)[i];
case RAD_F8E4M3: return rad_fp8e4m3_to_f32(((const uint8_t*)p)[i]);
case RAD_F8E5M2: return rad_fp8e5m2_to_f32(((const uint8_t*)p)[i]);
case RAD_I8: return (float)((const int8_t*)p)[i];
default: return 0.0f;
}
}
int host_lora_shrink(const RadArgs* a) {
if (!a || a->n_t < 5) return RAD_E_INVAL;
const RadTensor *X = rad_arg_in(a, 0), *XS = rad_arg_in(a, 1), *SL = rad_arg_in(a, 2);
const RadTensor *LA = rad_arg_in(a, 3), *T = rad_arg_in(a, 4);
if (!X || !SL || !LA || !T || X->rank != 2 || T->rank != 2) return RAD_E_INVAL;
if (XS && XS->dtype == RAD_DT_INVALID) XS = nullptr;
const int64_t K = rad_args_geti_or(a, "K", 0), R = rad_args_geti_or(a, "R", 0);
const int64_t S = rad_args_geti_or(a, "S", 0);
const int64_t M = T->shape[0];
/* libref's grid_block: the power of two whose ceiling division gives the stream's groups. */
const int64_t groups = XS ? XS->shape[1] : 1;
int64_t group = K;
if (groups > 1) {
const int64_t lo = (K + groups - 1) / groups;
int64_t b = 1;
while (b < lo) b <<= 1;
group = (K + b - 1) / b == groups ? b : lo;
}
for (int64_t m = 0; m < M; ++m) {
const int32_t s = ((const int32_t*)SL->data)[m * SL->stride[0]];
if (s < 0 || s >= S) continue;
for (int64_t j = 0; j < R; ++j) {
const uint16_t* ar = (const uint16_t*)LA->data + (s * R + j) * K;
float acc = 0.0f;
for (int64_t k = 0; k < K; ++k) {
float x = host_act(X->dtype, X->data, m * X->stride[0] + k);
if (XS) x = x * ((const float*)XS->data)[m * XS->stride[0] + k / group];
acc += x * rad_bf16_to_f32(ar[k]);
}
((float*)T->data)[m * T->stride[0] + j] = acc;
}
}
return RAD_OK;
}
int host_lora_expand(const RadArgs* a) {
if (!a || a->n_t < 5 || !rad_args_have(a, 5)) return RAD_E_INVAL;
const RadTensor *T = rad_arg_in(a, 0), *SL = rad_arg_in(a, 1), *LB = rad_arg_in(a, 2);
const RadTensor *SC = rad_arg_in(a, 3), *Y = rad_arg_in(a, 4);
if (T->rank != 2 || Y->rank != 2) return RAD_E_INVAL;
const int64_t N = rad_args_geti_or(a, "N", 0), R = rad_args_geti_or(a, "R", 0);
const int64_t S = rad_args_geti_or(a, "S", 0);
const int64_t M = Y->shape[0];
for (int64_t m = 0; m < M; ++m) {
const int32_t s = ((const int32_t*)SL->data)[m * SL->stride[0]];
if (s < 0 || s >= S) continue;
const float* tr = (const float*)T->data + m * T->stride[0];
for (int64_t n = 0; n < N; ++n) {
const uint16_t* br = (const uint16_t*)LB->data + (s * N + n) * R;
float acc = 0.0f;
for (int64_t j = 0; j < R; ++j) acc += tr[j] * rad_bf16_to_f32(br[j]);
const float d = ((const float*)SC->data)[s * N + n] * acc;
const int64_t o = m * Y->stride[0] + n;
if (Y->dtype == RAD_BF16) {
uint16_t* y = (uint16_t*)Y->data + o;
*y = rad_f32_to_bf16(rad_bf16_to_f32(*y) + d);
} else if (Y->dtype == RAD_F16) {
uint16_t* y = (uint16_t*)Y->data + o;
*y = rad_f32_to_f16(rad_f16_to_f32(*y) + d);
} else {
float* y = (float*)Y->data + o;
*y = *y + d;
}
}
}
return RAD_OK;
}