diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ed15426..ac489599 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1134,6 +1134,10 @@ if(STRATA_BUILD_TESTS OR STRATA_BUILD_CONVERSATION_TESTS) add_executable(batch_rows_test tests/core/batch_rows_test.cpp) target_include_directories(batch_rows_test PRIVATE include) add_test(NAME batch_rows_test COMMAND batch_rows_test) + # how many pipelined slot groups --batch runs with: the 0.1.41 default, and --batch-mtp keeping it serial (header-only, CPU) + add_executable(batch_groups_test src/program/batch_groups_test.cpp) + target_include_directories(batch_groups_test PRIVATE include) + add_test(NAME batch_groups_test COMMAND batch_groups_test) endif() add_library(strata_kernels_cpu STATIC src/kernels/cpu/expert.cpp src/kernels/cpu/pool.cpp diff --git a/docs/BATCHING.md b/docs/BATCHING.md index 4f706617..0b234a8f 100644 --- a/docs/BATCHING.md +++ b/docs/BATCHING.md @@ -47,7 +47,7 @@ With a layer split, the engine options go into the config's `args`: | --- | --- | | `"parallel": N` / `--batch N` / `--slots N` (2..8 normally) | up to N conversations have batch slots; more requests wait for a free slot. Each slot gets its own state (a session carved like the stage's own: GDN recurrence, QSA K/V and indexer, PLE history) on every GPU of the split. With grouped MTP, more than 8 slots can rotate through eight-row windows if memory permits. | | `--batch-groups G` | with a layer split (default: auto, below): the N slots in G groups that flow through the GPUs as a pipeline (GPU k runs one group while GPU k+1 runs another). G must divide N. 1 = all slots in one window, GPU after GPU. | -| `--batch-groups auto` | the default on a layer split since 0.1.41 (give no `--batch-groups`): the engine pipelines one group per GPU stage (the most that divide the slots; 8 slots on 4 GPUs = 4 groups of 2) and says so (`INFO batch_groups=G`). `--batch-groups 1` turns it off (all slots in one window, GPU after GPU). Measured, 8 clients, total tok/s against one group: 4 x R9700 166 against 86, 2 GPUs 109 against 78, 3 GPUs 110 against 78. | +| `--batch-groups auto` | the default on a layer split since 0.1.41 (give no `--batch-groups`): the engine pipelines one group per GPU stage (the most that divide the slots; 8 slots on 4 GPUs = 4 groups of 2) and says so (`INFO batch_groups=G`). `--batch-groups 1` turns it off (all slots in one window, GPU after GPU). Measured, 8 clients, total tok/s against one group: 4 x R9700 166 against 86, 2 GPUs 109 against 78, 3 GPUs 110 against 78. With `--batch-mtp` and no `--batch-groups` the default is one group (the slots' MTP drafts do not run in pipelined groups) and the log says so; `--batch-groups G` or `--batch-groups auto` given with `--batch-mtp` pipelines the groups and turns `--batch-mtp` off. | | `--trim-stage-weights` | with an **explicit** `--layer-split` (e.g. `12,24,36`, not `auto`): every GPU loads only the dense weights of its own layers instead of the whole model's (the same as `STRATA_STAGE_TRIM=1`, PR #639). The VRAM this frees goes to the expert cache. Useful without `--batch` too. | The engine never refuses a count it cannot run: it says so in its log and runs what it can - at most 8 slots by diff --git a/include/strata/program/batch_groups.hpp b/include/strata/program/batch_groups.hpp new file mode 100644 index 00000000..0cc7c1d9 --- /dev/null +++ b/include/strata/program/batch_groups.hpp @@ -0,0 +1,62 @@ +// include/strata/program/batch_groups.hpp - how many pipelined slot groups `--batch` runs with. +// +// `--batch-groups N` pipelines N groups of slots through the stages of a layer split; `--batch-groups auto` picks the +// most groups (at most one per stage) that divide the slots. Since 0.1.41 a layer split with `--batch` >= 2 and no +// `--batch-groups` at all means `auto`. The pipelined path does not run `--batch-mtp`'s drafts, so when `--batch-mtp` +// is asked for and the number of groups was NOT given, `auto` resolves to one group (the serial windows the drafts run +// in) and says why. A number or `auto` given on the command line is honoured as before. +// +// Pure (no GPU, no environment read): generate.cpp passes in what it knows. +#pragma once + +namespace strata::program::batch_groups { + +/// What kept `auto` from pipelining. +enum class Serial { None, BatchMtp }; + +struct Input { + bool set = false; ///< --batch-groups was given (a number or `auto`) + bool auto_given = false; ///< ... as `auto` + int given = 1; ///< the number given (1 when none) + int batch = 0; ///< the slots + int later_stages = 0; ///< the layer split's stages after CUDA0 (0: no layer split) + bool batch_mtp = false; ///< --batch-mtp is on (it passed the gates that do not concern the groups) +}; + +struct Result { + int groups = 1; + bool from_auto = false; ///< the number came from `auto` (given or the 0.1.41 default) + Serial serial = Serial::None; ///< `auto` would have pipelined, but --batch-mtp needs the serial windows + int would_be = 1; ///< what `auto` would have picked without it (== groups unless `serial`) +}; + +/// The most groups, at most one per stage of the layer split (`later_stages` + 1 GPUs), that divide `batch`. +inline int auto_groups(int later_stages, int batch) { + int best = 1; + if (later_stages <= 0) return best; + for (int d = 2; d <= later_stages + 1 && d <= batch; ++d) + if (batch % d == 0) best = d; + return best; +} + +inline Result resolve(const Input& in) { + Result r; + r.groups = in.given; + r.would_be = in.given; + const bool defaulted = !in.set && in.later_stages > 0 && in.batch > 1; // the 0.1.41 default + if (!in.auto_given && !defaulted) return r; + r.from_auto = true; + r.would_be = auto_groups(in.later_stages, in.batch); + r.groups = r.would_be; + if (!in.set && r.would_be > 1 && in.batch_mtp) { // only the default gives way; `--batch-groups auto` asked for the pipeline + r.serial = Serial::BatchMtp; + r.groups = 1; + } + return r; +} + +inline const char* serial_what(Serial s) { + return s == Serial::BatchMtp ? "--batch-mtp" : ""; +} + +} // namespace strata::program::batch_groups diff --git a/src/program/batch_groups_test.cpp b/src/program/batch_groups_test.cpp new file mode 100644 index 00000000..065fd5f6 --- /dev/null +++ b/src/program/batch_groups_test.cpp @@ -0,0 +1,93 @@ +// src/program/batch_groups_test.cpp - how many pipelined slot groups --batch runs with (header-only, CPU). +// +// 1. `auto` picks the most groups, at most one per stage, that divide the slots; +// 2. a layer split with --batch >= 2 and no --batch-groups means `auto` (0.1.41); +// 3. --batch-mtp turns that default into one group, and says which feature did it; +// 4. a number or `auto` given explicitly is honoured, with or without --batch-mtp; +// 5. no layer split: the number given (1) stays. +#include "strata/program/batch_groups.hpp" + +#include + +namespace bg = strata::program::batch_groups; + +namespace { +int g_fail = 0; +void check(bool ok, const char* what) { + std::printf(" %-78s %s\n", what, ok ? "ok" : "FAIL"); + if (!ok) ++g_fail; +} +bg::Input in(int batch, int stages) { + bg::Input i; + i.batch = batch; + i.later_stages = stages; + return i; +} +} // namespace + +int main() { + std::printf("batch_groups_test\n"); + check(bg::auto_groups(0, 4) == 1, "auto: no layer split, one group"); + check(bg::auto_groups(1, 2) == 2 && bg::auto_groups(1, 4) == 2 && bg::auto_groups(1, 3) == 1, + "auto: two GPUs, 2 groups when the slots divide by 2"); + check(bg::auto_groups(3, 8) == 4 && bg::auto_groups(3, 6) == 3 && bg::auto_groups(3, 2) == 2, + "auto: at most one group per GPU, and a divisor of the slots"); + + { // the 0.1.41 default + const auto r = bg::resolve(in(2, 1)); + check(r.groups == 2 && r.from_auto && r.serial == bg::Serial::None, "layer split, --batch 2, nothing given: 2 groups"); + } + { + const auto r = bg::resolve(in(1, 1)); + check(r.groups == 1 && !r.from_auto, "--batch 1: no default"); + } + { + const auto r = bg::resolve(in(2, 0)); + check(r.groups == 1 && !r.from_auto, "no layer split: one group"); + } + + { // the shape this is for: parallel 2 on a 2-GPU split with --batch-mtp + bg::Input i = in(2, 1); + i.batch_mtp = true; + const auto r = bg::resolve(i); + check(r.groups == 1 && r.from_auto && r.serial == bg::Serial::BatchMtp && r.would_be == 2, + "--batch-mtp, nothing given: one group, because of --batch-mtp"); + check(bg::serial_what(r.serial)[0] == '-', "the reason names the feature"); + } + { // nothing to give way: the slots do not divide, so auto is one group anyway and there is nothing to say + bg::Input i = in(3, 1); + i.batch_mtp = true; + const auto r = bg::resolve(i); + check(r.groups == 1 && r.serial == bg::Serial::None && r.would_be == 1, "auto would be 1 group anyway: no reason"); + } + { // explicit values are honoured + bg::Input i = in(4, 1); + i.set = true; + i.given = 2; + i.batch_mtp = true; + const auto r = bg::resolve(i); + check(r.groups == 2 && !r.from_auto && r.serial == bg::Serial::None, "--batch-groups 2 with --batch-mtp: 2"); + i.given = 1; + check(bg::resolve(i).groups == 1, "--batch-groups 1: 1 (the opt-out)"); + i.given = 4; // not checked here: generate.cpp refuses what does not divide + check(bg::resolve(i).groups == 4, "--batch-groups 4 is returned as given"); + } + { + bg::Input i = in(2, 1); + i.set = true; + i.auto_given = true; + i.batch_mtp = true; + const auto r = bg::resolve(i); + check(r.groups == 2 && r.from_auto && r.serial == bg::Serial::None, + "--batch-groups auto with --batch-mtp: auto asked for the pipeline, 2 groups"); + } + { + bg::Input i = in(2, 0); + i.set = true; + i.given = 2; + check(bg::resolve(i).groups == 2, "an explicit number without a split is returned (the engine warns)"); + } + + std::printf("%s\n", g_fail == 0 ? "batch_groups_test: all passed" : "batch_groups_test: FAILED"); + return g_fail == 0 ? 0 : 1; +} diff --git a/src/program/generate.cpp b/src/program/generate.cpp index e07686df..fe1c48e0 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -72,6 +72,7 @@ #include "strata/spec/draft_policy.hpp" #include "strata/spec/draft_source.hpp" #include "strata/spec/suffix_drafter.hpp" +#include "strata/program/batch_groups.hpp" #include "strata/kernels/cvec.hpp" #include "strata/core/progress.hpp" #include "strata/core/device.hpp" @@ -4117,15 +4118,31 @@ int main(int argc, char** argv) { // force: when it cannot run it is said and left off (plain batching still starts); default off = exactly 0.1.39. // With a layer split the slots' drafters live on the LAST stage's GPU (where the solo drafter, the head and the final // residual rows are); the windows are the ones --batch runs, two rows per slot, through every stage. + // --batch-groups: given as a number or auto, or (0.1.41) auto by default for a layer split with --batch >= 2. The + // pipelined slot groups do not run --batch-mtp's drafts, so when it is asked for and the groups were not given, the + // default is one group (batch_groups.hpp). Evaluated again once the slots that fit are known, and BEFORE the + // --batch-mtp gate below, which must see the resolved number. + auto batch_groups_for = [&](bool mtp_on) { + strata::program::batch_groups::Input bin; + bin.set = o.batch_groups_set; + bin.auto_given = o.batch_groups_auto; + bin.given = o.batch_groups; + bin.batch = o.batch; + bin.later_stages = (int) stages.size(); + bin.batch_mtp = mtp_on; + return strata::program::batch_groups::resolve(bin); + }; const char* batch_mtp_env = std::getenv("STRATA_BATCH_MTP"); bool batch_mtp = o.batch_mtp || (batch_mtp_env != nullptr && batch_mtp_env[0] != '\0' && batch_mtp_env[0] != '0'); if (batch_mtp) { // (--pipeline-windows is already off with --batch slots) const char* why = o.batch < 2 ? "it needs --batch 2 or more" : o.mtp.empty() ? "it needs --mtp" : o.spec < 2 ? "it needs --spec T (T >= 2)" : split_same - ? "it needs each stage of a layer split on its own GPU" : (o.batch_groups > 1 && !stages.empty()) - ? "it does not combine with --batch-groups (pipelined slot groups) yet" + ? "it needs each stage of a layer split on its own GPU" : !o.serve ? "it needs --serve" : nullptr; + // the groups it would run with (the default gives way to it: one group; a number or auto given pipelines) + if (why == nullptr && !stages.empty() && batch_groups_for(true).groups > 1) + why = "it does not combine with --batch-groups (pipelined slot groups) yet"; if (why != nullptr) { std::fprintf(stderr, "strata generate: WARNING: --batch-mtp is off: %s\n", why); batch_mtp = false; @@ -4147,17 +4164,22 @@ int main(int argc, char** argv) { } // --batch-groups auto: the pipeline needs one group per GPU stage to keep every card busy (4 x R9700, 8 clients: // 90 tok/s in one group, 136 in 2, 166 in 4). The most groups, at most one per stage, that divide the slots. + // The default (0.1.41: a layer split with --batch pipelines one group per stage; --batch-groups 1 opts out) gives + // way to --batch-mtp, which needs the serial windows: one group, said once. + int groups_said = 0; auto resolve_groups_auto = [&] { - // default (0.1.41): a layer split with --batch pipelines one group per stage; --batch-groups 1 opts out - if (!o.batch_groups_set && !stages.empty() && o.batch > 1) o.batch_groups_auto = true; - if (!o.batch_groups_auto) return; - int best = 1; - for (int d = 2; d <= (int) stages.size() + 1 && d <= o.batch; ++d) - if (o.batch % d == 0) best = d; - if (stages.empty()) best = 1; - o.batch_groups = best; - std::fprintf(stderr, "strata generate: --batch-groups auto: %d group%s of %d slot%s\n", best, best == 1 ? "" : "s", - o.batch / best, o.batch / best == 1 ? "" : "s"); + const auto r = batch_groups_for(batch_mtp); + if (!r.from_auto) return; + o.batch_groups = r.groups; + if (groups_said == r.groups) return; // (the second call, once the slots that fit are known, says nothing new) + groups_said = r.groups; + if (r.serial != strata::program::batch_groups::Serial::None) + std::fprintf(stderr, "strata generate: --batch-groups auto: 1 group of %d slot%s - %s does not run in pipelined " + "groups; --batch-groups %d would pipeline them and turn it off\n", o.batch, + o.batch == 1 ? "" : "s", strata::program::batch_groups::serial_what(r.serial), r.would_be); + else + std::fprintf(stderr, "strata generate: --batch-groups auto: %d group%s of %d slot%s\n", r.groups, + r.groups == 1 ? "" : "s", o.batch / r.groups, o.batch / r.groups == 1 ? "" : "s"); }; if (o.batch > 0) resolve_groups_auto(); if (o.batch > 0 && o.batch_groups > 1 && (stages.empty() || o.batch % o.batch_groups != 0)) {