Files
Strata/engine/tests/hy4flash/hy4_plan_math_test.cpp
Niko1221 2e8f436d24 fix(hy4): count the context-sized score buffer in the memory plan, and name a failed device allocation (#6744)
Hy4-Flash at a long context ended "HIP error 2 (out of memory)" right after the experts
were placed (SQ-64 at 1,048,576 and 524,288 on an RTX A6000 48 GB, and on the reporter's
R9700 / RTX 3090 Ti). An allocation trace shows why: the runner's work arena holds
chunk x (max_ctx / 4 + 8) floats of IndexPool scores, 8,590,196,736 bytes at a 1M context and
a chunk of 8192 (4.3 GB at 512K, 1.07 GB at 128K). plan_memory counted only the activation
scratch and the MoE workspace, so from about 256K up the reserve was too small and the
experts took the memory this buffer needed.

plan_memory now takes the arena as the largest of the three (hy4_plan_math.h, header-only,
tested in hy4_plan_math_test with the traced byte counts). Up to 128K nothing changes.

A failed device allocation in the runner now prints its size, the free memory and the
settings that move the KV and index buffers (HY4_KV_HOST=3, HY4_RESERVE_GB); HY4_ALLOC_TRACE=1
lists the allocations of 300 MiB and more.

Boston, 1 A6000, SQ-64, before: 1M and 512K ended in HIP error 2. After: 128K, 512K and 1M all
start without extra variables (decode 35 / 21 / 17 tok/s; 128K placement and speed unchanged;
the 33 GB cap, an R9700's size, also starts at all three).
2026-11-23 13:44:59 +01:00

28 lines
1.5 KiB
C++

// #6744: the Hy4-Flash work arena's score buffer (no GPU, no model). The numbers are the reporter's / Boston's allocation trace.
#include <cstdint>
#include <cstdio>
#include "hy4_plan_math.h"
static int failed = 0;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
int main() {
using namespace hy4;
// the trace on an RTX A6000 at --max-context 1048576, chunk 8192: dalloc of 8,590,196,736 bytes right after the experts
CHECK(score_buffer_bytes(8192, 1048576) == 8590196736ull);
CHECK(score_buffer_bytes(8192, 524288) == 4295229440ull); // 512K: 4,295,229,440 (also in the trace)
CHECK(score_buffer_bytes(4096, 1048576) == 4295098368ull); // the R9700's chunk of 4096 at 1M
// at 128K it is small, so the old plan was right there: the activation scratch is the larger term
const uint64_t act = 2ull * 8192 * 16384 * 4, moe = 8192ull * 16 * 1536 * 6;
CHECK(score_buffer_bytes(8192, 131072) < moe);
CHECK(work_arena_bytes(act, 8192, 131072, moe) == moe);
// from 256K up the score buffer is the arena and the plan has to reserve it: 1M at chunk 8192 adds 7.4 GB over the old estimate
CHECK(work_arena_bytes(act, 8192, 1048576, moe) == 8590196736ull);
CHECK(work_arena_bytes(act, 8192, 1048576, moe) > moe + 7000000000ull);
CHECK(work_arena_bytes(act, 8192, 262144, moe) == 2147745792ull);
if (failed) return 1;
std::printf("hy4_plan_math_test: ok\n");
return 0;
}