src/jit/kernel_disk_cache.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | // ─── brotensor's On-Disk Kernel Cache ──────────────────────────────────────── | ||
| 4 | // | ||
| 5 | // What brotensor compiles — CPU kernels (MIR -> a linked, loadable image), | ||
| 6 | // Vulkan SPIR-V kernels with their launch metadata, CUDA PTX — kept across | ||
| 7 | // processes in brass's artifact cache (brass docs/artifact_cache.md). brass | ||
| 8 | // owns the file format, the keys' hashing, atomic stores, the byte budget and | ||
| 9 | // "MIR -> cached loadable native module"; this file owns only brotensor's | ||
| 10 | // policy: where the directory is, its budget, what goes into each key, and the | ||
| 11 | // SpirvKernel encoding. | ||
| 12 | // | ||
| 13 | // Directory (first that applies): | ||
| 14 | // 1. set_cache_dir() (tests) | ||
| 15 | // 2. $BRO_ML_CACHE_DIR | ||
| 16 | // 3. $XDG_CACHE_HOME/brotensor/kernels | ||
| 17 | // 4. $HOME/.cache/brotensor/kernels | ||
| 18 | // 5. <temp_directory_path>/brotensor_kernel_cache | ||
| 19 | // Budget: set_max_bytes(), else $BRO_ML_CACHE_MAX_MB, else 256 MiB; the | ||
| 20 | // least recently used entries go first. BRO_ML_CACHE_DISABLE=1 turns the | ||
| 21 | // cache off (CPU kernels are then compiled in memory by brass's KernelJit). | ||
| 22 | // | ||
| 23 | // Every key is KeyBuilder(kKeyDomain) + add_brass_build() (brass's codegen fingerprint, | ||
| 24 | // the binary holding brass — so any rebuild of brotensor is a new key space — | ||
| 25 | // the target and the host CPU's AVX/AVX2/FMA) + the kind ("cpu", "spirv", | ||
| 26 | // "ptx") + the caller's `signature`, hashed in full. A signature must name | ||
| 27 | // everything the build reads that the binary does not fix: the trace | ||
| 28 | // compilers pass TraceDAG::key_material() and their plan parameters, never a | ||
| 29 | // digest. CPU keys add the entry name, every CpuCompileOptions field and the | ||
| 30 | // pass budgets the build runs with. | ||
| 31 | // | ||
| 32 | // Files of the old format (*.btkc, the .rt/ load copies) are never read; they | ||
| 33 | // are deleted the first time a directory is opened. | ||
| 34 | |||
| 35 | #include <brotensor/tensor.h> | ||
| 36 | |||
| 37 | #include <cstddef> | ||
| 38 | #include <cstdint> | ||
| 39 | #include <filesystem> | ||
| 40 | #include <functional> | ||
| 41 | #include <memory> | ||
| 42 | #include <mutex> | ||
| 43 | #include <string> | ||
| 44 | #include <string_view> | ||
| 45 | #include <vector> | ||
| 46 | |||
| 47 | #if defined(BROTENSOR_HAS_BRASS_JIT) || defined(BROTENSOR_HAS_VULKAN_TRACE_JIT) || defined(BRASS_HOST_BACKEND) | ||
| 48 | #ifndef BROTENSOR_HAS_BRASS_JIT | ||
| 49 | #define BROTENSOR_HAS_BRASS_JIT 1 | ||
| 50 | #endif | ||
| 51 | #endif | ||
| 52 | |||
| 53 | #if BROTENSOR_HAS_BRASS_JIT | ||
| 54 | #include <brass/cache/artifact_cache.hpp> | ||
| 55 | #include <brass/mir/module.hpp> | ||
| 56 | #include <brass/target/msl_target.hpp> | ||
| 57 | #include <brass/target/spirv_target.hpp> | ||
| 58 | #endif | ||
| 59 | |||
| 60 | namespace brotensor::jit { | ||
| 61 | |||
| 62 | std::string_view kernel_source_fingerprint() noexcept; | ||
| 63 | |||
| 64 | class KernelDiskCache { | ||
| 65 | public: | ||
| 66 | // The key schema. Bump the suffix when what a key covers changes. | ||
| 67 | static constexpr std::string_view kKeyDomain = "brotensor.kernel/5"; | ||
| 68 | static constexpr uint64_t kDefaultMaxBytes = uint64_t{256} << 20; | ||
| 69 | |||
| 70 | 136 | struct Stats { | |
| 71 | 136 | size_t hits = 0; // loaded from disk | |
| 72 | 136 | size_t misses = 0; // compiled | |
| 73 | 136 | size_t writes = 0; // stored after a compile | |
| 74 | 136 | size_t corruptions = 0; // an entry that was there but could not be used | |
| 75 | }; | ||
| 76 | |||
| 77 | // Everything here reaches the CPU kernel's code, so all of it is in the key. | ||
| 78 | 62 | struct CpuCompileOptions { | |
| 79 | 62 | bool enable_loop_opt = true; | |
| 80 | 62 | bool enable_avx2 = true; | |
| 81 | 62 | bool enable_fma = true; | |
| 82 | 62 | bool enable_vectorize = true; | |
| 83 | 62 | bool enable_unroll = true; | |
| 84 | 62 | uint32_t unroll_factor = 4; | |
| 85 | 62 | bool enable_fp_reassociation = true; | |
| 86 | 62 | bool enable_smt_guard_elim = true; | |
| 87 | 62 | bool enable_mir_superopt = true; | |
| 88 | 62 | bool enable_trace_layout = true; | |
| 89 | 62 | bool enable_post_ra_schedule = true; | |
| 90 | 62 | bool enable_software_pipelining = true; | |
| 91 | }; | ||
| 92 | |||
| 93 | static KernelDiskCache& instance(); | ||
| 94 | |||
| 95 | // False when BRO_ML_CACHE_DISABLE=1 (or true), or when brotensor was built | ||
| 96 | // without brass. | ||
| 97 | bool is_enabled() const noexcept; | ||
| 98 | |||
| 99 | std::filesystem::path cache_dir() const; | ||
| 100 | void set_cache_dir(std::filesystem::path dir); | ||
| 101 | void clear_cache_dir_override(); | ||
| 102 | |||
| 103 | uint64_t max_bytes() const; | ||
| 104 | // 0 restores the default (the environment, else kDefaultMaxBytes). | ||
| 105 | void set_max_bytes(uint64_t bytes); | ||
| 106 | |||
| 107 | // CUDA PTX text. | ||
| 108 | bool store_ptx(std::string_view signature, std::string_view ptx); | ||
| 109 | bool load_ptx(std::string_view signature, std::string& out_ptx); | ||
| 110 | |||
| 111 | // Generic raw binary artifact (e.g. Vulkan pipeline cache, sibling MIR caches). | ||
| 112 | bool store_blob(std::string_view kind, std::string_view signature, const void* data, std::size_t size); | ||
| 113 | bool load_blob(std::string_view kind, std::string_view signature, std::vector<uint8_t>& out); | ||
| 114 | |||
| 115 | #if BROTENSOR_HAS_BRASS_JIT | ||
| 116 | // The key a PTX / SPIR-V blob of `kind` is stored under. | ||
| 117 | static brass::cache::CacheKey blob_key(std::string_view kind, std::string_view signature); | ||
| 118 | // The key a CPU kernel is stored under (NativeModuleCache adds the link | ||
| 119 | // options to it): what compile_or_load_cpu_kernel builds from. | ||
| 120 | static brass::cache::CacheKey cpu_key(std::string_view signature, std::string_view entry_name, | ||
| 121 | const CpuCompileOptions& opts); | ||
| 122 | // Where those live (tests). | ||
| 123 | std::filesystem::path blob_entry_path(std::string_view kind, std::string_view signature) const; | ||
| 124 | std::filesystem::path cpu_entry_path(std::string_view signature, std::string_view entry_name, | ||
| 125 | const CpuCompileOptions& opts) const; | ||
| 126 | |||
| 127 | // A SPIR-V kernel and everything needed to launch it. | ||
| 128 | bool store_spirv_kernel(std::string_view signature, const brass::target::SpirvKernel& kernel); | ||
| 129 | bool load_spirv_kernel(std::string_view signature, brass::target::SpirvKernel& out_kernel); | ||
| 130 | |||
| 131 | // A Metal (MSL) kernel and its dispatch/parameter metadata. | ||
| 132 | bool store_msl_kernel(std::string_view signature, const brass::target::MslKernel& kernel); | ||
| 133 | bool load_msl_kernel(std::string_view signature, brass::target::MslKernel& out_kernel); | ||
| 134 | |||
| 135 | // A CPU kernel: loaded from the cache, or built — build_fn fills the | ||
| 136 | // module, then brotensor's passes run (SMT guard elimination, MIR | ||
| 137 | // superoptimization, loop optimization, per `opts`) — compiled, linked, | ||
| 138 | // stored and loaded by brass's NativeModuleCache. A module that calls | ||
| 139 | // functions it does not define cannot be a cached image; it is compiled | ||
| 140 | // in memory by KernelJit instead (also when the cache is disabled). The | ||
| 141 | // code stays loaded until clear_loaded_libraries(), whatever happens to | ||
| 142 | // its cache entry. Null when the kernel cannot be had; an exception from | ||
| 143 | // build_fn propagates. | ||
| 144 | void* compile_or_load_cpu_kernel( | ||
| 145 | std::string_view signature, | ||
| 146 | std::string_view entry_name, | ||
| 147 | const std::function<void(brass::Module&)>& build_fn, | ||
| 148 | const CpuCompileOptions& opts); | ||
| 149 | |||
| 150 | void* compile_or_load_cpu_kernel( | ||
| 151 | std::string_view signature, | ||
| 152 | std::string_view entry_name, | ||
| 153 | const std::function<void(brass::Module&)>& build_fn) { | ||
| 154 | return compile_or_load_cpu_kernel(signature, entry_name, build_fn, CpuCompileOptions{}); | ||
| 155 | } | ||
| 156 | |||
| 157 | template <typename FnPtr, typename BuildFn> | ||
| 158 | 125 | FnPtr compile_or_load_cpu_fn( | |
| 159 | std::string_view signature, | ||
| 160 | std::string_view entry_name, | ||
| 161 | BuildFn&& build_fn, | ||
| 162 | const CpuCompileOptions& opts) { | ||
| 163 | 125 | return reinterpret_cast<FnPtr>( | |
| 164 | 250 | compile_or_load_cpu_kernel(signature, entry_name, | |
| 165 | 125 | std::function<void(brass::Module&)>(std::forward<BuildFn>(build_fn)), | |
| 166 | 125 | opts)); | |
| 167 | ✗ | } | |
| 168 | |||
| 169 | template <typename FnPtr, typename BuildFn> | ||
| 170 | FnPtr compile_or_load_cpu_fn( | ||
| 171 | std::string_view signature, | ||
| 172 | std::string_view entry_name, | ||
| 173 | BuildFn&& build_fn) { | ||
| 174 | return compile_or_load_cpu_fn<FnPtr>(signature, entry_name, std::forward<BuildFn>(build_fn), | ||
| 175 | CpuCompileOptions{}); | ||
| 176 | } | ||
| 177 | |||
| 178 | // Walks the directory now, evicting down to the budget (tests, | ||
| 179 | // maintenance); returns what the entries take afterwards. | ||
| 180 | uint64_t trim(); | ||
| 181 | #endif | ||
| 182 | |||
| 183 | Stats stats() const noexcept; | ||
| 184 | void reset_stats() noexcept; | ||
| 185 | |||
| 186 | // Unloads every CPU kernel this cache handed out (only after the callers' | ||
| 187 | // function pointers are gone: single-threaded test resets). | ||
| 188 | void clear_loaded_libraries(); | ||
| 189 | |||
| 190 | std::size_t entry_count() const; | ||
| 191 | bool export_bundle(const std::filesystem::path& out_path, | ||
| 192 | std::string_view metadata, | ||
| 193 | std::size_t* entries_written = nullptr, | ||
| 194 | std::size_t* bytes_out = nullptr, | ||
| 195 | std::string* error = nullptr) const; | ||
| 196 | bool import_bundle(const std::filesystem::path& in_path, | ||
| 197 | std::string* metadata_out = nullptr, | ||
| 198 | std::size_t* entries_imported = nullptr, | ||
| 199 | std::size_t* entries_skipped = nullptr, | ||
| 200 | std::size_t* bytes_out = nullptr, | ||
| 201 | std::string* error = nullptr); | ||
| 202 | |||
| 203 | private: | ||
| 204 | 408 | KernelDiskCache() = default; | |
| 205 | |||
| 206 | struct Stores; | ||
| 207 | std::shared_ptr<Stores> stores() const; // null when disabled | ||
| 208 | |||
| 209 | mutable std::mutex mutex_; | ||
| 210 | std::filesystem::path override_dir_; | ||
| 211 | 136 | uint64_t override_max_bytes_ = 0; | |
| 212 | mutable std::shared_ptr<Stores> stores_; | ||
| 213 | std::vector<std::shared_ptr<const void>> loaded_; // NativeModules and KernelJit engines | ||
| 214 | |||
| 215 | mutable Stats stats_; | ||
| 216 | }; | ||
| 217 | |||
| 218 | using MlKernelDiskCache = KernelDiskCache; | ||
| 219 | |||
| 220 | using PipelineCacheFlushFn = void (*)(int device_index); | ||
| 221 | void register_pipeline_flush_hook(DeviceType type, PipelineCacheFlushFn fn); | ||
| 222 | void flush_pipeline_caches_for(Device dev); | ||
| 223 | |||
| 224 | } // namespace brotensor::jit | ||
| 225 |