diff --git a/include/strata/program/split_rules.hpp b/include/strata/program/split_rules.hpp index 682255d9b0..b186cfb7ce 100644 --- a/include/strata/program/split_rules.hpp +++ b/include/strata/program/split_rules.hpp @@ -6,6 +6,7 @@ // only falls back to the unrestricted best when no such placement can start the prompt path. #pragma once +#include #include #include @@ -35,6 +36,57 @@ inline bool has_tiny_stage(const std::vector& cuts, int64_t n_layers) { return smallest_stage(cuts, n_layers) < kMinStageLayers; } +// ---- #2644: the #1616 floor must not starve a card. On an asymmetric pair (RTX 4060 Ti 16 GB + RTX 3060 12 GB, 48 +// layers, IQ3_S) the predicted decode window prefers almost every layer on the faster card, the first pick was K=2, and the +// #1616 retry (every stage at least kMinStageLayers) landed on the edge of that restriction: K=3, the 3060 with 3 layers, +// 12 GB idle, 452 tok/s of prefill against 802 at 28 layers (the prompt flows through the stages as a pipeline, so one +// busy card is the whole rig). So before that retry, a two-stage rig is asked for a placement in which each stage owns at +// least its share of the layers by its room for experts; the three-layer rule stays the second try, and the first pick +// the last. + +/// Per stage, the fewest layers the share-of-room retry accepts: floor(n_layers x room_i / sum of rooms), at least +/// kMinStageLayers, and (when the floors would not fit) trimmed from the largest back toward kMinStageLayers until +/// they do. Rooms <= 0 count as 0; no room anywhere gives every stage kMinStageLayers. +inline std::vector share_floors(const std::vector& room, int64_t n_layers) { + std::vector floors(room.size(), kMinStageLayers); + double total = 0.0; + for (const int64_t r : room) total += r > 0 ? (double) r : 0.0; + if (total <= 0.0) return floors; + int64_t sum = 0; + for (size_t i = 0; i < room.size(); ++i) { + const double share = (room[i] > 0 ? (double) room[i] : 0.0) / total * (double) n_layers; + floors[i] = std::max(kMinStageLayers, (int64_t) share); // truncated: the floors sum to <= n_layers + sum += floors[i]; + } + while (sum > n_layers) { // only through the kMinStageLayers lift of a small share + size_t big = 0; + for (size_t i = 1; i < floors.size(); ++i) + if (floors[i] > floors[big]) big = i; + if (floors[big] <= kMinStageLayers) break; + --floors[big]; + --sum; + } + return floors; +} + +/// True when some stage of `cuts` owns fewer layers than its floor. +inline bool below_floors(const std::vector& cuts, int64_t n_layers, const std::vector& floors) { + int64_t prev = 0; + for (size_t i = 0; i <= cuts.size() && i < floors.size(); ++i) { + const int64_t end = i < cuts.size() ? cuts[i] : n_layers; + if (end - prev < floors[i]) return true; + prev = end; + } + return false; +} + +/// The share-of-room retry is worth running when some floor is above the plain three-layer rule's. +inline bool floors_exceed_minimum(const std::vector& floors) { + for (const int64_t f : floors) + if (f > kMinStageLayers) return true; + return false; +} + // ---- #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 diff --git a/src/program/generate.cpp b/src/program/generate.cpp index cc2a1aeeda..e2980f7751 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -4293,12 +4293,15 @@ int main(int argc, char** argv) { }; bool gate_on = true; // off only when no placement can start (below): then the best one is kept and a warning said int64_t min_stage_layers = 1; // #1616: 3 in the second pass below, when the first pick has a one- or two-layer stage + std::vector stage_floors; // #2644: each stage's share of the room, in the share pass below (empty: off) // #1428: 2 and 3 GPUs - every placement priced, kept (a few hundred) so the near-tie warning below can name the // best placement that is really a different one (not the neighbour one layer over) std::vector>> priced; auto consider = [&]() { if (min_stage_layers > 1 && strata::program::split_rules::smallest_stage(at, g.n_layers) < min_stage_layers) return; + if (!stage_floors.empty() && strata::program::split_rules::below_floors(at, g.n_layers, stage_floors)) + return; double hm = 0; int64_t held = 0; const double ms = predict(at, hm, held); @@ -4394,6 +4397,7 @@ int main(int argc, char** argv) { (double) (k3 - k2) * layer_ms[2] + (double) (L - k3) * layer_ms[3]; if (min_stage_layers > 1 && strata::program::split_rules::smallest_stage({k1, k2, k3}, L) < min_stage_layers) continue; + if (!stage_floors.empty() && strata::program::split_rules::below_floors({k1, k2, k3}, L, stage_floors)) continue; ++tried; const double mx = stage_max({k1, k2, k3}); if (!(ms < best_ms - tie_eps || (ms <= best_ms + tie_eps && mx < best_max - tie_eps))) continue; @@ -4435,28 +4439,57 @@ int main(int argc, char** argv) { strata::program::split_rules::has_tiny_stage(best, g.n_layers)) { const std::vector first = best; const double first_ms = best_ms; - best.clear(); - best_ms = 1e30; best_max = 1e30; best_mass = 0; best_held = 0; - min_stage_layers = strata::program::split_rules::kMinStageLayers; - search(); - min_stage_layers = 1; auto fmt = [](const std::vector& v) { std::string s; for (const int64_t k : v) s += (s.empty() ? "" : ",") + std::to_string(k); return s; }; - if (best.empty()) { - best = first; best_ms = first_ms; - std::fprintf(stderr, "strata generate: layer split auto: K=%s gives a stage fewer than %lld layers (a " - "tiny expert cache clamps the prompt chunk, #1616), but no placement with at least " - "that many layers can start; keeping it\n", fmt(first).c_str(), - (long long) strata::program::split_rules::kMinStageLayers); - } else { - std::fprintf(stderr, "strata generate: layer split auto: K=%s would give a stage fewer than %lld layers " - "(a tiny expert cache there clamps the prompt chunk of every card, #1616); chose " - "K=%s instead (%.1f ms per decode window, not %.1f)\n", fmt(first).c_str(), - (long long) strata::program::split_rules::kMinStageLayers, fmt(best).c_str(), best_ms, - first_ms); + // ---- #2644 (two GPUs): first a placement in which each stage owns at least its share of the layers by its room for + // experts. The three-layer rule below lands on its own edge when the card that is predicted slower has little + // room (4060 Ti 16 GB + 3060 12 GB: K=3, the 3060 idle, -44% prefill). The rooms are read at the even split + // (a session and a later stage's weights depend on the range). + bool share_pick = false; + if (ns == 2) { + std::vector rooms((size_t) ns); + for (int i = 0; i < ns; ++i) + rooms[(size_t) i] = stage_left(i, g.n_layers * i / ns, g.n_layers * (i + 1) / ns); + const std::vector floors = strata::program::split_rules::share_floors(rooms, g.n_layers); + if (strata::program::split_rules::floors_exceed_minimum(floors)) { + best.clear(); + best_ms = 1e30; best_max = 1e30; best_mass = 0; best_held = 0; + stage_floors = floors; + search(); + stage_floors.clear(); + share_pick = !best.empty(); + if (share_pick) + std::fprintf(stderr, "strata generate: layer split auto: K=%s would give a stage fewer than %lld layers " + "(a tiny expert cache there clamps the prompt chunk of every card, #1616) and " + "leave that card idle; chose K=%s instead, which gives each card at least its " + "share of the layers by its room for experts (%.1f ms per decode window, not %.1f; " + "--layer-split N sets it by hand, #2644)\n", fmt(first).c_str(), + (long long) strata::program::split_rules::kMinStageLayers, fmt(best).c_str(), best_ms, + first_ms); + } + } + if (!share_pick) { + best.clear(); + best_ms = 1e30; best_max = 1e30; best_mass = 0; best_held = 0; + min_stage_layers = strata::program::split_rules::kMinStageLayers; + search(); + min_stage_layers = 1; + if (best.empty()) { + best = first; best_ms = first_ms; + std::fprintf(stderr, "strata generate: layer split auto: K=%s gives a stage fewer than %lld layers (a " + "tiny expert cache clamps the prompt chunk, #1616), but no placement with at least " + "that many layers can start; keeping it\n", fmt(first).c_str(), + (long long) strata::program::split_rules::kMinStageLayers); + } else { + std::fprintf(stderr, "strata generate: layer split auto: K=%s would give a stage fewer than %lld layers " + "(a tiny expert cache there clamps the prompt chunk of every card, #1616); chose " + "K=%s instead (%.1f ms per decode window, not %.1f)\n", fmt(first).c_str(), + (long long) strata::program::split_rules::kMinStageLayers, fmt(best).c_str(), best_ms, + first_ms); + } } } // ---- no startable placement: fall back to proportional, and if that too is impossible, say what is short diff --git a/tests/program/split_rules_test.cpp b/tests/program/split_rules_test.cpp index 2ac80464aa..9c3aac3e76 100644 --- a/tests/program/split_rules_test.cpp +++ b/tests/program/split_rules_test.cpp @@ -91,6 +91,55 @@ int main() { CHECK(big[0] == 1.0 && big[1] > 4.3 && big[1] < 4.4 && big[2] == 64.0 && big[3] == 1.0); CHECK(link_miss_scale({-1.0, -1.0})[1] == 1.0); } + // #2644: RTX 4060 Ti 16 GB + RTX 3060 12 GB, 48 layers. The reporter's numbers: CUDA1 5.50 GiB free of 11.63 (the + // 3060 side), CUDA0 the 16 GB card with ~14.2 GiB room. The #1616 retry alone took K=3 (3 layers on the second card) + // although the first pick K=2 was the same problem; the share of the room gives it 13 layers of 48. + { + const int64_t MiB = 1 << 20, L = 48; + const std::vector room{14500 * MiB, 5632 * MiB}; + const std::vector f = share_floors(room, L); + CHECK(f.size() == 2 && f[0] == 34 && f[1] == 13); + CHECK(floors_exceed_minimum(f)); + // the cut K = the second stage's first layer. With the big card first, the old rule's edge is K=45 (the second card + // with 3 layers): below its floor. + CHECK(below_floors({45}, L, f)); + CHECK(below_floors({47}, L, f)); + CHECK(!below_floors({35}, L, f)); // second card: 13 layers + CHECK(!below_floors({34}, L, f)); // 14 layers + CHECK(below_floors({36}, L, f)); // 12 layers: under its share + CHECK(below_floors({33}, L, f)); // the big card under its share + // the same pair the other way round: the small card first + const std::vector r = share_floors({room[1], room[0]}, L); + CHECK(r[0] == 13 && r[1] == 34); + CHECK(below_floors({3}, L, r)); // the old retry's K=3 starves the small card + CHECK(!below_floors({13}, L, r)); + CHECK(!below_floors({14}, L, r)); + // a predicted-time pick under the floors sits at the edge of the floor, not at the three-layer edge + // (layer ms 0.82 on the 4060 Ti, 1.45 on the 3060 as the engine prices them: fewer layers on the 3060 predict faster) + auto pick = [&](const std::vector& fl) { + int64_t best_k = -1; + double best = 1e30; + for (int64_t k = 2; k < L; ++k) { + if (!fl.empty() && below_floors({k}, L, fl)) continue; + if (smallest_stage({k}, L) < (fl.empty() ? kMinStageLayers : 0)) continue; + const double ms = (double) k * 1.45 + (double) (L - k) * 0.82; // card 0 = the 3060 here + if (ms < best) { best = ms; best_k = k; } + } + return best_k; + }; + CHECK(pick({}) == 3); // today: the 3060 gets 3 layers + CHECK(pick(r) == 13); // with the floors: its share + std::printf("split_rules_test: #2644 floors %lld,%lld; 3060 first: old retry K=%lld, share retry K=%lld\n", + (long long) f[0], (long long) f[1], (long long) pick({}), (long long) pick(r)); + // identical cards: the even split, and the floors never exceed the layers + const std::vector eq = share_floors({10000 * MiB, 10000 * MiB}, L); + CHECK(eq[0] == 24 && eq[1] == 24); + const std::vector three = share_floors({1 * MiB, 1 * MiB, 30000 * MiB}, 12); + CHECK(three[0] == 3 && three[1] == 3 && three[2] == 6); // lifted to 3, the big one trimmed to fit 12 + CHECK(share_floors({0, 0}, L)[0] == kMinStageLayers && share_floors({-5, 0}, L)[1] == kMinStageLayers); + CHECK(!floors_exceed_minimum(share_floors({0, 0}, L))); + CHECK(!below_floors({}, 48, {})); + } if (failed == 0) std::printf("split_rules_test: ok\n"); return failed ? 1 : 0; }