From 7ee50cc8c71c94d504e38dee4ebc7e7fdfee6eee Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 10 Nov 2026 18:21:45 +0100 Subject: [PATCH] split: price each stage's prompt loan on its own card (#5722) Prefill::bytes_needed answers for the current device (prefill.cpp fused_ring decides the fused layout per card), and part_slots / ring_room priced every participant of a layer split with whatever device was current, CUDA0's. On an RTX 5080 + RTX 4090 split the 4090's loan was 2882 MiB for buffers that take 3014 MiB there, and its prompt path stepped down to small chunks. New helper split_ring.hpp split_part_figure(on_device, dev, fn) evaluates fn() with the participant's device current (OnDevice, put back after); generate.cpp part_slots and ring_room's bytes_needed_no_ring go through it. CUDA0's own part (dev -1) is priced as before. On like cards every device gives the same figure, so the numbers are unchanged. Test: tests/program/split_part_price_test.cpp (cpu-check, faked devices). With the old pricing: CUDA1 lent 2880 MiB for 3014 MiB of buffers, reads 6144-token chunks (fail); now lent 3016 MiB, reads 8192; 2 and 4 like cards give identical slot counts over the 256..32768 chunk grid. No mixed-card machine was available: no real mixed-card run backs this. --- CMakeLists.txt | 4 + include/strata/program/split_ring.hpp | 13 +++ src/program/generate.cpp | 9 +- tests/program/split_part_price_test.cpp | 106 ++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 tests/program/split_part_price_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ca70fe80bf..96ce4a4fea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -303,6 +303,10 @@ if(STRATA_BUILD_TESTS) 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) + # #5722: each layer-split participant's prompt loan priced on its own device (faked devices, no GPU) + add_executable(split_part_price_test tests/program/split_part_price_test.cpp) + target_include_directories(split_part_price_test PRIVATE include) + add_test(NAME split_part_price_test COMMAND split_part_price_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) diff --git a/include/strata/program/split_ring.hpp b/include/strata/program/split_ring.hpp index 13489e1673..84d71ed68d 100644 --- a/include/strata/program/split_ring.hpp +++ b/include/strata/program/split_ring.hpp @@ -33,4 +33,17 @@ inline int64_t split_lend_slots(int64_t slots, int64_t bytes, const uint64_t* of return max_blob == 0 ? 0 : (int64_t) ((need + max_blob - 1) / max_blob); } +/// A figure about one participant of a layer split's prompt path (its Prefill::bytes_needed, which the lend is sized +/// from): `fn()`, evaluated with device `dev` current (-1: CUDA0's own part, the device is left as it is). +/// `on_device(dev)` returns a guard that makes `dev` current and puts the previous one back (strata::core::OnDevice in +/// the engine; the CPU test fakes it). #5722: bytes_needed answers for the CURRENT device (the fused layout and its +/// smaller MoE buffers are decided per card, prefill.cpp fused_ring), and each stage's init carves on its own device, +/// so a stage priced while CUDA0 was current was lent CUDA0's figure: on an RTX 5080 + RTX 4090 split the 4090's loan +/// was 2882 MiB for buffers that take 3014 MiB there. On like cards every device answers the same, so nothing changes. +template +auto split_part_figure(OnDev&& on_device, int dev, Fn&& fn) { + [[maybe_unused]] auto guard = on_device(dev); + return fn(); +} + } // namespace strata::program diff --git a/src/program/generate.cpp b/src/program/generate.cpp index dcc9ada50e..b9bfa39823 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -10770,8 +10770,11 @@ int main(int argc, char** argv) { return strata::program::split_lend_slots(xc.slots(), xc.bytes(), xc.slot_offsets(), (uint64_t) strata::kernels::cpu::expert_layout().max_blob, need); }; + auto on_part_device = [](int dev) { return strata::core::OnDevice(dev); }; auto part_slots = [&](const PfPart& p, int64_t c) -> int64_t { - return cache_slots_for(*p.cache, strata::prefill::Prefill::bytes_needed(g, *p.ses, c, srcp != nullptr)); + return cache_slots_for(*p.cache, strata::program::split_part_figure(on_part_device, p.dev, [&] { + return strata::prefill::Prefill::bytes_needed(g, *p.ses, c, srcp != nullptr); + })); }; auto part_bytes = [&](const PfPart& p, int32_t first) -> uint64_t { strata::core::ExpertCache& xc = *p.cache; @@ -10835,7 +10838,9 @@ int main(int argc, char** argv) { const int64_t budget = std::max(0, std::min(p.cache->slots() - g_lend_keep, kAutoLendPct * p.cache->slots() / 100)); const uint64_t avail = part_bytes(p, (int32_t) (p.cache->slots() - budget)); - const uint64_t nonring = strata::prefill::Prefill::bytes_needed_no_ring(g, *p.ses, c, srcp != nullptr); + const uint64_t nonring = strata::program::split_part_figure(on_part_device, p.dev, [&] { + return strata::prefill::Prefill::bytes_needed_no_ring(g, *p.ses, c, srcp != nullptr); + }); room = std::min(room, (int64_t) ((avail - std::min(avail, nonring)) / (uint64_t) kBlob)); } return room; diff --git a/tests/program/split_part_price_test.cpp b/tests/program/split_part_price_test.cpp new file mode 100644 index 0000000000..7a96f7b16f --- /dev/null +++ b/tests/program/split_part_price_test.cpp @@ -0,0 +1,106 @@ +// #5722: a layer split prices each participant's prompt-path loan (Prefill::bytes_needed) on THAT participant's device. +// bytes_needed answers for the current device (prefill.cpp fused_ring: the fused layout and its smaller MoE buffers are +// decided per card), so a later stage priced while CUDA0 was current got CUDA0's figure: on an RTX 5080 (fused) + RTX +// 4090 (not) split the 4090's loan was 2882 MiB for a chunk whose buffers there take 3014 MiB, and its prompt path fell +// back to 1024-token chunks. No GPU: the devices are faked (a "current device" and a price per device). No real +// mixed-card run backs this test. +#include "strata/program/split_ring.hpp" + +#include +#include +#include + +using strata::program::split_lend_slots; +using strata::program::split_part_figure; + +namespace { +int failed = 0; +#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) + +constexpr uint64_t MiB = 1ull << 20; +int current = 0; // the faked current device + +struct FakeOnDevice { // strata::core::OnDevice's contract: -1 leaves the device alone + int previous = -1; + explicit FakeOnDevice(int dev) { if (dev >= 0 && dev != current) { previous = current; current = dev; } } + ~FakeOnDevice() { if (previous >= 0) current = previous; } + FakeOnDevice(const FakeOnDevice&) = delete; + FakeOnDevice& operator=(const FakeOnDevice&) = delete; +}; +auto on_device = [](int dev) { return FakeOnDevice(dev); }; + +// one card's buffers per chunk token, in bytes: a "fused" card needs fewer (its smaller MoE buffers) +struct Card { const char* name; uint64_t per_token; int64_t slots; }; + +// bytes_needed as the engine computes it: for the CURRENT device +uint64_t price_now(const std::vector& cards, int64_t chunk) { + return (uint64_t) chunk * cards[(size_t) current].per_token; +} + +struct Part { int dev; int64_t slots; }; // dev -1: CUDA0's own part + +// the lend of one participant: the slots of its cache that cover the price of `chunk` (uniform max_blob slots) +int64_t lend(const std::vector& cards, const Part& p, int64_t chunk, uint64_t blob) { + return split_lend_slots(p.slots, p.slots * (int64_t) blob, nullptr, blob, + split_part_figure(on_device, p.dev, [&] { return price_now(cards, chunk); })); +} + +// what the participant's own init carves, on its own device +uint64_t carved(const std::vector& cards, const Part& p, int64_t chunk) { + return (uint64_t) chunk * cards[(size_t) (p.dev < 0 ? 0 : p.dev)].per_token; +} + +std::vector parts_of(const std::vector& cards) { + std::vector parts{{-1, cards[0].slots}}; + for (size_t d = 1; d < cards.size(); ++d) parts.push_back({(int) d, cards[d].slots}); + return parts; +} +} // namespace + +int main() { + const uint64_t blob = 4 * MiB; + const int64_t chunks[] = {256, 512, 1024, 2048, 3072, 4096, 6144, 8192, 16384, 32768}; + + // 1. the reporter's pair: CUDA0 an RTX 5080 (fused: 2880 MiB at 8192), CUDA1 an RTX 4090 (3014 MiB at 8192) + { + const std::vector cards{{"RTX 5080", 2880 * MiB / 8192, 4000}, {"RTX 4090", 3014 * MiB / 8192, 4000}}; + const auto parts = parts_of(cards); + current = 0; + const int64_t k1 = lend(cards, parts[1], 8192, blob); + const uint64_t lent = (uint64_t) k1 * blob; + std::printf("split_part_price_test: CUDA1 (%s) chunk 8192: lent %llu MiB, its buffers there %llu MiB\n", + cards[1].name, (unsigned long long) (lent / MiB), (unsigned long long) (carved(cards, parts[1], 8192) / MiB)); + // the stage's init then lays its buffers out in that loan, stepping its chunk down until they fit ("trying a + // 6144-token chunk ..." in the report) + int64_t reads = 0; + for (const int64_t c : {8192, 6144, 4096, 3072, 2048, 1024}) + if (carved(cards, parts[1], c) <= lent) { reads = c; break; } + std::printf("split_part_price_test: CUDA1 reads its prompt in %lld-token chunks (planned 8192)\n", (long long) reads); + CHECK(reads == 8192); + CHECK(lent >= carved(cards, parts[1], 8192)); // the loan covers what the stage's init carves + CHECK(current == 0); // the device is put back + const int64_t k0 = lend(cards, parts[0], 8192, blob); + CHECK((uint64_t) k0 * blob >= carved(cards, parts[0], 8192)); // CUDA0's own part unchanged + CHECK((uint64_t) (k0 - 1) * blob < carved(cards, parts[0], 8192)); + } + + // 2. like cards: the device switch changes no number. Two cards (z440's 2x A4000 class) and four cards (the 4x + // R9700 class), every chunk of the scan's grid, priced with the switch against priced on the current device + // alone (the old rule). Equal per-device prices are what like cards give: fused_ring answers the same on each. + for (int n : {2, 4}) { + const std::vector cards((size_t) n, Card{"like card", 2950 * MiB / 8192, 3500}); + const auto parts = parts_of(cards); + for (const int64_t c : chunks) + for (const Part& p : parts) { + current = 0; + const int64_t with_switch = lend(cards, p, c, blob); + const int64_t old_rule = split_lend_slots(p.slots, p.slots * (int64_t) blob, nullptr, blob, price_now(cards, c)); + CHECK(with_switch == old_rule); + CHECK(current == 0); + } + } + + if (failed) { std::fprintf(stderr, "split_part_price_test: %d failed\n", failed); return 1; } + std::printf("split_part_price_test: ok\n"); + return 0; +}