Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2413,13 +2413,14 @@ Pipeline creation:
merges into the offline seed. `LEONIDA_DIRECT_SESSION_CACHE=0` also uses a merge when only the session exists.
The files must match the Vulkan vendor, device and pipeline-cache UUID. Vulkan supplies an opaque snapshot.
Replacing part of the previous file would corrupt it.
`LEONIDA_PIPELINE_CACHE_BYTES` caps session writes (256 MiB by default, zero disables the session file).
`LEONIDA_PIPELINE_CACHE_BYTES` caps what a session adds, at a checkpoint or at teardown (an offline `pipeline_cache.bin` seed, which the driver hands back as part of the union, is not counted; 4 GiB by default, zero disables
the session file).
`LEONIDA_SKIP_COMPUTE=<part of a name>[,<part>...]` (`all`: every compute program) never builds the matching
compute programs and drops their dispatches, direct and indirect; the first dropped dispatch of each is logged.
It is a way past a program that hangs the GPU while the program or its input is fixed, not a fix.
`LEONIDA_STARTUP_CACHE_CHECKPOINTS=1` also schedules a snapshot after a successful pipeline build,
at most once per 120 seconds. It starts on the compile worker when queued builds have drained and the device
has no active builds. These snapshots have an additional 512 MiB ceiling. The driver fills a mapped staging
has no active builds. The driver fills a mapped staging
file directly. A successful snapshot atomically replaces the session. An interrupted write preserves the
previous cache; the next launch removes unheld staging files older than ten minutes.
While the compute warm-up has builds queued that idle checkpoint never starts, and a boot that ends by a crash,
Expand Down
23 changes: 15 additions & 8 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,10 @@ const std::filesystem::path& pipeline_journal_dir() {
return pipeline_journal_roots().writable;
}

// The offline seed's size (pipeline_cache.bin) when the device cache was created from it, 0 otherwise: the save limit
// does not count it (pipeline_cache_store::over_save_limit).
std::atomic<std::size_t> g_offline_seed_bytes{0};

