From 2c0dd8535e4e1252dc183c7d54d340d2080eb72b Mon Sep 17 00:00:00 2001 From: Nunze Date: Sun, 18 Oct 2026 18:55:37 +0200 Subject: [PATCH] sample: at eight ranks a rank keeps the candidates the merge can hold Over a split vocabulary every rank's candidates are gathered and merged in one workgroup (sample_merge_topk), and libr4d's row holds world_size * n_cand <= 4096 pairs in LDS. The engine declared the default 1024 a rank, 8192 pairs at eight ranks, and the first step stopped on a shape the merge does not serve. Declare now narrows the width to 4096 / world_size, 512 at eight ranks, and says so in one line; a request whose top_k is wider is refused at admission, as a top_k over the width already was. Two and four ranks keep 1024. sample_test: at two, four and eight ranks declare succeeds, top_k at the width is admitted and one more is refused, naming the width. Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Claude Fable 5.1 --- core/sample/host_ref.h | 4 ++++ core/sample/sampler.cpp | 8 ++++++++ tests/sample_test.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/core/sample/host_ref.h b/core/sample/host_ref.h index c7155d16..787309a5 100644 --- a/core/sample/host_ref.h +++ b/core/sample/host_ref.h @@ -197,6 +197,10 @@ int32_t host_argmax(const float* logits, int64_t n_vocab); /* The candidate width every stage after top-k works in, SamplerConfig::max_candidates' default. */ constexpr int64_t kSampleCandidates = 1024; +/* Under a split vocabulary the merge (sample_merge_topk) holds world_size * n_cand gathered pairs + * in one workgroup's LDS; libr4d's row serves 4096 at most, so the per-rank width shrinks with + * the world (512 at eight ranks). */ +constexpr int64_t kSampleMergeCandidates = 4096; enum class ChainOrder { Radiance = 0, /* penalties, DRY, temperature, mask, top-k, top-p/min-p/typical, XTC, pick */ diff --git a/core/sample/sampler.cpp b/core/sample/sampler.cpp index 68b58a2e..7a16de8b 100644 --- a/core/sample/sampler.cpp +++ b/core/sample/sampler.cpp @@ -145,6 +145,14 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) { const int64_t rows = rows_for(cfg_); const int64_t nv = cfg_.n_vocab_local; + if (vocab_parallel() && cfg_.max_candidates * cfg_.world_size > kSampleMergeCandidates) { + const int64_t cap = kSampleMergeCandidates / cfg_.world_size; + RAD_INFO("sampler: candidate width %lld a rank -> %lld: the merge of %d ranks' candidate " + "sets holds %lld pairs at most (top_k above %lld is refused per request)", + (long long)cfg_.max_candidates, (long long)cap, cfg_.world_size, + (long long)kSampleMergeCandidates, (long long)cap); + cfg_.max_candidates = cap; + } const int64_t ncand = cfg_.max_candidates; /* ---------------------------------------------------------- buffers */ diff --git a/tests/sample_test.cpp b/tests/sample_test.cpp index 3043f33d..cc62d6dd 100644 --- a/tests/sample_test.cpp +++ b/tests/sample_test.cpp @@ -2100,6 +2100,46 @@ TEST(a_greedy_row_over_a_split_vocabulary_is_staged_as_a_chain_row) { CHECK_EQ(p[1].top_k, 1); } +/* Over a split vocabulary the merge holds every rank's candidates in one workgroup, 4096 pairs at + * most (kSampleMergeCandidates, libr4d's sample_merge_topk). At two and four ranks the default + * width of 1024 a rank fits; at eight it would ask 8192 and the step's merge would find no kernel, + * so declare narrows it to 512 and admission refuses a wider top_k instead. */ +TEST(the_candidate_width_over_a_split_vocabulary_is_what_the_merge_holds) { + Registry reg; + (void)reg.load_plugin(home() + "/kernels/libref.so", 0); + for (int world : { 2, 4, 8 }) { + RadModelMeta meta{}; + meta.arch_id = "sampler_split"; + meta.name = "sampler_split"; + RadBuildCtx bc{}; + bc.world_size = world; + bc.max_seqs = 2; + bc.max_tok = 2; + bc.max_ctx = 64; + bc.scope = ""; + Builder b(reg, meta, bc); + Sampler s; + SamplerConfig cfg; + cfg.n_vocab = 2048 * (int64_t)world; + cfg.n_vocab_local = 2048; + cfg.max_seqs = 2; + cfg.world_size = world; + cfg.rank = world - 1; + cfg.vocab_off = 2048 * (int64_t)(world - 1); + cfg.max_hist = 16; + CHECK_OK(s.declare(&b, cfg)); + + const int64_t width = world == 8 ? 512 : kSampleCandidates; + SamplingParams sp = greedy_params(); + std::string why; + sp.top_k = (int)width; + CHECK_OK(s.check_request(sp, &why)); + sp.top_k = (int)width + 1; + CHECK_EQ(s.check_request(sp, &why), RAD_E_INVAL); + CHECK(why.find("candidate width of " + std::to_string(width)) != std::string::npos); + } +} + /* Triggers are resolved at admission, with special tokens recognised: a one-token word becomes an * id, a longer word is scanned for as bytes, a token trigger that is not one token is a bad * request, and a regular expression is refused by name (it would be matched over the reply once