diff --git a/src/dsv41/ds4_experts_rule.hpp b/src/dsv41/ds4_experts_rule.hpp index 25f7c13a..33ca7877 100644 --- a/src/dsv41/ds4_experts_rule.hpp +++ b/src/dsv41/ds4_experts_rule.hpp @@ -24,17 +24,23 @@ inline int experts_per_word(int requested, uint64_t vram_bytes, int model_k) { return n < 1 ? 1 : n; } -// Prompt chunk (--prefill-chunk) of the single-card default, decided once at load from bytes (no timing). The chunk's buffers come out of the VRAM -// the expert tier would have: 1024 -> 4096 tokens takes 83 of 469 resident experts on a 12 GB card, which cost nothing measurable in decode -// (RTX 3060: x0.995 code, x1.002 agent) and made 8K prompts 20% faster (RTX 3060 175 -> 210, RTX 5070 186 -> 223, 32 GB class 285 -> 353 tok/s); -// 8192 returns up to 4% of agent decode. Cards under 10 GiB keep 1024 (their tier is the first thing a bigger buffer would eat). +// Prompt chunk (--prefill-chunk) default, one rule decided once at load from bytes (no timing). The chunk's buffers come out of the VRAM the +// expert tier would have, and every chunk streams the experts the cards do not hold once, so a bigger chunk is fewer passes. Measured (8K prompt): +// - the GPUs hold most of the experts (4 x R9700, share 0.86): 1024 370 tok/s, 8192 214 -> small chunk; +// - the experts are read in place and the CPU lane streams most of them (RTX 5070 / 3060 12 GB, 1 x R9700): 8192 best (5070 244 against 186 +// at 1024, 3060 228 against 175, 1 x R9700 431 against 200) -> the big chunk (the GGUF pack's long-standing default); +// - the experts copied into RAM (--mmap-experts off): H100 32 GiB 2048 502, 4096 538, 8192 495 -> 4096 (not measured on HIP: no). +// Reference KL against the original model does not move with the chunk (1024 0.1705, 2048 0.1692, 4096 0.1688, 8192 0.1695). +// Alloc failure halves the chunk at load (4096 -> 2048 -> 1024 ...), KV and score buffers are allocated whole at the full --max-context. constexpr uint64_t kChunk4096MinVram = 10ull << 30; -// fallback: the value the case had before (1024: an explicit --tp 1 single card; 2048: a GGUF pack with --mmap-experts off, the experts copied into RAM; the experts -// keep 8192, which is not decided here). hip: a HIP build keeps the fallback until a one-card Radeon sweep shows 4096 is no slower. -// The result does not depend on --max-context: the chunk's buffers and the KV are allocated whole at load for the full context, and a -// chunk the card cannot hold is halved there (4096 -> 2048 -> 1024 -> ...), so a session never runs out later. -inline int prefill_chunk_default(uint64_t min_vram_bytes, bool hip = false, int fallback = 1024) { - return !hip && min_vram_bytes >= kChunk4096MinVram ? 4096 : fallback; +// own_default: a single-card run (no --threads, tp 1); gpu_share: the share of the routed experts the cards can hold (1 = all); +// gguf_copy: a GGUF pack with --mmap-experts off; file_experts: the experts stay in the files; hip: a HIP build; min_vram_bytes: the +// smallest card; big: the chunk of the streamed case (the engine's kVlRows, 8192) +inline int prefill_chunk_default(bool own_default, double gpu_share, bool gguf_copy, bool file_experts, bool hip, uint64_t min_vram_bytes, + int big = 8192) { + if (own_default && gpu_share >= 0.5) return 1024; + if (gguf_copy && !hip && min_vram_bytes >= kChunk4096MinVram) return 4096; + return file_experts ? big : 2048; } // DS4_TOPK=N (a test, not for users): over the flag and the rule; returns the count to use diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 4620eee4..b4671266 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -1969,29 +1969,26 @@ struct Model::Impl { o.vram_experts; file_experts = exp_b - std::max(0.0, room - other_b) + (double) kRamMargin > (double) ram_avail(); } - const bool own_default = !(par || o.tp > 1); // (the single-card default run the chunked path newly takes: chunks of 1024 - 8K prompt 370 tok/s on 4 x R9700, 214 at 8192; the threads / tp runs keep theirs) - // the chunk of the cases that are not the experts-read-in-place one (8192, measured best there): an explicit --tp 1 single card - // (was 1024) and the experts copied into RAM (was 2048) take 4096 from 10 GiB of VRAM (ds4_experts_rule.hpp) - // (a GGUF pack always has file_experts set: it is copied into RAM only by an explicit --mmap-experts off, which is the case - // measured: H100 8K, 32 GiB, experts copied: 2048 502, 4096 538, 8192 495 tok/s) + // The default prompt chunk, one rule (ds4_experts_rule.hpp: prefill_chunk_default). Measured: the chunked pass streams the experts the GPUs + // do not hold once per chunk, so where they hold less than half of them a larger chunk is fewer passes (1 x R9700 + 251 GB, 8K prompt: + // 1024 200, 2048 277, 4096 353, 8192 431 tok/s; RTX 5070 / 3060 12 GB in place: 8192 244 / 228 against 1024 186 / 175); where they hold + // most (4 x R9700: 8192 214, 1024 370) the small one wins; with the experts copied into RAM (--mmap-experts off) 4096 beats 8192 by 7-9% + // (H100 32 GiB: 2048 502, 4096 538, 8192 495). A GGUF pack always has file_experts set (copied only by --mmap-experts off). + const bool own_default = !(par || o.tp > 1); // (a single-card run: no --threads, tp 1 - a launch with several cards passes --tp 1; one card passes neither) const bool gguf_copy = pk.gguf && o.mmap_experts == 0; - int own_chunk = 1024, copy_chunk = 2048; - if (!devs.empty() && (own_default || gguf_copy)) { - uint64_t vmin = ~0ull; - for (const Dev& dv : devs) vmin = std::min(vmin, (uint64_t) dv.total); + uint64_t vmin_bytes = ~0ull; + for (const Dev& dv : devs) vmin_bytes = std::min(vmin_bytes, (uint64_t) dv.total); + if (devs.empty()) vmin_bytes = 0; #if defined(__HIP_PLATFORM_AMD__) || defined(__HIPCC__) - const bool hip_build = true; + const bool hip_build = true; #else - const bool hip_build = false; + const bool hip_build = false; #endif - own_chunk = prefill_chunk_default(vmin, hip_build, 1024); - if (gguf_copy) copy_chunk = prefill_chunk_default(vmin, hip_build, 2048); - const int shown = own_default ? own_chunk : copy_chunk; - if (prompt_par && o.prefill_chunk < 0 && shown == 4096) - std::fprintf(stderr, "ds4: prompts in chunks of %d tokens (%.0f GiB of graphics memory: bigger chunks read the experts fewer times; " - "--prefill-chunk N changes it)\n", shown, (double) vmin / (double) (1ull << 30)); - } - chunk = prompt_par ? (o.prefill_chunk >= 0 ? o.prefill_chunk : own_default ? own_chunk : file_experts && !gguf_copy ? kVlRows : copy_chunk) : 0; + const int rule_chunk = prefill_chunk_default(own_default, gpu_expert_share(), gguf_copy, file_experts, hip_build, vmin_bytes, kVlRows); + chunk = prompt_par ? (o.prefill_chunk >= 0 ? o.prefill_chunk : rule_chunk) : 0; + if (prompt_par && o.prefill_chunk < 0 && rule_chunk == 4096) + std::fprintf(stderr, "ds4: prompts in chunks of 4096 tokens (the experts are copied into RAM, %.0f GiB of graphics memory; --prefill-chunk N changes it)\n", + (double) vmin_bytes / (double) (1ull << 30)); const char* dc = std::getenv("DS4_CHUNK"); // (tests: down to 16, a short prompt in several chunks) if (dc != nullptr) chunk = prompt_par ? std::atoi(dc) : 0; if (const char* v = std::getenv("DS4_CHUNK_MIN")) chunk_min = std::max(1, std::atoi(v)); @@ -2836,7 +2833,9 @@ struct Model::Impl { // the staging ring's size (MB) for the experts a chunk streams from the CPU lane: 400 where a good part of them are // streamed; 16 where the cards hold most of the experts (they are within 30% of fitting) (a 4-card box) - the ring's VRAM is then worth more // as expert slots, and the decode after a prompt keeps the windowed path's speed (R9700 x4: 29.6 -> 31.7 tok/s) - size_t ring_mb_auto() const { + // The share of the routed experts the GPUs can hold, from what is known at layout (their free memory less the reserve, the + // prompt path's buffers and the dense weights; the trunk's experts only when no drafter is loaded): 1 = all of them. + double gpu_expert_share(double* exp_out = nullptr, double* room_out = nullptr, double* other_out = nullptr) const { double exp_b = 0, other_b = 0, room = 0; for (const auto& kt : pk.t) { const std::string& n = kt.first; @@ -2847,7 +2846,15 @@ struct Model::Impl { } for (const Dev& d : devs) room += std::max(0.0, (double) d.free0 - (double) ((size_t) o.reserve_mib << 20) - 1.5 * (1 << 30)) * o.vram_experts; - const bool fit = exp_b > 0 && exp_b <= 1.3 * std::max(1.0, room - other_b); // (R9700 x4: 1.16 -> ring 16: decode -1.8% after a prompt, 64: -4.4%, 400: -4.6%) + if (exp_out) *exp_out = exp_b; + if (room_out) *room_out = room; + if (other_out) *other_out = other_b; + return exp_b > 0 ? std::min(1.0, std::max(0.0, room - other_b) / exp_b) : 1.0; + } + size_t ring_mb_auto() const { + double exp_b = 0, other_b = 0, room = 0; + const double share = gpu_expert_share(&exp_b, &room, &other_b); + const bool fit = exp_b > 0 && share >= 1.0 / 1.3; // (R9700 x4: 0.86 -> ring 16: decode -1.8% after a prompt, 64: -4.4%, 400: -4.6%) if (o.verbose) std::fprintf(stderr, "ds4: staging ring: experts %.1f GB, VRAM room %.1f GB, dense %.1f GB -> %d MB\n", exp_b / 1e9, room / 1e9, other_b / 1e9, fit ? 16 : 400); diff --git a/tests/dsv41/experts_rule_test.cpp b/tests/dsv41/experts_rule_test.cpp index 73264141..dec47073 100644 --- a/tests/dsv41/experts_rule_test.cpp +++ b/tests/dsv41/experts_rule_test.cpp @@ -63,30 +63,36 @@ int main() { unsetenv("DS4_TOPK"); #endif CHECK_EQ(experts_test_override(5, 6), 5); - // prompt chunk: 4096 from 10 GiB of VRAM (12 GB cards, 16, 24, 32, 80 GB), 1024 below + // prompt chunk: prefill_chunk_default(own_default, gpu_share, gguf_copy, file_experts, hip, min_vram, big=8192). What `strata launch` runs on one card + // passes neither --tp nor --threads, but the engine's default tp is 2, so own_default is false there; a launch with several cards passes --tp 1. using strata::ds4::prefill_chunk_default; - CHECK_EQ(prefill_chunk_default(12227ull << 20), 4096); - CHECK_EQ(prefill_chunk_default(12 * GiB), 4096); - CHECK_EQ(prefill_chunk_default(32 * GiB), 4096); - CHECK_EQ(prefill_chunk_default(80 * GiB), 4096); - CHECK_EQ(prefill_chunk_default(10 * GiB), 4096); - CHECK_EQ(prefill_chunk_default(10 * GiB - 1), 1024); - CHECK_EQ(prefill_chunk_default(8 * GiB), 1024); - CHECK_EQ(prefill_chunk_default(0), 1024); - CHECK_EQ(prefill_chunk_default(16 * GiB), 4096); - CHECK_EQ(prefill_chunk_default(24 * GiB), 4096); - CHECK_EQ(prefill_chunk_default(12 * GiB, true), 1024); // HIP builds: 1024 until measured - CHECK_EQ(prefill_chunk_default(32 * GiB, true), 1024); - // the experts-copied-into-RAM case (fallback 2048): 4096 on 12/16/24/32 GB cards, 2048 below 10 GiB and on HIP builds - CHECK_EQ(prefill_chunk_default(12227ull << 20, false, 2048), 4096); - CHECK_EQ(prefill_chunk_default(16 * GiB, false, 2048), 4096); - CHECK_EQ(prefill_chunk_default(24 * GiB, false, 2048), 4096); - CHECK_EQ(prefill_chunk_default(32 * GiB, false, 2048), 4096); - CHECK_EQ(prefill_chunk_default(8 * GiB, false, 2048), 2048); - CHECK_EQ(prefill_chunk_default(0, false, 2048), 2048); - CHECK_EQ(prefill_chunk_default(32 * GiB, true, 2048), 2048); - // the rule takes no --max-context: a 128K session on a 12 GB card gets the same 4096, because the chunk's buffers and the KV are allocated - // whole at load for that context and a chunk that does not fit there is halved before the first prompt (4096 -> 2048 -> 1024 -> ...) + // RTX 5070 / RTX 3060 (12 GB), launch default: experts in place -> 8192 (their logs say "prompts in chunks of 8192") + CHECK_EQ(prefill_chunk_default(false, 0.06, false, true, false, 12227ull << 20), 8192); + CHECK_EQ(prefill_chunk_default(false, 0.06, false, true, false, 12 * GiB), 8192); + // the same cards with --tp 1 on one card: share 0.06 < 0.5 -> 8192 as well (the streamed case) + CHECK_EQ(prefill_chunk_default(true, 0.06, false, true, false, 12 * GiB), 8192); + // a 5090-class card (32 GB, share 0.19): in place 8192; with the experts copied into RAM 4096 + CHECK_EQ(prefill_chunk_default(false, 0.19, false, true, false, 32 * GiB), 8192); + CHECK_EQ(prefill_chunk_default(false, 0.19, true, true, false, 32 * GiB), 4096); + CHECK_EQ(prefill_chunk_default(true, 0.19, true, true, false, 32 * GiB), 4096); + CHECK_EQ(prefill_chunk_default(false, 0.10, true, true, false, 16 * GiB), 4096); + CHECK_EQ(prefill_chunk_default(false, 0.05, true, true, false, 24 * GiB), 4096); + CHECK_EQ(prefill_chunk_default(false, 0.06, true, true, false, 12227ull << 20), 4096); + // copied into RAM on a card under 10 GiB (short VRAM): the streamed default, never the 4096 rule + CHECK_EQ(prefill_chunk_default(false, 0.0, true, true, false, 8 * GiB), 8192); + CHECK_EQ(prefill_chunk_default(false, 0.0, true, true, false, 0), 8192); + // 1 x R9700 (HIP, 32 GB, share 0.19), single-card default run (tp 1 passed by a multi-card box): 8192, also when copied (HIP is not measured at 4096) + CHECK_EQ(prefill_chunk_default(true, 0.19, false, true, true, 32 * GiB), 8192); + CHECK_EQ(prefill_chunk_default(true, 0.19, true, true, true, 32 * GiB), 8192); + // 4 x R9700 (share 0.86), single-card default run (--tp 1 --threads off): the small chunk + CHECK_EQ(prefill_chunk_default(true, 0.86, false, true, true, 32 * GiB), 1024); + CHECK_EQ(prefill_chunk_default(true, 0.50, false, true, true, 32 * GiB), 1024); + CHECK_EQ(prefill_chunk_default(true, 0.49, false, true, true, 32 * GiB), 8192); + // threads / tp runs (own_default false) keep their defaults whatever the share: 8192 streamed, 2048 for a pack that is not a GGUF + CHECK_EQ(prefill_chunk_default(false, 0.86, false, true, true, 32 * GiB), 8192); + CHECK_EQ(prefill_chunk_default(false, 0.30, false, false, false, 24 * GiB), 2048); + // the rule takes no --max-context: a 128K session on a 12 GB card gets the same chunk, because the chunk's buffers and the KV are + // allocated whole at load for that context and a chunk that does not fit there is halved before the first prompt (4096 -> 2048 -> 1024 ...) if (fails == 0) std::printf("experts rule: all checks passed\n"); return fails == 0 ? 0 : 1; }