Archived
libr4d: the gdn conv kernels read an f32 weight -- a GGUF ships ssm_conv1d in f32
llama.cpp keeps a Gated DeltaNet's ssm_conv1d unquantised, in f32, and a container converted from a GGUF holds it so. The three kernels that read the convolution weight, gdn_conv_prep, gdn_conv_update and the convolution gdn_conv_recurrent_update folds in, read bf16, so their shims refuse an f32 weight. The weight's element type is now a template parameter of all three. r4d_gdn_conv_tap.h, which already holds the arithmetic the fold shares with the standalone path, gives the vector a channel's taps are loaded as (ushort4 or float4) and the widening of one tap, exact for either type. The shims take a bf16 or an f32 weight and pick the instance by its dtype; any other dtype is still refused. The raw entry points keep their signatures. r4d.h gets an f32 twin of each standalone one, r4d_gdn_conv_prep_w4_h128_bf16_f32wgt and r4d_gdn_conv_update_w4_h128_bf16_f32wgt, rather than a flag, so no caller of the bf16 ones changes. The fold has no raw entry point; its CV parameter now also says the weight's type (1 bf16, 2 f32), which leaves every existing instance's template arguments and name as they were. That is twelve new conv instances and sixteen new fold ones. The bf16 instances compile to the same code. Built before and after with the build's flags, all 116 kernels of the two units have the same instructions, resource counts and metadata; only the conv kernels' names change with the added template argument. The bf16 reads keep their shape: widening gdn_conv_update's ushort4 to a float4 before use reorders its loads. The f32 conv instances use no scratch, conv_prep 56 to 57 VGPRs (53 at bf16) and conv_update 108 (96 at bf16, 12 waves per SIMD instead of 16); each f32 fold instance has exactly its bf16 twin's counts. r4d_selftest gets three cases for the card. gdn_conv_prep and gdn_conv_update over an f32 weight holding bf16's values must write the bytes they write over the bf16 weight: q, k and v, g and beta at prefill, and the conv state. The sequences are uneven; at prefill two are seeded from their cache, one of them over two chunks, and a one-token one is not. Outputs are poisoned, the cache must move and an f16 weight must be refused. The fold over the f32 weight must match the pair over the bf16 one, byte for byte as the fold already does at bf16. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d73abc28d5
commit
64c3dc9893
8 files changed
+323
-52
No files matched your search
+179
-4
@@ -4146,9 +4146,10 @@ static int64_t i8_code_misses(const uint16_t* x, int64_t x_ld, const int8_t* q,
|
||||
*
|
||||
* The key heads' conv state must have moved, so the check is sensitive. The anchor form runs in a
|
||||
* child process, as in gdn_recurrent_anchor. `i8` runs both at int8 codes and also holds the pair's
|
||||
* codes to quant_act_i8g over its `o`. */
|
||||
* codes to quant_act_i8g over its `o`. `f32w` hands the fold the conv weight in f32, as a GGUF
|
||||
* stores it, and leaves the pair's in bf16: the values are bf16's, so the bytes must not change. */
|
||||
static void gdn_conv_fold_run(const Plugin& pl, const char* nm, const char* form,
|
||||
bool i8 = false) {
|
||||
bool i8 = false, bool f32w = false) {
|
||||
const RadKernelInfo* rf = find(pl, "gdn_conv_recurrent_update_k128_v128_bf16");
|
||||
const RadKernelInfo* rc = find(pl, "gdn_conv_update_w4_h128_bf16");
|
||||
const RadKernelInfo* rr = find(pl, "gdn_recurrent_update_k128_v128_bf16_fp32state");
|
||||
@@ -4175,7 +4176,8 @@ static void gdn_conv_fold_run(const Plugin& pl, const char* nm, const char* form
|
||||
const std::vector<uint16_t> hw = bf((size_t)(conv_dim * W), 0.5f);
|
||||
const std::vector<uint16_t> hcs = bf((size_t)(CSLOTS * conv_dim * SL), 1.0f);
|
||||
std::vector<float> hst((size_t)(SSLOTS * H * V * K)), halog((size_t)H), hdtb((size_t)H),
|
||||
hnw((size_t)V);
|
||||
hnw((size_t)V), hwf(hw.size());
|
||||
for (size_t i = 0; i < hw.size(); ++i) hwf[i] = h_bf16(hw[i]);
|
||||
for (auto& e : hst) e = 0.1f * rnd();
|
||||
for (int64_t h = 0; h < H; ++h) { halog[(size_t)h] = -2.0f + 0.25f * (float)h;
|
||||
hdtb[(size_t)h] = -1.0f + 0.5f * (float)h; }
|
||||
@@ -4196,6 +4198,7 @@ static void gdn_conv_fold_run(const Plugin& pl, const char* nm, const char* form
|
||||
void* d_in = dev(nullptr, in_b);
|
||||
void* d_ab = dev(nullptr, (size_t)(Td * 2 * H) * 2);
|
||||
void* d_w = dev(hw.data(), hw.size() * 2);
|
||||
void* d_wf = dev(hwf.data(), hwf.size() * 4);
|
||||
void* d_alog = dev(halog.data(), halog.size() * 4);
|
||||
void* d_dtb = dev(hdtb.data(), hdtb.size() * 4);
|
||||
void* d_nw = dev(hnw.data(), hnw.size() * 4);
|
||||
@@ -4224,6 +4227,7 @@ static void gdn_conv_fold_run(const Plugin& pl, const char* nm, const char* form
|
||||
const RadTensor ta = view(d_ab, RAD_BF16, Td, H, 2 * H, 0);
|
||||
const RadTensor tb = view(d_ab, RAD_BF16, Td, H, 2 * H, H);
|
||||
const RadTensor tw = T(d_w, RAD_BF16, { conv_dim, W });
|
||||
const RadTensor tfw = f32w ? T(d_wf, RAD_F32, { conv_dim, W }) : tw;
|
||||
const RadTensor tal = T(d_alog, RAD_F32, { H }), tdt = T(d_dtb, RAD_F32, { H });
|
||||
const RadTensor tnw = T(d_nw, RAD_F32, { V });
|
||||
const RadTensor tci = T(d_cidx, RAD_I32, { N }), tsi = T(d_sidx, RAD_I32, { N, COLS });
|
||||
@@ -4269,7 +4273,7 @@ static void gdn_conv_fold_run(const Plugin& pl, const char* nm, const char* form
|
||||
e = run(rr, { tq, tk, tv, ta, tb, tal, tdt, st(0), tsi, tna, tcu, tz, tnw, to(0),
|
||||
toq(0), tos(0) }, pr);
|
||||
if (e != RAD_OK) { why = "pair rc=" + std::to_string(e); break; }
|
||||
e = run(rf, { tx, tw, none, cs(1), tci, ta, tb, tal, tdt, st(1), tsi, tna, tcu, tz, tnw,
|
||||
e = run(rf, { tx, tfw, none, cs(1), tci, ta, tb, tal, tdt, st(1), tsi, tna, tcu, tz, tnw,
|
||||
to(1), toq(1), tos(1) }, pf);
|
||||
if (e != RAD_OK) { why = "fold rc=" + std::to_string(e); break; }
|
||||
|
||||
@@ -4316,6 +4320,8 @@ static void gdn_conv_fold(const Plugin& pl) {
|
||||
const char* row = "gdn_conv_recurrent_update_k128_v128_bf16";
|
||||
const char* nmp = "gdn_conv_recurrent_update is byte-identical to the pair, per_candidate";
|
||||
const char* nma = "gdn_conv_recurrent_update is byte-identical to the pair, anchor";
|
||||
const char* nmf = "gdn_conv_recurrent_update over an f32 conv weight is byte-identical to the "
|
||||
"pair over its bf16 twin, per_candidate";
|
||||
if (sel(row)) {
|
||||
gdn_conv_fold_run(pl, nmp, "per_candidate");
|
||||
gdn_conv_fold_run(pl, "gdn_conv_recurrent_update is byte-identical to the pair at int8 "
|
||||
@@ -4323,6 +4329,174 @@ static void gdn_conv_fold(const Plugin& pl) {
|
||||
"per_candidate", true);
|
||||
}
|
||||
if (sel(row) || sel(nma)) gdn_conv_fold_run(pl, nma, "anchor");
|
||||
if (sel(row) || sel(nmf)) gdn_conv_fold_run(pl, nmf, "per_candidate", false, true);
|
||||
}
|
||||
|
||||
/* gdn_conv_prep and gdn_conv_update over an f32 weight, as a GGUF stores ssm_conv1d, against the
|
||||
* same weight in bf16.
|
||||
*
|
||||
* The weight's values are bf16's, so both forms widen to the same taps and every byte a row writes
|
||||
* must match: q, k and v, g and beta at prefill, and the conv state. Both runs start from identical
|
||||
* copies of the cache and from outputs poisoned with 0xFF, so an element the f32 instance never
|
||||
* writes is caught even where the bf16 one misses it as well. Prefill has two sequences seeded
|
||||
* from their cached history, one over two chunks, and a one-token sequence without; decode has
|
||||
* uneven windows and read offsets, sequence 1 at slot 0. The cache must have moved, and an f16
|
||||
* weight is refused. */
|
||||
static void gdn_conv_f32_weight(const Plugin& pl) {
|
||||
const struct { const char* row; const char* nm; bool prep; } forms[] = {
|
||||
{ "gdn_conv_prep_w4_h128_bf16",
|
||||
"gdn_conv_prep over an f32 weight is byte-identical to its bf16 twin", true },
|
||||
{ "gdn_conv_update_w4_h128_bf16",
|
||||
"gdn_conv_update over an f32 weight is byte-identical to its bf16 twin", false },
|
||||
};
|
||||
for (const auto& f : forms) {
|
||||
if (!sel(f.row) && !sel(f.nm)) continue;
|
||||
const RadKernelInfo* r = find(pl, f.row);
|
||||
if (!r) { ok(f.nm, false, "row absent"); continue; }
|
||||
|
||||
const int64_t K = 128, V = 128, H = 4, Hg = 2, N = 3, W = 4, MQ = 4, CSLOTS = 3;
|
||||
const int64_t xdim = 2 * Hg * K + H * V;
|
||||
/* Prefill reads W - 1 taps of history from a W-deep slot; decode rolls a W - 2 + MQ one. */
|
||||
const int64_t SL = f.prep ? W : W - 2 + MQ;
|
||||
const int32_t plen[N] = { 70, 1, 57 }, dlen[N] = { 4, 1, 3 };
|
||||
const int32_t slot[N] = { 2, 0, 1 }, nacc[N] = { 2, 1, 3 };
|
||||
const uint8_t init[N] = { 1, 0, 1 };
|
||||
int64_t Tn = 0;
|
||||
std::vector<int32_t> cu(1, 0);
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
Tn += f.prep ? plen[n] : dlen[n];
|
||||
cu.push_back((int32_t)Tn);
|
||||
}
|
||||
|
||||
uint32_t seed = 0x2f37c0d5u;
|
||||
auto rnd = [&seed]() {
|
||||
seed = st_mix32(seed + 0x9e3779b9u);
|
||||
return (float)(seed & 0xFFFFu) * (1.0f / 32768.0f) - 1.0f;
|
||||
};
|
||||
auto bf = [&](size_t n, float s) {
|
||||
std::vector<uint16_t> v(n);
|
||||
for (auto& e : v) e = h_to_bf16(s * rnd());
|
||||
return v;
|
||||
};
|
||||
const std::vector<uint16_t> hw = bf((size_t)(xdim * W), 0.5f), hb = bf((size_t)xdim, 0.25f);
|
||||
const std::vector<uint16_t> hx = bf((size_t)(Tn * xdim), 1.0f);
|
||||
const std::vector<uint16_t> hab = bf((size_t)(Tn * 2 * H), 1.0f);
|
||||
const std::vector<uint16_t> hcs = bf((size_t)(CSLOTS * xdim * SL), 1.0f);
|
||||
std::vector<float> hwf(hw.size()), halog((size_t)H), hdtb((size_t)H);
|
||||
for (size_t i = 0; i < hw.size(); ++i) hwf[i] = h_bf16(hw[i]);
|
||||
for (int64_t h = 0; h < H; ++h) { halog[(size_t)h] = -2.0f + 0.25f * (float)h;
|
||||
hdtb[(size_t)h] = -1.0f + 0.5f * (float)h; }
|
||||
|
||||
auto dev = [](const void* h, size_t bytes) {
|
||||
void* d = dalloc(bytes);
|
||||
if (d && h) st_memcpy(d, h, bytes, hipMemcpyHostToDevice);
|
||||
return d;
|
||||
};
|
||||
const size_t cs_b = hcs.size() * 2, qk_b = (size_t)(Tn * Hg * K) * 2,
|
||||
v_b = (size_t)(Tn * H * V) * 2, g_b = (size_t)(Tn * H) * 4;
|
||||
void* d_x = dev(hx.data(), hx.size() * 2);
|
||||
void* d_ab = dev(hab.data(), hab.size() * 2);
|
||||
void* d_b = dev(hb.data(), hb.size() * 2);
|
||||
void* d_w[2] = { dev(hw.data(), hw.size() * 2), dev(hwf.data(), hwf.size() * 4) };
|
||||
void* d_alog = dev(halog.data(), halog.size() * 4);
|
||||
void* d_dtb = dev(hdtb.data(), hdtb.size() * 4);
|
||||
void* d_slot = dev(slot, sizeof(slot));
|
||||
void* d_nacc = dev(nacc, sizeof(nacc));
|
||||
void* d_init = dev(init, sizeof(init));
|
||||
void* d_cu = dev(cu.data(), cu.size() * 4);
|
||||
void* d_cs[2]; void* d_q[2]; void* d_k[2]; void* d_v[2]; void* d_g[2]; void* d_beta[2];
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
d_cs[i] = dev(hcs.data(), cs_b);
|
||||
d_q[i] = dev(nullptr, qk_b); d_k[i] = dev(nullptr, qk_b); d_v[i] = dev(nullptr, v_b);
|
||||
d_g[i] = dev(nullptr, g_b); d_beta[i] = dev(nullptr, g_b);
|
||||
}
|
||||
if (!d_x || !d_cu || !d_cs[1] || !d_beta[1]) { ok(f.nm, false, "hipMalloc"); continue; }
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
st_memset(d_q[i], 0xFF, qk_b); st_memset(d_k[i], 0xFF, qk_b);
|
||||
st_memset(d_v[i], 0xFF, v_b); st_memset(d_g[i], 0xFF, g_b);
|
||||
st_memset(d_beta[i], 0xFF, g_b);
|
||||
}
|
||||
|
||||
/* a and b_gate are column views of one [Tn, 2H] row, as the block lays them out. */
|
||||
auto view = [](void* base, int64_t rows, int64_t cols, int64_t pitch, int64_t col0) {
|
||||
RadTensor t = T((char*)base + (size_t)col0 * 2, RAD_BF16, { rows, cols });
|
||||
t.stride[0] = pitch;
|
||||
return t;
|
||||
};
|
||||
auto ops = [&](int i, uint32_t wdt) {
|
||||
const RadTensor tx = T(d_x, RAD_BF16, { Tn, xdim }), tb = T(d_b, RAD_BF16, { xdim });
|
||||
const RadTensor tw = T(d_w[i], wdt, { xdim, W });
|
||||
const RadTensor tcs = T(d_cs[i], RAD_BF16, { CSLOTS, xdim, SL });
|
||||
const RadTensor tcu = T(d_cu, RAD_I32, { N + 1 }), tsl = T(d_slot, RAD_I32, { N });
|
||||
const RadTensor tq = T(d_q[i], RAD_BF16, { Tn, Hg, K }),
|
||||
tk = T(d_k[i], RAD_BF16, { Tn, Hg, K }),
|
||||
tv = T(d_v[i], RAD_BF16, { Tn, H, V });
|
||||
if (!f.prep)
|
||||
return std::vector<RadTensor>{ tx, tw, tb, tcs, tsl, T(d_nacc, RAD_I32, { N }), tcu,
|
||||
tq, tk, tv };
|
||||
return std::vector<RadTensor>{
|
||||
tx, tw, tb, T(d_alog, RAD_F32, { H }), T(d_dtb, RAD_F32, { H }), tcs, tcu,
|
||||
view(d_ab, Tn, H, 2 * H, 0), view(d_ab, Tn, H, 2 * H, H), tsl,
|
||||
T(d_init, RAD_U8, { N }), tq, tk, tv, T(d_g[i], RAD_F32, { Tn, H }),
|
||||
T(d_beta[i], RAD_F32, { Tn, H }) };
|
||||
};
|
||||
std::vector<RadParam> ps = f.prep
|
||||
? std::vector<RadParam>{ PI("M", Tn), PI("head_k", K), PI("head_v", V),
|
||||
PI("chunk", 64), PI("conv_width", W) }
|
||||
: std::vector<RadParam>{ PI("q_len", MQ), PI("head_k", K), PI("head_v", V),
|
||||
PI("conv_width", W) };
|
||||
auto run = [&](std::vector<RadTensor> ts) {
|
||||
RadArgs a; std::memset(&a, 0, sizeof(a));
|
||||
a.t = ts.data(); a.n_t = (int)ts.size(); a.p = ps.data(); a.n_p = (int)ps.size();
|
||||
a.world_size = 1;
|
||||
const int rc_ = r->launch(&a, (RadStream) nullptr);
|
||||
return rc_ != RAD_OK ? rc_ : (st_stream_sync(nullptr) != hipSuccess ? -1000 : RAD_OK);
|
||||
};
|
||||
auto fetch = [](void* d, size_t b) {
|
||||
std::vector<uint8_t> h(b);
|
||||
st_memcpy(h.data(), d, b, hipMemcpyDeviceToHost);
|
||||
return h;
|
||||
};
|
||||
|
||||
std::string why;
|
||||
for (int i = 0; i < 2 && why.empty(); ++i) {
|
||||
const int e = run(ops(i, i ? RAD_F32 : RAD_BF16));
|
||||
if (e != RAD_OK) why = std::string(i ? "f32" : "bf16") + " rc=" + std::to_string(e);
|
||||
}
|
||||
struct Out { const char* what; void* a; void* b; size_t n; size_t es; };
|
||||
std::vector<Out> outs = { { "q", d_q[0], d_q[1], qk_b, 2 },
|
||||
{ "k", d_k[0], d_k[1], qk_b, 2 },
|
||||
{ "v", d_v[0], d_v[1], v_b, 2 },
|
||||
{ "conv_state", d_cs[0], d_cs[1], cs_b, 2 } };
|
||||
if (f.prep) {
|
||||
outs.push_back({ "g", d_g[0], d_g[1], g_b, 4 });
|
||||
outs.push_back({ "beta", d_beta[0], d_beta[1], g_b, 4 });
|
||||
}
|
||||
for (const Out& o : outs) {
|
||||
if (!why.empty()) break;
|
||||
const std::vector<uint8_t> A = fetch(o.a, o.n), B = fetch(o.b, o.n);
|
||||
size_t diff = 0, poisoned = 0;
|
||||
for (size_t j = 0; j < o.n; ++j) diff += A[j] != B[j];
|
||||
for (size_t j = 0; j < o.n; j += o.es) {
|
||||
size_t ff = 0;
|
||||
for (size_t e = 0; e < o.es; ++e) ff += B[j + e] == 0xFF;
|
||||
poisoned += ff == o.es;
|
||||
}
|
||||
if (diff)
|
||||
why = std::string(o.what) + " differs in " + std::to_string(diff) + " of " +
|
||||
std::to_string(o.n) + " bytes";
|
||||
else if (poisoned)
|
||||
why = std::string(o.what) + ": " + std::to_string(poisoned) + " elements unwritten";
|
||||
}
|
||||
if (why.empty() && fetch(d_cs[1], cs_b) == std::vector<uint8_t>(
|
||||
(const uint8_t*)hcs.data(), (const uint8_t*)hcs.data() + cs_b))
|
||||
why = "the conv state never moved";
|
||||
if (why.empty()) {
|
||||
const int e = run(ops(1, RAD_F16));
|
||||
if (e != RAD_E_DTYPE) why = "an f16 weight gave rc=" + std::to_string(e);
|
||||
}
|
||||
ok(f.nm, why.empty(), why);
|
||||
}
|
||||
}
|
||||
|
||||
/* The gated norm's gate as a column slice, which one pitch cannot describe.
|
||||
@@ -29534,6 +29708,7 @@ static void device_phase(const Plugin& pl) {
|
||||
}
|
||||
gdn_recurrent_anchor(pl);
|
||||
gdn_conv_fold(pl);
|
||||
gdn_conv_f32_weight(pl);
|
||||
if (sel("gdn_gated_rmsnorm_h128_bf16")) {
|
||||
const RadKernelInfo* r = find(pl, "gdn_gated_rmsnorm_h128_bf16");
|
||||
if (!r) ok("gdn_gated_rmsnorm_h128_bf16", false, "row absent");
|
||||
|
||||
Reference in new issue
Block a user