diff --git a/include/strata/core/native_dense.hpp b/include/strata/core/native_dense.hpp index 8330fac5..911b31b6 100644 --- a/include/strata/core/native_dense.hpp +++ b/include/strata/core/native_dense.hpp @@ -25,6 +25,9 @@ public: /// supported type, 2-D), read from the GGUF headers only - so the canonical arena can skip them. static bool served_names(const std::vector& shards, bool include_ple_key, std::set& out, std::string& err); + /// Layer split: load only blocks [lb, le) (every other `blk.N.` projection belongs to another GPU's stage; the + /// PLE tensors are loaded everywhere). Process-wide, read by the next `load`; (-1, -1) = all layers. + static void set_layer_range(int lb, int le); /// #326: a native pack whose `blk.1.ple_key.weight` row is unquantized (iq_pack --compat-bf16 of a GGUF key /// the native kernel also reads, e.g. OrcaRouter's IQ3_XXS) serves the PLE from that row, so it is taken out /// of `skip` and `load` does not upload the GGUF key over it. A quantized row leaves `skip` unchanged. diff --git a/src/core/native_dense.cpp b/src/core/native_dense.cpp index 4a86d48c..86d70fb8 100644 --- a/src/core/native_dense.cpp +++ b/src/core/native_dense.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -13,6 +14,12 @@ namespace strata::core { namespace { +int g_layer_lb = -1, g_layer_le = -1; // set_layer_range; -1: every layer +bool in_range(const std::string& name) { + if (g_layer_lb < 0 || name.rfind("blk.", 0) != 0) return true; + const int l = std::atoi(name.c_str() + 4); + return l >= g_layer_lb && l < g_layer_le; +} bool eligible(const strata::TensorInfo& tensor, bool include_ple_key) { const auto& name = tensor.name; if (name.rfind("blk.", 0) != 0) return false; @@ -53,6 +60,7 @@ bool NativeDense::served_names(const std::vector& shards, bool incl } } +void NativeDense::set_layer_range(int lb, int le) { g_layer_lb = lb; g_layer_le = le; } bool NativeDense::keep_unquantized_ple_key(const std::string& pack_dir, std::set& skip, std::string& err) { const std::string key = "blk.1.ple_key.weight"; @@ -139,6 +147,7 @@ bool NativeDense::load(const std::vector& shards, WeightTable& tabl } for (const auto& tensor : gguf.tensors()) { if (!eligible(tensor, include_ple_key)) continue; + if (!in_range(tensor.name) && tensor.name.find("ple") == std::string::npos) continue; if (!seen.insert(tensor.name).second) { err = "native dense: duplicate tensor " + tensor.name; return false; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index a7c77d0e..30cb7957 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -1984,6 +1984,31 @@ int main(int argc, char** argv) { } if (native_pack) skip.insert("token_embd.weight"); } + // Layer split with explicit split points: every GPU holds only the dense weights of ITS layers (the PLE tensors + // stay everywhere). Without this each card keeps a full copy (~3.4 GB for the Coder) that its stage never + // reads - VRAM the expert cache wants. STRATA_STAGE_TRIM=0 keeps the full copies. + const std::set skip_base = skip; + const bool stage_trim = multi_gpu && !split_auto && !split_at.empty() && + !(std::getenv("STRATA_STAGE_TRIM") && std::string(std::getenv("STRATA_STAGE_TRIM")) == "0"); + auto add_foreign = [&](int64_t lb, int64_t le, std::set& out) { + std::FILE* f = std::fopen((o.pack + "/index.txt").c_str(), "rb"); + if (!f) return; + char line[1024], name[256]; + while (std::fgets(line, sizeof line, f)) { + if (line[0] == '#' || std::sscanf(line, "%255s", name) != 1) continue; + const std::string n = name; + if (n.rfind("blk.", 0) != 0 || n.find("ple") != std::string::npos) continue; + const int64_t l = std::atoll(name + 4); + if (l < lb || l >= le) out.insert(n); + } + std::fclose(f); + }; + if (stage_trim) { + add_foreign(0, split_at[0], skip); + strata::core::NativeDense::set_layer_range(0, (int) split_at[0]); + std::fprintf(stderr, "strata generate: layer split: CUDA0 loads the dense weights of layers 0-%lld only\n", + (long long) split_at[0] - 1); + } uint64_t pool_bytes = 0; if (!strata::core::WeightTable::pool_bytes(o.pack, pool_bytes, err, skip.empty() ? nullptr : &skip)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); @@ -2303,16 +2328,31 @@ int main(int argc, char** argv) { return 1; } const strata::core::OnDevice on(st.dev); + std::set skip_s = skip_base; + uint64_t pool_s = pool_bytes; + if (stage_trim) { + const int64_t lb = split_at[i], le = i + 1 < split_at.size() ? split_at[i + 1] : g.n_layers; + add_foreign(lb, le, skip_s); + strata::core::NativeDense::set_layer_range((int) lb, (int) le); + if (!strata::core::WeightTable::pool_bytes(o.pack, pool_s, err, &skip_s)) { + std::fprintf(stderr, "strata generate: %s\n", err.c_str()); + return 1; + } + std::fprintf(stderr, "strata generate: layer split: CUDA%d loads the dense weights of layers %lld-%lld only\n", + st.dev, (long long) lb, (long long) le - 1); + } else { + skip_s = skip; + } void* arena_s = nullptr; - if (cudaMalloc(&arena_s, pool_bytes) != cudaSuccess || - !st.wt.load(o.pack, arena_s, pool_bytes, err, skip.empty() ? nullptr : &skip)) { + if (cudaMalloc(&arena_s, pool_s) != cudaSuccess || + !st.wt.load(o.pack, arena_s, pool_s, err, skip_s.empty() ? nullptr : &skip_s)) { cudaGetLastError(); size_t free_b = 0, total_b = 0; // #486: what that card had free cudaMemGetInfo(&free_b, &total_b); std::fprintf(stderr, "strata generate: layer split, CUDA%d weights: %s (%llu MiB needed, %llu MiB of %llu " "MiB free on that card)\n", st.dev, err.empty() ? "the weight arena does not fit" : err.c_str(), - (unsigned long long) (pool_bytes >> 20), (unsigned long long) (free_b >> 20), + (unsigned long long) (pool_s >> 20), (unsigned long long) (free_b >> 20), (unsigned long long) (total_b >> 20)); return 1; } @@ -3124,6 +3164,12 @@ int main(int argc, char** argv) { // to hold back is the windows - and `free_b` has already lost the drafter. const int64_t room = stage_room(st.dev, true, false); const strata::core::OnDevice on(st.dev); + { + size_t fb = 0, tb = 0; + cudaMemGetInfo(&fb, &tb); + std::fprintf(stderr, "strata generate: layer split, CUDA%d: %.2f GiB free of %.2f, room for experts %.2f GiB\n", + st.dev, (double) fb / 1073741824.0, (double) tb / 1073741824.0, (double) room / 1073741824.0); + } std::vector sized; int64_t used = 0; for (const auto& pr : st.profile) {