ling32: MTP controller corrected for the context, MLA kernel grid token-fastest

- The adaptive K controller calibrates its cycle-time table once at spec_init (~60 positions) and froze it, so it could not see that a verify of N rows costs N times the attention
  at long context.  Calibration now also measures how every verify length grows with the context (N rows at positions 128 and min(max_ctx - 64, 32768), zeroed caches) and the
  cost of each K is corrected for the current position; the K = 0 probe interval grows with the cost of a probe (about 1 % of the cycles at most).  ABAB on a PRO 6000
  (essay prompts, adaptive vs off): 4K 238.0 vs 180.4 tok/s, 16K 212.2-214.4 vs 173.7, 64K 190.9 vs 158.4 (old controller 183.2), 125K 155.7 vs 142.3 (old controller 146.5).
- MLA kernel grids are launched token-fastest, so the blocks of a verify batch that read the same cache chunk run side by side and share it through L2.  Bit-identical
  (mla_latent_attn_test 256/256; golden logit dumps old vs new byte-equal at ctx 16640 and 131072; invariance 9/9 at 4400 / 131072): verify of 5 rows at 256K 2.13 -> 2.04 ms per layer.
This commit is contained in:
Niko1221 committed 2026-12-01 19:57:24 +01:00
1 parent 9cec1ce5b4
commit 7bcd97bf98
3 files changed
+43 -9

No files matched your search

