Archived
radiance 2.0.0
Radiance becomes a multi-model engine: one process serves several models behind one port, loads each when a request names it, parks it in host memory or restores it from its container, and shares the cards between them (docs/MULTIMODEL.md). The configuration is one YAML file; the command line flags are gone (`radiance --config FILE`, hot reload on SIGHUP). Formats. IQ8R (rotated int8) and IQR-V4 replace FP8 and MXFP4 as whole-model formats, with an int8 KV cache (`kv-cache-dtype: i8`) in place of the fp8 one. Containers whose weights are block FP8, MXFP4, M2XFP4, NVFP4 or ParoQuant are refused at startup by the name of the first such weight. Not served until they have int8 containers: GLM-5.3-Flash, Ornith-1.5, Sage-A1.5, Nemotron-3.5-Bolt-74B, Ling-3.0-flash, drama-3 and Anima's FP8 container; Nemotron-3.5-Bolt-E4B, the Gemma 4 31B DFlash containers and MiniCPM5-2B bf16 serve without their drafter. 1.17.0 serves all of them. Speed. Chained launches for Qwen3.8-27B, Gemma 4, Gemma 3 and the small bf16 models, a new Qwen3.8-Flash-Next container (IQR-V4 experts, int8 trunk) with its decode step and prefill reworked, and a reworked adaptive draft window. Deployment. deploy/compose has two fleet files, tp1.yaml and tp2.yaml, each with its configuration beside it, in place of the per-model files. `host-pool-mib` is a model's host pool in all, split among its ranks. The dashboard is rebuilt: an hour of history kept by the server, each accelerator's memory by model, model states over time. Merged since 1.17.0: Nemotron-3.5-Bolt-74B-A9B by keptn42 (#339); GLM-5.3-Flash on four cards by AnimusNull (#397); chained launches for Flash-Next's decode step (#396) and preempted requests keeping their prefix and place (#405) by mici81; --prefill-interleave auto by Code000 (#410); eight more libr3d kernels and the w4a4 reference by RikshaDriver (#403, #407, #408); libr3d large-BAR and E4M3 decode fixes by neuromanMD (#438, #439); int4/int5 decode tiles by Kezi (#426); attention for Nanbeige 4.3 33B by everplays (#417); expert_stats_report.py by baKon (#431). Fixed: #398, #402, #419, #437 (dschmitz's patch); added the metrics of #392, and multi-model serving as asked for in #367 and #109. Plugin ABI 39: out-of-tree plugins need to be rebuilt. Thanks to @keptn42, @AnimusNull, @mici81, @Code000, @RikshaDriver, @neuromanMD, @Kezi, @everplays, @baKon and @dschmitz.
This commit is contained in:
1 parent
b4c04b6042
commit
ddcb9c69bb
696 files changed
+120770
-56596
No files matched your search
@@ -21,7 +21,10 @@
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using rad::DirectFile;
|
||||
@@ -134,6 +137,33 @@ TEST(chained_launches_follow_the_plugins_ask_on_the_aql_backend_only) {
|
||||
CHECK_EQ(rad_dev_chain_enabled(), 0);
|
||||
}
|
||||
|
||||
/* An engine of more than one rank claims its cards (rad_dev_cards_claim). The claim that makes a
|
||||
* card shared returns only once everything issued on that card's queues before it has finished:
|
||||
* from then on no chained launch waits on the card behind a collective, and nothing issued under
|
||||
* the old rule is left. A backend that never launches ahead has nothing to wait for and accepts
|
||||
* any claim. */
|
||||
TEST(a_claim_that_shares_a_card_returns_with_the_cards_queues_drained) {
|
||||
const bool aql = std::strcmp(rad::device_backend_name(), "aql") == 0;
|
||||
const int card = 0;
|
||||
CHECK_EQ(rad_dev_cards_claim(nullptr, 1, 1), RAD_E_INVAL);
|
||||
CHECK_EQ(rad_dev_cards_claim(&card, 0, 1), RAD_E_INVAL);
|
||||
CHECK_OK(rad_dev_set(card));
|
||||
const int64_t big = 256 << 20;
|
||||
Stream st;
|
||||
Event done;
|
||||
DevBuf<uint8_t> fat(big, RAD_MEM_DEVICE);
|
||||
CHECK(!!fat);
|
||||
if (!fat) return;
|
||||
CHECK_OK(rad_dev_cards_claim(&card, 1, 1)); /* the first engine: the card is its own */
|
||||
for (int k = 0; k < 24; ++k) CHECK_OK(rad_memset_async(fat, k, big, st.s));
|
||||
CHECK_OK(rad_event_record(done.e, st.s));
|
||||
CHECK_OK(rad_dev_cards_claim(&card, 1, 1)); /* a second: shared from here */
|
||||
if (aql) CHECK_EQ(rad_event_query(done.e), 1);
|
||||
CHECK_OK(rad_dev_cards_claim(&card, 1, 0));
|
||||
CHECK_OK(rad_dev_cards_claim(&card, 1, 0));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
}
|
||||
|
||||
TEST(host_vram_budget_comes_from_the_environment) {
|
||||
/* A real card reports its own VRAM. */
|
||||
if (!need_host_backend()) return;
|
||||
@@ -197,6 +227,33 @@ TEST(alloc_free_roundtrip) {
|
||||
CHECK(rad_dev_host_ptr(nullptr) == nullptr);
|
||||
}
|
||||
|
||||
/* A kernel library's free hook (rad_plugin_freed) is registered by every registry that loads the
|
||||
* library and goes with the last of them: a library closed and opened again can sit at another
|
||||
* address, and a hook left behind called into memory nothing mapped. */
|
||||
namespace {
|
||||
std::atomic<int> g_hook_calls{0};
|
||||
void count_free(const void*, int64_t) { ++g_hook_calls; }
|
||||
} // namespace
|
||||
|
||||
TEST(a_free_hook_goes_with_its_last_registration) {
|
||||
auto one_free = [] {
|
||||
void* p = rad_dev_alloc(4096, RAD_MEM_DEVICE);
|
||||
REQUIRE(p != nullptr);
|
||||
rad_dev_free(p, RAD_MEM_DEVICE);
|
||||
};
|
||||
rad::rad_dev_on_free(count_free);
|
||||
rad::rad_dev_on_free(count_free);
|
||||
one_free();
|
||||
CHECK_EQ(g_hook_calls.load(), 1);
|
||||
rad::rad_dev_off_free(count_free);
|
||||
one_free();
|
||||
CHECK_EQ(g_hook_calls.load(), 2);
|
||||
rad::rad_dev_off_free(count_free);
|
||||
one_free();
|
||||
CHECK_EQ(g_hook_calls.load(), 2);
|
||||
rad::rad_dev_off_free(count_free); /* one too many: nothing to take back */
|
||||
}
|
||||
|
||||
/* A pinned range larger than one pin is locked in pieces (core/device/aql.cpp, pin_pieces),
|
||||
* invisibly above the device layer: one base, every byte at the same address on the card, and
|
||||
* copies and fills across a piece boundary land whole. A fill of pageable memory is refused, so the
|
||||
@@ -1611,4 +1668,652 @@ TEST(a_damaged_code_object_is_refused_not_misread) {
|
||||
CHECK(has(why, "amdhsa.kernels"));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ parking (paging.cpp)
|
||||
*
|
||||
* An engine's large device allocations are mapped in chunks a park can take off the card and put
|
||||
* back at the same address. Whatever route a chunk's bytes take while it is out (a host copy, the
|
||||
* disk tier, a copy kept from an earlier park, the container it was loaded from), they must come
|
||||
* back exact.
|
||||
*/
|
||||
namespace {
|
||||
|
||||
/* The paging chunk (paging.cpp's kChunk). */
|
||||
constexpr int64_t kParkChunk = (int64_t)64 << 20;
|
||||
|
||||
/* One pageable allocation under engine owner `owner`, and in `want` the pattern it should hold
|
||||
* (upload() writes it). The owner's scope stays current for the object's life, so further
|
||||
* allocations join the same owner. */
|
||||
struct Paged {
|
||||
rad::EngineScope es;
|
||||
int owner;
|
||||
int64_t n;
|
||||
void* d = nullptr;
|
||||
std::vector<uint8_t> want;
|
||||
RadStream s = nullptr;
|
||||
Paged(int o, int64_t bytes, uint32_t seed)
|
||||
: es(o, "paging-test"), owner(o), n(bytes), want((size_t)bytes) {
|
||||
rad_dev_set(0);
|
||||
fill(want.data(), n, seed);
|
||||
d = rad_dev_alloc(n, RAD_MEM_DEVICE);
|
||||
rad_stream_create(&s, 0);
|
||||
}
|
||||
~Paged() {
|
||||
if (d) rad_dev_free(d, RAD_MEM_DEVICE);
|
||||
if (s) rad_stream_destroy(s);
|
||||
}
|
||||
/* Through pinned memory: an async copy reads its host side when it runs. */
|
||||
int upload() {
|
||||
void* h = rad_dev_alloc(n, RAD_MEM_HOST_PINNED);
|
||||
if (!h) return RAD_E_NOMEM;
|
||||
std::memcpy(h, want.data(), (size_t)n);
|
||||
int rc = rad_memcpy_async(d, h, n, s);
|
||||
if (rc >= 0) rc = rad_stream_sync(s);
|
||||
rad_dev_free(h, RAD_MEM_HOST_PINNED);
|
||||
return rc;
|
||||
}
|
||||
/* Index of the first byte that differs from `want`, -1 when none does, -2 when the copy back
|
||||
* fails. */
|
||||
int64_t first_bad() {
|
||||
std::vector<uint8_t> got((size_t)n);
|
||||
void* h = rad_dev_alloc(n, RAD_MEM_HOST_PINNED);
|
||||
if (!h) return -2;
|
||||
const bool ok = rad_memcpy_async(h, d, n, s) >= 0 && rad_stream_sync(s) >= 0;
|
||||
if (ok) std::memcpy(got.data(), h, (size_t)n);
|
||||
rad_dev_free(h, RAD_MEM_HOST_PINNED);
|
||||
if (!ok) return -2;
|
||||
int64_t first = -1, count = 0;
|
||||
for (int64_t i = 0; i < n; ++i)
|
||||
if (got[(size_t)i] != want[(size_t)i]) {
|
||||
if (first < 0) first = i;
|
||||
++count;
|
||||
}
|
||||
if (first >= 0) {
|
||||
std::fprintf(stderr, " %lld of %lld bytes differ, first at %lld (got %02x, want %02x); by chunk:",
|
||||
(long long)count, (long long)n, (long long)first, got[(size_t)first],
|
||||
want[(size_t)first]);
|
||||
for (int64_t c0 = 0; c0 < n; c0 += kParkChunk) {
|
||||
int64_t b = 0;
|
||||
for (int64_t i = c0; i < std::min(n, c0 + kParkChunk); ++i) b += got[(size_t)i] != want[(size_t)i];
|
||||
std::fprintf(stderr, " %lld", (long long)b);
|
||||
}
|
||||
std::fprintf(stderr, "\n");
|
||||
}
|
||||
return first;
|
||||
}
|
||||
};
|
||||
|
||||
/* A disk tier of the case's own, under the binary's temporary directory. */
|
||||
std::string park_dir() {
|
||||
static int n = 0;
|
||||
const std::string d = radtest::tmpdir() + "/park-" + std::to_string(++n);
|
||||
return ::mkdir(d.c_str(), 0700) == 0 ? d : "";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
/* Three and a bit chunks, so the last one is short. */
|
||||
constexpr int64_t kParkBytes = 3 * kParkChunk + (5 << 20);
|
||||
|
||||
/* Chunks mapped again at their old addresses and written at once: until the cards' translation
|
||||
* caches are flushed in full the writes land nowhere (Linux 7.1 with gfx1201), so the maps are
|
||||
* followed by dev_settle_maps's flush before anything writes them. This is the page-in's order:
|
||||
* every chunk released, then all mapped, then each written. */
|
||||
TEST(a_chunk_remapped_right_after_its_release_keeps_what_is_written) {
|
||||
if (!need_vmem()) return;
|
||||
const int64_t c = kParkChunk;
|
||||
const int K = 4;
|
||||
rad_dev_set(0);
|
||||
const auto* be = rad::active_backend();
|
||||
char* res = (char*)be->vmem_reserve(K * c, rad::VMem::granularity());
|
||||
CHECK(res != nullptr);
|
||||
RadStream s = nullptr;
|
||||
CHECK_OK(rad_stream_create(&s, 0));
|
||||
std::vector<uint8_t> want((size_t)(K * c));
|
||||
fill(want.data(), K * c, 99u);
|
||||
uint8_t* h = (uint8_t*)rad_dev_alloc(K * c, RAD_MEM_HOST_PINNED);
|
||||
CHECK(h != nullptr);
|
||||
for (int round = 0; round < 3; ++round) {
|
||||
for (int k = 0; k < K; ++k) CHECK_OK(be->vmem_commit(res + k * c, c));
|
||||
for (int k = K - 1; k >= 0; --k) CHECK_OK(be->vmem_decommit(res + k * c, c));
|
||||
std::memcpy(h, want.data(), (size_t)(K * c));
|
||||
for (int k = 0; k < K; ++k) CHECK_OK(be->vmem_commit(res + k * c, c));
|
||||
CHECK_OK(rad::dev_settle_maps());
|
||||
for (int k = 0; k < K; ++k) CHECK_OK(rad_memcpy_async(res + k * c, h + k * c, c, s));
|
||||
CHECK_OK(rad_stream_sync(s));
|
||||
std::memset(h, 0, (size_t)(K * c));
|
||||
CHECK_OK(rad_memcpy_async(h, res, K * c, s));
|
||||
CHECK_OK(rad_stream_sync(s));
|
||||
for (int k = 0; k < K; ++k)
|
||||
CHECK_EQ(std::memcmp(h + k * c, want.data() + k * c, (size_t)c), 0);
|
||||
for (int k = 0; k < K; ++k) CHECK_OK(be->vmem_decommit(res + k * c, c));
|
||||
}
|
||||
rad_dev_free(h, RAD_MEM_HOST_PINNED);
|
||||
be->vmem_release(res, K * c);
|
||||
rad_stream_destroy(s);
|
||||
}
|
||||
|
||||
TEST(a_parked_allocation_comes_back_exact_from_host_copies) {
|
||||
if (!need_vmem()) return;
|
||||
Paged a(9101, kParkBytes, 11u);
|
||||
CHECK(a.d != nullptr);
|
||||
CHECK_OK(a.upload());
|
||||
CHECK_EQ(a.first_bad(), -1); /* the round trip itself, before any park */
|
||||
rad::PageOut req;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_EQ(res.disk, 0);
|
||||
CHECK(res.host >= kParkBytes);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
|
||||
TEST(a_parked_allocation_comes_back_exact_from_the_disk_tier) {
|
||||
if (!need_vmem()) return;
|
||||
const std::string dir = park_dir();
|
||||
CHECK(!dir.empty());
|
||||
Paged a(9102, kParkBytes, 12u);
|
||||
CHECK_OK(a.upload());
|
||||
rad::PageOut req;
|
||||
req.host_budget = 0;
|
||||
req.dir = dir;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_EQ(res.host, 0);
|
||||
CHECK(res.disk >= kParkBytes);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
/* Sealed, the disk copy stays good for the next park, which writes nothing. */
|
||||
rad::dev_page_seal(a.d);
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_EQ(res.disk, 0);
|
||||
CHECK(res.reused >= kParkBytes);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
|
||||
/* More chunks than a page-out batch (16) and than the read-back's staging ring (16), so buffers
|
||||
* are reused on both sides. */
|
||||
TEST(a_large_parked_allocation_comes_back_exact_from_the_disk_tier) {
|
||||
if (!need_vmem()) return;
|
||||
const std::string dir = park_dir();
|
||||
const int64_t n = 18 * kParkChunk + (3 << 20);
|
||||
Paged a(9105, n, 15u);
|
||||
CHECK(a.d != nullptr);
|
||||
CHECK_OK(a.upload());
|
||||
rad::PageOut req;
|
||||
req.host_budget = 0;
|
||||
req.dir = dir;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK(res.disk >= n);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
|
||||
/* An engine owns several pageable allocations, each with its own page file. */
|
||||
TEST(two_allocations_of_one_owner_come_back_exact_from_the_disk_tier) {
|
||||
if (!need_vmem()) return;
|
||||
const std::string dir = park_dir();
|
||||
const int64_t n = 6 * kParkChunk + (3 << 20);
|
||||
Paged a(9106, n, 21u);
|
||||
void* d2 = rad_dev_alloc(n / 2, RAD_MEM_DEVICE); /* same owner: a's scope is current */
|
||||
CHECK(a.d != nullptr && d2 != nullptr);
|
||||
std::vector<uint8_t> want2((size_t)(n / 2));
|
||||
fill(want2.data(), n / 2, 22u); /* not 20: the filler sets the seed's low bit, so 20 is 21 */
|
||||
{
|
||||
void* h = rad_dev_alloc(n / 2, RAD_MEM_HOST_PINNED);
|
||||
std::memcpy(h, want2.data(), (size_t)(n / 2));
|
||||
CHECK_OK(rad_memcpy_async(d2, h, n / 2, a.s));
|
||||
CHECK_OK(rad_stream_sync(a.s));
|
||||
rad_dev_free(h, RAD_MEM_HOST_PINNED);
|
||||
}
|
||||
CHECK_OK(a.upload());
|
||||
rad::PageOut req;
|
||||
req.host_budget = 0;
|
||||
req.dir = dir;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK(res.disk >= n + n / 2);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
{
|
||||
void* h = rad_dev_alloc(n / 2, RAD_MEM_HOST_PINNED);
|
||||
CHECK_OK(rad_memcpy_async(h, d2, n / 2, a.s));
|
||||
CHECK_OK(rad_stream_sync(a.s));
|
||||
CHECK_EQ(std::memcmp(h, want2.data(), (size_t)(n / 2)), 0);
|
||||
rad_dev_free(h, RAD_MEM_HOST_PINNED);
|
||||
}
|
||||
rad_dev_free(d2, RAD_MEM_DEVICE);
|
||||
}
|
||||
|
||||
TEST(a_partial_park_takes_only_what_is_asked_and_comes_back_exact) {
|
||||
if (!need_vmem()) return;
|
||||
const std::string dir = park_dir();
|
||||
Paged a(9103, kParkBytes, 13u);
|
||||
CHECK_OK(a.upload());
|
||||
const int dev = 0; /* Paged allocates on card 0 */
|
||||
rad::PageOut req;
|
||||
req.want[dev] = (int64_t)100 << 20;
|
||||
req.host_budget = kParkChunk; /* what does not fit goes to disk */
|
||||
req.dir = dir;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
/* Whole chunks, from the top: the short last one and two more, not all four. */
|
||||
CHECK(res.freed[dev] >= ((int64_t)100 << 20));
|
||||
CHECK(res.freed[dev] < kParkBytes);
|
||||
CHECK_EQ(res.host + res.disk, res.freed[dev]);
|
||||
CHECK(res.host <= kParkChunk);
|
||||
CHECK(res.disk > 0);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
|
||||
TEST(a_chunk_loaded_from_a_container_parks_against_it) {
|
||||
if (!need_vmem()) return;
|
||||
const std::string dir = park_dir();
|
||||
Paged a(9104, kParkBytes, 14u);
|
||||
/* The container holds the bytes at an offset that is not chunk-aligned, as weights are; the
|
||||
* last 5 MiB of the allocation never came from it and are zero. */
|
||||
const int64_t at = 3 * 4096 + 512, from = kParkBytes - (5 << 20);
|
||||
std::memset(a.want.data() + from, 0, (size_t)(kParkBytes - from));
|
||||
const std::string path = dir + "/container.bin";
|
||||
{
|
||||
FILE* f = std::fopen(path.c_str(), "wb");
|
||||
CHECK(f != nullptr);
|
||||
std::vector<uint8_t> pad((size_t)at, 0x7e);
|
||||
std::fwrite(pad.data(), 1, pad.size(), f);
|
||||
std::fwrite(a.want.data(), 1, (size_t)from, f);
|
||||
std::fclose(f);
|
||||
}
|
||||
CHECK_OK(a.upload());
|
||||
const int fid = rad::dev_page_file(path.c_str());
|
||||
/* As the load's sweep records it (engine_bringup.cpp): a run as a single piece, and one as
|
||||
* strided rows. */
|
||||
rad::dev_page_source(a.d, fid, at, (int64_t)100 << 20, 1, 0);
|
||||
const int64_t rows = (from - ((int64_t)100 << 20)) / 4096;
|
||||
rad::dev_page_source((char*)a.d + ((int64_t)100 << 20), fid, at + ((int64_t)100 << 20), 4096, rows, 4096);
|
||||
rad::PageOut req;
|
||||
req.host_budget = 0;
|
||||
req.dir = dir;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK(res.file >= 3 * kParkChunk);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
/* A write since the load: that chunk no longer matches, and goes to disk instead. */
|
||||
a.want[70 << 20] ^= 0xff;
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_invalidate((char*)a.d + (70 << 20), 1);
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK(res.disk >= kParkChunk);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
|
||||
/* A write into a sealed chunk after a park leaves its copy exact but for the blocks written: the
|
||||
* next park moves only those, into the host copy or onto the disk copy. */
|
||||
TEST(a_write_after_a_park_moves_only_its_blocks_back_out) {
|
||||
if (!need_vmem()) return;
|
||||
const std::string dir = park_dir();
|
||||
CHECK(!dir.empty());
|
||||
const int64_t blk = (int64_t)64 << 10;
|
||||
/* Two writes: one byte in chunk 1, and 20 bytes across a block boundary in chunk 2. */
|
||||
auto scribble = [&](Paged& a, uint8_t x) {
|
||||
a.want[(size_t)(70 << 20)] ^= x;
|
||||
for (int k = 0; k < 20; ++k) a.want[(size_t)((130 << 20) - 10 + k)] ^= x;
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_invalidate((char*)a.d + (70 << 20), 1);
|
||||
rad::dev_page_invalidate((char*)a.d + (130 << 20) - 10, 20);
|
||||
};
|
||||
for (int host = 0; host < 2; ++host) {
|
||||
Paged a(9107 + host, kParkBytes, 31u + 2 * host);
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_seal(a.d);
|
||||
rad::PageOut req;
|
||||
req.host_budget = host ? ((int64_t)1 << 30) : 0;
|
||||
req.dir = dir;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
scribble(a, 0x5a);
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_EQ(host ? res.host : res.disk, 3 * blk);
|
||||
CHECK_EQ(host ? res.disk : res.host, 0);
|
||||
CHECK_EQ(res.reused, res.freed[0] - 3 * blk); /* the span: kParkBytes rounded up */
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
}
|
||||
|
||||
/* An allocation its owner refills (a weights pool and its container) leaves the card with nothing
|
||||
* copied, whatever host room or disk tier there is; the page-in maps it back and hands it to the
|
||||
* owner, which here writes it again; a page-in that would not hand it back refuses. */
|
||||
TEST(an_allocation_its_owner_refills_parks_with_no_copy_and_comes_back_to_be_filled) {
|
||||
if (!need_vmem()) return;
|
||||
const std::string dir = park_dir();
|
||||
CHECK(!dir.empty());
|
||||
Paged a(9111, kParkBytes, 53u);
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_seal(a.d);
|
||||
rad::dev_page_refilled(a.d);
|
||||
CHECK_EQ(rad::dev_page_uncopied(a.owner), 0);
|
||||
for (int round = 0; round < 2; ++round) {
|
||||
rad::PageOut req;
|
||||
req.host_budget = (int64_t)1 << 30;
|
||||
req.dir = dir;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_EQ(res.host, 0);
|
||||
CHECK_EQ(res.disk, 0);
|
||||
CHECK_EQ(res.file, res.freed[0]);
|
||||
CHECK(res.freed[0] >= kParkBytes);
|
||||
CHECK_EQ(rad::dev_page_in(a.owner), RAD_E_STATE); /* nobody to fill it */
|
||||
std::vector<rad::PageSpan> spans;
|
||||
CHECK_OK(rad::dev_page_in(a.owner, &spans));
|
||||
REQUIRE_EQ(spans.size(), (size_t)1); /* adjacent chunks merged */
|
||||
CHECK(spans[0].p == (char*)a.d);
|
||||
CHECK(spans[0].bytes >= kParkBytes);
|
||||
CHECK_EQ(spans[0].device, 0);
|
||||
CHECK_OK(a.upload());
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
/* A partial park takes chunks from the top (the short last one, then a whole one) and hands
|
||||
* back only those. */
|
||||
rad::PageOut part;
|
||||
part.want[0] = kParkChunk;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, part, &res));
|
||||
CHECK(res.file >= kParkChunk);
|
||||
CHECK(res.file < res.freed[0] + 1 && res.file < kParkBytes);
|
||||
std::vector<rad::PageSpan> spans;
|
||||
CHECK_OK(rad::dev_page_in(a.owner, &spans));
|
||||
REQUIRE_EQ(spans.size(), (size_t)1);
|
||||
CHECK_EQ(spans[0].bytes, res.file);
|
||||
CHECK(spans[0].p > (char*)a.d);
|
||||
CHECK_OK(a.upload());
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
|
||||
/* A host fill of an allocation its owner refills: host memory mapped at its parked chunks, filled by
|
||||
* the owner (here a copy from the card's side, as the refill's DMA writes it), comes back over the
|
||||
* link with nothing handed back; the copy stays exact, so the next park leaves by the swap; once
|
||||
* the host copies are dropped the chunks are the owner's to refill again. */
|
||||
TEST(a_refilled_allocation_fills_into_host_memory_and_keeps_that_copy) {
|
||||
if (!need_vmem()) return;
|
||||
Paged a(9113, kParkBytes, 59u);
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_seal(a.d);
|
||||
rad::dev_page_refilled(a.d);
|
||||
rad::PageOut req;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
const std::vector<rad::PageSpan> pend = rad::dev_page_refill_chunks(a.owner);
|
||||
CHECK_EQ(pend.size(), (size_t)4);
|
||||
CHECK(rad::dev_page_fillable(a.owner) >= kParkBytes);
|
||||
CHECK_OK(rad::dev_page_fill_begin(a.owner, pend));
|
||||
CHECK_OK(a.upload()); /* into the host memory now mapped at the chunks */
|
||||
rad::dev_page_fill_end(a.owner, pend, true);
|
||||
CHECK_EQ(rad::dev_page_fillable(a.owner), 0);
|
||||
CHECK(rad::dev_page_refill_chunks(a.owner).empty());
|
||||
std::vector<rad::PageSpan> spans;
|
||||
CHECK_OK(rad::dev_page_in(a.owner, &spans));
|
||||
CHECK(spans.empty());
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
/* The kept copy is exact: the park swaps, copies nothing, and the restore copies it back. */
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_EQ(res.reused, res.freed[0]);
|
||||
CHECK_EQ(res.file, 0);
|
||||
CHECK_EQ(res.host, 0);
|
||||
CHECK_OK(rad::dev_page_in(a.owner, &spans));
|
||||
CHECK(spans.empty());
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
/* Dropped while parked, the copies leave the chunks to the owner again. */
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK(rad::dev_page_drop_host(a.owner) >= kParkBytes);
|
||||
CHECK_EQ(rad::dev_page_refill_chunks(a.owner).size(), (size_t)4);
|
||||
CHECK_OK(rad::dev_page_in(a.owner, &spans));
|
||||
REQUIRE_EQ(spans.size(), (size_t)1);
|
||||
CHECK_OK(a.upload());
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
/* A fill that fails gives its host memory back and leaves the chunks to be refilled. */
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
const int64_t host0 = rad::dev_page_host_bytes();
|
||||
const std::vector<rad::PageSpan> again = rad::dev_page_refill_chunks(a.owner);
|
||||
CHECK_OK(rad::dev_page_fill_begin(a.owner, again));
|
||||
CHECK(rad::dev_page_host_bytes() >= host0 + kParkBytes);
|
||||
rad::dev_page_fill_end(a.owner, again, false);
|
||||
CHECK_EQ(rad::dev_page_host_bytes(), host0);
|
||||
CHECK_EQ(rad::dev_page_refill_chunks(a.owner).size(), (size_t)4);
|
||||
spans.clear();
|
||||
CHECK_OK(rad::dev_page_in(a.owner, &spans));
|
||||
CHECK_EQ(spans.size(), (size_t)1);
|
||||
CHECK_OK(a.upload());
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
|
||||
/* Parked to host memory, the allocation stays mapped at its address: the card reads it and writes
|
||||
* it there, over the link, and a restore brings back what was written meanwhile. A second park
|
||||
* of the sealed allocation, its host copy kept, leaves by a swap of mappings and copies nothing. */
|
||||
TEST(a_parked_chunk_is_read_and_written_in_place_from_host_memory) {
|
||||
if (!need_vmem()) return;
|
||||
Paged a(9109, kParkBytes, 41u);
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_seal(a.d);
|
||||
rad::PageOut req;
|
||||
req.host_budget = (int64_t)1 << 30;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK(res.host >= kParkBytes);
|
||||
CHECK(rad::dev_page_host_bytes() >= kParkBytes);
|
||||
CHECK_EQ(a.first_bad(), -1); /* read in place, out of host memory */
|
||||
a.want[(size_t)(100 << 20)] ^= 0x3c; /* written in place */
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_invalidate((char*)a.d + (100 << 20), 1);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_EQ(res.host, 0);
|
||||
CHECK_EQ(res.reused, res.freed[0]);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
/* The kept copies go; the host memory with them. */
|
||||
const int64_t before = rad::dev_page_host_bytes();
|
||||
CHECK(rad::dev_page_drop_copies(a.owner) >= kParkBytes);
|
||||
CHECK(rad::dev_page_host_bytes() <= before - kParkBytes);
|
||||
}
|
||||
|
||||
/* A live restore leaves a sealed allocation's chunks in host memory, read in place; promotion
|
||||
* moves them onto the card in a quiet window. A write that lands between the promotion's copy and
|
||||
* its swap (here, inside the quiet hook before the swap runs) is copied again, not lost. */
|
||||
TEST(a_live_restore_serves_from_host_memory_until_promoted) {
|
||||
if (!need_vmem()) return;
|
||||
Paged a(9110, kParkBytes, 51u);
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_seal(a.d);
|
||||
rad::PageOut req;
|
||||
req.host_budget = (int64_t)1 << 30;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
int64_t left = 0;
|
||||
CHECK_OK(rad::dev_page_in_live(a.owner, &left));
|
||||
CHECK_EQ(left, res.freed[0]);
|
||||
CHECK_EQ(rad::dev_page_live_bytes(a.owner), left);
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
bool wrote = false;
|
||||
auto quiet = [&](const std::function<int()>& fn) -> int {
|
||||
if (!wrote) {
|
||||
wrote = true;
|
||||
a.want[(size_t)(10 << 20)] ^= 0x77;
|
||||
if (a.upload() < 0) return RAD_E_DEVICE;
|
||||
rad::dev_page_invalidate((char*)a.d + (10 << 20), 1);
|
||||
}
|
||||
return fn();
|
||||
};
|
||||
/* One chunk at a time: the write lands in the first. */
|
||||
int64_t moved = 0;
|
||||
for (int k = 0; k < 8; ++k) {
|
||||
const int64_t n = rad::dev_page_promote(a.owner, kParkChunk, quiet);
|
||||
CHECK(n >= 0);
|
||||
if (n <= 0) break;
|
||||
moved += n;
|
||||
}
|
||||
CHECK_EQ(moved, left);
|
||||
CHECK_EQ(rad::dev_page_live_bytes(a.owner), 0);
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
/* Promoted with its host copy kept: the next park is a swap. */
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_EQ(res.host, 0);
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
|
||||
/* Parked to the disk tier alone, then read into host memory while it waits: the chunks are
|
||||
* readable in place there, and the restore copies them over the link. */
|
||||
TEST(a_chunk_parked_on_the_drive_fills_into_host_memory_and_comes_back_exact) {
|
||||
if (!need_vmem()) return;
|
||||
const std::string dir = park_dir();
|
||||
CHECK(!dir.empty());
|
||||
Paged a(9111, kParkBytes, 61u);
|
||||
CHECK_OK(a.upload());
|
||||
rad::PageOut req;
|
||||
req.host_budget = 0;
|
||||
req.dir = dir;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK_EQ(res.host, 0);
|
||||
const int64_t out = res.freed[0];
|
||||
CHECK_EQ(rad::dev_page_fillable(a.owner), out);
|
||||
const int64_t held = rad::dev_page_host_bytes();
|
||||
/* One chunk, then the rest. */
|
||||
CHECK_EQ(rad::dev_page_fill_host(a.owner, 1), kParkChunk);
|
||||
CHECK_EQ(rad::dev_page_fillable(a.owner), out - kParkChunk);
|
||||
CHECK_EQ(rad::dev_page_fill_host(a.owner, INT64_MAX), out - kParkChunk);
|
||||
CHECK_EQ(rad::dev_page_fillable(a.owner), 0);
|
||||
CHECK_EQ(rad::dev_page_fill_host(a.owner, INT64_MAX), 0);
|
||||
CHECK_EQ(rad::dev_page_host_bytes() - held, out);
|
||||
CHECK_EQ(a.first_bad(), -1); /* read in place from host memory */
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
CHECK_EQ(rad::dev_page_host_bytes(), held);
|
||||
}
|
||||
|
||||
/* A chunk read in place from host memory and written there (a live restore after a fill from the
|
||||
* drive) has its host copy as its only exact one: the drive's copy is stale. Promoted onto the
|
||||
* card and its host copy then let go, the next park writes it again rather than unmapping it
|
||||
* against the drive, and it comes back with the write. */
|
||||
TEST(a_chunk_written_in_place_does_not_go_out_against_its_stale_drive_copy) {
|
||||
if (!need_vmem()) return;
|
||||
const std::string dir = park_dir();
|
||||
CHECK(!dir.empty());
|
||||
Paged a(9112, kParkBytes, 71u);
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_seal(a.d);
|
||||
rad::PageOut req;
|
||||
req.host_budget = 0;
|
||||
req.dir = dir;
|
||||
rad::PageResult res;
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
const int64_t out = res.freed[0];
|
||||
CHECK_EQ(rad::dev_page_fill_host(a.owner, INT64_MAX), out);
|
||||
int64_t left = 0;
|
||||
CHECK_OK(rad::dev_page_in_live(a.owner, &left));
|
||||
CHECK_EQ(left, out);
|
||||
a.want[(size_t)(10 << 20)] ^= 0x5a; /* written in place, in host memory */
|
||||
CHECK_OK(a.upload());
|
||||
rad::dev_page_invalidate((char*)a.d + (10 << 20), 1);
|
||||
int64_t moved = 0;
|
||||
for (int k = 0; k < 64; ++k) {
|
||||
const int64_t n = rad::dev_page_promote(a.owner, INT64_MAX,
|
||||
[](const std::function<int()>& fn) { return fn(); });
|
||||
CHECK(n >= 0);
|
||||
if (n <= 0) break;
|
||||
moved += n;
|
||||
}
|
||||
CHECK_EQ(moved, left);
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
(void)rad::dev_page_drop_copies(a.owner);
|
||||
CHECK_OK(rad::dev_page_out(a.owner, req, &res));
|
||||
CHECK(res.disk >= kParkChunk); /* the written chunk, at least, went to the drive again */
|
||||
CHECK_OK(rad::dev_page_in(a.owner));
|
||||
CHECK_EQ(a.first_bad(), -1);
|
||||
}
|
||||
|
||||
/* Granules mapped and unmapped at random, so the physical pages a freed granule held come back
|
||||
* under other addresses and addresses come back on other pages: each live granule must hold only
|
||||
* what was last written to it. On Linux 7.1 with gfx1201 an address mapped again kept its previous
|
||||
* page until the cards' translations were flushed in full (dev_settle_maps, which VMem::commit
|
||||
* runs): two live granules then shared a page and one's writes showed in the other. A process that
|
||||
* opens the render node meanwhile (rocm-smi) made the alias far more frequent, so this is also run
|
||||
* beside one. */
|
||||
TEST(granules_remapped_at_random_never_share_a_page) {
|
||||
if (!need_vmem()) return;
|
||||
const int64_t g = rad::VMem::granularity();
|
||||
constexpr int kGranules = 128, kCycles = 1000;
|
||||
/* Each granule's head and tail are written and read back: a page two granules share shows
|
||||
* at both ends. */
|
||||
constexpr int64_t kProbe = 4096;
|
||||
rad_dev_set(0);
|
||||
rad::VMem v;
|
||||
CHECK_OK(v.reserve(kGranules * g));
|
||||
RadStream s = nullptr;
|
||||
CHECK_OK(rad_stream_create(&s, 0));
|
||||
uint32_t* stage = static_cast<uint32_t*>(rad_dev_alloc(kProbe, RAD_MEM_HOST_PINNED));
|
||||
uint32_t* back = static_cast<uint32_t*>(rad_dev_alloc(kGranules * 2 * kProbe, RAD_MEM_HOST_PINNED));
|
||||
CHECK(stage && back);
|
||||
std::vector<uint32_t> tag(kGranules, 0); /* the word a live granule holds; 0: unmapped */
|
||||
uint32_t next = 1;
|
||||
std::mt19937 rng(20261106u);
|
||||
char* const base = static_cast<char*>(v.base());
|
||||
auto map = [&](int i) -> int {
|
||||
if (const int rc = v.commit(i * g, g); rc < 0) return rc;
|
||||
tag[i] = next++;
|
||||
std::fill(stage, stage + kProbe / 4, tag[i]);
|
||||
if (const int rc = rad_memcpy_async(base + i * g, stage, kProbe, s); rc < 0) return rc;
|
||||
if (const int rc = rad_memcpy_async(base + (i + 1) * g - kProbe, stage, kProbe, s); rc < 0) return rc;
|
||||
return rad_stream_sync(s);
|
||||
};
|
||||
for (int i = 0; i < kGranules; i += 2) CHECK_OK(map(i));
|
||||
int bad = 0, first_bad_cycle = -1, first_bad_granule = -1;
|
||||
for (int c = 0; c < kCycles && bad == 0; ++c) {
|
||||
std::vector<int> live, dead;
|
||||
for (int i = 0; i < kGranules; ++i) (tag[i] ? live : dead).push_back(i);
|
||||
if (!live.empty() && (dead.empty() || rng() % 2)) {
|
||||
const int i = live[rng() % live.size()];
|
||||
CHECK_OK(v.decommit(i * g, g));
|
||||
tag[i] = 0;
|
||||
continue;
|
||||
}
|
||||
CHECK_OK(map(dead[rng() % dead.size()]));
|
||||
/* Every live granule's head and tail, in one batch. */
|
||||
int k = 0;
|
||||
for (int i = 0; i < kGranules; ++i) {
|
||||
if (!tag[i]) continue;
|
||||
char* dst = reinterpret_cast<char*>(back) + (int64_t)k * 2 * kProbe;
|
||||
CHECK_OK(rad_memcpy_async(dst, base + i * g, kProbe, s));
|
||||
CHECK_OK(rad_memcpy_async(dst + kProbe, base + (i + 1) * g - kProbe, kProbe, s));
|
||||
++k;
|
||||
}
|
||||
CHECK_OK(rad_stream_sync(s));
|
||||
k = 0;
|
||||
for (int i = 0; i < kGranules; ++i) {
|
||||
if (!tag[i]) continue;
|
||||
const uint32_t* w = back + (int64_t)k * 2 * kProbe / 4;
|
||||
for (int64_t j = 0; j < 2 * kProbe / 4; ++j)
|
||||
if (w[j] != tag[i]) {
|
||||
if (bad++ == 0) { first_bad_cycle = c; first_bad_granule = i; }
|
||||
break;
|
||||
}
|
||||
++k;
|
||||
}
|
||||
}
|
||||
if (bad) fprintf(stderr, " %d granule(s) held another's bytes, first granule %d at cycle %d\n", bad,
|
||||
first_bad_granule, first_bad_cycle);
|
||||
CHECK_EQ(bad, 0);
|
||||
rad_dev_free(stage, RAD_MEM_HOST_PINNED);
|
||||
rad_dev_free(back, RAD_MEM_HOST_PINNED);
|
||||
rad_stream_destroy(s);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN_HOST_KARGS()
|
||||
Reference in new issue
Block a user