brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
96 / 182
52.7%
Branches:
52 / 248
21.0%
Functions:
11 / 12
91.7%

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/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (const char* v = std::getenv("BROTENSOR_AUTOTUNE")) {
21
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ 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
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 if (ov >= 0) return ov != 0;
46 2 return env_autotune_on();
47 8 }
48
49 8 std::string kernel_cache_dir() {
50
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
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
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
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
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
3 if (!is_minimal && !is_default && !is_llm && !is_diff) {
77
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ 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
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Tensor a = Tensor::zeros_on(dev, 8, 32, Dtype::FP32);
88
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Tensor b = Tensor::zeros_on(dev, 8, 32, Dtype::FP32);
89
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Tensor c = Tensor::zeros_on(dev, 8, 32, Dtype::FP32);
90
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Tensor gamma = Tensor::zeros_on(dev, 1, 32, Dtype::FP32);
91 3 Tensor y_ew, y_rn;
92 {
93
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 TraceScope ts;
94
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 y_ew = silu(a * b + c);
95 3 }
96 {
97
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 TraceScope ts;
98
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 y_rn = rms_norm(a + b, gamma, 1e-5f);
99 3 }
100 (void)y_ew;
101 (void)y_rn;
102
103
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Tensor mm_a = Tensor::zeros_on(dev, 32, 32, Dtype::FP32);
104
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Tensor mm_b = Tensor::zeros_on(dev, 32, 32, Dtype::FP32);
105 3 Tensor mm_c;
106
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 matmul(mm_a, mm_b, mm_c);
107
108
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (dev.is_gpu()) {
109
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Tensor a16 = Tensor::zeros_on(dev, 128, 128, Dtype::FP16);
110
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Tensor b16 = Tensor::zeros_on(dev, 128, 128, Dtype::FP16);
111
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Tensor c16 = Tensor::zeros_on(dev, 128, 128, Dtype::FP16);
112
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!is_minimal) {
118
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor x = Tensor::zeros_on(dev, 16, 64, Dtype::FP32);
119
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor g = Tensor::zeros_on(dev, 1, 64, Dtype::FP32);
120
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor b = Tensor::zeros_on(dev, 1, 64, Dtype::FP32);
121 ✗ Tensor y, xh, m, r;
122
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ layernorm_forward_batched_with_caches(x, g, b, y, xh, m, r, 1e-5f);
123
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ softmax_forward(x, y);
124
125 ✗ Tensor t_gelu;
126 {
127
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ TraceScope ts;
128
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ t_gelu = gelu(x + b);
129 ✗ }
130 (void)t_gelu;
131
132
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor cx = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP32);
133
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor cw = Tensor::zeros_on(dev, 16, 16 * 9, Dtype::FP32);
134 ✗ Tensor cy;
135
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ conv2d_forward(cx, cw, nullptr, 1, 16, 8, 8, 16, 3, 3, 1, 1, 1, 1, 1, 1, 1, cy);
136
137
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor q = Tensor::zeros_on(dev, 32, 4 * 64, Dtype::FP32);
138
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor k = Tensor::zeros_on(dev, 32, 4 * 64, Dtype::FP32);
139
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor v = Tensor::zeros_on(dev, 32, 4 * 64, Dtype::FP32);
140 ✗ Tensor ao;
141
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (is_llm) {
146 ✗ const auto warm_llm_dtype = [&](Dtype wdt) {
147 ✗ Tensor x = Tensor::zeros_on(dev, 16, 128, wdt);
148
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor g = Tensor::zeros_on(dev, 1, 128, wdt);
149 ✗ Tensor y;
150
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ rms_norm_forward(x, g, 1e-5f, y);
151
152
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor w_glu = Tensor::zeros_on(dev, 256, 128, wdt);
153 ✗ Tensor out_glu;
154
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ linear_forward_batched_ex(w_glu, nullptr, x, kLinearActNone, kLinearEpiSwiglu, nullptr, out_glu);
155
156
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (dev.is_gpu()) {
157
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor x_wide = Tensor::zeros_on(dev, 128, 128, wdt);
158 ✗ Tensor out_wide;
159
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ linear_forward_batched_ex(w_glu, nullptr, x_wide, kLinearActNone, kLinearEpiSwiglu, nullptr, out_wide);
160
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor b128 = Tensor::zeros_on(dev, 128, 128, wdt);
161
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor c128 = Tensor::zeros_on(dev, 128, 128, wdt);
162
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ matmul_abt(x_wide, b128, c128, 1, 128, 128, 128, 0, 0, 0, nullptr, kLinearActNone);
163 ✗ }
164
165
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor dec_a = Tensor::zeros_on(dev, 1, 128, wdt);
166
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor dec_b = Tensor::zeros_on(dev, 128, 128, wdt);
167
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor dec_c = Tensor::zeros_on(dev, 1, 128, wdt);
168
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (wdt == Dtype::FP16 || wdt == Dtype::BF16) {
169
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ matmul_abt(dec_a, dec_b, dec_c, 1, 1, 128, 128, 0, 0, 0, nullptr, kLinearActNone);
170 ✗ } else {
171
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ matmul(dec_a, dec_b, dec_c);
172 }
173
174
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int hd : {64, 128}) {
175
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor q = Tensor::zeros_on(dev, 64, 4 * hd, wdt);
176
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor k = Tensor::zeros_on(dev, 64, 4 * hd, wdt);
177
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor v = Tensor::zeros_on(dev, 64, 4 * hd, wdt);
178 ✗ Tensor ao;
179
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ flash_attention_gqa_forward(q, k, v, nullptr, 4, 4, true, ao);
180 ✗ }
181 ✗ };
182
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (dev.is_gpu()) {
183
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ warm_llm_dtype(Dtype::FP16);
184
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ warm_llm_dtype(Dtype::BF16);
185 ✗ } else {
186
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ warm_llm_dtype(Dtype::FP32);
187 }
188 ✗ }
189
190 // 4. Diffusion profile: GroupNorm, diffusion sampler steps, timestep embedding, GeGLU + conv2d.
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (is_diff) {
192
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor x = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP32);
193
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor g = Tensor::zeros_on(dev, 1, 16, Dtype::FP32);
194
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor b = Tensor::zeros_on(dev, 1, 16, Dtype::FP32);
195 ✗ Tensor y;
196
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ group_norm_forward(x, g, b, 1, 16, 8, 8, 4, 1e-5f, y);
197
198
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor eps = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP32);
199
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor xp = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP32);
200
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ ddim_step(x, eps, 0.9f, 0.8f, 0.0f, xp);
201
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ euler_step(x, eps, 1.0f, 0.5f, xp);
202
203
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor ts = Tensor::zeros_on(dev, 2, 1, Dtype::FP32);
204 ✗ Tensor emb;
205
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ timestep_embedding(ts, 64, 10000.0f, emb);
206
207
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (dev.is_gpu()) {
208
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (Dtype dt : {Dtype::FP16, Dtype::BF16}) {
209
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor gx = Tensor::zeros_on(dev, 128, 128, dt);
210
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor gw = Tensor::zeros_on(dev, 256, 128, dt);
211 ✗ Tensor gy;
212
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ linear_forward_batched_ex(gw, nullptr, gx, kLinearActNone, kLinearEpiGeglu, nullptr, gy);
213 ✗ }
214
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor cx = Tensor::zeros_on(dev, 1, 16 * 8 * 8, Dtype::FP16);
215
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Tensor cw = Tensor::zeros_on(dev, 16, 16 * 9, Dtype::FP16);
216 ✗ Tensor cy;
217
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ conv2d_forward(cx, cw, nullptr, 1, 16, 8, 8, 16, 3, 3, 1, 1, 1, 1, 1, 1, 1, cy);
218 ✗ }
219 ✗ }
220
221
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 brotensor::sync(dev);
222
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (opts.flush) {
223
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 flush_kernel_caches(dev);
224 3 }
225
226 3 WarmupStats stats;
227
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 const std::size_t writes_after = jit::KernelDiskCache::instance().stats().writes;
228
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 stats.kernels_compiled = (writes_after >= writes_before) ? (writes_after - writes_before) : 0;
229 3 stats.autotune_entries = 0;
230
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 stats.elapsed_ms = std::chrono::duration<double, std::milli>(
231
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
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
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 const std::string meta = std::string("device=") + device_name(dev) +
240
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 ";dir=" + kernel_cache_dir();
241
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
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
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 const bool ok = jit::KernelDiskCache::instance().import_bundle(
249 4 bundle_path, nullptr, &imp, &skip, &bytes, err);
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
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