From 793ceaad9b67d9139f9c40f22cfce86212841da1 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Sun, 15 Nov 2026 07:43:35 -0400 Subject: [PATCH] tests/fast-greedy: tail lengths, boundary ties, a -inf ban block, and the scalar body run on AVX2 hosts Review on #398: the 248,320-entry trials were a multiple of 8, so the vector loop's scalar tail never ran there; trials now use n = 248320 + trial % 8 and add ties across lane boundaries and on the last element, a last-element-only maximum, and a 65,536-entry -inf block. The portable body is exported as common_sampler_fast_greedy_argmax_scalar so the test checks it against the reference on an AVX2 host too. A mutant that drops the vector tail fails 10 checks. Co-Authored-By: Claude Opus 5.5 --- common/sampling.cpp | 6 +++--- common/sampling.h | 2 ++ tests/test-sampling-fast-greedy.cpp | 28 +++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/common/sampling.cpp b/common/sampling.cpp index d2c91cf9eb56..13918a21052e 100644 --- a/common/sampling.cpp +++ b/common/sampling.cpp @@ -234,7 +234,7 @@ bool common_sampler_fast_greedy_eligible(const common_params_sampling & params, return ok; } -static int32_t fast_greedy_argmax_scalar(const float * logits, int32_t n_vocab, const char * mask) { +int32_t common_sampler_fast_greedy_argmax_scalar(const float * logits, int32_t n_vocab, const char * mask) { int32_t best = -1; float best_v = -INFINITY; for (int32_t i = 0; i < n_vocab; ++i) { @@ -290,7 +290,7 @@ __attribute__((target("avx2"))) static int32_t fast_greedy_argmax_avx2(const flo } } } - return fast_greedy_argmax_scalar(logits, n_vocab, mask); + return common_sampler_fast_greedy_argmax_scalar(logits, n_vocab, mask); } #endif @@ -301,7 +301,7 @@ int32_t common_sampler_fast_greedy_argmax(const float * logits, int32_t n_vocab, return fast_greedy_argmax_avx2(logits, n_vocab, mask); } #endif - return fast_greedy_argmax_scalar(logits, n_vocab, mask); + return common_sampler_fast_greedy_argmax_scalar(logits, n_vocab, mask); } struct common_sampler * common_sampler_init( diff --git a/common/sampling.h b/common/sampling.h index 0a28e93a27c2..7eb2d4c5c83f 100644 --- a/common/sampling.h +++ b/common/sampling.h @@ -72,6 +72,8 @@ llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_co bool common_sampler_fast_greedy_eligible(const common_params_sampling & params, bool has_grammar, bool has_reasoning_budget); // argmax over logits skipping mask[i] != 0 (mask may be null); -1 when a logit is NaN or every id is masked / -inf int32_t common_sampler_fast_greedy_argmax(const float * logits, int32_t n_vocab, const char * mask); +// the portable body the dispatch above falls back to (non-AVX2 hosts); exported so a test can run it on an AVX2 host +int32_t common_sampler_fast_greedy_argmax_scalar(const float * logits, int32_t n_vocab, const char * mask); // generalized version of common_sampler_sample // diff --git a/tests/test-sampling-fast-greedy.cpp b/tests/test-sampling-fast-greedy.cpp index e45d9d6cfe90..08f889e72030 100644 --- a/tests/test-sampling-fast-greedy.cpp +++ b/tests/test-sampling-fast-greedy.cpp @@ -139,10 +139,12 @@ static int32_t reference_argmax(const float * logits, int32_t n, const char * ma static void test_vector_matches_scalar() { std::mt19937 rng(4321); std::uniform_real_distribution dist(-20.0f, 20.0f); - const int32_t n = 248320; - std::vector logits(n); - std::vector mask(n, 0); + std::vector logits; + std::vector mask; for (int trial = 0; trial < 120; ++trial) { + const int32_t n = 248320 + trial % 8; // every remainder mod the vector width: the tail runs on 7 of 8 + logits.assign(n, 0.0f); + mask.assign(n, 0); for (auto & v : logits) { v = dist(rng); } @@ -173,8 +175,22 @@ static void test_vector_matches_scalar() { if (trial % 13 == 5) { logits[rng() % n] = INFINITY; } - const char * m = (trial % 2) ? mask.data() : nullptr; - CHECK(common_sampler_fast_greedy_argmax(logits.data(), n, m) == reference_argmax(logits.data(), n, m)); + if (trial % 6 == 0) { // ties across lane / vector boundaries and on the last element (in the tail) + const int32_t b = 8 * (int32_t) (rng() % (uint32_t) (n / 8 - 2)) + 7; + logits[b] = logits[b + 1] = logits[b + 9] = logits[n - 1] = 80.0f; + } + if (trial % 9 == 7) { // the last element alone holds the maximum + logits[n - 1] = 90.0f; + } + if (trial % 10 == 8) { // a vocabulary ban: a large adjacent block at -inf, the max just after it + const int32_t a = (int32_t) (rng() % (uint32_t) (n - 70000)); + std::fill(logits.begin() + a, logits.begin() + a + 65536, -INFINITY); + logits[a + 65536] = 85.0f; + } + const char * m = (trial % 2) ? mask.data() : nullptr; + const int32_t ref = reference_argmax(logits.data(), n, m); + CHECK(common_sampler_fast_greedy_argmax(logits.data(), n, m) == ref); + CHECK(common_sampler_fast_greedy_argmax_scalar(logits.data(), n, m) == ref); // the non-AVX2 path, run here too } for (const int32_t k : { 1, 7, 8, 9, 15, 16, 17, 31 }) { // the vector tail std::vector s(k); @@ -182,9 +198,11 @@ static void test_vector_matches_scalar() { v = dist(rng); } CHECK(common_sampler_fast_greedy_argmax(s.data(), k, nullptr) == reference_argmax(s.data(), k, nullptr)); + CHECK(common_sampler_fast_greedy_argmax_scalar(s.data(), k, nullptr) == reference_argmax(s.data(), k, nullptr)); } std::vector all_inf(16, -INFINITY); CHECK(common_sampler_fast_greedy_argmax(all_inf.data(), 16, nullptr) == -1); + CHECK(common_sampler_fast_greedy_argmax_scalar(all_inf.data(), 16, nullptr) == -1); } int main() {