diff --git a/engine/src/inkling2/cuda/ink_runner.cuh b/engine/src/inkling2/cuda/ink_runner.cuh index 18a38bd008..ab75ee27af 100644 --- a/engine/src/inkling2/cuda/ink_runner.cuh +++ b/engine/src/inkling2/cuda/ink_runner.cuh @@ -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><<>>(mp, X, M, K); else k_ink_gemv_multi_bf16<16, false><<>>(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<<>>(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><<>>(mp, cur_moe_ws->xq_sk, M, K); else if (M <= 8) k_ink_gemv_multi_q8row_preq<8><<>>(mp, cur_moe_ws->xq_sk, M, K); else k_ink_gemv_multi_q8row_preq<16><<>>(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><<>>(mp, X, M, K); - else if (M <= 4) k_ink_gemv_multi_q8row<4><<>>(mp, X, M, K); - else if (M <= 8) k_ink_gemv_multi_q8row<8><<>>(mp, X, M, K); - else k_ink_gemv_multi_q8row<16><<>>(mp, X, M, K); } + const int g4 = (mp.cumN[count] + 3) / 4; + if (M == 1) k_ink_gemv_multi_q8row<1><<>>(mp, X, M, K); + else if (M <= 4) k_ink_gemv_multi_q8row<4><<>>(mp, X, M, K); + else if (M <= 8) k_ink_gemv_multi_q8row<8><<>>(mp, X, M, K); + else k_ink_gemv_multi_q8row<16><<>>(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><<>>(mp, cur_moe_ws->xq_sk, M, K); else if (M <= 8) k_ink_gemv_multi_q8row_preq<8><<>>(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<<>>(Wsub, w_dst, nt, K); else if (ti.fmt == FMT_S6_K) k_ink_sk_to_bf16<<>>(Wsub, w_dst, nt, K); else k_ink_sk_to_bf16<<>>(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