fix(inkling2/cuda): do not reuse a stale converted X buffer between mixed-format attention projections (#6788)

From PR #6788 by @paperniuk.

On Inkling-2-Mini-SQ-15 the attention weights of one layer are in two formats (wq_du and gate_attn
Q8_ROW; wr_du, wk_dv and wv_dv BF16). linear_multi() ran them one by one with reuse_x_bf16 = (i > 0),
which assumes the first projection already converted X into d_x_bf16; the Q8_ROW GEMV never does, so the
BF16 GEMMs read whatever an earlier GEMM had left there. The same prompt then answered differently
depending on what ran before it (prefill M = 4..16).

linear_x_cache_fmt() says which format linear() converts X into for a weight, linear_cached() reuses the
buffer only when it already holds that format; used in the linear_multi() fallback and in the M > 16 QKV
block. The pre-quantized Q8_ROW path checks cur_moe_ws and the xq_sk capacity like linear() does.

Checked here on three cards: SQ-15, prompts of 15, 19, 15 (and more) tokens, greedy, --draft off. On
origin/main the 15-token prompt answers with one token repeated (6648, 24757, 68911 ... depending on the card)
or a different text on every repeat; with this change every run gives the same right answer
(140215, 68014, 68025, 220, 16, 13 ...), 4 and 2 repeats identical.
- Tesla V100 16 GB (sm_70, the FP16 attention path), so the PR fixes Volta too, not only sm_120.
- RTX 3060 12 GB (sm_86), 9 prompts of 8..19 tokens, 4 repeats of the 15-token one.
- RTX 4090 24 GB (sm_89), 25000 and 40000 token prompts give the same tokens as origin/main.
This commit is contained in:
Niko1221 committed 2026-11-23 11:19:41 +01:00
1 parent de3cb634a0
commit fd203ddf6a
1 file changed
+60 -18
+60 -18
View File
@@ -2621,6 +2621,40 @@ struct InkCudaRunner {
}
}
// Tracks which converted representation of a shared X buffer is currently valid in d_x_bf16.
enum XCacheFmt { XC_NONE = 0, XC_BF16 = 1, XC_FP16 = 2, XC_I8 = 3 };
XCacheFmt linear_x_cache_fmt(const std::string& tname, int M, int N_override, bool exact_fp32_x) const {
const auto& ti = m.tinfo(tname);
const int N = N_override > 0 ? N_override : (int)ti.rows();
const int K = (int)ti.cols();
const int cur_cc = m.ls_active ? m.stage_cc[cur_stage] : m.sm_cc;
const bool cur_attn_fp16 = m.ls_active ? m.stage_trunk_attn_fp16[cur_stage] : m.trunk_attn_fp16;
const XCacheFmt gemm_fmt = (cur_cc >= 80) ? XC_BF16 : ((cur_cc >= 70) ? XC_FP16 : XC_NONE);
if (ti.fmt == FMT_BF16) {
const bool is_fp16 = cur_attn_fp16 && (tname.find(".attn.") != std::string::npos);
const int bf16_gemv_max = is_fp16 ? 3 : ((exact_fp32_x || cur_cc < 80) ? 16 : 7);
if (M <= bf16_gemv_max) return XC_NONE;
return is_fp16 ? XC_FP16 : gemm_fmt;
}
if (ti.fmt == FMT_F32) return XC_NONE;
if (ti.fmt == FMT_Q8ROW) {
if (M > 16 && cur_cc == 61 && (K % 4 == 0) && (N % 4 == 0) && N_override == 0) return XC_I8;
const size_t cap = (size_t)kInkDsparkInter * kInkHidden;
const size_t max_tile = (cur_cc < 70) ? (cap / 3) : cap;
if (M <= 16 || (size_t)N * K > max_tile) return XC_NONE;
return gemm_fmt;
}
return gemm_fmt;
}
void linear_cached(const std::string& tname, const float* X, float* Y, int M, XCacheFmt& x_cached, bool exact_fp32_x = false) {
const XCacheFmt need = linear_x_cache_fmt(tname, M, 0, exact_fp32_x);
const bool reuse = (need != XC_NONE && need == x_cached);
linear(tname, X, Y, M, 0, reuse, exact_fp32_x);
if (need != XC_NONE) x_cached = need;
}
// ---- Dense linear projection helper: Y[M, N] = X[M, K] @ W[N, K]^T ----
void linear_multi(const std::string* names, float* const* Ys, int count, const float* X, int M, bool exact_fp32_x = false) {
if (count <= 0) return;
@@ -2655,26 +2689,32 @@ struct InkCudaRunner {
else if (M <= 8) k_ink_gemv_multi_bf16<8, false><<<grid, 128, 0, st>>>(mp, X, M, K);
else k_ink_gemv_multi_bf16<16, false><<<grid, 128, 0, st>>>(mp, X, M, K);
}
} else if ((K % 64) == 0 && K <= kInkQDim && (cur_cc == 61 || cur_cc >= 75)) {
return;
}
if ((K % 64) == 0 && K <= kInkQDim && (cur_cc == 61 || cur_cc >= 75)) {
if (M == 1) {
k_ink_gemv_multi_q8row_m1<<<grid, 128, 0, st>>>(mp, X, K);
} else {
const int nblk = M * (K / 64);
return;
}
const int nblk = M * (K / 64);
if (cur_moe_ws && (size_t)nblk <= (size_t)cur_moe_ws->cap_M * (kInkHidden / 64)) {
k_ink_quant_q8_1<<<(nblk + 7) / 8, 256, 0, st>>>(X, cur_moe_ws->xq_sk, M, K);
if (M <= 4) k_ink_gemv_multi_q8row_preq<4><<<grid, 128, 0, st>>>(mp, cur_moe_ws->xq_sk, M, K);
else if (M <= 8) k_ink_gemv_multi_q8row_preq<8><<<grid, 128, 0, st>>>(mp, cur_moe_ws->xq_sk, M, K);
else k_ink_gemv_multi_q8row_preq<16><<<grid, 128, 0, st>>>(mp, cur_moe_ws->xq_sk, M, K);
return;
}
} else {
const int g4 = (mp.cumN[count] + 3) / 4;
if (M == 1) k_ink_gemv_multi_q8row<1><<<g4, 128, 0, st>>>(mp, X, M, K);
else if (M <= 4) k_ink_gemv_multi_q8row<4><<<g4, 128, 0, st>>>(mp, X, M, K);
else if (M <= 8) k_ink_gemv_multi_q8row<8><<<g4, 128, 0, st>>>(mp, X, M, K);
else k_ink_gemv_multi_q8row<16><<<g4, 128, 0, st>>>(mp, X, M, K);
}
const int g4 = (mp.cumN[count] + 3) / 4;
if (M == 1) k_ink_gemv_multi_q8row<1><<<g4, 128, 0, st>>>(mp, X, M, K);
else if (M <= 4) k_ink_gemv_multi_q8row<4><<<g4, 128, 0, st>>>(mp, X, M, K);
else if (M <= 8) k_ink_gemv_multi_q8row<8><<<g4, 128, 0, st>>>(mp, X, M, K);
else k_ink_gemv_multi_q8row<16><<<g4, 128, 0, st>>>(mp, X, M, K);
return;
}
for (int i = 0; i < count; ++i) linear(names[i], X, Ys[i], M, 0, i > 0, exact_fp32_x);
// Mixed-format or non-fusible projections: only reuse d_x_bf16 when a prior projection populated the same format.
XCacheFmt x_cached = XC_NONE;
for (int i = 0; i < count; ++i) linear_cached(names[i], X, Ys[i], M, x_cached, exact_fp32_x);
}
void linear(const std::string& tname, const float* X, float* Y, int M, int N_override = 0, bool reuse_x_bf16 = false, bool exact_fp32_x = false) {
@@ -2768,7 +2808,7 @@ struct InkCudaRunner {
return;
}
const int nblk = M * (K / 64);
if ((size_t)nblk <= (size_t)cur_moe_ws->cap_M * (kInkHidden / 64)) {
if (cur_moe_ws && (size_t)nblk <= (size_t)cur_moe_ws->cap_M * (kInkHidden / 64)) {
k_ink_quant_q8_1<<<(nblk + 7) / 8, 256, 0, st>>>(X, cur_moe_ws->xq_sk, M, K);
if (M <= 4) k_ink_gemv_multi_q8row_preq<4><<<grid, 128, 0, st>>>(mp, cur_moe_ws->xq_sk, M, K);
else if (M <= 8) k_ink_gemv_multi_q8row_preq<8><<<grid, 128, 0, st>>>(mp, cur_moe_ws->xq_sk, M, K);
@@ -2800,7 +2840,7 @@ struct InkCudaRunner {
if (ti.fmt == FMT_S5_K) k_ink_sk_to_bf16<FMT_S5_K><<<dg, 256, 0, st>>>(Wsub, w_dst, nt, K);
else if (ti.fmt == FMT_S6_K) k_ink_sk_to_bf16<FMT_S6_K><<<dg, 256, 0, st>>>(Wsub, w_dst, nt, K);
else k_ink_sk_to_bf16<FMT_S8_K><<<dg, 256, 0, st>>>(Wsub, w_dst, nt, K);
gemm_bf16_compat(w_dst, X, Y + n0, M, nt, K, N, n0 > 0, true);
gemm_bf16_compat(w_dst, X, Y + n0, M, nt, K, N, (n0 > 0) || reuse_x_bf16, true);
}
}
}
@@ -2939,12 +2979,14 @@ struct InkCudaRunner {
linear(p + ".attn.wk_dv.weight", in_k, d_k_raw, M, 0, false, exact_x);
linear(p + ".attn.wv_dv.weight", in_v, d_v_raw, M, 0, false, exact_x);
} else {
linear(p + ".attn.wq_du.weight", in_q, d_q, M, 0, false);
linear(p + ".attn.wr_du.weight", in_q, d_r, M, 0, true);
linear(p + ".attn.gate_attn.weight", in_q, d_gate_attn, M, 0, true);
linear(p + ".attn.attn_sink_proj.weight", in_q, d_sinks, M, 0, false);
linear(p + ".attn.wk_dv.weight", in_k, d_k_raw, M, 0, share_qkv);
linear(p + ".attn.wv_dv.weight", in_v, d_v_raw, M, 0, share_qkv);
XCacheFmt x_q = XC_NONE;
linear_cached(p + ".attn.wq_du.weight", in_q, d_q, M, x_q);
linear_cached(p + ".attn.wr_du.weight", in_q, d_r, M, x_q);
linear_cached(p + ".attn.gate_attn.weight", in_q, d_gate_attn, M, x_q);
linear_cached(p + ".attn.attn_sink_proj.weight", in_q, d_sinks, M, x_q);
XCacheFmt x_k = XC_NONE, x_v = XC_NONE;
linear_cached(p + ".attn.wk_dv.weight", in_k, d_k_raw, M, share_qkv ? x_q : x_k);
linear_cached(p + ".attn.wv_dv.weight", in_v, d_v_raw, M, share_qkv ? x_q : x_v);
}
// 4. Dynamic K/V ShortConv tap projections on global layers