Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

layer split auto: avoid a one- or two-layer stage (#1616)

Niko12211c6c82ef4fed
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6e0ce1c49..782f5caf8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -250,6 +250,11 @@ if(STRATA_BUILD_TESTS)
add_executable(tuning_header_arch_family_test tests/prefill/tuning_header_arch_family_test.cpp)
target_include_directories(tuning_header_arch_family_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/prefill")
add_test(NAME tuning_header_arch_family_test COMMAND tuning_header_arch_family_test)
+
+ # the auto layer split's minimum stage size (#1616): header-only rule, no GPU
+ add_executable(split_rules_test tests/program/split_rules_test.cpp)
+ target_include_directories(split_rules_test PRIVATE include)
+ add_test(NAME split_rules_test COMMAND split_rules_test)
endif()
# ---- strata_plan ----------------------------------------------------------
diff --git a/include/strata/program/split_rules.hpp b/include/strata/program/split_rules.hpp
new file mode 100644
index 000000000..b9382272f
--- /dev/null
+++ b/include/strata/program/split_rules.hpp
@@ -0,0 +1,38 @@
+// include/strata/program/split_rules.hpp - rules for the automatic layer split that do not need a GPU to check.
+//
+// A stage that owns one or two layers holds a handful of expert slots (a 12 GB 3060 with one layer held 512), and
+// the prompt chunk of the whole pipeline is clamped to what the smallest cache can lend (#448): 576 -> 347 tok/s in
+// #1616. So the search first looks for a placement in which every stage owns at least kMinStageLayers layers, and
+// only falls back to the unrestricted best when no such placement can start the prompt path.
+#pragma once
+
+#include <cstdint>
+#include <vector>
+
+namespace strata::program::split_rules {
+
+constexpr int64_t kMinStageLayers = 3;
+
+/// The restriction only makes sense when the model has the layers for it.
+inline bool restriction_possible(int64_t n_layers, int stages) {
+ return stages >= 2 && n_layers >= kMinStageLayers * stages;
+}
+
+/// cuts: the first layer of each later stage (stages - 1 of them); n_layers: the model's layers.
+/// Returns the size of the smallest stage.
+inline int64_t smallest_stage(const std::vector<int64_t>& cuts, int64_t n_layers) {
+ int64_t lo = n_layers, prev = 0;
+ for (const int64_t c : cuts) {
+ if (c - prev < lo) lo = c - prev;
+ prev = c;
+ }
+ if (n_layers - prev < lo) lo = n_layers - prev;
+ return lo;
+}
+
+/// True when some stage owns fewer than kMinStageLayers layers.
+inline bool has_tiny_stage(const std::vector<int64_t>& cuts, int64_t n_layers) {
+ return smallest_stage(cuts, n_layers) < kMinStageLayers;
+}
+
+} // namespace strata::program::split_rules
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 433d39576..285954dd6 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -60,6 +60,7 @@
#include "strata/core/native_dense.hpp"
#include "strata/program/logits_selection.hpp"
#include "strata/program/conv_cache.hpp"
+#include "strata/program/split_rules.hpp"
#include "strata/program/message_boundary.hpp"
#include "strata/spec/draft_policy.hpp"
#include "strata/spec/draft_source.hpp"
@@ -3555,7 +3556,10 @@ int main(int argc, char** argv) {
return parts;
};
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
auto consider = [&]() {
+ if (min_stage_layers > 1 && strata::program::split_rules::smallest_stage(at, g.n_layers) < min_stage_layers)
+ return;
double hm = 0;
int64_t held = 0;
const double ms = predict(at, hm, held);
@@ -3647,6 +3651,8 @@ int main(int argc, char** argv) {
const double ms = miss_ms * (1.0 - hm / denom) +
(double) k1 * layer_ms[0] + (double) (k2 - k1) * layer_ms[1] +
(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;
++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;
@@ -3680,6 +3686,38 @@ int main(int argc, char** argv) {
}
};
search();
+ // ---- #1616: a stage with one or two layers holds a handful of expert slots (a 12 GB 3060: 512), and the
+ // prompt chunk of every card is clamped to what the smallest cache can lend. When the pick has such a
+ // stage, look again among placements that give every stage at least kMinStageLayers layers; keep the new
+ // pick if there is one, and say so. Recommend, never force: with none startable the first pick stays.
+ if (!best.empty() && strata::program::split_rules::restriction_possible(g.n_layers, ns) &&
+ strata::program::split_rules::has_tiny_stage(best, g.n_layers)) {
+ const std::vector<int64_t> 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<int64_t>& 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);
+ }
+ }
// ---- no startable placement: fall back to proportional, and if that too is impossible, say what is short
auto split_diagnosis = [&](const std::vector<int64_t>& at) {
const int64_t c = 512;
diff --git a/tests/program/split_rules_test.cpp b/tests/program/split_rules_test.cpp
new file mode 100644
index 000000000..4c3140a2b
--- /dev/null
+++ b/tests/program/split_rules_test.cpp
@@ -0,0 +1,34 @@
+// The auto layer split's stage-size rule (#1616): no GPU, no model.
+#include <cstdio>
+#include <cstdlib>
+#include <vector>
+
+#include "strata/program/split_rules.hpp"
+
+using namespace strata::program::split_rules;
+
+static int failed = 0;
+#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
+
+int main() {
+ // the reporter's 4-GPU pick on a 48-layer model: layers 0-1, 2, 3-19, 20-47 -> cuts 2,3,20
+ CHECK(smallest_stage({2, 3, 20}, 48) == 1);
+ CHECK(has_tiny_stage({2, 3, 20}, 48));
+ // the manual split that fixed it: 4,8,24 is not a cut list of 4 stages; as cuts 4,8,24 -> 4,4,16,24
+ CHECK(smallest_stage({4, 8, 24}, 48) == 4);
+ CHECK(!has_tiny_stage({4, 8, 24}, 48));
+ // identical cards: the balanced split has no tiny stage, so the rule never fires and the pick stays
+ CHECK(!has_tiny_stage({12, 24, 36}, 48));
+ CHECK(!has_tiny_stage({24}, 48));
+ CHECK(has_tiny_stage({47}, 48));
+ CHECK(has_tiny_stage({2}, 48));
+ CHECK(!has_tiny_stage({3}, 48));
+ CHECK(smallest_stage({}, 48) == 48);
+ // the restriction needs the layers for it
+ CHECK(restriction_possible(48, 4));
+ CHECK(restriction_possible(12, 4));
+ CHECK(!restriction_possible(11, 4));
+ CHECK(!restriction_possible(48, 1));
+ if (failed == 0) std::printf("split_rules_test: ok\n");
+ return failed ? 1 : 0;
+}