sm120 ATTN_DAILY for the MTP draft step: opt-in LELEKA_SM120_ATTN_DAILY_MTP=1 (default off, not run on the GPU yet)

The draft step attends over the 32,768-cell MTP window (512 chunks, one query): with the opt-in, the Original chunk kernel and chain_qsa_decode_merge_n of that
step become sm120_attn_daily.ptx's leleka_qsa_decode_chunk_daily / leleka_qsa_decode_merge_v2 (module slot 22 after the chain slot; the window_ids pre-launch
keys on the new chunk entry too). sm120_probe `attn512` byte-compares the pair against the Original on the draft shape and times them; it has not been run
yet (no GPU slot), so the switch stays default off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-29 03:04:43 +01:00
co-authored by Claude Sonnet 5.5
parent 7636ec5d91
commit d6aee34af4
4 changed files with 111 additions and 9 deletions
@@ -662,12 +662,18 @@ impl<'ctx> OriginalMtpFullForward<'ctx> {
for (p, e) in &symbols::ASSETS[..symbols::CHAIN_ASSET] {
o.modules.push(Rc::new(FM::load(p, e)?));
}
if chain_mtp_assets() {
if chain_mtp_assets() || daily_mtp() {
// chain_window.ptx: loaded only when selected, and only if it is the sealed one
crate::original_verify::chain::CHAIN.sealed()?;
let (p, e) = &symbols::ASSETS[symbols::CHAIN_ASSET];
o.modules.push(Rc::new(FM::load(p, e)?));
}
if daily_mtp() {
// sm120_attn_daily.ptx: the draft step's attention (opt-in, LELEKA_SM120_ATTN_DAILY_MTP=1); slot 22 follows the chain slot
crate::original_verify::sm120::ATTN_DAILY.sealed()?;
let (p, e) = &symbols::ASSETS[symbols::DAILY_ASSET];
o.modules.push(Rc::new(FM::load(p, e)?));
}
Ok(())
})();
let pop = g.finish();
@@ -865,6 +871,13 @@ fn push(
});
Ok(())
}
/// `LELEKA_SM120_ATTN_DAILY_MTP=1` (opt-in, default off, untested on the real model): the MTP draft step's attention chunk and merge are upstream's
/// `attn_chunk_kernel_pre75` / `attn_merge_v2_kernel` (sm120_attn_daily.cu, byte-identical to the Original's by the probe) instead of the Original's chunk
/// kernel and `chain_qsa_decode_merge_n`. sm_120 only.
fn daily_mtp() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var("LELEKA_SM120_ATTN_DAILY_MTP").is_ok_and(|v| v == "1"))
}
/// `LELEKA_CHAIN` selects a group that launches from the chain module inside the MTP draft program.
fn chain_mtp_assets() -> bool {
let f = crate::original_verify::chain::flags();
@@ -1014,10 +1027,11 @@ impl OriginalMtpFullForward<'_> {
let (pa, pm, pl, ident) = o.attention(p.ring)?;
let chain_mtp = crate::original_verify::chain::flags().mtp;
// the Original's chunk kernel in both modes (the load-hoisted variant measured no faster); only the merge changes
let daily = daily_mtp();
push(
&mut v,
19,
0,
if daily { symbols::DAILY_ASSET } else { 19 },
if daily { symbols::DAILY_CHUNK } else { 0 },
[CHUNKS as u32, 2, 1],
[256, 1, 1],
0,
@@ -1049,13 +1063,24 @@ impl OriginalMtpFullForward<'_> {
F::U64(attn),
F::I64(SCRATCH_FLOATS as i64),
];
if chain_mtp {
if daily {
// merge_v2 takes the step words after n_chunks: (acc, m, l, n_chunks, steps, attn, stride)
merge_args = vec![
F::U64(pa),
F::U64(pm),
F::U64(pl),
F::I32(CHUNKS as i32),
F::U64(p.step),
F::U64(attn),
F::I64(SCRATCH_FLOATS as i64),
];
} else if chain_mtp {
merge_args.push(F::U64(p.step)); // the row's step words: n_ids bounds the merge to the chunks that hold cells
}
push(
&mut v,
if chain_mtp { symbols::CHAIN_ASSET } else { 19 },
if chain_mtp { symbols::CHAIN_QSA_MERGE_N } else { 1 },
if daily { symbols::DAILY_ASSET } else if chain_mtp { symbols::CHAIN_ASSET } else { 19 },
if daily { symbols::DAILY_MERGE_V2 } else if chain_mtp { symbols::CHAIN_QSA_MERGE_N } else { 1 },
[24, 1, 1],
[256, 1, 1],
0,
@@ -2433,7 +2458,7 @@ impl OriginalMtpForwardProgram<'_> {
if command.asset == 16 && command.index == 0 {
self.tail.modules[13].copy2d(&self.split, stream)?;
}
if command.asset == 19 && command.index == 0 {
if (command.asset == 19 || command.asset == symbols::DAILY_ASSET) && command.index == 0 {
self.ring.physical.modules[1]
.as_ref()
.ok_or(Error::ContextPoisoned)?
@@ -426,9 +426,19 @@ pub(super) const ASSETS: &[(&[u8], &[&CStr])] = &[
c"chain_row_top_prob_kernel",
],
),
// 22: sm120_attn_daily.ptx (claude/decode-sm120-switches), loaded only for the opt-in `LELEKA_SM120_ATTN_DAILY_MTP=1` (`DAILY_ASSET`); checked against its pin
// (`original_verify::sm120::ATTN_DAILY`) before it is handed to the driver.
(
concat!(include_str!("sm120_attn_daily.ptx"), "").as_bytes(),
&[c"leleka_qsa_decode_chunk_daily", c"leleka_qsa_decode_merge_v2"],
),
];
/// Index of the chain module in `ASSETS` (absent from the loaded modules unless `LELEKA_CHAIN` selects `mtp` or `head`).
pub(super) const CHAIN_ASSET: usize = 21;
/// Index of the sm_120 attention module (loaded only with `LELEKA_SM120_ATTN_DAILY_MTP=1`; it brings the chain slot with it so the indices stay fixed).
pub(super) const DAILY_ASSET: usize = 22;
pub(super) const DAILY_CHUNK: usize = 0;
pub(super) const DAILY_MERGE_V2: usize = 1;
pub(super) const CHAIN_QSA_CHUNK: usize = 0;
pub(super) const CHAIN_QSA_MERGE_N: usize = 1;
pub(super) const CHAIN_SAMPLER_GREEDY: usize = 2;
+1 -1
View File
@@ -8,7 +8,7 @@ out="${1:-$HOME/sm120-bin}"
nvcc="${NVCC:-$HOME/cuda-toolkits/13.2.2/bin/nvcc}"
mkdir -p "$out/ptx"
"$nvcc" -O2 -std=c++17 "$here/sm120_probe.cpp" -lcuda -o "$out/sm120_probe"
for f in mtp_original_full_attention original_verify_window_moe_experts mtp_original_full_head_iq sm120_attn_daily sm120_gu_swiglu; do
for f in mtp_original_full_attention original_verify_window_moe_experts mtp_original_full_head_iq chain_window sm120_attn_daily sm120_gu_swiglu; do
cp "$root/crates/cuda-driver/src/$f.ptx" "$out/ptx/$f.ptx"
done
sha256sum "$out"/sm120_probe "$out"/ptx/* > "$out/SHA256SUMS"
+68 -1
View File
@@ -161,6 +161,72 @@ static void test_attn() {
printf("RESULT attn_summary cases=%d byte_mismatches=%d\n", cases, g_fail - bad_before);
}
// ---------------------------------------------------------------------------------------------------------------
// ATTN_DAILY_MTP: the MTP draft step's attention (one query, 512 chunks sized for the 32,768-cell window): the Original chunk kernel + chain_qsa_decode_merge_n
// (what LELEKA_CHAIN=all runs) against leleka_qsa_decode_chunk_daily + leleka_qsa_decode_merge_v2. Byte comparison of the attention output and timing.
static void test_attn512() {
CUmodule mAttn = load_module(g_dir + "/mtp_original_full_attention.ptx");
CUmodule mChain = load_module(g_dir + "/chain_window.ptx");
CUmodule mNew = load_module(g_dir + "/sm120_attn_daily.ptx");
const int nkv = 2, G = 12, HD = 256, chunks = 512, cap = 32768, page = 4, cells = 262144 / 4;
const int pages = cells / page;
const size_t rows = (size_t)pages * nkv * page;
std::vector<int8_t> kq(rows * HD), vq(rows * HD); std::vector<uint16_t> ks(rows * 4), vs(rows * 4);
for (auto& v : kq) v = (int8_t)(rnd() & 0xff);
for (auto& v : vq) v = (int8_t)(rnd() & 0xff);
auto half_bits = [](float f) { uint32_t u; memcpy(&u, &f, 4); uint32_t sign = (u >> 16) & 0x8000; int e = ((u >> 23) & 0xff) - 127 + 15; uint32_t m = (u >> 13) & 0x3ff; return (uint16_t)(sign | (e << 10) | m); };
for (auto& v : ks) v = half_bits(0.001f + 0.01f * fabsf(rndf()));
for (auto& v : vs) v = half_bits(0.001f + 0.01f * fabsf(rndf()));
std::vector<int32_t> table(pages);
for (int i = 0; i < pages; ++i) table[i] = (int32_t)(((int64_t)i * 7 + 3) % pages);
table[5] = -1; table[pages / 2 + 1] = -1;
Dev dkq, dvq, dks, dvs, dtab;
dkq.alloc(kq.size()); dvq.alloc(vq.size()); dks.alloc(ks.size() * 2); dvs.alloc(vs.size() * 2); dtab.alloc(pages * 4);
dkq.put(kq.data(), kq.size()); dvq.put(vq.data(), vq.size()); dks.put(ks.data(), ks.size() * 2); dvs.put(vs.data(), vs.size() * 2); dtab.put(table.data(), pages * 4);
CUfunction fo = get_fn(mAttn, "leleka_qsa_decode_chunk"), fn = get_fn(mNew, "leleka_qsa_decode_chunk_daily");
CUfunction mo = get_fn(mAttn, "leleka_qsa_decode_merge"), mc = get_fn(mChain, "chain_qsa_decode_merge_n"), mn = get_fn(mNew, "leleka_qsa_decode_merge_v2");
int cases = 0, bad_before = g_fail;
for (int n_ids : {32768, 30000, 20000, 8192, 2051, 1025, 65, 64, 1}) {
std::vector<int32_t> ids(cap), step = {cells - 1, cells, cells / 4, n_ids};
for (int j = 0; j < cap; ++j) ids[j] = (int32_t)(((int64_t)j * 5 + (j % 3)) % cells);
std::vector<float> qh((size_t)nkv * G * HD); for (auto& v : qh) v = rndf();
const size_t stride = (size_t)chunks * 24 * (HD + 2) + 64;
Dev q, did, dstep, a0, a1, o0, o1, o2;
q.alloc(qh.size() * 4); did.alloc(ids.size() * 4); dstep.alloc(16); a0.alloc(stride * 4); a1.alloc(stride * 4);
o0.alloc(24 * HD * 4); o1.alloc(24 * HD * 4); o2.alloc(24 * HD * 4);
q.put(qh.data(), qh.size() * 4); did.put(ids.data(), ids.size() * 4); dstep.put(step.data(), 16);
int nk = nkv, ps = page, nch = chunks, cp = cap; float scale = 1.0f / 16.0f; long long st = (long long)stride;
CUdeviceptr pm0 = a0.p + (uint64_t)chunks * 24 * HD * 4, pl0 = pm0 + (uint64_t)chunks * 24 * 4;
CUdeviceptr pm1 = a1.p + (uint64_t)chunks * 24 * HD * 4, pl1 = pm1 + (uint64_t)chunks * 24 * 4;
void* pa0[] = {&q.p, &dkq.p, &dvq.p, &dks.p, &dvs.p, &dtab.p, &did.p, &dstep.p, &nk, &ps, &scale, &a0.p, &pm0, &pl0, &nch, &cp, &st};
void* pa1[] = {&q.p, &dkq.p, &dvq.p, &dks.p, &dvs.p, &dtab.p, &did.p, &dstep.p, &nk, &ps, &scale, &a1.p, &pm1, &pl1, &nch, &cp, &st};
auto go = [&](CUfunction f, void** a) { CU(cuLaunchKernel(f, chunks, nkv, 1, 256, 1, 1, 0, g_s, a, nullptr)); };
go(fo, pa0); go(fn, pa1);
void* m_old[] = {&a0.p, &pm0, &pl0, &nch, &o0.p, &st};
void* m_chain[] = {&a0.p, &pm0, &pl0, &nch, &o1.p, &st, &dstep.p};
void* m_v2[] = {&a1.p, &pm1, &pl1, &nch, &dstep.p, &o2.p, &st};
CU(cuLaunchKernel(mo, 24, 1, 1, 256, 1, 1, 0, g_s, m_old, nullptr));
CU(cuLaunchKernel(mc, 24, 1, 1, 256, 1, 1, 0, g_s, m_chain, nullptr));
CU(cuLaunchKernel(mn, 24, 1, 1, 256, 1, 1, 0, g_s, m_v2, nullptr));
CU(cuStreamSynchronize(g_s));
char tag[96];
snprintf(tag, sizeof tag, "attn512 chunk n_ids=%d", n_ids);
bool ok = same(tag, a0.get(), a1.get());
snprintf(tag, sizeof tag, "attn512 merge_n vs merge n_ids=%d", n_ids);
ok &= same(tag, o0.get(), o1.get());
snprintf(tag, sizeof tag, "attn512 merge_v2 vs merge n_ids=%d", n_ids);
ok &= same(tag, o0.get(), o2.get());
++cases;
if (n_ids == 32768 || n_ids == 8192 || n_ids == 2051) {
double to = time_graph([&](int) { go(fo, pa0); }, 30), tn = time_graph([&](int) { go(fn, pa1); }, 30);
double tmc = time_graph([&](int) { CU(cuLaunchKernel(mc, 24, 1, 1, 256, 1, 1, 0, g_s, m_chain, nullptr)); }, 30);
double tm2 = time_graph([&](int) { CU(cuLaunchKernel(mn, 24, 1, 1, 256, 1, 1, 0, g_s, m_v2, nullptr)); }, 30);
printf("RESULT attn512 n_ids=%d ok=%d chunk_old_us=%.3f chunk_new_us=%.3f merge_n_us=%.3f merge_v2_us=%.3f\n", n_ids, ok, to, tn, tmc, tm2);
}
}
printf("RESULT attn512_summary cases=%d byte_mismatches=%d\n", cases, g_fail - bad_before);
}
// ---------------------------------------------------------------------------------------------------------------
// GU_SWIGLU: window_moe_gu_<T> + swiglu_entries_kernel + quantize_q8_1_kernel (sealed Original) against sm120_moe_gu_swiglu_<T>; compares hq (the q8_1 blocks
// the down kernel reads) byte for byte, on random expert blobs (valid fp16 block scales) and random q8_1 activations.
@@ -270,7 +336,7 @@ int main(int argc, char** argv) {
g_dir = argv[1];
std::vector<std::string> tests;
for (int i = 2; i < argc; ++i) tests.push_back(argv[i]);
if (tests.empty()) tests = {"attn", "gu"};
if (tests.empty()) tests = {"attn", "gu", "attn512"};
CU(cuInit(0)); CUdevice dev; CU(cuDeviceGet(&dev, 0)); CUcontext ctx; CU(cuDevicePrimaryCtxRetain(&ctx, dev)); CU(cuCtxSetCurrent(ctx));
char name[256]; cuDeviceGetName(name, sizeof name, dev);
int maj, min; cuDeviceGetAttribute(&maj, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, dev); cuDeviceGetAttribute(&min, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, dev);
@@ -279,6 +345,7 @@ int main(int argc, char** argv) {
for (auto& t : tests) {
if (t == "attn") test_attn();
else if (t == "gu") test_gu();
else if (t == "attn512") test_attn512();
else fprintf(stderr, "unknown test %s\n", t.c_str());
}
printf("RESULT summary failures=%d\n", g_fail);