mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 15:58:53 +00:00
fix(inkling2/hip): placement reserves the K/V of every slot and the batch scratch before the card is filled with experts (INKOTHER boxia: --bench --parallel 32)
The auto budget capped the K/V estimate at a quarter of the card (32 slots at 4096 tokens need more than that) and left the batch scratch out, so every expert was placed first and ensure_batch_scratch ended with "[ink-hip] scratch alloc: out of memory". ink_batch.h gets estimate_batch_scratch_bytes (the allocation list of ensure_batch_scratch as a formula) and plan_expert_vram_budget; the HIP budget uses them. Test ink_batch_budget_test: the formula equals the buffer list one by one, the boxia case (20 GiB card, 32 slots) fails with the old rule and fits with the new one. Not built on HIP here (no HIP box in this session): the change in ink_hip_backend.hip.cpp is the six lines at the budget.
This commit is contained in:
1 parent
7d1ae1e5d6
commit
1999e3e822
4 files changed
+142
-7
No files matched your search
@@ -315,6 +315,10 @@ if(STRATA_BUILD_TESTS)
|
||||
add_executable(kv_dtype_guard_test tests/core/kv_dtype_guard_test.cpp)
|
||||
target_include_directories(kv_dtype_guard_test PRIVATE src/inkling2)
|
||||
add_test(NAME kv_dtype_guard_test COMMAND kv_dtype_guard_test)
|
||||
# placement leaves room for the N-slot K/V and the batch scratch before it fills the card with experts (no GPU)
|
||||
add_executable(ink_batch_budget_test tests/core/ink_batch_budget_test.cpp)
|
||||
target_include_directories(ink_batch_budget_test PRIVATE src/inkling2)
|
||||
add_test(NAME ink_batch_budget_test COMMAND ink_batch_budget_test)
|
||||
# #6770 / #6755: when strata-inkling's CUDA global K/V may grow in VRAM (net growth, not the whole new K/V; no GPU)
|
||||
add_executable(ink_kv_grow_test tests/core/ink_kv_grow_test.cpp)
|
||||
target_include_directories(ink_kv_grow_test PRIVATE include)
|
||||
|
||||
@@ -978,13 +978,15 @@ public:
|
||||
} else {
|
||||
const size_t trunk_now = vram_used_bytes();
|
||||
// Reserve headroom for KV + activations + OS/desktop (always keep at least OS reserve + 768 MiB)
|
||||
const size_t kv_est = std::min<size_t>(
|
||||
estimate_slot_bytes(std::min(max_ctx_, 4096), default_kv_dtype_, has_dspark_).total_bytes() * static_cast<size_t>(max_slots_),
|
||||
mem_info_.effective_total_bytes / 4);
|
||||
const size_t overhead = mem_info_.reserve_bytes + trunk_now + kv_est + (768ULL << 20);
|
||||
if (mem_info_.effective_total_bytes > overhead) {
|
||||
vram_budget = mem_info_.effective_total_bytes - overhead;
|
||||
}
|
||||
// The K/V of every slot that will run and the batch scratch of the biggest step are reserved BEFORE the card is filled with experts (no cap
|
||||
// at a quarter of the card: 32 clients need 32 slots; a plan without them ended with "scratch alloc: out of memory" at the first batch).
|
||||
const size_t kv_est =
|
||||
estimate_slot_bytes(std::min(max_ctx_, 4096), default_kv_dtype_, has_dspark_).total_bytes() * static_cast<size_t>(max_slots_);
|
||||
ink2::BatchGeom bg;
|
||||
bg.hidden = c_.hidden; bg.n_head = c_.n_head; bg.head_dim = c_.head_dim; bg.n_kv = c_.n_kv; bg.d_rel = c_.d_rel;
|
||||
bg.rel_extent = c_.rel_extent; bg.inter = c_.inter; bg.n_experts = c_.n_experts; bg.top_k = c_.top_k;
|
||||
const size_t scratch_est = estimate_batch_scratch_bytes(bg, std::max(max_slots_, std::min(guard_.max_prefill_chunk, 512)));
|
||||
vram_budget = plan_expert_vram_budget(mem_info_.effective_total_bytes, mem_info_.reserve_bytes, trunk_now, kv_est, scratch_est, 768ULL << 20);
|
||||
}
|
||||
|
||||
// Check whether all experts fit in vram_budget; if hybrid (partial VRAM), reserve elastic VRAM cache inside budget!
|
||||
|
||||
@@ -158,6 +158,49 @@ inline SlotMemBudget estimate_slot_bytes(int max_ctx, KvDtype kv_dtype, bool wit
|
||||
return b;
|
||||
}
|
||||
|
||||
// ---- Batch scratch of B rows (the buffers a backend allocates for a step of B tokens / slots): the HIP backend's ensure_batch_scratch(B) as a formula ----
|
||||
// Backends must reserve this (and the K/V of every slot they will run) BEFORE they fill the card with experts: a plan made without it ends with
|
||||
// "scratch alloc: out of memory" at the first batch of 32 clients (INKOTHER, boxia, --bench --parallel 32).
|
||||
struct BatchGeom {
|
||||
int hidden = 2304, n_head = 24, head_dim = 128, n_kv = 8, d_rel = 16, rel_extent = 1024, inter = 1152, n_experts = 128, top_k = 8;
|
||||
};
|
||||
inline size_t estimate_batch_scratch_bytes(const BatchGeom& g, int B) {
|
||||
const size_t b = static_cast<size_t>(std::max(1, B));
|
||||
const size_t D = static_cast<size_t>(g.hidden);
|
||||
const size_t q_dim = static_cast<size_t>(g.n_head) * g.head_dim;
|
||||
const size_t kv_dim = static_cast<size_t>(g.n_kv) * g.head_dim;
|
||||
const size_t r_dim = static_cast<size_t>(g.n_head) * g.d_rel;
|
||||
const size_t qkvrg = 2 * q_dim + r_dim + 2 * kv_dim;
|
||||
const size_t inter = static_cast<size_t>(g.inter);
|
||||
const size_t top_k = static_cast<size_t>(g.top_k);
|
||||
const size_t gu_cap = std::max<size_t>(b, std::min<size_t>(b * top_k, 512));
|
||||
const size_t x_bf16_k = std::max<size_t>({2 * D, q_dim, 4096});
|
||||
size_t t = 0;
|
||||
t += b * 4 * 7 + (b + 1) * 4; // slot ids, positions, token ids, image index, wanted index, group slot / token, group offsets
|
||||
t += std::max<size_t>(b, 1025) * x_bf16_k * 2; // bf16 activations
|
||||
t += b * qkvrg * 4;
|
||||
t += 41 * b * D * 4; // the [B, D] fp32 buffers (cur, res, inputs, mixes, messages, control, blocks, bank, taps ...)
|
||||
t += 3 * b * q_dim * 4 + 4 * b * kv_dim * 4 + b * r_dim * 4; // q, gate, attention out / k, v raw and conv / r
|
||||
t += b * g.n_head * static_cast<size_t>(g.rel_extent) * 4 + b * g.n_head * 4;
|
||||
t += 2 * b * g.n_kv * 16;
|
||||
t += std::max<size_t>(b, 4) * g.n_head * 4 * 130 * 4; // split-KV partials
|
||||
t += gu_cap * D * 2 + gu_cap * 2 * inter * 4 + gu_cap * D * 4 + 2 * gu_cap * 4; // MoE per-expert scratch
|
||||
t += b * static_cast<size_t>(g.n_experts) * 4 + 2 * b * top_k * 4;
|
||||
t += b * top_k * inter * 2;
|
||||
t += (129 + 128) * 4;
|
||||
t += b * D + b * (D >> 4) * 4 + b * top_k * inter + b * top_k * (inter >> 4) * 4; // int8 activations and scales
|
||||
t += 2 * b * top_k * 4 + b * top_k * D * 2 + b * top_k * D * 4;
|
||||
return t;
|
||||
}
|
||||
|
||||
// Bytes of expert bodies a card may take: the card minus the runtime's reserve, the weights already in it, the K/V of every slot that will run (no
|
||||
// cap: 32 clients need 32 slots) and the batch scratch, minus a margin. 0 when nothing is left (the experts then go to RAM).
|
||||
inline size_t plan_expert_vram_budget(size_t card_bytes, size_t reserve_bytes, size_t resident_bytes, size_t kv_bytes_all_slots,
|
||||
size_t scratch_bytes, size_t margin_bytes) {
|
||||
const size_t overhead = reserve_bytes + resident_bytes + kv_bytes_all_slots + scratch_bytes + margin_bytes;
|
||||
return card_bytes > overhead ? card_bytes - overhead : 0;
|
||||
}
|
||||
|
||||
// ---- Paged KV Cache for a single slot (on-demand 64-token page allocation -> tiny footprint for active slots!) ----
|
||||
// Supports FP16, FP8_E4M3, K8V8 (per-64 INT8 K + INT8 V), and K8V4 (per-64 INT8 K + 4-bit asymmetric V)
|
||||
struct SlotKvPages {
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
// Placement must leave room for the N-slot K/V and the batch scratch before the card is filled with experts (INKOTHER, boxia RX 7900 XT:
|
||||
// `--bench --parallel 32` ended with "[ink-hip] scratch alloc: out of memory" because every expert was placed first). No GPU.
|
||||
#include "ink_batch.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
using namespace ink2;
|
||||
|
||||
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 size_t GiB = 1ull << 30;
|
||||
|
||||
// the allocation list of the HIP backend's ensure_batch_scratch(B), one buffer per line, as the backend writes it
|
||||
size_t listed(const BatchGeom& g, size_t B) {
|
||||
const size_t D = g.hidden, q = (size_t)g.n_head * g.head_dim, kv = (size_t)g.n_kv * g.head_dim, r = (size_t)g.n_head * g.d_rel;
|
||||
const size_t qkvrg = 2 * q + r + 2 * kv, inter = g.inter, ne = g.n_experts, tk = g.top_k;
|
||||
const size_t cap = std::max<size_t>(B, std::min<size_t>(B * tk, 512));
|
||||
const size_t xk = std::max<size_t>({2 * D, q, 4096});
|
||||
size_t t = 0;
|
||||
t += B * 4 * 7 + (B + 1) * 4; // slot ids, pos, tok ids, img idx, want idx, grp slot, grp tok, grp off
|
||||
t += std::max<size_t>(B, 1025) * xk * 2; // x bf16
|
||||
t += B * qkvrg * 4;
|
||||
for (int i = 0; i < 5; ++i) t += B * D * 4; // cur, res, in0, in1, in2
|
||||
t += B * 3 * D * 4 + B * 4 * D * 4; // mx3, msgs
|
||||
t += B * D * 4 + B * 2 * D * 4 + 2 * (B * D * 4);// ctrl tmp, ctrl in, ctrl gain, ctrl forget
|
||||
t += B * q * 4; // q
|
||||
t += 4 * (B * kv * 4); // k raw, v raw, k conv, v conv
|
||||
t += B * r * 4; // r raw
|
||||
t += B * g.n_head * (size_t)g.rel_extent * 4; // rel bias
|
||||
t += B * g.n_head * 4; // sinks
|
||||
t += B * q * 4; // gate a
|
||||
t += 2 * (B * g.n_kv * 4 * 4); // k dyn, v dyn
|
||||
t += B * q * 4; // attn out
|
||||
t += std::max<size_t>(B, 4) * g.n_head * 4 * 130 * 4; // split-KV partials
|
||||
t += B * D * 4; // o proj
|
||||
t += B * D * 4 + B * D * 4; // a sconv, mlp in
|
||||
t += cap * D * 2; // mlp f16
|
||||
t += B * ne * 4; // r logits
|
||||
t += 2 * (B * tk * 4); // topk ids, topk w
|
||||
t += B * tk * inter * 2; // moe act
|
||||
t += cap * 2 * inter * 4 + cap * D * 4; // moe gu, moe ye
|
||||
t += 129 * 4 + 128 * 4; // exp off, exp cur
|
||||
t += B * D + B * (D >> 4) * 4; // x q8, x d
|
||||
t += B * tk * inter + B * tk * (inter >> 4) * 4; // act q8, act d
|
||||
t += B * tk * 4 + B * tk * 4; // bucket tok, slotk bidx
|
||||
t += B * tk * D * 2 + B * tk * D * 4; // bucket x f16, bucket y
|
||||
t += B * D * 4 + B * D * 4; // moe out, m sconv
|
||||
t += 2 * (cap * 4); // exp tok idx, exp tok w
|
||||
t += B * D * 4; // h norm
|
||||
t += 2 * (B * D * 4); // prelude, bank out
|
||||
t += 8 * (B * D * 4) + 4 * (B * D * 4) + 4 * (B * D * 4); // depth blocks, bank, taps
|
||||
return t;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
const BatchGeom g;
|
||||
for (int B : {1, 4, 32, 64, 512, 1024}) CHECK(estimate_batch_scratch_bytes(g, B) == listed(g, (size_t)B));
|
||||
CHECK(estimate_batch_scratch_bytes(g, 32) > (16u << 20) && estimate_batch_scratch_bytes(g, 32) < (512u << 20));
|
||||
CHECK(estimate_batch_scratch_bytes(g, 512) > estimate_batch_scratch_bytes(g, 32));
|
||||
CHECK(estimate_batch_scratch_bytes(g, 1024) > estimate_batch_scratch_bytes(g, 512));
|
||||
|
||||
// the boxia case: a 20 GiB card, 32 slots at 4096 tokens of FP16 K/V, the trunk in the card
|
||||
const size_t card = 20 * GiB, reserve = 1 * GiB, trunk = 3 * GiB, margin = 768ull << 20;
|
||||
const size_t per_slot = estimate_slot_bytes(4096, KvDtype::FP16, false).total_bytes();
|
||||
const size_t kv32 = per_slot * 32;
|
||||
const size_t scratch = estimate_batch_scratch_bytes(g, 512);
|
||||
const size_t old_budget = card - (reserve + trunk + std::min<size_t>(kv32, card / 4) + margin); // the earlier rule: K/V estimate capped at a quarter of the card
|
||||
const size_t new_budget = plan_expert_vram_budget(card, reserve, trunk, kv32, scratch, margin);
|
||||
CHECK(kv32 > card / 4); // the cap really cut the estimate
|
||||
CHECK(new_budget < old_budget);
|
||||
CHECK(new_budget + reserve + trunk + kv32 + scratch + margin <= card); // everything fits
|
||||
// with the old budget filled with experts the 32 slots (K/V + scratch) no longer fit: this was the failure
|
||||
CHECK(card - (old_budget + reserve + trunk) < kv32 + scratch);
|
||||
// with the new one they do
|
||||
CHECK(card - (new_budget + reserve + trunk) >= kv32 + scratch);
|
||||
// more slots, fewer experts; one slot: almost the whole card
|
||||
CHECK(plan_expert_vram_budget(card, reserve, trunk, per_slot * 4, estimate_batch_scratch_bytes(g, 512), margin) > new_budget);
|
||||
CHECK(plan_expert_vram_budget(card, reserve, trunk, per_slot, estimate_batch_scratch_bytes(g, 512), margin) > plan_expert_vram_budget(card, reserve, trunk, per_slot * 4, scratch, margin));
|
||||
// a card that cannot hold even the overhead gives 0 (the experts go to RAM), never a wrapped-around size
|
||||
CHECK(plan_expert_vram_budget(2 * GiB, reserve, trunk, kv32, scratch, margin) == 0);
|
||||
if (failed) { std::fprintf(stderr, "%d checks failed\n", failed); return 1; }
|
||||
std::printf("ink_batch_budget_test OK\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user