diff --git a/arch/common/rad_block_glm_indexer.h b/arch/common/rad_block_glm_indexer.h index 989871b4..72d91141 100644 --- a/arch/common/rad_block_glm_indexer.h +++ b/arch/common/rad_block_glm_indexer.h @@ -27,6 +27,9 @@ struct DsaCache { rad_buf query = 0, codes = 0, scales = 0, gate = 0, kg = 0; rad_buf stage = 0, page = 0, bpos = 0, nc = 0, pooled = 0, pool_scale = 0; rad_buf score = 0, selected = 0, nsel = 0, length = 0; + /* A taking-all step's zero scores, [max_tok, topk + 1]: such a step does not split, and + * the score plane holds only a run of rows when the step may. */ + rad_buf unscored = 0; /* TP only: a rank's rebased bounds, page tables and lengths before the gathers. */ rad_buf cu_part = 0, sel_part = 0, len_part = 0; }; @@ -44,7 +47,7 @@ struct DsaCache { rad_kvgroup index = 0, tail = 0, attention = 0; rad_weight ape = 0; rad_op op_query = 0, op_work = 0, op_pool = 0, op_tail = 0, op_score = 0, op_select = 0; - rad_op op_unscored = 0; + rad_op op_unscored = 0, op_select_all = 0; rad_op op_score_part = 0, op_select_part = 0, op_gather_sel = 0, op_gather_len = 0; int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Config& cfg, @@ -121,7 +124,7 @@ struct DsaCache { RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", unscored_cols()), RAD_STR("dtype", "f32")), RAD_NOWEIGHTS), - {}, { w.score }); + {}, { w.unscored }); op_score = rw(b, RAD_OP(b, "dsa_score", RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 128), @@ -149,6 +152,14 @@ struct DsaCache { RAD_INT("seqs", g.max_seqs), RAD_INT("signed", 1)), RAD_NOWEIGHTS), { w.score, w.nc }, { w.selected, w.nsel, w.length }); + op_select_all = rw(b, + RAD_OP(b, "qsa_select", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), + RAD_INT("blocks", unscored_cols()), + RAD_INT("topk", 512), RAD_INT("ratio", 4), + RAD_INT("seqs", g.max_seqs), RAD_INT("signed", 1)), + RAD_NOWEIGHTS), + { w.unscored, w.nc }, { w.selected, w.nsel, w.length }); if (split) { op_select_part = rw( b, @@ -175,7 +186,9 @@ struct DsaCache { if (!op_score_part || !op_select_part || !op_gather_sel || !op_gather_len) return RAD_E_INVAL; } - return op_query && op_work && op_tail && op_unscored && op_score && op_select ? RAD_OK + return op_query && op_work && op_tail && op_unscored && op_score && op_select && + op_select_all + ? RAD_OK : RAD_E_INVAL; } int64_t unscored_cols() const { return c.topk + 1 < c.blocks ? c.topk + 1 : c.blocks; } @@ -191,20 +204,19 @@ struct DsaCache { } /* The decision reads the same host bound as the score plane's width (max_ctx_len, rounded by * the core), so every rank takes the same path and a recorded decode pass changes it only when - * that bound moves. */ + * that bound moves. A step that takes all its blocks never splits: with nothing to score, every + * rank selects the whole step itself, and the gathers have nothing to save. */ bool splits(const RadBatch* batch) const { const int64_t T = batch->n_tok; - return op_score_part && (GlmTp::splits(T, g.world) || - (T >= GlmTp::kSplitWorkRows && - T * live_blocks(batch) >= GlmTp::kSplitWork)); + return op_score_part && live_blocks(batch) > c.topk && + (GlmTp::splits(T, g.world) || + (T >= GlmTp::kSplitWorkRows && T * live_blocks(batch) >= GlmTp::kSplitWork)); } /* Every row's history fits the selection, so the query side (projection, head gates, * quantiser, scorer) has nothing to decide: the selected table is every live block in order, * bit for bit what scoring would select. Read from the same host bound as splits(), so every - * rank agrees. A split step keeps scoring: its selection's rebased bounds come off the scorer. */ - bool takes_all(const RadBatch* batch) const { - return !splits(batch) && live_blocks(batch) <= c.topk; - } + * rank agrees. */ + bool takes_all(const RadBatch* batch) const { return live_blocks(batch) <= c.topk; } /* The query rows this rank runs: the whole step, or its run of a split step (GlmTp::run_of). * A rank whose run lies wholly past a small step runs the step's first R rows instead: no * sequence holds its run, so its scores are -inf, and its answers land past T. The caller's @@ -253,15 +265,22 @@ struct DsaCache { step_split(ctx, batch, live); return; } - if (all) - RAD_ISSUE_N(ctx, op_unscored, T, bcol_at(w.score, 0, c.blocks, 0, unscored_cols(), T)); - else - // dsa_score declares ten operands, cu_part the optional tenth; the engine counts them. - RAD_ISSUE_N(ctx, op_score, T, brows(w.codes, T * 32), brows(w.scales, T * 32), - brows(w.gate, T), kv_cache(index, layer), RAD_NONE, - praw2(ib->block_table, RAD_I32, S, ib->block_table_pitch), - praw(batch->cu_seqlens, RAD_I32, S + 1), brows(w.nc, S), - bcol_at(w.score, 0, c.blocks, 0, live, T), RAD_NONE); + if (all) { + const int64_t U = unscored_cols(); + RAD_ISSUE_N(ctx, op_unscored, T, brows(w.unscored, T)); + RAD_ISSUE_N(ctx, op_select_all, T, bcol_at(w.unscored, 0, U, 0, live, T), + praw2(ab->block_table, RAD_I32, S, ab->block_table_pitch), + brows(w.nc, S), praw(batch->positions, RAD_I32, T), + praw(batch->cu_seqlens, RAD_I32, S + 1), brows(w.selected, T), + brows(w.nsel, T), brows(w.length, T)); + return; + } + // dsa_score declares ten operands, cu_part the optional tenth; the engine counts them. + RAD_ISSUE_N(ctx, op_score, T, brows(w.codes, T * 32), brows(w.scales, T * 32), + brows(w.gate, T), kv_cache(index, layer), RAD_NONE, + praw2(ib->block_table, RAD_I32, S, ib->block_table_pitch), + praw(batch->cu_seqlens, RAD_I32, S + 1), brows(w.nc, S), + bcol_at(w.score, 0, c.blocks, 0, live, T), RAD_NONE); RAD_ISSUE_N(ctx, op_select, T, bcol_at(w.score, 0, c.blocks, 0, live, T), praw2(ab->block_table, RAD_I32, S, ab->block_table_pitch), brows(w.nc, S), praw(batch->positions, RAD_I32, T), praw(batch->cu_seqlens, RAD_I32, S + 1), diff --git a/arch/glm5next/glm5next.cpp b/arch/glm5next/glm5next.cpp index 533d6c58..1b013449 100644 --- a/arch/glm5next/glm5next.cpp +++ b/arch/glm5next/glm5next.cpp @@ -211,6 +211,7 @@ static int indexer_buffers(RadBuilder* b, Model& m) { * at 64k of context the whole step's plane would be 256 MiB. */ const int64_t scored = g.world > 1 ? std::min(T, std::max(part, GlmTp::kSplitRows)) : T; c.score = decl_b(b, nm.f("index_scores"), RAD_F32, { scored, blocks }); + c.unscored = decl_b(b, nm.f("index_unscored"), RAD_F32, { T, std::min(513, blocks) }); c.selected = m.aw.core.selected; c.length = m.aw.core.length; c.nsel = decl_b(b, nm.f("selected_count"), RAD_I32, { T }); diff --git a/tests/glm5next_test.cpp b/tests/glm5next_test.cpp index 1707649a..df5c5f95 100644 --- a/tests/glm5next_test.cpp +++ b/tests/glm5next_test.cpp @@ -665,6 +665,21 @@ TEST( batch.out_ids = &last; batch.draft_pass = 0; batch.max_q_len = 512; + { + // 512 rows whose history fits the selection: the indexer takes every + // block and does not split, so no index head runs and nothing of its is + // gathered, while the routers, mHC reads and MLA still split. + ctx.issues.clear(); + glm5next::step(&ctx, &batch); + CHECK(ctx.step_fail.empty()); + const auto fit_ops = issued_ops(b, ctx); + CHECK_EQ(std::count(fit_ops.begin(), fit_ops.end(), "gemm_nt_f32"), 42); + CHECK_EQ(std::count(fit_ops.begin(), fit_ops.end(), "dsa_score"), 0); + CHECK_EQ(std::count(fit_ops.begin(), fit_ops.end(), "qsa_select"), 11); + CHECK_EQ(std::count(fit_ops.begin(), fit_ops.end(), "all_gather"), + world > 1 ? 42 + 90 + 11 : 0); + } + batch.max_ctx_len = 3000; // live blocks past the top-k: the indexer scores ctx.issues.clear(); glm5next::step(&ctx, &batch); CHECK(ctx.step_fail.empty()); @@ -672,11 +687,9 @@ TEST( // 512 rows reach the split: each router runs on its column slice, each mHC // read on its chunks, each indexer, its head gates included, on its run // of query rows, and each MLA on its run of rows, all gathered but the - // gates. At TP1 the step does not split, and 512 tokens of history fit the - // selection, so the index heads do not run. + // gates. At TP1 the step does not split, and the indexer scores whole. const auto long_ops = issued_ops(b, ctx); - CHECK_EQ(std::count(long_ops.begin(), long_ops.end(), "gemm_nt_f32"), - world > 1 ? 53 : 42); + CHECK_EQ(std::count(long_ops.begin(), long_ops.end(), "gemm_nt_f32"), 53); CHECK_EQ(std::count(long_ops.begin(), long_ops.end(), "all_gather"), world > 1 ? 42 + 90 + 22 + 11 : 0); // 1 embedding, 45 attention and 45 expert reductions, less the 11 MLA ones. @@ -807,8 +820,39 @@ TEST(glm53_indexer_scores_and_selects_one_run_of_query_rows_per_rank) { CHECK_EQ(b.bufs[w.selected - 1].d.shape[0], world * most); CHECK_EQ(b.bufs[w.codes - 1].d.shape[0], world * most * 32); CHECK_EQ(b.bufs[w.sel_part - 1].d.shape[0], most); - // Two decode rows beside a 498-row chunk: 500 rows, runs of R. - auto k = kit(b, { 1, 1 }, { 498 }, 100); + // Wide page tables, so that live blocks follow the history past the selection. + auto widen = [](Kit& kt) { + const int64_t pitch = 1024; + kt.table.resize((size_t)(kt.b.n_seq * pitch)); + for (int64_t i = 0; i < kt.b.n_seq * pitch; ++i) kt.table[(size_t)i] = (int32_t)i; + for (auto& g : kt.kv) { + g.block_table = kt.table.data(); + g.block_table_pitch = g.max_blocks = pitch; + } + }; + { + // 500 rows whose history fits the selection take every block without splitting: + // each rank selects the whole step from the unscored plane, and nothing gathers. + auto fit = kit(b, { 1, 1 }, { 498 }, 100); + widen(*fit); + auto fits = run_passes(b, glm5next::step, { fit.get() }, rank, world); + CHECK_PASSES(b, fits); + int all = 0; + for (const auto& issue : fits[0]->issues) { + CHECK(issue.op != cache.op_score_part && issue.op != cache.op_gather_sel && + issue.op != cache.op_score); + if (issue.op == cache.op_select_all) { + ++all; + CHECK_EQ(issue.n, 500); + CHECK_EQ(issue.opd[0].handle, w.unscored); + } + } + CHECK_EQ(all, 1); + } + // Two decode rows beside a 498-row chunk of 3000 tokens of history: 500 rows that + // score, in runs of R. + auto k = kit(b, { 1, 1 }, { 498 }, 3000); + widen(*k); const int64_t T = 500, R = DsaCache::part_rows(T, world), r0 = rank * R, have = std::min(R, T - r0); auto passes = run_passes(b, glm5next::step, { k.get() }, rank, world); @@ -904,10 +948,11 @@ TEST(glm53_indexer_scores_and_selects_one_run_of_query_rows_per_rank) { if (issue.op == cache.op_unscored) { ++unscored; CHECK_EQ(issue.n, 255); - CHECK_EQ(issue.opd[0].handle, w.score); - CHECK_EQ(issue.opd[0].cols, cache.unscored_cols()); + CHECK_EQ(issue.opd[0].handle, w.unscored); + CHECK_EQ(b.bufs[w.unscored - 1].d.shape[1], cache.unscored_cols()); } - picks += issue.op == cache.op_select; + CHECK(issue.op != cache.op_select); + picks += issue.op == cache.op_select_all; } CHECK_EQ(unscored, 1); CHECK_EQ(picks, 1); @@ -1331,6 +1376,7 @@ TEST(dsa_cache_uses_distinct_index_and_attention_pages_and_a_speculative_tail_ri w.pooled = decl_b(&b, "pooled", RAD_F8E4M3, { 12, 128 }); w.pool_scale = decl_b(&b, "pool_scale", RAD_F32, { 12, 1 }); w.score = decl_b(&b, "score", RAD_F32, { 32, 1024 }); + w.unscored = decl_b(&b, "unscored", RAD_F32, { 32, 513 }); w.selected = decl_b(&b, "sel", RAD_I32, { 32, 513 }); w.nsel = decl_b(&b, "nsel", RAD_I32, { 32 }); w.length = decl_b(&b, "len", RAD_I32, { 32 }); @@ -1430,10 +1476,11 @@ TEST(dsa_cache_uses_distinct_index_and_attention_pages_and_a_speculative_tail_ri } else { CHECK_EQ(fill.op, block.op_unscored); CHECK_EQ(fill.n, 8); - CHECK_EQ(fill.opd[0].handle, w.score); - CHECK_EQ(fill.opd[0].cols, 513); + CHECK_EQ(fill.opd[0].handle, w.unscored); + CHECK_EQ(fill.opd[0].rows, 8); } - CHECK_EQ(ctx.issues[q + 4].op, block.op_select); + CHECK_EQ(ctx.issues[q + 4].op, scores ? block.op_select : block.op_select_all); + CHECK_EQ(ctx.issues[q + 4].opd[0].handle, scores ? w.score : w.unscored); CHECK_EQ(ctx.issues[q + 4].opd[0].cols, live); CHECK(ctx.issues[q + 4].opd[1].raw == abt.data()); } @@ -1747,11 +1794,11 @@ TEST(glm53_is_the_pinned_graph_and_steps) { * change -- the merge re-recorded them for main's builder plus this branch's file-direct * expert ops, and the graphs again when the routing counts became RAD_BUF_PERSIST (the * steps did not change). */ - const Case cases[] = { { 1, 0x5881752504ad8dbaull, 0x017c2cbd833d9737ull }, + const Case cases[] = { { 1, 0xce4368c09bfdbbeeull, 0x19679d4fcc0321ebull }, /* World 8 is this branch's expert-parallel graph (288/world whole * experts, file-direct), so its pins differ from main's. */ - { 8, 0x220a154a1928ee7aull, 0xc315b28cdda60809ull }, - { 8, 0x5b97f4fd6b4eeb36ull, 0x2a82ce055211fc15ull, true } }; + { 8, 0x553f81a93cec76bfull, 0x17f0a90d3bed9713ull }, + { 8, 0x9685be82c8d55813ull, 0x75e775f0568636b5ull, true } }; for (const Case& cs : cases) { RadBuilder b; b.kv_block = 4; @@ -2298,7 +2345,9 @@ TEST(glm53_streamed_decode_resolves_after_select_and_before_attention_with_the_e }; auto check_layer_order = [&](const glm5next::Layer& l) { const size_t store = find_issue(l.mla.op_store_stream); - const size_t sel = find_issue(l.index.cache.op_select); + // A short history takes every block (DsaCache::takes_all), a long one scores. + const size_t sel = std::min(find_issue(l.index.cache.op_select), + find_issue(l.index.cache.op_select_all)); const size_t res_i = find_issue(l.mla.op_resolve); const size_t attn = find_issue(l.mla.core.op_attn); REQUIRE(store != SIZE_MAX);