+4 -4
View File
@@ -329,7 +329,7 @@ __global__ void __launch_bounds__(kThreads) mla_fast_kernel(const DevArgs a) {
constexpr int NV = SR * 4; // values per lane in the score phase
pdl_wait();
const int hb = blockIdx.x, s = blockIdx.y, t = blockIdx.z;
const int t = blockIdx.x, hb = blockIdx.y, s = blockIdx.z; // token fastest: the blocks of a verify batch that read the same chunk of the cache are neighbours in the launch order and share it through L2
const int tid = threadIdx.x, lane = tid & 31, w = tid >> 5;
const int h0 = hb * RM;
const int nkv = n_kv_of(a, t);
@@ -904,9 +904,9 @@ void fill_dev(const MlaLatentArgs& a, const Chosen& c, bool direct, bool zero_em
cudaError_t launch_main(const MlaLatentArgs& a, const Chosen& c, bool direct, bool zero_empty, float* part, float* pmd, cudaStream_t st) {
DevArgs d;
fill_dev(a, c, direct, zero_empty, part, pmd, &d);
if (c.variant == kVarTcFast || c.variant == kVarTcSplit) return launch_tc(d, a.type, c.variant == kVarTcSplit, c.rm == 32, dim3(ceil_div(a.n_head, c.rm), c.n_split, a.n_tok), st, a.use_pdl);
if (c.variant == kVarFast16) return launch_fast<16, 8>(d, a.type, dim3(ceil_div(a.n_head, 16), c.n_split, a.n_tok), st, a.use_pdl);
if (c.variant == kVarFast8) return launch_fast<8, 4>(d, a.type, dim3(ceil_div(a.n_head, 8), c.n_split, a.n_tok), st, a.use_pdl);
if (c.variant == kVarTcFast || c.variant == kVarTcSplit) return launch_tc(d, a.type, c.variant == kVarTcSplit, c.rm == 32, dim3(a.n_tok, ceil_div(a.n_head, c.rm), c.n_split), st, a.use_pdl);
if (c.variant == kVarFast16) return launch_fast<16, 8>(d, a.type, dim3(a.n_tok, ceil_div(a.n_head, 16), c.n_split), st, a.use_pdl);
if (c.variant == kVarFast8) return launch_fast<8, 4>(d, a.type, dim3(a.n_tok, ceil_div(a.n_head, 8), c.n_split), st, a.use_pdl);
const dim3 grid(a.n_head, c.n_split, a.n_tok);
if (a.R <= 128) return launch_generic<4>(d, grid, st, a.use_pdl);
if (a.R <= 256) return launch_generic<8>(d, grid, st, a.use_pdl);
@@ -75,7 +75,7 @@ __global__ void __launch_bounds__(kThreads, (SPLIT || MT > 1 ? 1 : 2)) mla_tc_ke
int* PreRow = Pnext + 3; // [kPreCap] pre-resolved rows of the whole block
pdl_wait();
const int hb = blockIdx.x, s = blockIdx.y, t = blockIdx.z;
const int t = blockIdx.x, hb = blockIdx.y, s = blockIdx.z; // token fastest (see mla_fast_kernel)
const int tid = threadIdx.x, lane = tid & 31, w = tid >> 5;
const int g = lane >> 2, tq = lane & 3, kg = w & 3, kh = w >> 2;
const int h0 = hb * RO;
+38 -4
View File
@@ -207,6 +207,11 @@ struct Ling32Engine::StageCtx {
if (n < kCalSkip) return;
m_ms[N] = (n == kCalSkip) ? ms : (m_ms[N] * (n - kCalSkip) + ms) / (n - kCalSkip + 1);
}
// L32-LONG: the cycle times above are measured at the context of the calibration (about 60 positions). The attention part of a verify of N rows grows with the context (N times the work of one
// row, measured: 256K, 5 rows = 4.75 x one row), so the table is corrected for the current position: m_slope[N] = ms per cached position of a verify of N rows, measured at two contexts when the
// controller is calibrated. Zero (no correction) when the reservation is below 4096 positions or the calibration is off.
double m_slope[10] = {};
int m_cal_ctx = 64;
int m_force_k = -1; // calibration: the K every cycle uses
int mtp_select_k() {
if (m_force_k >= 0) return std::min(m_force_k, sB);
@@ -215,17 +220,21 @@ struct Ling32Engine::StageCtx {
for (int K = 0; K <= sB; ++K)
if (m_ms_n[K + 1] < kCalNeed && !(K == 0 && sB < 1)) return K; // explore: every verify length gets its steady-state samples
const bool cal = sp.mtp_calibrate && m_ms_n[1] >= kCalNeed && m_ms[1] > 0;
double best = 1.0, prod = 1.0, toks = 1.0;
const double dctx = std::max(0, E.pos_ - m_cal_ctx); // positions beyond the calibration context
const double base1 = cal ? m_ms[1] + m_slope[1] * dctx : 1.0;
double best = 1.0, prod = 1.0, toks = 1.0, cost1 = 1.0;
int bk = 0;
for (int K = 1; K <= sB; ++K) {
prod *= m_alpha[K - 1];
toks += prod;
const double cost = cal && m_ms[K + 1] > 0 ? m_ms[K + 1] / m_ms[1] : sp.mtp_verify_cost[std::min(K + 1, 8)] + K * sp.mtp_step_cost;
const double cost = cal && m_ms[K + 1] > 0 ? (m_ms[K + 1] + m_slope[K + 1] * dctx) / base1 : sp.mtp_verify_cost[std::min(K + 1, 8)] + K * sp.mtp_step_cost;
if (K == 1) cost1 = cost;
const double sp_up = toks / cost;
if (sp_up > best) { best = sp_up; bk = K; }
}
if (bk == 0) { // plain decode steps carry no acceptance information: probe now and then
if (++m_probe >= 12) { m_probe = 0; return 1; }
if (bk == 0) { // plain decode steps carry no acceptance information: probe now and then, rarely enough that the probes (cost1 - 1 extra plain steps each) cost at most ~1 %
const int every = std::max(12, static_cast<int>(std::ceil(100.0 * std::max(0.0, cost1 - 1.0))));
if (++m_probe >= every) { m_probe = 0; return 1; }
} else m_probe = 0;
return bk;
}
@@ -1226,6 +1235,31 @@ bool Ling32Engine::spec_calibrate(std::string& err) {
if (!ok) { I.m_force_k = -1; return false; }
}
I.m_force_k = -1;
// the growth of a verify with the context: N rows at a small and at a large position (zeroed caches: cost only), ms per cached position
for (int n = 0; n < 10; ++n) I.m_slope[n] = 0.0;
I.m_cal_ctx = 64;
const int c1 = 128, c2 = std::min(opt.max_ctx - 64, 32768);
if (I.sp.mtp_calibrate && c2 >= 4096) {
std::vector<int> ids(16, 1000);
auto time_n = [&](int N, int ctx, double& ms) {
double best = 1e30;
for (int rep = 0; rep < 4; ++rep) {
fake_context(ctx);
std::vector<float> lg;
const auto t0 = std::chrono::steady_clock::now();
if (!forward(ids.data(), N, N, &lg, err)) return false;
const double d = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
if (rep > 0) best = std::min(best, d); // the first run captures the graph
}
ms = best;
return true;
};
for (int n = 1; n <= I.sB + 1 && n <= 9; ++n) {
double a1 = 0, a2 = 0;
if (!time_n(n, c1, a1) || !time_n(n, c2, a2)) return false;
I.m_slope[n] = std::max(0.0, (a2 - a1) / static_cast<double>(c2 - c1));
}
}
reset();
sstats_ = SpecStats{};
for (int j = 0; j < 9; ++j) I.m_alpha[j] = j == 0 ? 0.85f : j == 1 ? 0.65f : j == 2 ? 0.50f : j == 3 ? 0.40f : 0.30f;