// LEONIDA_PIPELINE_CACHE_BYTES, in bytes; 0 turns the session file off. One line for a value that is not a number.
std::size_t pipeline_cache_save_limit() {
char text[32] = {};
Expand Down Expand Up @@ -1489,6 +1493,7 @@ VkPipelineCache create_seeded_pipeline_cache(PFN_vkCreatePipelineCache create_ca
return VK_NULL_HANDLE;
}
std::fprintf(stderr, "[rt] d3d12: loaded %zu bytes from %s\n", seed.size(), name);
if (!session) g_offline_seed_bytes.store(seed.size(), std::memory_order_relaxed);
if (trace_pipeline_compile())
std::fprintf(stderr, "[rt] pipeline-cache op=load bytes=%zu map_us=%lld driver_us=%lld file=%s\n",
seed.size(), mapped-start, compile_us()-mapped, name);
Expand Down Expand Up @@ -1629,16 +1634,17 @@ bool startup_cache_checkpoints_enabled() {
void save_session_pipeline_cache(const leonida::VulkanDeviceFunctions& vk, VkDevice device, VkPipelineCache cache,
bool checkpoint = false, const char* op = nullptr) {
if (!cache || !vk.vkGetPipelineCacheData) return;
auto limit = pipeline_cache_save_limit();
// The output is an opaque Vulkan blob. A prefix is not an incremental cache.
if (checkpoint) limit = (std::min)(limit, std::size_t{512} << 20);
// The output is an opaque Vulkan blob. A prefix is not an incremental cache, so past the limit nothing is written.
const auto limit = pipeline_cache_save_limit();
if (!limit) return;
const auto start = compile_us();
std::size_t size = 0;
if (vk.vkGetPipelineCacheData(device, cache, &size, nullptr) != VK_SUCCESS ||
size <= sizeof(VkPipelineCacheHeaderVersionOne)) return;
if (size > limit) {
std::fprintf(stderr, "[rt] d3d12: pipeline cache is %zu bytes, over the %zu-byte write limit\n", size, limit);
const std::size_t seed = g_offline_seed_bytes.load(std::memory_order_relaxed);
if (leonida::pipeline_cache_store::over_save_limit(size, seed, limit)) {
std::fprintf(stderr, "[rt] d3d12: pipeline cache is %zu bytes (%zu of them the offline seed), over the "
"%zu-byte write limit for what a session adds\n", size, seed, limit);
return;
}
const auto queried = compile_us();
Expand Down Expand Up @@ -19734,9 +19740,10 @@ bool stop_pipeline_compiles(unsigned wait_ms) {
return idle;
}

// How long an exit save waits for the write itself (save_session_pipeline_cache): a 77 MiB cache was measured at about
// 36 ms (fill and publish), so this only bounds a driver that blocks the read.
constexpr unsigned kExitCacheWriteMs = 3000;
// How long an exit save waits for the write itself (save_session_pipeline_cache). Measured fill and publish: a 77 MiB
// cache about 36 ms; 1.87 GB 1.4 s and 2.9 GB 1.3-1.6 s (NVIDIA, Windows 11, NVMe), so about 2.2 s at the 4 GiB
// default limit. The wait ends with the write; the bound only covers a driver or disk that blocks.
constexpr unsigned kExitCacheWriteMs = 6000;

bool save_pipeline_caches_at_exit(unsigned wait_ms) {
if (exit_cache_saved.exchange(true)) return false; // once: a crash during the normal exit's save does not redo it
Expand Down
17 changes: 12 additions & 5 deletions runtime/src/pipeline_cache_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@

namespace leonida::pipeline_cache_store {

// A session cache normally holds what one boot compiled (a few MB to a few hundred MB). The cap is what keeps a
// cache seeded from the multi-GB corpus file -- whose data comes back as the whole union -- from being rewritten
// at every exit. Past it the previous session file is left alone; raise the limit with
// LEONIDA_PIPELINE_CACHE_BYTES when a session really compiles that much.
inline constexpr std::size_t default_save_limit = std::size_t{256} << 20; // 256 MiB
// A session cache holds what the boots so far compiled: 1.28 GB after a few world frames on NVIDIA and still growing,
// where a 256 MiB cap kept every later build from being saved. The limit applies to what the session adds: a cache
// seeded from the offline corpus file comes back from the driver as the whole union, so the seed's size is not
// counted (over_save_limit), and a 9.7 GB seed plus a few hundred MB of new builds is still saved. Past the limit the
// previous session file is left alone; raise it with LEONIDA_PIPELINE_CACHE_BYTES when a session really compiles that
// much.
inline constexpr std::size_t default_save_limit = std::size_t{4096} << 20; // 4 GiB

// Whether a cache of `size` bytes, created from an offline seed of `seed` bytes (0: none), adds more than `limit`.
inline bool over_save_limit(std::size_t size, std::size_t seed, std::size_t limit) {
return size - (seed < size ? seed : size) > limit;
}

struct SaveLimit {
std::size_t bytes = default_save_limit;
Expand Down
8 changes: 8 additions & 0 deletions runtime/test/pipeline_cache_store_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ static int test_limit() {
CHECK(!limit.valid);
CHECK(limit.bytes == default_save_limit);
}
// The limit counts what a session adds to an offline seed, not the seed the driver hands back with it.
constexpr std::size_t gib = std::size_t{1} << 30;
CHECK(!store::over_save_limit(3 * gib, 0, 4 * gib));
CHECK(store::over_save_limit(5 * gib, 0, 4 * gib));
CHECK(!store::over_save_limit(10 * gib, 9 * gib, 4 * gib)); // a 9 GB seed plus 1 GB of new builds is saved
CHECK(store::over_save_limit(14 * gib, 9 * gib, 4 * gib)); // 5 GB added is over 4
CHECK(!store::over_save_limit(4 * gib, 9 * gib, 1)); // smaller than the seed: nothing added
CHECK(!store::over_save_limit(4 * gib + 1, 1, 4 * gib)); // exactly at the limit once the seed is taken off
return 0;
}

Expand Down
Loading