Files
Strata/include/strata/program/split_ring.hpp
T
Niko1221 7ee50cc8c7 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.
2026-11-10 18:30:45 +01:00

50 lines
3.0 KiB
C++

#pragma once
#include <cstdint>
#include <cstdlib>
// A layer split's prompt-path ring and the lend slots sized from it, kept apart from the engine so they can be tested
// on the CPU (header-only, like pipeline_gate.hpp).
//
// 1. split_ring_slots: the streamed-ring override a split chooses (STRATA_SPLIT_RING, else 96 when most experts are
// resident). The choice must be made BEFORE anything sizes the prompt path with Prefill::bytes_needed (which counts
// ring_slots * max_blob): the split's lend regions (the RAM copies of the experts the prompt path borrows) used to
// be sized first, with the default ring, so a larger ring borrowed more slots than the copy kept.
// 2. split_lend_slots: how many tail slots of a cache `need` bytes take - the scan's own arithmetic, used for the
// RAM-copy region and for the borrow itself so the two cannot disagree.
namespace strata::program {
/// The ring override (slots) for a layer split, 0 = none (the pinned-share rule stays).
/// env STRATA_SPLIT_RING as read from the environment (nullptr = unset); 0 / non-numeric = the pinned-share rule
/// res_share the share of (layer, expert) pairs resident in some stage cache, 0..1
inline int split_ring_slots(const char* env, double res_share) {
const int ring = env != nullptr ? std::atoi(env) : (res_share >= 0.75 ? 96 : 0);
return ring > 0 ? ring : 0;
}
/// The number of tail slots of a cache of `slots` slots whose bytes cover `need`. `offsets` is the cache's per-slot
/// byte-offset prefix sum (slots + 1 entries, `bytes` = offsets[slots]) or nullptr when every slot is `max_blob` bytes.
inline int64_t split_lend_slots(int64_t slots, int64_t bytes, const uint64_t* offsets, uint64_t max_blob, uint64_t need) {
if (offsets != nullptr) {
int64_t k = 0;
while (k < slots && (uint64_t) (bytes - (int64_t) offsets[slots - k]) < need) ++k;
return k;
}
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 <class OnDev, class Fn>
auto split_part_figure(OnDev&& on_device, int dev, Fn&& fn) {
[[maybe_unused]] auto guard = on_device(dev);
return fn();
}
} // namespace strata::program