include/brotensor/kernel_cache.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Public kernel cache management, shippable bundle export/import, warmup, | ||
| 4 | // and per-device runtime autotuning controls. | ||
| 5 | |||
| 6 | #include "runtime.h" | ||
| 7 | #include "tensor.h" | ||
| 8 | |||
| 9 | #include <cstddef> | ||
| 10 | #include <cstdint> | ||
| 11 | #include <string> | ||
| 12 | |||
| 13 | namespace brotensor { | ||
| 14 | |||
| 15 | 6 | struct KernelCacheStats { | |
| 16 | std::string dir; | ||
| 17 | 6 | std::size_t hits = 0; | |
| 18 | 6 | std::size_t misses = 0; | |
| 19 | 6 | std::size_t writes = 0; | |
| 20 | 6 | std::size_t corruptions = 0; | |
| 21 | 6 | std::size_t entries = 0; | |
| 22 | }; | ||
| 23 | |||
| 24 | 3 | struct WarmupOptions { | |
| 25 | /// "minimal", "default", "llm", or "diffusion". | ||
| 26 | 3 | std::string profile = "default"; | |
| 27 | 3 | bool autotune = false; | |
| 28 | 3 | bool flush = true; | |
| 29 | }; | ||
| 30 | |||
| 31 | 3 | struct WarmupStats { | |
| 32 | 3 | std::size_t kernels_compiled = 0; | |
| 33 | 3 | std::size_t autotune_entries = 0; | |
| 34 | 3 | double elapsed_ms = 0.0; | |
| 35 | }; | ||
| 36 | |||
| 37 | 2 | struct BundleImportReport { | |
| 38 | 2 | std::size_t entries_imported = 0; | |
| 39 | 2 | std::size_t entries_skipped = 0; | |
| 40 | 2 | std::size_t bytes = 0; | |
| 41 | }; | ||
| 42 | |||
| 43 | /// Returns statistics and directory path for the active kernel disk cache. | ||
| 44 | KernelCacheStats kernel_cache_stats(); | ||
| 45 | |||
| 46 | /// Returns the resolved directory used for persistent kernel caching. | ||
| 47 | std::string kernel_cache_dir(); | ||
| 48 | |||
| 49 | /// Flushes pending cache writes and backend driver pipeline archives (e.g., | ||
| 50 | /// Vulkan VkPipelineCache, Metal MTLBinaryArchive) to disk. | ||
| 51 | void flush_kernel_caches(Device dev = default_device()); | ||
| 52 | |||
| 53 | /// Controls whether live per-machine micro-autotuning runs on cache misses | ||
| 54 | /// for eligible Vulkan kernels. Cached autotune decisions are always used | ||
| 55 | /// when present regardless of this setting. Can also be enabled via | ||
| 56 | /// `BROTENSOR_AUTOTUNE=1`. | ||
| 57 | void set_autotune_enabled(bool enabled) noexcept; | ||
| 58 | bool autotune_enabled() noexcept; | ||
| 59 | |||
| 60 | /// Pre-compiles representative kernel variants on `dev` according to `opts`. | ||
| 61 | WarmupStats warmup_kernels(Device dev = default_device(), const WarmupOptions& opts = {}); | ||
| 62 | |||
| 63 | /// Exports portable disk-cache entries (plus device pipeline archives when | ||
| 64 | /// available) into a single checksum-verified `.bacb` bundle file. | ||
| 65 | bool export_kernel_bundle(const std::string& bundle_path, | ||
| 66 | Device dev = default_device(), | ||
| 67 | std::string* err = nullptr); | ||
| 68 | |||
| 69 | /// Validates `bundle_path` against this build's Brass codegen fingerprint and | ||
| 70 | /// imports its cached kernel artifacts into the local disk cache directory. | ||
| 71 | bool import_kernel_bundle(const std::string& bundle_path, | ||
| 72 | BundleImportReport* report = nullptr, | ||
| 73 | std::string* err = nullptr); | ||
| 74 | |||
| 75 | } // namespace brotensor | ||
| 76 |