Files
Strata/tests/core/kv_host_dma_test.cpp
T
Niko1221 f0356cb25f prefill: copy the prompt's new K/V cells to the host copy by DMA on CUDA
With KV streaming (--kv-resident, which setup writes from 64K context) the
prompt path's append kernel stored every new K/V cell into the pinned,
device-mapped host copy itself, one small store per value over the link.
On an RTX 5090 those stores ran at about 0.24 GB/s: 50-59 us per prompt
token when the host copy sat on the other socket's memory, about 17 us on
the GPU's own node, against about 0.5 us for the append into VRAM. That was
a fifth to a third of the prompt time at 8K-128K.

The append already writes the same cells into the one-layer staging pool in
VRAM. The HIP-only switch STRATA_KV_HOST_DMA=1 used that: the append writes
the staging pool and the resident slots only, and one cudaMemcpyAsync per
array copies the chunk's blocks to the host copy on the same stream, so
every later reader is ordered after it as it was after the kernel.

CUDA now takes that path by default; STRATA_KV_HOST_DMA=0 gives the
kernel's own stores back. HIP keeps its opt-in as before. A layer without a
staging pool keeps the kernel's stores. No new buffer: the staging pool is
allocated for every streamed session before any prompt is read.

Tests: kv_host_dma_test (the switch both ways, the backend defaults, no
pool) and kv_host_dma_parity (GPU: fp16, int8 and q4_0, four chunks with
unaligned starts, a reader on another stream behind an event; host copies,
read-backs and VRAM slots byte-equal both ways).
2026-11-14 03:21:21 +01:00

46 lines
1.9 KiB
C++

// The prompt path's K/V append under KV streaming (strata/core/kv_host_dma.hpp): CUDA copies the chunk's new cells to
// the host copy by DMA by default, HIP keeps the kernel's own stores unless asked, STRATA_KV_HOST_DMA=1 / 0 forces
// either way, and a layer without a staging pool always keeps the kernel's stores.
#include "strata/core/kv_host_dma.hpp"
#include <cstdio>
using namespace strata::core;
namespace {
int failed = 0;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
} // namespace
int main() {
// unset: the backend's default (CUDA true, HIP false)
CHECK(kv_host_dma_on(nullptr, true));
CHECK(!kv_host_dma_on(nullptr, false));
// "1" / "0" force it on both backends
CHECK(kv_host_dma_on("1", false));
CHECK(kv_host_dma_on("1", true));
CHECK(!kv_host_dma_on("0", true));
CHECK(!kv_host_dma_on("0", false));
// HIP as before this header: only a value that starts with '1' turns it on
CHECK(kv_host_dma_on("1yes", false));
CHECK(!kv_host_dma_on("", false));
CHECK(!kv_host_dma_on("yes", false));
CHECK(!kv_host_dma_on("2", false));
// CUDA: anything but a leading '0' keeps the default on
CHECK(kv_host_dma_on("", true));
CHECK(kv_host_dma_on("yes", true));
CHECK(!kv_host_dma_on("0n", true));
// one append: the switch, a streamed (staged) layer, and its staging pool; any one missing -> the kernel's stores
CHECK(kv_host_dma_use(true, true, true));
for (int on = 0; on < 2; ++on)
for (int staged = 0; staged < 2; ++staged)
for (int pool = 0; pool < 2; ++pool)
if (!(on && staged && pool)) CHECK(!kv_host_dma_use(on != 0, staged != 0, pool != 0));
if (failed) {
std::fprintf(stderr, "kv_host_dma_test: %d failed\n", failed);
return 1;
}
std::printf("kv_host_dma_test: ok\n");
return 0;
}