src/jit/kernel_cache_api.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <brotensor/kernel_cache.h> | ||
| 2 | |||
| 3 | #include "kernel_disk_cache.h" | ||
| 4 | #include <brotensor/jit/trace.h> | ||
| 5 | #include <brotensor/ops.h> | ||
| 6 | |||
| 7 | #include <atomic> | ||
| 8 | #include <chrono> | ||
| 9 | #include <cstdlib> | ||
| 10 | #include <stdexcept> | ||
| 11 | #include <string> | ||
| 12 | |||
| 13 | namespace brotensor { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | std::atomic<int> g_autotune_override{-1}; // -1 = check env BROTENSOR_AUTOTUNE | ||
| 18 | |||
| 19 | 2 | bool env_autotune_on() noexcept { | |
| 20 | 2 | if (const char* v = std::getenv("BROTENSOR_AUTOTUNE")) { | |
| 21 | ✗ | return v[0] == '1' || v[0] == 't' || v[0] == 'T' || v[0] == 'y' || v[0] == 'Y'; | |
| 22 | } | ||
| 23 | 2 | return false; | |
| 24 | 2 | } | |
| 25 | |||
| 26 | struct AutotuneScopeGuard { | ||
| 27 | int prev; | ||
| 28 | 6 | explicit AutotuneScopeGuard(bool enable) | |
| 29 | 6 | : prev(g_autotune_override.exchange(enable ? 1 : 0, std::memory_order_relaxed)) {} | |
| 30 | 6 | ~AutotuneScopeGuard() { | |
| 31 | 3 | g_autotune_override.store(prev, std::memory_order_relaxed); | |
| 32 | 6 | } | |
| 33 | AutotuneScopeGuard(const AutotuneScopeGuard&) = delete; | ||
| 34 | AutotuneScopeGuard& operator=(const AutotuneScopeGuard&) = delete; | ||
| 35 | }; | ||
| 36 | |||
| 37 | } // namespace | ||
| 38 | |||
| 39 | 9 | void set_autotune_enabled(bool enabled) noexcept { | |
| 40 | 9 | g_autotune_override.store(enabled ? 1 : 0, std::memory_order_relaxed); | |
| 41 | 9 | } | |
| 42 | |||
| 43 | 8 | bool autotune_enabled() noexcept { | |
| 44 | 8 | const int ov = g_autotune_override.load(std::memory_order_relaxed); | |
| 45 | 8 | if (ov >= 0) return ov != 0; | |
| 46 | 2 | return env_autotune_on(); | |
| 47 | 8 | } | |
| 48 | |||
| 49 | 8 | std::string kernel_cache_dir() { | |
| 50 | 8 | return jit::KernelDiskCache::instance().cache_dir().string(); | |
| 51 | ✗ | } | |
| 52 | |||
| 53 | 6 | KernelCacheStats kernel_cache_stats() { | |
| 54 | 6 | auto& kdc = jit::KernelDiskCache::instance(); | |
| 55 | 6 | const auto s = kdc.stats(); | |
| 56 | 6 | KernelCacheStats out; | |
| 57 | 6 | out.dir = kdc.cache_dir().string(); | |
| 58 | 6 | out.hits = s.hits; | |
| 59 | 6 | out.misses = s.misses; | |
| 60 | 6 | out.writes = s.writes; | |
| 61 | 6 | out.corruptions = s.corruptions; | |
| 62 | 6 | out.entries = kdc.entry_count(); | |
| 63 | 6 | return out; | |
| 64 | 6 | } | |
| 65 | |||
| 66 | 6 | void flush_kernel_caches(Device dev) { | |
| 67 | 6 | jit::flush_pipeline_caches_for(dev); | |
| 68 | 6 | } | |
| 69 | |||
| 70 | 3 | WarmupStats warmup_kernels(Device dev, const WarmupOptions& opts) { | |
| 71 | 3 | const std::string& p = opts.profile; | |
| 72 | 3 | const bool is_minimal = (p == "minimal"); | |
| 73 | 3 | const bool is_default = (p == "default" || p.empty()); | |
| 74 | 3 | const bool is_llm = (p == "llm"); | |
| 75 | 3 | const bool is_diff = (p == "diffusion"); | |
| 76 | 3 | if (!is_minimal && !is_default && !is_llm && !is_diff) { | |
| 77 | ✗ | throw std::invalid_argument("warmup_kernels: unknown profile '" + p + | |
| 78 | "' (expected minimal, default, llm, or diffusion)"); | ||
| 79 | } | ||
| 80 | |||
| 81 | 3 | const auto t0 = std::chrono::steady_clock::now(); | |
| 82 | 3 | const std::size_t writes_before = jit::KernelDiskCache::instance().stats().writes; | |
| 83 | 3 | AutotuneScopeGuard at_guard(opts.autotune); | |
| 84 | |||
| 85 | // 1. Minimal warmup: traced elementwise + RMSNorm + basic matmul/matmul_abt. | ||
| 86 | { | ||
| 87 | 3 | Tensor a = Tensor::zeros_on(dev, 8, 32, Dtype::FP32); | |
| 88 | 3 | Tensor b = Tensor::zeros_on(dev, 8, 32, Dtype::FP32); | |
| 89 | 3 | Tensor c = Tensor::zeros_on(dev, 8, 32, Dtype::FP32); | |
| 90 | 3 | Tensor gamma = Tensor::zeros_on(dev, 1, 32, Dtype::FP32); | |
| 91 | 3 | Tensor y_ew, y_rn; | |
| 92 | { | ||
| 93 | 3 | TraceScope ts; | |
| 94 | 3 | y_ew = silu(a * b + c); | |
| 95 | 3 | } | |
| 96 | { | ||
| 97 | 3 | TraceScope ts; | |
| 98 | 3 | y_rn = rms_norm(a + b, gamma, 1e-5f); | |
| 99 | 3 | } | |
| 100 | (void)y_ew; | ||
| 101 | (void)y_rn; | ||
| 102 | |||
| 103 | 3 | Tensor mm_a = Tensor::zeros_on(dev, 32, 32, Dtype::FP32); | |
| 104 | 3 | Tensor mm_b = Tensor::zeros_on(dev, 32, 32, Dtype::FP32); | |
| 105 | 3 | Tensor mm_c; | |
| 106 | 3 | matmul(mm_a, mm_b, mm_c); | |
| 107 | |||
| 108 | 3 | if (dev.is_gpu()) { | |
| 109 | 3 | Tensor a16 = Tensor::zeros_on(dev, 128, 128, Dtype::FP16); | |
| 110 | 3 | Tensor b16 = Tensor::zeros_on(dev, 128, 128, Dtype::FP16); | |
| 111 | 3 | Tensor c16 = Tensor::zeros_on(dev, 128, 128, Dtype::FP16); | |
| 112 | 3 | matmul_abt(a16, b16, c16, 1, 128, 128, 128, 0, 0, 0, nullptr, kLinearActNone); | |
| 113 | 3 | } | |
| 114 | 3 | } | |
| 115 | |||
| 116 | // 2. Default & shared variants. | ||
| 117 | 3 | if (!is_minimal) { | |
| 118 | ✗ | Tensor x = Tensor::zeros_on(dev, 16, 64, Dtype::FP32); | |
| 119 | ✗ | Tensor g = Tensor::zeros_on(dev, 1, 64, Dtype::FP32); | |
| 120 | ✗ | Tensor b = Tensor::zeros_on(dev, 1, 64, Dtype::FP32); | |
| 121 | ✗ | Tensor y, xh, m, r; | |
| 122 | ✗ | layernorm_forward_batched_with_caches(x, g, b, y, xh, m, r, 1e-5f); | |
| 123 | ✗ | softmax_forward(x, y); | |
| 124 | |||
| 125 | ✗ | Tensor t_gelu; | |
| 126 | { | ||
| 127 | ✗ | TraceScope ts; | |
| 128 | ✗ | t_gelu = gelu(x + b); | |
| 129 | ✗ | } | |
| 130 | (void)t_gelu; | ||
| 131 | |||
| 132 | ✗ | Tensor cx = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP32); | |
| 133 | ✗ | Tensor cw = Tensor::zeros_on(dev, 16, 16 * 9, Dtype::FP32); | |
| 134 | ✗ | Tensor cy; | |
| 135 | ✗ | conv2d_forward(cx, cw, nullptr, 1, 16, 8, 8, 16, 3, 3, 1, 1, 1, 1, 1, 1, 1, cy); | |
| 136 | |||
| 137 | ✗ | Tensor q = Tensor::zeros_on(dev, 32, 4 * 64, Dtype::FP32); | |
| 138 | ✗ | Tensor k = Tensor::zeros_on(dev, 32, 4 * 64, Dtype::FP32); | |
| 139 | ✗ | Tensor v = Tensor::zeros_on(dev, 32, 4 * 64, Dtype::FP32); | |
| 140 | ✗ | Tensor ao; | |
| 141 | ✗ | flash_attention_gqa_forward(q, k, v, nullptr, 4, 4, false, ao); | |
| 142 | ✗ | } | |
| 143 | |||
| 144 | // 3. LLM profile: RMSNorm op, SwiGLU linear epilogue (skinny + MMA), decode GEMV, causal FA. | ||
| 145 | 3 | if (is_llm) { | |
| 146 | ✗ | const auto warm_llm_dtype = [&](Dtype wdt) { | |
| 147 | ✗ | Tensor x = Tensor::zeros_on(dev, 16, 128, wdt); | |
| 148 | ✗ | Tensor g = Tensor::zeros_on(dev, 1, 128, wdt); | |
| 149 | ✗ | Tensor y; | |
| 150 | ✗ | rms_norm_forward(x, g, 1e-5f, y); | |
| 151 | |||
| 152 | ✗ | Tensor w_glu = Tensor::zeros_on(dev, 256, 128, wdt); | |
| 153 | ✗ | Tensor out_glu; | |
| 154 | ✗ | linear_forward_batched_ex(w_glu, nullptr, x, kLinearActNone, kLinearEpiSwiglu, nullptr, out_glu); | |
| 155 | |||
| 156 | ✗ | if (dev.is_gpu()) { | |
| 157 | ✗ | Tensor x_wide = Tensor::zeros_on(dev, 128, 128, wdt); | |
| 158 | ✗ | Tensor out_wide; | |
| 159 | ✗ | linear_forward_batched_ex(w_glu, nullptr, x_wide, kLinearActNone, kLinearEpiSwiglu, nullptr, out_wide); | |
| 160 | ✗ | Tensor b128 = Tensor::zeros_on(dev, 128, 128, wdt); | |
| 161 | ✗ | Tensor c128 = Tensor::zeros_on(dev, 128, 128, wdt); | |
| 162 | ✗ | matmul_abt(x_wide, b128, c128, 1, 128, 128, 128, 0, 0, 0, nullptr, kLinearActNone); | |
| 163 | ✗ | } | |
| 164 | |||
| 165 | ✗ | Tensor dec_a = Tensor::zeros_on(dev, 1, 128, wdt); | |
| 166 | ✗ | Tensor dec_b = Tensor::zeros_on(dev, 128, 128, wdt); | |
| 167 | ✗ | Tensor dec_c = Tensor::zeros_on(dev, 1, 128, wdt); | |
| 168 | ✗ | if (wdt == Dtype::FP16 || wdt == Dtype::BF16) { | |
| 169 | ✗ | matmul_abt(dec_a, dec_b, dec_c, 1, 1, 128, 128, 0, 0, 0, nullptr, kLinearActNone); | |
| 170 | ✗ | } else { | |
| 171 | ✗ | matmul(dec_a, dec_b, dec_c); | |
| 172 | } | ||
| 173 | |||
| 174 | ✗ | for (int hd : {64, 128}) { | |
| 175 | ✗ | Tensor q = Tensor::zeros_on(dev, 64, 4 * hd, wdt); | |
| 176 | ✗ | Tensor k = Tensor::zeros_on(dev, 64, 4 * hd, wdt); | |
| 177 | ✗ | Tensor v = Tensor::zeros_on(dev, 64, 4 * hd, wdt); | |
| 178 | ✗ | Tensor ao; | |
| 179 | ✗ | flash_attention_gqa_forward(q, k, v, nullptr, 4, 4, true, ao); | |
| 180 | ✗ | } | |
| 181 | ✗ | }; | |
| 182 | ✗ | if (dev.is_gpu()) { | |
| 183 | ✗ | warm_llm_dtype(Dtype::FP16); | |
| 184 | ✗ | warm_llm_dtype(Dtype::BF16); | |
| 185 | ✗ | } else { | |
| 186 | ✗ | warm_llm_dtype(Dtype::FP32); | |
| 187 | } | ||
| 188 | ✗ | } | |
| 189 | |||
| 190 | // 4. Diffusion profile: GroupNorm, diffusion sampler steps, timestep embedding, GeGLU + conv2d. | ||
| 191 | 3 | if (is_diff) { | |
| 192 | ✗ | Tensor x = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP32); | |
| 193 | ✗ | Tensor g = Tensor::zeros_on(dev, 1, 16, Dtype::FP32); | |
| 194 | ✗ | Tensor b = Tensor::zeros_on(dev, 1, 16, Dtype::FP32); | |
| 195 | ✗ | Tensor y; | |
| 196 | ✗ | group_norm_forward(x, g, b, 1, 16, 8, 8, 4, 1e-5f, y); | |
| 197 | |||
| 198 | ✗ | Tensor eps = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP32); | |
| 199 | ✗ | Tensor xp = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP32); | |
| 200 | ✗ | ddim_step(x, eps, 0.9f, 0.8f, 0.0f, xp); | |
| 201 | ✗ | euler_step(x, eps, 1.0f, 0.5f, xp); | |
| 202 | |||
| 203 | ✗ | Tensor ts = Tensor::zeros_on(dev, 2, 1, Dtype::FP32); | |
| 204 | ✗ | Tensor emb; | |
| 205 | ✗ | timestep_embedding(ts, 64, 10000.0f, emb); | |
| 206 | |||
| 207 | ✗ | if (dev.is_gpu()) { | |
| 208 | ✗ | for (Dtype dt : {Dtype::FP16, Dtype::BF16}) { | |
| 209 | ✗ | Tensor gx = Tensor::zeros_on(dev, 128, 128, dt); | |
| 210 | ✗ | Tensor gw = Tensor::zeros_on(dev, 256, 128, dt); | |
| 211 | ✗ | Tensor gy; | |
| 212 | ✗ | linear_forward_batched_ex(gw, nullptr, gx, kLinearActNone, kLinearEpiGeglu, nullptr, gy); | |
| 213 | ✗ | } | |
| 214 | ✗ | Tensor cx = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP16); | |
| 215 | ✗ | Tensor cw = Tensor::zeros_on(dev, 16, 16 * 9, Dtype::FP16); | |
| 216 | ✗ | Tensor cy; | |
| 217 | ✗ | conv2d_forward(cx, cw, nullptr, 1, 16, 8, 8, 16, 3, 3, 1, 1, 1, 1, 1, 1, 1, cy); | |
| 218 | ✗ | } | |
| 219 | ✗ | } | |
| 220 | |||
| 221 | 3 | brotensor::sync(dev); | |
| 222 | 3 | if (opts.flush) { | |
| 223 | 3 | flush_kernel_caches(dev); | |
| 224 | 3 | } | |
| 225 | |||
| 226 | 3 | WarmupStats stats; | |
| 227 | 3 | const std::size_t writes_after = jit::KernelDiskCache::instance().stats().writes; | |
| 228 | 3 | stats.kernels_compiled = (writes_after >= writes_before) ? (writes_after - writes_before) : 0; | |
| 229 | 3 | stats.autotune_entries = 0; | |
| 230 | 6 | stats.elapsed_ms = std::chrono::duration<double, std::milli>( | |
| 231 | 3 | std::chrono::steady_clock::now() - t0).count(); | |
| 232 | return stats; | ||
| 233 | 3 | } | |
| 234 | |||
| 235 | 3 | bool export_kernel_bundle(const std::string& bundle_path, | |
| 236 | Device dev, | ||
| 237 | std::string* err) { | ||
| 238 | 3 | flush_kernel_caches(dev); | |
| 239 | 6 | const std::string meta = std::string("device=") + device_name(dev) + | |
| 240 | 3 | ";dir=" + kernel_cache_dir(); | |
| 241 | 3 | return jit::KernelDiskCache::instance().export_bundle(bundle_path, meta, nullptr, nullptr, err); | |
| 242 | 3 | } | |
| 243 | |||
| 244 | 4 | bool import_kernel_bundle(const std::string& bundle_path, | |
| 245 | BundleImportReport* report, | ||
| 246 | std::string* err) { | ||
| 247 | 4 | std::size_t imp = 0, skip = 0, bytes = 0; | |
| 248 | 8 | const bool ok = jit::KernelDiskCache::instance().import_bundle( | |
| 249 | 4 | bundle_path, nullptr, &imp, &skip, &bytes, err); | |
| 250 | 4 | if (report) { | |
| 251 | 4 | report->entries_imported = imp; | |
| 252 | 4 | report->entries_skipped = skip; | |
| 253 | 4 | report->bytes = bytes; | |
| 254 | 4 | } | |
| 255 | 4 | return ok; | |
| 256 | ✗ | } | |
| 257 | |||
| 258 | } // namespace brotensor | ||
| 259 |