From 9c5a45f8794caf0d1016bde7aa17cd02b4832230 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 13 Oct 2026 00:44:27 +0200 Subject: [PATCH] SYCL layer split auto: among near-tied placements, the one that leaves its tightest card the most room (#1440) On two equal cards every K whose caches hold all the profiled pairs predicts the same window time, and the search kept the first one tried: the smallest K, which gives the last stage (it also carries the MTP drafter) the most layers and lets its cache fill the card to the reserve. 2x Arc Pro B70 with IQ3_S: auto K=24, CUDA1 at 31.8 of 31.9 GiB, ~11 tok/s; --layer-split 30 ~54 tok/s (agnaylor in #1440). A full xe card does not fail an allocation, it moves buffers to system memory. The SYCL search now scores every placement it tries with its smallest spare room (a stage's room after its session minus every expert of its layers) and, among the placements within STRATA_SPLIT_HEADROOM_TOL (default 0.2%) of the best predicted time, takes the one with the most (split_rules::pick_with_headroom). A log line names both picks when it moves the split. STRATA_SPLIT_HEADROOM=0 keeps the old pick. CUDA/HIP placement is unchanged. Test: split_rules_test (a 48-layer two-card model of the report: plain K=24 with 664 MiB spare on the last card, headroom K=25 with 1728 MiB; tolerance and tie rules). --- include/strata/program/split_rules.hpp | 54 ++++++++++++++++++++++++++ sycl/src/program/generate.cpp | 50 ++++++++++++++++++++++++ tests/program/split_rules_test.cpp | 46 ++++++++++++++++++++++ 3 files changed, 150 insertions(+) diff --git a/include/strata/program/split_rules.hpp b/include/strata/program/split_rules.hpp index b9382272..6cff00b9 100644 --- a/include/strata/program/split_rules.hpp +++ b/include/strata/program/split_rules.hpp @@ -35,4 +35,58 @@ inline bool has_tiny_stage(const std::vector& cuts, int64_t n_layers) { return smallest_stage(cuts, n_layers) < kMinStageLayers; } +// ---- #1440: headroom among (near-)ties. The search scores a placement by its predicted window time alone, so on two +// equal cards every K whose caches hold all the profiled pairs ties, and the first one tried wins: the smallest K, which +// gives the LAST stage (the one that also carries the MTP drafter) the most layers and lets its cache fill the card to +// the reserve. On Linux xe a card that runs out of VRAM does not fail an allocation, the driver moves buffers to system +// memory: 2x Arc Pro B70, IQ3_S, auto picked K=24 with CUDA1 at 31.8 of 31.9 GiB and decoded ~11 tok/s, against ~54 with +// --layer-split 30. So among the placements within `tol` of the best predicted time, the one whose tightest stage +// keeps the most VRAM beyond all of its layers' experts wins (ties on that: the faster, then the first). + +/// One scored placement: its cuts, its predicted window time and the smallest spare room over its stages (bytes; a +/// stage's room after its session minus every expert of its layers; negative when its cache cannot hold them all). +struct Placement { + std::vector cuts; + double ms = 0.0; + int64_t min_spare = 0; +}; + +/// The index of the plain pick (the first with the lowest time) in `c`, or 0 when `c` is empty. +inline size_t fastest(const std::vector& c) { + size_t best = 0; + for (size_t i = 1; i < c.size(); ++i) + if (c[i].ms < c[best].ms) best = i; + return best; +} + +/// The index of the pick with headroom: of the placements within `tol` (a fraction) of the fastest, the one with the +/// largest `min_spare`. tol <= 0 still breaks exact ties. 0 when `c` is empty. +inline size_t pick_with_headroom(const std::vector& c, double tol) { + if (c.empty()) return 0; + const size_t best = fastest(c); + const double limit = c[best].ms + c[best].ms * (tol > 0.0 ? tol : 0.0); + size_t pick = best; + for (size_t i = 0; i < c.size(); ++i) { + if (c[i].ms > limit) continue; + if (c[i].min_spare > c[pick].min_spare || (c[i].min_spare == c[pick].min_spare && c[i].ms < c[pick].ms)) + pick = i; + } + return pick; +} + +/// The smallest spare room over the stages of `cuts`: room[i] - session(lb, le) - need(lb, le), where `need_prefix[l]` +/// is the bytes of every expert of layers [0, l) (n_layers + 1 entries). +template +inline int64_t min_spare(const std::vector& cuts, int64_t n_layers, const std::vector& room, + const std::vector& need_prefix, Session&& session) { + int64_t lo = INT64_MAX; + for (size_t i = 0; i <= cuts.size(); ++i) { + const int64_t lb = i == 0 ? 0 : cuts[i - 1]; + const int64_t le = i < cuts.size() ? cuts[i] : n_layers; + const int64_t spare = room[i] - session(lb, le) - (need_prefix[(size_t) le] - need_prefix[(size_t) lb]); + if (spare < lo) lo = spare; + } + return lo; +} + } // namespace strata::program::split_rules diff --git a/sycl/src/program/generate.cpp b/sycl/src/program/generate.cpp index 4d97cb30..666a35c4 100644 --- a/sycl/src/program/generate.cpp +++ b/sycl/src/program/generate.cpp @@ -65,6 +65,7 @@ namespace strata::prefill { void set_nonresident_share(double share); } // SYC #include "strata/kernels/native_rope.hpp" #include "strata/kernels/mrope.hpp" #include "strata/kernels/kv_q4.hpp" +#include "strata/program/split_rules.hpp" #include "strata/kernels/qsa.hpp" #include "strata/core/native_head.hpp" #include "strata/core/verify.hpp" @@ -3641,11 +3642,38 @@ int main(int argc, char **argv) try { std::vector best, at((size_t) ns - 1); double best_ms = 1e30, best_mass = 0; int64_t best_held = 0, tried = 0; + // #1440: of the placements within STRATA_SPLIT_HEADROOM_TOL (default 0.2%) of the best predicted time, the one + // whose tightest card keeps the most VRAM beyond all its layers' experts (split_rules::pick_with_headroom). + // On by default here (a full xe card moves buffers to system memory instead of failing); STRATA_SPLIT_HEADROOM=0 + // keeps the first best, as before. + static const bool split_headroom = [] { + const char* v = std::getenv("STRATA_SPLIT_HEADROOM"); + return v == nullptr || std::atoi(v) != 0; + }(); + const double headroom_tol = + std::getenv("STRATA_SPLIT_HEADROOM_TOL") ? std::atof(std::getenv("STRATA_SPLIT_HEADROOM_TOL")) : 0.002; + std::vector need_prefix((size_t) g.n_layers + 1, 0); + for (int64_t l = 0; l < g.n_layers; ++l) + need_prefix[(size_t) l + 1] = need_prefix[(size_t) l] + (int64_t) g.n_expert * cost(l); + auto range_session = [&](int64_t lb, int64_t le) -> int64_t { + return (int64_t) strata::core::session_bytes(g, o.max_context, K, lb, le); + }; + std::vector scored; + std::vector scored_mass; + std::vector scored_held; + auto score = [&](const std::vector& cuts, double ms, double hm, int64_t held) { + if (!split_headroom) return; + scored.push_back({cuts, ms, strata::program::split_rules::min_spare(cuts, g.n_layers, cap, need_prefix, + range_session)}); + scored_mass.push_back(hm); + scored_held.push_back(held); + }; auto consider = [&]() { double hm = 0; int64_t held = 0; const double ms = predict(at, hm, held); ++tried; + score(at, ms, hm, held); if (ms < best_ms) { best = at; best_ms = ms; best_mass = hm; best_held = held; } }; const int64_t L = g.n_layers; @@ -3722,6 +3750,7 @@ int main(int argc, char **argv) try { (double) k1 * layer_ms[0] + (double) (k2 - k1) * layer_ms[1] + (double) (k3 - k2) * layer_ms[2] + (double) (L - k3) * layer_ms[3]; ++tried; + score({k1, k2, k3}, ms, hm / denom, held_at[a] + held_at[b] + held_at[c] + held_at[d]); if (ms < best_ms) { best = {k1, k2, k3}; best_ms = ms; @@ -3737,6 +3766,27 @@ int main(int argc, char **argv) try { // 163,185 at five). Nothing on this rig reaches it. proportional(); } + if (split_headroom && scored.size() > 1) { + const size_t first = strata::program::split_rules::fastest(scored); + const size_t pick = strata::program::split_rules::pick_with_headroom(scored, headroom_tol); + if (pick != first && scored[first].cuts == best) { + auto fmt_k = [](const std::vector& v) { + std::string s; + for (const int64_t k : v) s += (s.empty() ? "" : ",") + std::to_string(k); + return s; + }; + std::fprintf(stderr, "strata generate: layer split auto: K=%s instead of K=%s (predicted %.1f vs %.1f ms): its " + "tightest card keeps %.2f GiB beyond all its layers' experts, against %.2f GiB " + "(#1440: a full card can page to system memory); STRATA_SPLIT_HEADROOM=0 keeps K=%s\n", + fmt_k(scored[pick].cuts).c_str(), fmt_k(best).c_str(), scored[pick].ms, best_ms, + (double) scored[pick].min_spare / 1073741824.0, + (double) scored[first].min_spare / 1073741824.0, fmt_k(best).c_str()); + best = scored[pick].cuts; + best_ms = scored[pick].ms; + best_mass = scored_mass[pick]; + best_held = scored_held[pick]; + } + } split_at = best; std::string ks; for (const int64_t k : split_at) ks += (ks.empty() ? "" : ",") + std::to_string(k); diff --git a/tests/program/split_rules_test.cpp b/tests/program/split_rules_test.cpp index 4c3140a2..beef1255 100644 --- a/tests/program/split_rules_test.cpp +++ b/tests/program/split_rules_test.cpp @@ -29,6 +29,52 @@ int main() { CHECK(restriction_possible(12, 4)); CHECK(!restriction_possible(11, 4)); CHECK(!restriction_possible(48, 1)); + + // #1440: two equal cards, 48 layers x 512 experts of ~2 MiB (IQ3_S-like). Card 0 has more room than card 1 (the + // last stage also carries the drafter). Every K whose two caches hold all their pairs predicts the same time. + { + const int64_t L = 48, E = 512, blob = 2 << 20, MiB = 1 << 20; + const std::vector room{29000 * MiB, 26200 * MiB}; + std::vector need(L + 1, 0); + for (int64_t l = 0; l < L; ++l) need[(size_t) l + 1] = need[(size_t) l] + E * blob; + auto session = [&](int64_t lb, int64_t le) { return (le - lb) * 40 * MiB; }; + std::vector c; + for (int64_t k = 2; k < L; ++k) { + Placement p; + p.cuts = {k}; + p.min_spare = min_spare(p.cuts, L, room, need, session); + // the miss term: the pairs a stage's cache cannot hold (each costs the same here) + double missing = 0; + for (int i = 0; i < 2; ++i) { + const int64_t lb = i == 0 ? 0 : k, le = i == 0 ? k : L; + const int64_t fit = (room[(size_t) i] - session(lb, le)) / blob; + const int64_t want = (le - lb) * E; + if (fit < want) missing += (double) (want - (fit > 0 ? fit : 0)); + } + p.ms = 48 * 0.5 + 190.0 * missing / (double) (L * E); + c.push_back(p); + } + const size_t plain = fastest(c), head = pick_with_headroom(c, 0.002); + // the plain pick is the first full placement, and it leaves card 1 (almost) nothing + CHECK(c[plain].ms == c[head].ms); + CHECK(c[plain].cuts[0] < c[head].cuts[0]); + CHECK(c[plain].min_spare < 1024 * MiB); + CHECK(c[head].min_spare > 1536 * MiB); + // the pick is the most balanced full placement: no other full one keeps more on its tightest card + for (const Placement& p : c) + if (p.ms == c[plain].ms) CHECK(p.min_spare <= c[head].min_spare); + std::printf("split_rules_test: #1440 plain K=%lld (spare %lld MiB), headroom K=%lld (spare %lld MiB)\n", + (long long) c[plain].cuts[0], (long long) (c[plain].min_spare / MiB), (long long) c[head].cuts[0], + (long long) (c[head].min_spare / MiB)); + // a slower placement is never taken for its headroom beyond the tolerance + std::vector two{{{10}, 100.0, 0}, {{20}, 101.0, 5000 * MiB}}; + CHECK(pick_with_headroom(two, 0.002) == 0); + CHECK(pick_with_headroom(two, 0.02) == 1); + // exact ties with equal spare keep the first (the old pick) + std::vector same{{{10}, 100.0, 7}, {{20}, 100.0, 7}}; + CHECK(pick_with_headroom(same, 0.0) == 0); + CHECK(pick_with_headroom({}, 0.1) == 0); + } if (failed == 0) std::printf("split_rules_test: ok\n"); return failed ? 1 : 0; }