From 7104c8b8ef33d8d6cda23083d881d239fd759875 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 14 Nov 2026 04:16:24 +0100 Subject: [PATCH 1/2] prefill: equal-chunk cut rule for long prompt segments, with its unit test A prompt segment longer than its chunk is read in full chunks and a remainder (20,931 tokens at 10,240: 10,240 + 10,240 + 451). The new header equal_cut_rule.hpp holds the rule for reading it in n = ceil(tokens / chunk) chunks of equal size instead: the first n - 1 chunks take ceil(tokens / n) tokens and the last one at most n - 1 fewer, never more chunks than today's cut and never a chunk larger than the configured one. A segment that fits one chunk, or a cut whose last chunk would fall under the stream floor, keeps today's chunk. The older opt-in rule (STRATA_PREFILL_EQUAL=1, #693) is kept value for value, and a loan guard keeps today's cut when the equal chunk would lend more cache slots. Header only; nothing calls it yet. tests/prefill/equal_cut_rule_test.cpp checks the switch, the examples measured on an RTX 5070, a sweep of segment lengths and chunks, and the old opt-in rule against its current code. --- CMakeLists.txt | 5 + include/strata/prefill/equal_cut_rule.hpp | 94 ++++++++++++++ tests/prefill/equal_cut_rule_test.cpp | 149 ++++++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 include/strata/prefill/equal_cut_rule.hpp create mode 100644 tests/prefill/equal_cut_rule_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b1307e5ece..5e3b17d224 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -402,6 +402,11 @@ if(STRATA_BUILD_TESTS) target_include_directories(loan_rules_test PRIVATE include) add_test(NAME loan_rules_test COMMAND loan_rules_test) + # how a long prompt segment is cut into chunks: equal chunks, ceil(tokens / chunk) of them, on one GPU in a build without MMQ (header-only rule, no GPU) + add_executable(equal_cut_rule_test tests/prefill/equal_cut_rule_test.cpp) + target_include_directories(equal_cut_rule_test PRIVATE include) + add_test(NAME equal_cut_rule_test COMMAND equal_cut_rule_test) + # which work item (128 or 64 routed rows) the CUDA fused prompt kernels take, per card class (pick_ww): the pure rule, pinned at the two sides of every threshold for the # measured classes and compared with the previous rule for every class that must not change (header-only, no GPU) add_executable(fused_tile_rule_test tests/prefill/fused_tile_rule_test.cpp) diff --git a/include/strata/prefill/equal_cut_rule.hpp b/include/strata/prefill/equal_cut_rule.hpp new file mode 100644 index 0000000000..5ccd8fb013 --- /dev/null +++ b/include/strata/prefill/equal_cut_rule.hpp @@ -0,0 +1,94 @@ +// include/strata/prefill/equal_cut_rule.hpp - how a long prompt segment is cut into chunks (no GPU needed to check). +// +// A segment longer than its chunk was read in full chunks and a remainder: 20,931 tokens at a 10,240-token chunk as +// 10,240 + 10,240 + 451. On one GPU every chunk at or above the stream floor streams each expert the card does not +// hold, and a remainder under the floor runs the staged walk, which on a card that streams most of its experts costs +// close to a full pass. Reading the same segment in n = ceil(tokens / chunk) chunks of EQUAL size (3 x 6,977) keeps +// the number of chunks and gives every chunk the streamed walk. Measured on an RTX 5070 12 GB, Q2_0 plain pack, build +// without MMQ: 0.923-1.001 of the time of today's cut at 14,000-32,000 tokens (0.937 at 20,932, 0.930 at 32,000); one +// chunk more than needed was slower (1.054-1.145), so the count stays ceil(tokens / chunk). +// +// The rule applies where a segment's chunk is chosen (generate.cpp: the serve lend and generate's prompt) and only on +// one GPU: a layer split keeps today's cut (prefill.cpp pipeline_chunk: evening out a short tail added a full pass +// over the experts there, two RTX 4090: 25 % slower). STRATA_PREFILL_EQUAL=0 keeps today's cut everywhere; +// STRATA_PREFILL_EQUAL=1 on a layer split keeps the older opt-in rule (#693, `opt_in_chunk`). +#pragma once + +#include +#include + +namespace strata::prefill::equal_cut { + +/// Which cut a segment gets. +enum class Cut { + today, // full chunks and a remainder (the chunk of the request, rounded up to 256, at most the configured chunk) + opt_in, // the older opt-in (#693): equal chunks rounded up to 256, but a last chunk under the floor keeps today's cut + equal, // n = ceil(tokens / chunk) chunks of equal size, every one of them at or above the floor +}; + +/// `env`: STRATA_PREFILL_EQUAL as set (nullptr: unset); `one_gpu`: the segment is read by one GPU (no layer split, no peer +/// card, no --batch above one slot, no head alias). "0" keeps today's cut; one GPU otherwise gets the equal cut; a +/// layer split gets the older opt-in rule only when it is asked for with "1". +constexpr Cut pick(const char* env, bool one_gpu) { + if (env != nullptr && env[0] == '0') return Cut::today; + if (one_gpu) return Cut::equal; + return env != nullptr && env[0] == '1' ? Cut::opt_in : Cut::today; +} + +/// Today's chunk for a segment of `tokens` (generate.cpp request_chunk): rounded up to the 256-token grid, at most `max_chunk`. +constexpr int64_t today_chunk(int64_t tokens, int64_t max_chunk) { + if (tokens <= 0 || max_chunk <= 0) return 0; + const int64_t rounded = tokens > std::numeric_limits::max() - 255 ? tokens : ((tokens + 255) / 256) * 256; + return rounded < max_chunk ? rounded : max_chunk; +} + +/// The number of chunks a segment of `tokens` is read in at chunk `chunk` (prefill.cpp run_impl: c0 += C). +constexpr int64_t chunk_count(int64_t tokens, int64_t chunk) { + return tokens <= 0 || chunk <= 0 ? 0 : tokens / chunk + (tokens % chunk != 0); +} + +/// The older opt-in rule (#693), unchanged: n = ceil(tokens / max_chunk) chunks of ceil(tokens / n) rounded up to 256 (at most +/// `max_chunk`); one chunk, or a last chunk of today's cut under `floor`, keeps today's chunk. +constexpr int64_t opt_in_chunk(int64_t tokens, int64_t max_chunk, int64_t floor) { + const int64_t plain = today_chunk(tokens, max_chunk); + if (tokens <= 0 || max_chunk <= 0) return plain; + const int64_t n = chunk_count(tokens, max_chunk); + const int64_t last = tokens - (n - 1) * max_chunk; + if (n <= 1 || last < floor) return plain; + const int64_t per = tokens / n + (tokens % n != 0); + const int64_t rounded = per > std::numeric_limits::max() - 255 ? per : ((per + 255) / 256) * 256; + return rounded < max_chunk ? rounded : max_chunk; +} + +/// The equal cut: a segment longer than `max_chunk` is read in n = ceil(tokens / max_chunk) chunks of E = ceil(tokens / n) +/// tokens. The reading loop takes E at a time, so the first n - 1 chunks hold E tokens and the last one tokens - (n - 1) * E, +/// between E - (n - 1) and E: the remainder of the division is taken off the last chunk, at most n - 1 tokens. E is not +/// rounded to the 256 grid (rounding would leave the last chunk up to (n - 1) * 255 tokens short). E <= max_chunk, so every +/// buffer stays within the size laid out for max_chunk, and exactly n chunks are read (never more than today's cut). A +/// segment that fits one chunk keeps today's chunk; so does a cut whose last chunk would fall under `floor` (every chunk of the +/// equal cut takes the streamed walk, as today's full chunks do). +constexpr int64_t equal_chunk(int64_t tokens, int64_t max_chunk, int64_t floor) { + const int64_t plain = today_chunk(tokens, max_chunk); + if (tokens <= max_chunk || max_chunk <= 0) return plain; + const int64_t n = chunk_count(tokens, max_chunk); + const int64_t per = tokens / n + (tokens % n != 0); + const int64_t last = tokens - (n - 1) * per; + if (per > max_chunk || last < floor || last <= 0) return plain; + return per; +} + +/// The chunk a segment of `tokens` is read in under `cut`, at most `max_chunk`; `floor`: stream_all_min_tokens(). +constexpr int64_t chunk(Cut cut, int64_t tokens, int64_t max_chunk, int64_t floor) { + return cut == Cut::equal ? equal_chunk(tokens, max_chunk, floor) + : cut == Cut::opt_in ? opt_in_chunk(tokens, max_chunk, floor) + : today_chunk(tokens, max_chunk); +} + +/// The equal cut's loan of expert-cache slots must not be larger than today's (a chunk at or below the auto scan's small_max +/// lays out a bigger ring than a larger chunk, generate.cpp #2090): `equal_slots` / `today_slots` for the two chunks; 0 or less +/// means "does not fit". False: the segment keeps today's cut. +constexpr bool loan_ok(int64_t equal_slots, int64_t today_slots) { + return equal_slots > 0 && today_slots > 0 && equal_slots <= today_slots; +} + +} // namespace strata::prefill::equal_cut diff --git a/tests/prefill/equal_cut_rule_test.cpp b/tests/prefill/equal_cut_rule_test.cpp new file mode 100644 index 0000000000..047dbae48e --- /dev/null +++ b/tests/prefill/equal_cut_rule_test.cpp @@ -0,0 +1,149 @@ +// How a long prompt segment is cut into chunks (equal_cut_rule.hpp): no GPU, no model. +#include +#include +#include +#include +#include + +#include "strata/prefill/equal_cut_rule.hpp" + +namespace ec = strata::prefill::equal_cut; + +static int failed = 0; +#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) + +// The older opt-in's lambda as it stands in generate.cpp (equal_chunk with STRATA_PREFILL_EQUAL=1), kept here as the reference +static int64_t old_request_chunk(int64_t tokens, int64_t max_chunk) { + if (tokens <= 0 || max_chunk <= 0) return 0; + const int64_t rounded = tokens > std::numeric_limits::max() - 255 ? tokens : ((tokens + 255) / 256) * 256; + return std::min(max_chunk, rounded); +} +static int64_t old_equal_chunk(int64_t tokens, int64_t max_chunk, int64_t floor) { + const int64_t plain = old_request_chunk(tokens, max_chunk); + if (tokens <= 0 || max_chunk <= 0) return plain; + const int64_t n = tokens / max_chunk + (tokens % max_chunk != 0); + const int64_t last = tokens - (n - 1) * max_chunk; + if (n <= 1 || last < floor) return plain; + const int64_t per = tokens / n + (tokens % n != 0); + const int64_t rounded = per > std::numeric_limits::max() - 255 ? per : ((per + 255) / 256) * 256; + return std::min(max_chunk, rounded); +} + +// the chunks the reading loop makes of `tokens` at chunk `c` (prefill.cpp run_impl: T = min(C, n - c0)): count, smallest, largest +struct Cutout { int64_t n, lo, hi, last; }; +static Cutout cut_of(int64_t tokens, int64_t c) { + Cutout r{0, std::numeric_limits::max(), 0, 0}; + for (int64_t c0 = 0; c0 < tokens; c0 += c) { + const int64_t t = std::min(c, tokens - c0); + ++r.n; + r.lo = std::min(r.lo, t); + r.hi = std::max(r.hi, t); + r.last = t; + } + return r; +} + +int main() { + const int64_t F = 1536; // the plain pack's floor on a discrete card in a build without MMQ + // ---- the switch + CHECK(ec::pick(nullptr, true) == ec::Cut::equal); // unset, one GPU: the equal cut + CHECK(ec::pick("1", true) == ec::Cut::equal); + CHECK(ec::pick("0", true) == ec::Cut::today); // the way back + CHECK(ec::pick("0", false) == ec::Cut::today); + CHECK(ec::pick(nullptr, false) == ec::Cut::today); // a layer split: today's cut + CHECK(ec::pick("1", false) == ec::Cut::opt_in); // a layer split that asks for the older opt-in keeps it + CHECK(ec::pick("", true) == ec::Cut::equal); + CHECK(ec::pick("", false) == ec::Cut::today); + + // ---- the examples measured on the RTX 5070 (auto chunk 10,240 without MMQ, 9,984 with it) + { + const int64_t c = ec::equal_chunk(20932, 10240, F); // today 10,240 x 2 + 452 + CHECK(c == 6978); + const Cutout k = cut_of(20932, c); + CHECK(k.n == 3 && k.hi == 6978 && k.last == 6976); + } + CHECK(ec::equal_chunk(32000, 10240, F) == 8000 && cut_of(32000, 8000).n == 4 && cut_of(32000, 8000).lo == 8000); + CHECK(ec::equal_chunk(14000, 10240, F) == 7000 && cut_of(14000, 7000).n == 2); + CHECK(ec::equal_chunk(20000, 9984, F) == 6667 && cut_of(20000, 6667).n == 3 && cut_of(20000, 6667).last == 6666); + CHECK(ec::equal_chunk(10500, 10240, F) == 5250 && cut_of(10500, 5250).n == 2); + CHECK(ec::equal_chunk(20000, 10240, F) == 10000); + CHECK(ec::equal_chunk(22980, 10240, F) == 7660); + CHECK(ec::equal_chunk(18932, 10240, F) == 9466); + // the engine reads N - 1 prompt tokens in a batch: 20,931 -> 3 x 6,977 + CHECK(ec::equal_chunk(20931, 10240, F) == 6977 && cut_of(20931, 6977).lo == 6977 && cut_of(20931, 6977).n == 3); + // ---- a segment that fits one chunk is unchanged (today's chunk: rounded up to 256, at most the chunk) + for (int64_t t : {1, 255, 256, 300, 999, 1000, 1536, 8000, 10239, 10240}) + CHECK(ec::equal_chunk(t, 10240, F) == ec::today_chunk(t, 10240)); + CHECK(ec::equal_chunk(8000, 10240, F) == 8192); + CHECK(ec::equal_chunk(10240, 10240, F) == 10240); + CHECK(ec::equal_chunk(10241, 10240, F) == 5121); + // ---- the cases the change is for: today's cut leaves a short tail under the floor, the equal cut evens it out + CHECK(ec::today_chunk(20932, 10240) == 10240 && cut_of(20932, 10240).last == 452); // today: 10,240 x 2 + 452 + CHECK(cut_of(20932, ec::equal_chunk(20932, 10240, F)).n == 3 && cut_of(20932, ec::equal_chunk(20932, 10240, F)).lo >= 6976); + CHECK(cut_of(32000, 10240).last == 1280); // today: 10,240 x 3 + 1,280 + CHECK(ec::equal_chunk(32000, 10240, F) == 8000); // 4 x 8,000 + CHECK(ec::equal_chunk(32000, 10240, 3072) == 8000); // the CPU share's floor too + // ---- a small chunk: the EQUAL chunk itself would fall under the floor (auto chunk 4,096, CPU share floor 3,072): + // today's 4,096 + 904 keeps one chunk on the streamed walk; 2 x 2,500 would put both under the floor + CHECK(ec::equal_chunk(5000, 4096, 3072) == 4096); + CHECK(ec::equal_chunk(7000, 4096, 3072) == 3500); // 2 x 3,500: both at or above the floor, evened + CHECK(ec::equal_chunk(6200, 4096, 3072) == 3100); // evened from 2 x 3,072 up + CHECK(ec::equal_chunk(6100, 4096, 3072) == 4096); // 2 x 3,050: under the floor, today's cut + // ---- a cut whose chunks would fall under the floor keeps today's cut (a small chunk) + CHECK(ec::equal_chunk(2100, 2048, F) == 2048); // 2 x 1,050 would both walk staged + CHECK(ec::equal_chunk(3100, 2048, F) == 1550); // 2 x 1,550: both at or above 1,536 + CHECK(ec::equal_chunk(3071, 2048, F) == 2048); // 1,536 + 1,535: the last under the floor + CHECK(ec::equal_chunk(3072, 2048, F) == 1536); + // ---- degenerate input + CHECK(ec::equal_chunk(0, 10240, F) == 0 && ec::equal_chunk(-5, 10240, F) == 0 && ec::equal_chunk(5000, 0, F) == 0); + CHECK(ec::chunk_count(0, 5) == 0 && ec::chunk_count(5, 0) == 0 && ec::chunk_count(10, 5) == 2 && ec::chunk_count(11, 5) == 3); + + // ---- the properties, over a sweep of segment lengths and chunks (non-multiples of 256 too: a numeric --prefill) + long long cases = 0; + for (int64_t mc : {1024, 2048, 3072, 4096, 6144, 7000, 8192, 9216, 9984, 10240, 10496, 16384, 32768}) { + for (int64_t floor : {64, 1024, 1536, 3072}) { + for (int64_t t = 1; t <= 140000; t += (t < 4 * mc ? 7 : 997)) { + ++cases; + const int64_t today = ec::today_chunk(t, mc); + const int64_t e = ec::equal_chunk(t, mc, floor); + const Cutout kt = cut_of(t, today), ke = cut_of(t, e); + CHECK(e > 0 && e <= mc); // within the buffers laid out for the chunk + CHECK(ke.n <= kt.n); // never more chunks than today's cut + if (t <= mc) { CHECK(e == today); continue; } // one chunk: unchanged + if (e == today) { CHECK(ke.n == kt.n); continue; } // kept today's cut (a chunk would fall under the floor) + CHECK(ke.n == ec::chunk_count(t, mc)); // exactly ceil(t / chunk) chunks + CHECK(ke.hi - ke.lo <= ke.n - 1); // equal, within the n - 1 tokens the division leaves + CHECK(ke.lo == ke.last && ke.lo >= floor); // only the last is shorter, and it streams + } + } + } + // ---- the older opt-in rule is the old lambda, value for value + for (int64_t mc : {1024, 3072, 7000, 9984, 10240}) { + for (int64_t floor : {1024, 1536, 3072}) { + for (int64_t t = 0; t <= 70000; t += 13) { + CHECK(ec::opt_in_chunk(t, mc, floor) == old_equal_chunk(t, mc, floor)); + CHECK(ec::today_chunk(t, mc) == old_request_chunk(t, mc)); + } + } + } + // ---- chunk(): the three cuts + CHECK(ec::chunk(ec::Cut::today, 20932, 10240, F) == 10240); + CHECK(ec::chunk(ec::Cut::equal, 20932, 10240, F) == 6978); + CHECK(ec::chunk(ec::Cut::opt_in, 20932, 10240, F) == 10240); // the opt-in keeps a 452-token last chunk under the floor + CHECK(ec::chunk(ec::Cut::opt_in, 22980, 10240, F) == 7680); // 2,500 above the floor: evened, on the 256 grid + CHECK(ec::chunk(ec::Cut::equal, 22980, 10240, F) == 7660); + // ---- the loan guard + CHECK(ec::loan_ok(100, 120) && ec::loan_ok(120, 120)); + CHECK(!ec::loan_ok(121, 120)); // a larger loan than today's: keep today's cut + CHECK(!ec::loan_ok(0, 120) && !ec::loan_ok(-1, 120) && !ec::loan_ok(100, 0)); + // constexpr: usable at compile time + static_assert(ec::equal_chunk(32000, 10240, 1536) == 8000, "constexpr equal cut"); + static_assert(ec::pick("0", true) == ec::Cut::today, "constexpr switch"); + + if (failed) { + std::fprintf(stderr, "equal_cut_rule_test: %d check(s) failed\n", failed); + return 1; + } + std::printf("equal_cut_rule_test: all checks passed (%lld swept cases)\n", cases); + return 0; +} From 51e5e7adcc833fa642b8ebad7e1538650b12154b Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 14 Nov 2026 05:21:09 +0100 Subject: [PATCH 2/2] prefill: read long prompts in equal chunks on one GPU in a build without MMQ In a build without MMQ, a prompt segment longer than its chunk is now read in ceil(tokens / chunk) chunks of equal size when one GPU reads it: 20,932 tokens at a 10,240-token chunk become 3 x 6,978 instead of 10,240 + 10,240 + 452, and 32,000 tokens become 4 x 8,000 instead of 3 x 10,240 + 1,280. The number of chunks stays the same and the short last chunk under the stream floor goes away. Measured on an RTX 5070 12 GB with the Q2_0 plain pack, 14,000 to 32,000 tokens: 0.923 to 1.001 of the time of today's cut; first-token KL at 10,500 tokens no worse. What STRATA_PREFILL_EQUAL does now in a build without MMQ: - not set, one GPU: the equal cut - not set, a layer split, a peer card, more than one slot or a head alias: today's cut, unchanged - 0: today's cut everywhere - 1, one GPU: the equal cut - 1, a layer split: the existing opt-in rule, unchanged (equal chunks on the 256 grid, a short last chunk kept) Today's cut is also kept when the equal chunk itself would fall under the stream floor (a small chunk: two chunks under the floor instead of one above it), and when the equal chunk would lend more cache slots than today's chunk. The layer split's own chunk rule (--prefill-pipe) is not touched. A build with MMQ compiles the previous code unchanged. --- src/program/generate.cpp | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 98173e7277..74d66140c1 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -110,6 +110,9 @@ #include "strata/program/split_walk.hpp" #include "strata/program/gpu_report.hpp" #include "strata/prefill/loan_rules.hpp" +#if defined(STRATA_PREFILL_FUSED) && !defined(STRATA_PREFILL_MMQ) +#include "strata/prefill/equal_cut_rule.hpp" +#endif #include "strata/prefill/share_rules.hpp" #include "strata/platform/host_limits.hpp" #include "strata/program/split_ring.hpp" @@ -10171,6 +10174,25 @@ int main(int argc, char** argv) { return loan_by_walk ? strata::prefill::loan_rules::walk_chunk(tokens, chunk, strata::prefill::Prefill::stream_all_min_tokens()) : chunk; }; +#if defined(STRATA_PREFILL_FUSED) && !defined(STRATA_PREFILL_MMQ) + // A build without MMQ (prefill.cpp STRATA_PF_OWN_ONLY): ONE GPU reads a segment longer than its chunk in n = ceil(tokens / + // max_chunk) chunks of EQUAL size by default, also when today's last chunk would be under the stream floor (a sub-floor + // remainder costs close to a full pass where most experts stream: RTX 5070 12 GB, Q2_0, 14,000-32,000 tokens, 0.923-1.001 of + // today's time; equal_cut_rule.hpp). `one_gpu`: no layer split, no peer card, one slot, no head alias. A layer split keeps + // today's cut, or with STRATA_PREFILL_EQUAL=1 the older opt-in rule below (#693); STRATA_PREFILL_EQUAL=0 keeps today's cut + // everywhere. `request_chunk` is unchanged (STRATA_SPLIT_SMALL_OWN uses it too). + static const strata::prefill::equal_cut::Cut equal_cut_one = strata::prefill::equal_cut::pick(std::getenv("STRATA_PREFILL_EQUAL"), true); + static const strata::prefill::equal_cut::Cut equal_cut_split = strata::prefill::equal_cut::pick(std::getenv("STRATA_PREFILL_EQUAL"), false); + auto equal_chunk = [&](int64_t tokens, int64_t max_chunk, bool one_gpu) -> int64_t { + return strata::prefill::equal_cut::chunk(one_gpu ? equal_cut_one : equal_cut_split, tokens, max_chunk, + strata::prefill::Prefill::stream_all_min_tokens()); + }; + // #2824: as below; a prompt with no host plan takes the cut above + auto planned_chunk = [&](int64_t tokens, int64_t cap, int64_t plan, bool one_gpu) -> int64_t { + if (plan <= 0 || plan >= cap || tokens <= 0) return equal_chunk(tokens, cap, one_gpu); + return strata::program::prefill_plan::balanced_chunk(tokens, cap, plan); + }; +#else // STRATA_PREFILL_EQUAL=1 (opt-in, PR #693): a segment of `tokens` reads in n = ceil(tokens / max_chunk) chunks of EQUAL // size (ceil(tokens / n) rounded up to 256) instead of full chunks and a short last one. A chunk of // stream_all_min_tokens() or more streams every expert the GPU does not hold, whatever its length, so a prompt's cost @@ -10197,6 +10219,7 @@ int main(int argc, char** argv) { if (plan <= 0 || plan >= cap || tokens <= 0) return equal_chunk(tokens, cap); return strata::program::prefill_plan::balanced_chunk(tokens, cap, plan); }; +#endif // The prompt path's chunk and the slots it borrows for its buffers: the requested chunk halved until it fits, // or with --prefill auto the largest chunk whose buffers take at most kAutoLendPct % of the slots (a // lent slot's expert is streamed during the prompt and refilled after it; measured on a 12 GB card, 32K Q2_0 @@ -17882,7 +17905,27 @@ int main(int argc, char** argv) { const auto t_ln = Clock::now(); // what this segment needs, capped by the configured chunk: a request lends only what its own // prompt needs, so a large chunk costs a short prompt nothing +#if defined(STRATA_PREFILL_FUSED) && !defined(STRATA_PREFILL_MMQ) + // one GPU: equal chunks (equal_cut_rule.hpp), unless their loan would take more slots than today's cut + const bool eq_one_gpu = pf_parts.size() == 1 && stages.empty() && !multi_gpu && !peer.valid() && + !o.prefill_head_alias && o.batch <= 1; + int64_t want_full = loan_chunk(tokens, planned_chunk(tokens, o.prefill_chunk, pf_plan_chunk, eq_one_gpu)); + if (eq_one_gpu) { + const int64_t want_today = loan_chunk(tokens, planned_chunk(tokens, o.prefill_chunk, pf_plan_chunk, false)); + if (want_full != want_today && pf_parts[0].first >= 0) { + const int64_t floor_eq = strata::prefill::Prefill::stream_all_min_tokens(); + auto tail_eq = [&](int64_t w) -> int64_t { + return tail_loan_on && strata::prefill::Prefill::tail_matters(w, srcp != nullptr) + ? strata::prefill::loan_rules::segment_tail(tokens, w, floor_eq) : -1; + }; + if (!strata::prefill::equal_cut::loan_ok(part_slots(pf_parts[0], want_full, tail_eq(want_full)), + part_slots(pf_parts[0], want_today, tail_eq(want_today)))) + want_full = want_today; + } + } +#else const int64_t want_full = loan_chunk(tokens, planned_chunk(tokens, o.prefill_chunk, pf_plan_chunk)); +#endif // #340: a short enough request reads in the stages' own S-token chunks (nothing lent) const int64_t want_seg = o.prefill_head_alias ? o.prefill_chunk : split_small > 0 && tokens <= split_small_max ? std::min(want_full, split_small) @@ -21050,12 +21093,28 @@ int main(int argc, char** argv) { int64_t c2 = chunk_in; (void) plan_lend(c2); } else { +#if defined(STRATA_PREFILL_FUSED) && !defined(STRATA_PREFILL_MMQ) + chunk = std::min(vram_chunk, planned_chunk(n_batched, vram_chunk, chunk, false)); // equal chunks, no tail +#else chunk = std::min(vram_chunk, planned_chunk(n_batched, vram_chunk, chunk)); // equal chunks, no tail +#endif } k = lend_slots(chunk); } } +#if defined(STRATA_PREFILL_FUSED) && !defined(STRATA_PREFILL_MMQ) + // one GPU: equal chunks (equal_cut_rule.hpp), unless their loan would take more slots than today's cut + const bool eq_one_gpu = !multi_gpu && !peer.valid() && !o.prefill_head_alias && o.batch <= 1; + int64_t request_sized = loan_chunk(n_batched, equal_chunk(n_batched, chunk, eq_one_gpu)); + if (eq_one_gpu) { + const int64_t request_today = loan_chunk(n_batched, equal_chunk(n_batched, chunk, false)); + if (request_sized != request_today && + !strata::prefill::equal_cut::loan_ok(lend_slots(request_sized), lend_slots(request_today))) + request_sized = request_today; + } +#else const int64_t request_sized = loan_chunk(n_batched, equal_chunk(n_batched, chunk)); +#endif if (k > 0 && request_sized < chunk) { // no bigger than this prompt segment needs chunk = request_sized; k = lend_slots(chunk);