brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
408 / 567
72.0%
Branches:
176 / 518
34.0%
Functions:
30 / 35
85.7%

src/cpu/cpu_jit_quant.cpp
Line Branch Exec Source
1 #include "cpu_jit.h"
2 #include <brotensor/detail/cpu/thread_pool.h>
3 #include <brotensor/tensor.h>
4 #include <brotensor/ops.h>
5
6 #include <algorithm>
7 #include <cmath>
8 #include <cstdint>
9 #include <cstring>
10 #include <iostream>
11 #include <memory>
12 #include <mutex>
13 #include <stdexcept>
14 #include <vector>
15
16 namespace brotensor::detail::cpu::jit {
17 namespace {
18
19 658131 static inline void ref_dequant_q8_0_block(const void* blk_ptr, float* out32) {
20 uint16_t d_raw;
21 658131 std::memcpy(&d_raw, blk_ptr, 2);
22 658131 const float d = fp16_bits_to_fp32(d_raw);
23 658131 const int8_t* qs = reinterpret_cast<const int8_t*>(static_cast<const char*>(blk_ptr) + 2);
24
2/2
✓ Branch 0 taken 20823007 times.
✓ Branch 1 taken 658131 times.
21481138 for (int i = 0; i < 32; ++i) {
25 20823007 out32[i] = static_cast<float>(qs[i]) * d;
26 20823007 }
27 658131 }
28
29 86475 static inline void ref_dequant_q4k_block(const void* blk_ptr, float* out256) {
30 86475 const uint8_t* p = static_cast<const uint8_t*>(blk_ptr);
31 uint16_t d_raw, dmin_raw;
32 86475 std::memcpy(&d_raw, p, 2);
33 86475 std::memcpy(&dmin_raw, p + 2, 2);
34 86475 const float d = fp16_bits_to_fp32(d_raw);
35 86475 const float dmin = fp16_bits_to_fp32(dmin_raw);
36
37 86475 const uint8_t* scales = p + 4;
38 86475 const uint8_t* qs = p + 16;
39
40 uint8_t sc[8], m[8];
41
2/2
✓ Branch 0 taken 691652 times.
✓ Branch 1 taken 86475 times.
778127 for (int j = 0; j < 8; ++j) {
42
2/2
✓ Branch 0 taken 345839 times.
✓ Branch 1 taken 345813 times.
691652 if (j < 4) {
43 345839 sc[j] = scales[j] & 0x3F;
44 345839 m[j] = scales[j + 4] & 0x3F;
45 345839 } else {
46 345813 sc[j] = static_cast<uint8_t>((scales[j + 4] & 0x0F) | ((scales[j - 4] >> 6) << 4));
47 345813 m[j] = static_cast<uint8_t>((scales[j + 4] >> 4) | ((scales[j - 0] >> 6) << 4));
48 }
49 691652 }
50
51
2/2
✓ Branch 0 taken 344830 times.
✓ Branch 1 taken 86475 times.
431305 for (int p_idx = 0; p_idx < 4; ++p_idx) {
52 344830 const int is_lo = 2 * p_idx;
53 344830 const int is_hi = 2 * p_idx + 1;
54 344830 const float w_lo = static_cast<float>(sc[is_lo]) * d;
55 344830 const float w_hi = static_cast<float>(sc[is_hi]) * d;
56 344830 const float b_lo = static_cast<float>(m [is_lo]) * dmin;
57 344830 const float b_hi = static_cast<float>(m [is_hi]) * dmin;
58
59
2/2
✓ Branch 0 taken 10806389 times.
✓ Branch 1 taken 344830 times.
11151219 for (int l = 0; l < 32; ++l) {
60 10806389 const uint8_t byte = qs[p_idx * 32 + l];
61 10806389 const int n_lo = byte & 0x0F;
62 10806389 const int n_hi = (byte >> 4) & 0x0F;
63 10806389 out256[is_lo * 32 + l] = w_lo * static_cast<float>(n_lo) - b_lo;
64 10806389 out256[is_hi * 32 + l] = w_hi * static_cast<float>(n_hi) - b_hi;
65 10806389 }
66 344830 }
67 86475 }
68
69 160 static inline void ref_dequant_q6k_block(const void* blk_ptr, float* out256) {
70 160 const uint8_t* p = static_cast<const uint8_t*>(blk_ptr);
71 160 const uint8_t* ql = p;
72 160 const uint8_t* qh = p + 128;
73 160 const int8_t* scales = reinterpret_cast<const int8_t*>(p + 192);
74 uint16_t d_raw;
75 160 std::memcpy(&d_raw, p + 208, 2);
76 160 const float d = fp16_bits_to_fp32(d_raw);
77
78
2/2
✓ Branch 0 taken 40960 times.
✓ Branch 1 taken 160 times.
41120 for (int e = 0; e < 256; ++e) {
79 40960 const int group = e >> 7;
80 40960 const int local = e & 127;
81 40960 const int quad = local >> 5;
82 40960 const int l = local & 31;
83 40960 const int sb = (group << 3) + (quad << 1) + (l >> 4);
84 40960 const uint8_t ql_b = ql[group * 64 + (quad & 1) * 32 + l];
85 40960 const uint8_t qh_b = qh[group * 32 + l];
86
2/2
✓ Branch 0 taken 20480 times.
✓ Branch 1 taken 20480 times.
40960 const int raw4 = (quad < 2) ? (ql_b & 0x0F) : (ql_b >> 4);
87 40960 const int high2 = (qh_b >> (quad * 2)) & 0x03;
88 40960 const int val6 = (raw4 | (high2 << 4)) - 32;
89 40960 out256[e] = d * static_cast<float>(scales[sb]) * static_cast<float>(val6);
90 40960 }
91 160 }
92
93 ✗ static void ref_gemv_q8_0(const void* W, const float* X, float* Y, int N, int K) {
94 ✗ const int bpr = K / 32;
95 ✗ const char* W_bytes = static_cast<const char*>(W);
96 float blk_buf[32];
97
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int r = 0; r < N; ++r) {
98 ✗ double acc = 0.0;
99 ✗ const char* row_ptr = W_bytes + static_cast<size_t>(r) * bpr * 34;
100
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int b = 0; b < bpr; ++b) {
101 ✗ ref_dequant_q8_0_block(row_ptr + b * 34, blk_buf);
102 ✗ const float* x_sub = X + b * 32;
103
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < 32; ++i) {
104 ✗ acc += static_cast<double>(blk_buf[i]) * static_cast<double>(x_sub[i]);
105 ✗ }
106 ✗ }
107 ✗ Y[r] = static_cast<float>(acc);
108 ✗ }
109 ✗ }
110
111 ✗ static void ref_gemv_q4k(const void* W, const float* X, float* Y, int N, int K) {
112 ✗ const int bpr = K / 256;
113 ✗ const char* W_bytes = static_cast<const char*>(W);
114 float blk_buf[256];
115
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int r = 0; r < N; ++r) {
116 ✗ double acc = 0.0;
117 ✗ const char* row_ptr = W_bytes + static_cast<size_t>(r) * bpr * 144;
118
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int b = 0; b < bpr; ++b) {
119 ✗ ref_dequant_q4k_block(row_ptr + b * 144, blk_buf);
120 ✗ const float* x_sub = X + b * 256;
121
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < 256; ++i) {
122 ✗ acc += static_cast<double>(blk_buf[i]) * static_cast<double>(x_sub[i]);
123 ✗ }
124 ✗ }
125 ✗ Y[r] = static_cast<float>(acc);
126 ✗ }
127 ✗ }
128
129 ✗ static void ref_gemv_q6k(const void* W, const float* X, float* Y, int N, int K) {
130 ✗ const int bpr = K / 256;
131 ✗ const char* W_bytes = static_cast<const char*>(W);
132 float blk_buf[256];
133
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int r = 0; r < N; ++r) {
134 ✗ double acc = 0.0;
135 ✗ const char* row_ptr = W_bytes + static_cast<size_t>(r) * bpr * 210;
136
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int b = 0; b < bpr; ++b) {
137 ✗ ref_dequant_q6k_block(row_ptr + b * 210, blk_buf);
138 ✗ const float* x_sub = X + b * 256;
139
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < 256; ++i) {
140 ✗ acc += static_cast<double>(blk_buf[i]) * static_cast<double>(x_sub[i]);
141 ✗ }
142 ✗ }
143 ✗ Y[r] = static_cast<float>(acc);
144 ✗ }
145 ✗ }
146
147 } // namespace
148 } // namespace brotensor::detail::cpu::jit
149
150 #if BROTENSOR_HAS_BRASS_JIT
151
152 #include <brass/codegen/ml_fusion.hpp>
153 #include <brass/codegen/kernel_jit.hpp>
154 #include <brass/mir/builder.hpp>
155 #include <brass/mir/module.hpp>
156 #include <brass/mir/function.hpp>
157 #include "../jit/kernel_disk_cache.h"
158 #include <atomic>
159 #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
160 #include <immintrin.h>
161 #endif
162
163 namespace brotensor::detail::cpu::jit {
164
165 namespace {
166
167 using namespace brass;
168 using namespace brass::codegen;
169
170 using BlockAccFn = void (*)(const float* w_blk, const float* x_blk, float* acc8);
171
172 3 static void populate_gemv_q8_0_acc_block(Module& mod) {
173 6 Function* fn = mod.create_function(
174 3 "gemv_q8_0_acc_block",
175 3 Type::void_type(),
176 3 {Type::ptr(), Type::ptr(), Type::ptr()});
177 3 KernelBuilder kb(mod, fn);
178
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 BasicBlock* entry = kb.builder().append_block("entry");
179 3 kb.position_at_end(entry);
180
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* w_blk = kb.builder().add_block_param(entry, Type::ptr());
181
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* x_blk = kb.builder().add_block_param(entry, Type::ptr());
182
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* acc8 = kb.builder().add_block_param(entry, Type::ptr());
183
184
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* vsum = kb.vload_f32x8(acc8, 0);
185
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
15 for (int c = 0; c < 4; ++c) {
186
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vw = kb.vload_f32x8(w_blk, c * 32);
187
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vx = kb.vload_f32x8(x_blk, c * 32);
188
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 vsum = kb.vfma(vw, vx, vsum);
189 12 }
190
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 kb.vstore_f32x8(acc8, vsum, 0);
191
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 kb.builder().build_ret_void();
192 3 }
193
194 3 static void populate_gemv_q4_k_acc_block(Module& mod) {
195 6 Function* fn = mod.create_function(
196 3 "gemv_q4_k_acc_block",
197 3 Type::void_type(),
198 3 {Type::ptr(), Type::ptr(), Type::ptr()});
199 3 KernelBuilder kb(mod, fn);
200
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 BasicBlock* entry = kb.builder().append_block("entry");
201 3 kb.position_at_end(entry);
202
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* w_blk = kb.builder().add_block_param(entry, Type::ptr());
203
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* x_blk = kb.builder().add_block_param(entry, Type::ptr());
204
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* acc8 = kb.builder().add_block_param(entry, Type::ptr());
205
206
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* vsum = kb.vload_f32x8(acc8, 0);
207
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 96 times.
99 for (int c = 0; c < 32; ++c) {
208
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 Value* vw = kb.vload_f32x8(w_blk, c * 32);
209
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 Value* vx = kb.vload_f32x8(x_blk, c * 32);
210
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 vsum = kb.vfma(vw, vx, vsum);
211 96 }
212
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 kb.vstore_f32x8(acc8, vsum, 0);
213
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 kb.builder().build_ret_void();
214 3 }
215
216 657671 static inline void fast_dequant_q8_0_block(const void* blk_ptr, float* out32) {
217 #if defined(__AVX2__)
218 uint16_t d_raw;
219 std::memcpy(&d_raw, blk_ptr, 2);
220 const float d = fp16_bits_to_fp32(d_raw);
221 const int8_t* qs = reinterpret_cast<const int8_t*>(static_cast<const char*>(blk_ptr) + 2);
222 __m256 d_vec = _mm256_set1_ps(d);
223 for (int i = 0; i < 4; ++i) {
224 __m128i q8 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(qs + i * 8));
225 __m256i q32 = _mm256_cvtepi8_epi32(q8);
226 __m256 qf = _mm256_cvtepi32_ps(q32);
227 __m256 w = _mm256_mul_ps(qf, d_vec);
228 _mm256_storeu_ps(out32 + i * 8, w);
229 }
230 #else
231 657671 ref_dequant_q8_0_block(blk_ptr, out32);
232 #endif
233 657671 }
234
235 struct QuantJitKernels {
236 bool available = false;
237 BlockAccFn q8_0_acc_fn = nullptr;
238 BlockAccFn q4_k_acc_fn = nullptr;
239 };
240
241 static std::mutex s_quant_jit_mu;
242 static std::atomic<bool> s_quant_jit_ready{false};
243 static QuantJitKernels s_quant_kernels;
244
245 271 static const QuantJitKernels& get_quant_kernels() {
246
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 5 times.
271 if (s_quant_jit_ready.load(std::memory_order_acquire)) {
247 266 return s_quant_kernels;
248 }
249 5 std::lock_guard<std::mutex> lk(s_quant_jit_mu);
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!s_quant_jit_ready.load(std::memory_order_relaxed)) {
251 try {
252
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto& cache = brotensor::jit::KernelDiskCache::instance();
253 5 brotensor::jit::KernelDiskCache::CpuCompileOptions opts;
254
255
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 s_quant_kernels.q8_0_acc_fn = cache.compile_or_load_cpu_fn<BlockAccFn>(
256
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 "cpu_quant_gemv_q8_0_v1", "gemv_q8_0_acc_block",
257 populate_gemv_q8_0_acc_block, opts);
258
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 s_quant_kernels.q4_k_acc_fn = cache.compile_or_load_cpu_fn<BlockAccFn>(
259
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 "cpu_quant_gemv_q4_k_v1", "gemv_q4_k_acc_block",
260 populate_gemv_q4_k_acc_block, opts);
261
262 5 s_quant_kernels.available =
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 (s_quant_kernels.q8_0_acc_fn != nullptr && s_quant_kernels.q4_k_acc_fn != nullptr);
264
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
5 } catch (const std::exception& e) {
265
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ std::cerr << "brotensor: Brass JIT quant kernels initialization failed: " << e.what() << "\n";
266 ✗ s_quant_kernels.available = false;
267 ✗ }
268 5 s_quant_jit_ready.store(true, std::memory_order_release);
269 5 }
270 5 return s_quant_kernels;
271 271 }
272
273 324 static void run_q8_0_rows(BlockAccFn acc_fn, const void* W, const float* X, float* Y, int N, int K) {
274 324 const int bpr = K / 32;
275 324 const char* W_bytes = static_cast<const char*>(W);
276 alignas(32) float blk_buf[32];
277 alignas(32) float acc8[8];
278
2/2
✓ Branch 0 taken 5424 times.
✓ Branch 1 taken 324 times.
5748 for (int r = 0; r < N; ++r) {
279 5424 std::memset(acc8, 0, sizeof(acc8));
280 5424 const char* row_ptr = W_bytes + static_cast<size_t>(r) * bpr * 34;
281
2/2
✓ Branch 0 taken 666360 times.
✓ Branch 1 taken 5424 times.
671784 for (int b = 0; b < bpr; ++b) {
282 666360 fast_dequant_q8_0_block(row_ptr + b * 34, blk_buf);
283 666360 acc_fn(blk_buf, X + b * 32, acc8);
284 666360 }
285 10848 Y[r] = ((acc8[0] + acc8[1]) + (acc8[2] + acc8[3])) +
286 5424 ((acc8[4] + acc8[5]) + (acc8[6] + acc8[7]));
287 5424 }
288 324 }
289
290 330 static void run_q4_k_rows(BlockAccFn acc_fn, const void* W, const float* X, float* Y, int N, int K) {
291 330 const int bpr = K / 256;
292 330 const char* W_bytes = static_cast<const char*>(W);
293 alignas(32) float blk_buf[256];
294 alignas(32) float acc8[8];
295
2/2
✓ Branch 0 taken 5439 times.
✓ Branch 1 taken 330 times.
5769 for (int r = 0; r < N; ++r) {
296 5439 std::memset(acc8, 0, sizeof(acc8));
297 5439 const char* row_ptr = W_bytes + static_cast<size_t>(r) * bpr * 144;
298
2/2
✓ Branch 0 taken 86510 times.
✓ Branch 1 taken 5439 times.
91949 for (int b = 0; b < bpr; ++b) {
299 86510 ref_dequant_q4k_block(row_ptr + b * 144, blk_buf);
300 86510 acc_fn(blk_buf, X + b * 256, acc8);
301 86510 }
302 10878 Y[r] = ((acc8[0] + acc8[1]) + (acc8[2] + acc8[3])) +
303 5439 ((acc8[4] + acc8[5]) + (acc8[6] + acc8[7]));
304 5439 }
305 330 }
306
307 15 static void run_q6_k_rows(BlockAccFn acc_fn, const void* W, const float* X, float* Y, int N, int K) {
308 15 const int bpr = K / 256;
309 15 const char* W_bytes = static_cast<const char*>(W);
310 alignas(32) float blk_buf[256];
311 alignas(32) float acc8[8];
312
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 15 times.
95 for (int r = 0; r < N; ++r) {
313 80 std::memset(acc8, 0, sizeof(acc8));
314 80 const char* row_ptr = W_bytes + static_cast<size_t>(r) * bpr * 210;
315
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 80 times.
240 for (int b = 0; b < bpr; ++b) {
316 160 ref_dequant_q6k_block(row_ptr + b * 210, blk_buf);
317 160 acc_fn(blk_buf, X + b * 256, acc8);
318 160 }
319 160 Y[r] = ((acc8[0] + acc8[1]) + (acc8[2] + acc8[3])) +
320 80 ((acc8[4] + acc8[5]) + (acc8[6] + acc8[7]));
321 80 }
322 15 }
323
324 } // namespace
325
326 ✗ void prewarm_quant_jit_kernels() {
327 ✗ (void)get_quant_kernels();
328 ✗ }
329
330 6 void reset_quant_jit_caches_for_testing() {
331 6 std::lock_guard<std::mutex> lk(s_quant_jit_mu);
332 6 s_quant_jit_ready.store(false, std::memory_order_relaxed);
333 6 s_quant_kernels = QuantJitKernels{};
334 6 }
335
336 132 void gemv_q8_0(const void* W, const float* X, float* Y, int N, int K) {
337
2/4
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 132 times.
132 if (N <= 0 || K <= 0) return;
338 132 const auto& k = get_quant_kernels();
339
2/4
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 132 times.
132 if (k.available && k.q8_0_acc_fn != nullptr) {
340
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 36 times.
132 if (N >= 8) {
341 96 const size_t row_bytes = static_cast<size_t>(K / 32) * 34;
342 96 int n_threads = std::min<int>(detail::cpu::ThreadPool::instance().num_threads(), N);
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (n_threads <= 1) {
344 ✗ run_q8_0_rows(k.q8_0_acc_fn, W, X, Y, N, K);
345 ✗ } else {
346 96 int chunk_size = (N + n_threads - 1) / n_threads;
347 96 int num_chunks = (N + chunk_size - 1) / chunk_size;
348
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
384 detail::cpu::parallel_for(static_cast<std::size_t>(num_chunks), [&](std::size_t c) {
349 288 int r_start = static_cast<int>(c) * chunk_size;
350 288 int r_end = std::min(r_start + chunk_size, N);
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
288 if (r_start >= r_end) return;
352 288 const char* W_bytes = static_cast<const char*>(W);
353 288 const void* W_slice = W_bytes + static_cast<size_t>(r_start) * row_bytes;
354 288 float* Y_slice = Y + r_start;
355 288 run_q8_0_rows(k.q8_0_acc_fn, W_slice, X, Y_slice, r_end - r_start, K);
356 288 });
357 }
358 96 } else {
359 36 run_q8_0_rows(k.q8_0_acc_fn, W, X, Y, N, K);
360 }
361 132 return;
362 }
363 ✗ ref_gemv_q8_0(W, X, Y, N, K);
364 132 }
365
366 134 void gemv_q4_k(const void* W, const float* X, float* Y, int N, int K) {
367
2/4
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 134 times.
134 if (N <= 0 || K <= 0) return;
368 134 const auto& k = get_quant_kernels();
369
2/4
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 134 times.
134 if (k.available && k.q4_k_acc_fn != nullptr) {
370
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 36 times.
134 if (N >= 8) {
371 98 const size_t row_bytes = static_cast<size_t>(K / 256) * 144;
372 98 int n_threads = std::min<int>(detail::cpu::ThreadPool::instance().num_threads(), N);
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 if (n_threads <= 1) {
374 ✗ run_q4_k_rows(k.q4_k_acc_fn, W, X, Y, N, K);
375 ✗ } else {
376 98 int chunk_size = (N + n_threads - 1) / n_threads;
377 98 int num_chunks = (N + chunk_size - 1) / chunk_size;
378
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
392 detail::cpu::parallel_for(static_cast<std::size_t>(num_chunks), [&](std::size_t c) {
379 294 int r_start = static_cast<int>(c) * chunk_size;
380 294 int r_end = std::min(r_start + chunk_size, N);
381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294 times.
294 if (r_start >= r_end) return;
382 294 const char* W_bytes = static_cast<const char*>(W);
383 294 const void* W_slice = W_bytes + static_cast<size_t>(r_start) * row_bytes;
384 294 float* Y_slice = Y + r_start;
385 294 run_q4_k_rows(k.q4_k_acc_fn, W_slice, X, Y_slice, r_end - r_start, K);
386 294 });
387 }
388 98 } else {
389 36 run_q4_k_rows(k.q4_k_acc_fn, W, X, Y, N, K);
390 }
391 134 return;
392 }
393 ✗ ref_gemv_q4k(W, X, Y, N, K);
394 134 }
395
396 5 void gemv_q6_k(const void* W, const float* X, float* Y, int N, int K) {
397
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (N <= 0 || K <= 0) return;
398 5 const auto& k = get_quant_kernels();
399
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (k.available && k.q4_k_acc_fn != nullptr) {
400
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (N >= 8) {
401 5 const size_t row_bytes = static_cast<size_t>(K / 256) * 210;
402 5 int n_threads = std::min<int>(detail::cpu::ThreadPool::instance().num_threads(), N);
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (n_threads <= 1) {
404 ✗ run_q6_k_rows(k.q4_k_acc_fn, W, X, Y, N, K);
405 ✗ } else {
406 5 int chunk_size = (N + n_threads - 1) / n_threads;
407 5 int num_chunks = (N + chunk_size - 1) / chunk_size;
408
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
20 detail::cpu::parallel_for(static_cast<std::size_t>(num_chunks), [&](std::size_t c) {
409 15 int r_start = static_cast<int>(c) * chunk_size;
410 15 int r_end = std::min(r_start + chunk_size, N);
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (r_start >= r_end) return;
412 15 const char* W_bytes = static_cast<const char*>(W);
413 15 const void* W_slice = W_bytes + static_cast<size_t>(r_start) * row_bytes;
414 15 float* Y_slice = Y + r_start;
415 15 run_q6_k_rows(k.q4_k_acc_fn, W_slice, X, Y_slice, r_end - r_start, K);
416 15 });
417 }
418 5 } else {
419 ✗ run_q6_k_rows(k.q4_k_acc_fn, W, X, Y, N, K);
420 }
421 5 return;
422 }
423 ✗ ref_gemv_q6k(W, X, Y, N, K);
424 5 }
425
426 } // namespace brotensor::detail::cpu::jit
427
428 #else // !BROTENSOR_HAS_BRASS_JIT
429
430 namespace brotensor::detail::cpu::jit {
431
432 void prewarm_quant_jit_kernels() {}
433 void reset_quant_jit_caches_for_testing() {}
434
435 void gemv_q8_0(const void* W, const float* X, float* Y, int N, int K) {
436 if (N <= 0 || K <= 0) return;
437 ref_gemv_q8_0(W, X, Y, N, K);
438 }
439
440 void gemv_q4_k(const void* W, const float* X, float* Y, int N, int K) {
441 if (N <= 0 || K <= 0) return;
442 ref_gemv_q4k(W, X, Y, N, K);
443 }
444
445 void gemv_q6_k(const void* W, const float* X, float* Y, int N, int K) {
446 if (N <= 0 || K <= 0) return;
447 ref_gemv_q6k(W, X, Y, N, K);
448 }
449
450 } // namespace brotensor::detail::cpu::jit
451
452 #endif // BROTENSOR_HAS_BRASS_JIT
453
454 // ─── OpsVTable Registrations for CPU Quantization Ops ───────────────────────
455
456 namespace brotensor::detail::cpu {
457
458 namespace {
459
460 5 static void validate_w_q8_0(const Tensor& W, const char* op) {
461
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (W.dtype != Dtype::Q8_0) {
462
0/12
✗ 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.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ throw std::runtime_error(std::string("brotensor: ") + op + ": W must be Dtype::Q8_0");
463 }
464
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (W.cols % 32 != 0) {
465
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::runtime_error(std::string("brotensor: ") + op + ": W cols must be a multiple of 32");
466 }
467 5 }
468
469 7 static void validate_w_q4k(const Tensor& W, const char* op) {
470
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (W.dtype != Dtype::Q4_K) {
471
0/12
✗ 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.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ throw std::runtime_error(std::string("brotensor: ") + op + ": W must be Dtype::Q4_K");
472 }
473
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (W.cols % 256 != 0) {
474
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::runtime_error(std::string("brotensor: ") + op + ": W cols must be a multiple of 256");
475 }
476 7 }
477
478 123 static inline void local_dequant_q8_0_block(const void* blk_ptr, float* out32) {
479 uint16_t d_raw;
480 123 std::memcpy(&d_raw, blk_ptr, 2);
481 123 const float d = fp16_bits_to_fp32(d_raw);
482 123 const int8_t* qs = reinterpret_cast<const int8_t*>(static_cast<const char*>(blk_ptr) + 2);
483
2/2
✓ Branch 0 taken 3868 times.
✓ Branch 1 taken 123 times.
3991 for (int i = 0; i < 32; ++i) {
484 3868 out32[i] = static_cast<float>(qs[i]) * d;
485 3868 }
486 123 }
487
488 16 static inline void local_dequant_q4k_block(const void* blk_ptr, float* out256) {
489 16 const uint8_t* p = static_cast<const uint8_t*>(blk_ptr);
490 uint16_t d_raw, dmin_raw;
491 16 std::memcpy(&d_raw, p, 2);
492 16 std::memcpy(&dmin_raw, p + 2, 2);
493 16 const float d = fp16_bits_to_fp32(d_raw);
494 16 const float dmin = fp16_bits_to_fp32(dmin_raw);
495
496 16 const uint8_t* scales = p + 4;
497 16 const uint8_t* qs = p + 16;
498
499 uint8_t sc[8], m[8];
500
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 16 times.
144 for (int j = 0; j < 8; ++j) {
501
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
128 if (j < 4) {
502 64 sc[j] = scales[j] & 0x3F;
503 64 m[j] = scales[j + 4] & 0x3F;
504 64 } else {
505 64 sc[j] = static_cast<uint8_t>((scales[j + 4] & 0x0F) | ((scales[j - 4] >> 6) << 4));
506 64 m[j] = static_cast<uint8_t>((scales[j + 4] >> 4) | ((scales[j - 0] >> 6) << 4));
507 }
508 128 }
509
510
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 for (int p_idx = 0; p_idx < 4; ++p_idx) {
511 64 const int is_lo = 2 * p_idx;
512 64 const int is_hi = 2 * p_idx + 1;
513 64 const float w_lo = static_cast<float>(sc[is_lo]) * d;
514 64 const float w_hi = static_cast<float>(sc[is_hi]) * d;
515 64 const float b_lo = static_cast<float>(m [is_lo]) * dmin;
516 64 const float b_hi = static_cast<float>(m [is_hi]) * dmin;
517
518
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 64 times.
2112 for (int l = 0; l < 32; ++l) {
519 2048 const uint8_t byte = qs[p_idx * 32 + l];
520 2048 const int n_lo = byte & 0x0F;
521 2048 const int n_hi = (byte >> 4) & 0x0F;
522 2048 out256[is_lo * 32 + l] = w_lo * static_cast<float>(n_lo) - b_lo;
523 2048 out256[is_hi * 32 + l] = w_hi * static_cast<float>(n_hi) - b_hi;
524 2048 }
525 64 }
526 16 }
527
528 } // namespace
529
530 1 void dequant_q8_0_to_fp16(const Tensor& W_q8, Tensor& W_fp16) {
531 1 validate_w_q8_0(W_q8, "dequant_q8_0_to_fp16");
532 1 const int out = W_q8.rows;
533 1 const int K = W_q8.cols;
534
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
1 if (W_fp16.rows != out || W_fp16.cols != K || W_fp16.dtype != Dtype::FP16) {
535 ✗ W_fp16.resize(out, K, Dtype::FP16);
536 ✗ }
537
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 if (out == 0 || K == 0) return;
538 1 const int bpr = K / 32;
539 1 const char* src = static_cast<const char*>(W_q8.data);
540 1 uint16_t* dst = static_cast<uint16_t*>(W_fp16.data);
541
542 9 auto dequant_row = [&](int r) {
543 float buf[32];
544 8 const char* rsrc = src + static_cast<size_t>(r) * bpr * 34;
545 8 uint16_t* rdst = dst + static_cast<size_t>(r) * K;
546
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 8 times.
131 for (int b = 0; b < bpr; ++b) {
547 123 local_dequant_q8_0_block(rsrc + b * 34, buf);
548
2/2
✓ Branch 0 taken 1279 times.
✓ Branch 1 taken 123 times.
1402 for (int i = 0; i < 32; ++i) {
549 1279 rdst[b * 32 + i] = fp32_to_fp16_bits(buf[i]);
550 1279 }
551 123 }
552 8 };
553
554
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (out >= 8) {
555
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
9 detail::cpu::parallel_for(static_cast<std::size_t>(out), [&](std::size_t r) {
556 8 dequant_row(static_cast<int>(r));
557 8 });
558 1 } else {
559
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int r = 0; r < out; ++r) dequant_row(r);
560 }
561 1 }
562
563 1 void dequant_q4k_to_fp16(const Tensor& W_q4k, Tensor& W_fp16) {
564 1 validate_w_q4k(W_q4k, "dequant_q4k_to_fp16");
565 1 const int out = W_q4k.rows;
566 1 const int K = W_q4k.cols;
567
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
1 if (W_fp16.rows != out || W_fp16.cols != K || W_fp16.dtype != Dtype::FP16) {
568 ✗ W_fp16.resize(out, K, Dtype::FP16);
569 ✗ }
570
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 if (out == 0 || K == 0) return;
571 1 const int bpr = K / 256;
572 1 const char* src = static_cast<const char*>(W_q4k.data);
573 1 uint16_t* dst = static_cast<uint16_t*>(W_fp16.data);
574
575 9 auto dequant_row = [&](int r) {
576 float buf[256];
577 8 const char* rsrc = src + static_cast<size_t>(r) * bpr * 144;
578 8 uint16_t* rdst = dst + static_cast<size_t>(r) * K;
579
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int b = 0; b < bpr; ++b) {
580 16 local_dequant_q4k_block(rsrc + b * 144, buf);
581
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 16 times.
4112 for (int i = 0; i < 256; ++i) {
582 4096 rdst[b * 256 + i] = fp32_to_fp16_bits(buf[i]);
583 4096 }
584 16 }
585 8 };
586
587
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (out >= 8) {
588
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
9 detail::cpu::parallel_for(static_cast<std::size_t>(out), [&](std::size_t r) {
589 8 dequant_row(static_cast<int>(r));
590 8 });
591 1 } else {
592
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int r = 0; r < out; ++r) dequant_row(r);
593 }
594 1 }
595
596 4 void linear_forward_q8_0_fp16(const Tensor& W_q8, const Tensor* bias,
597 const Tensor& x, Tensor& y) {
598 4 validate_w_q8_0(W_q8, "linear_forward_q8_0_fp16");
599 4 const int out = W_q8.rows;
600 4 const int K = W_q8.cols;
601
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (x.size() != K) {
602
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_q8_0_fp16: x size mismatch");
603 }
604
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 if (bias && bias->size() > 0 && bias->size() != out) {
605
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_q8_0_fp16: bias size mismatch");
606 }
607
3/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
4 if (y.rows != out || y.cols != 1 || (y.dtype != Dtype::FP16 && y.dtype != Dtype::FP32)) {
608 ✗ y.resize(out, 1, x.dtype == Dtype::FP32 ? Dtype::FP32 : Dtype::FP16);
609 ✗ }
610
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (out == 0) return;
611
612 4 std::vector<float> x_f32(K);
613
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (x.dtype == Dtype::FP16) {
614 4 const uint16_t* xp = static_cast<const uint16_t*>(x.data);
615
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16384 times.
✓ Branch 2 taken 16384 times.
✗ Branch 3 not taken.
16388 for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]);
616
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
4 } else if (x.dtype == Dtype::FP32) {
617 ✗ std::memcpy(x_f32.data(), x.data, K * sizeof(float));
618 ✗ } else {
619
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_q8_0_fp16: x must be FP16 or FP32");
620 }
621
622
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 std::vector<float> y_f32(out);
623
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 jit::gemv_q8_0(W_q8.data, x_f32.data(), y_f32.data(), out, K);
624
625
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 if (bias && bias->size() > 0) {
626
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (bias->dtype == Dtype::FP16) {
627 ✗ const uint16_t* bp = static_cast<const uint16_t*>(bias->data);
628
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]);
629
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (bias->dtype == Dtype::FP32) {
630 ✗ const float* bp = static_cast<const float*>(bias->data);
631
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < out; ++i) y_f32[i] += bp[i];
632 ✗ }
633 ✗ }
634
635
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (y.dtype == Dtype::FP16) {
636 4 uint16_t* yp = static_cast<uint16_t*>(y.data);
637
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 169 times.
✓ Branch 2 taken 169 times.
✗ Branch 3 not taken.
173 for (int i = 0; i < out; ++i) yp[i] = fp32_to_fp16_bits(y_f32[i]);
638 4 } else {
639 ✗ std::memcpy(y.data, y_f32.data(), out * sizeof(float));
640 }
641 4 }
642
643 ✗ void linear_forward_batched_q8_0_fp16(const Tensor& W_q8, const Tensor* bias,
644 const Tensor& X_BD, Tensor& Y_BD) {
645 ✗ validate_w_q8_0(W_q8, "linear_forward_batched_q8_0_fp16");
646 ✗ const int B = X_BD.rows;
647 ✗ const int K = X_BD.cols;
648 ✗ const int out = W_q8.rows;
649
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (W_q8.cols != K) {
650
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_batched_q8_0_fp16: shape mismatch");
651 }
652
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (bias && bias->size() > 0 && bias->size() != out) {
653
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_batched_q8_0_fp16: bias size mismatch");
654 }
655
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.
✗ if (Y_BD.rows != B || Y_BD.cols != out || (Y_BD.dtype != Dtype::FP16 && Y_BD.dtype != Dtype::FP32)) {
656 ✗ Y_BD.resize(B, out, X_BD.dtype == Dtype::FP32 ? Dtype::FP32 : Dtype::FP16);
657 ✗ }
658
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (B == 0 || out == 0) return;
659
660
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int b = 0; b < B; ++b) {
661 ✗ std::vector<float> x_f32(K);
662
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (X_BD.dtype == Dtype::FP16) {
663 ✗ const uint16_t* xp = static_cast<const uint16_t*>(X_BD.data) + static_cast<size_t>(b) * K;
664
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]);
665
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (X_BD.dtype == Dtype::FP32) {
666 ✗ const float* xp = static_cast<const float*>(X_BD.data) + static_cast<size_t>(b) * K;
667 ✗ std::memcpy(x_f32.data(), xp, K * sizeof(float));
668 ✗ } else {
669
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_batched_q8_0_fp16: X must be FP16 or FP32");
670 }
671
672
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ std::vector<float> y_f32(out);
673
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ jit::gemv_q8_0(W_q8.data, x_f32.data(), y_f32.data(), out, K);
674
675
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (bias && bias->size() > 0) {
676
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (bias->dtype == Dtype::FP16) {
677 ✗ const uint16_t* bp = static_cast<const uint16_t*>(bias->data);
678
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]);
679
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (bias->dtype == Dtype::FP32) {
680 ✗ const float* bp = static_cast<const float*>(bias->data);
681
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < out; ++i) y_f32[i] += bp[i];
682 ✗ }
683 ✗ }
684
685
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (Y_BD.dtype == Dtype::FP16) {
686 ✗ uint16_t* yp = static_cast<uint16_t*>(Y_BD.data) + static_cast<size_t>(b) * out;
687
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (int i = 0; i < out; ++i) yp[i] = fp32_to_fp16_bits(y_f32[i]);
688 ✗ } else {
689 ✗ float* yp = static_cast<float*>(Y_BD.data) + static_cast<size_t>(b) * out;
690 ✗ std::memcpy(yp, y_f32.data(), out * sizeof(float));
691 }
692 ✗ }
693 ✗ }
694
695 5 void linear_forward_q4k_fp16(const Tensor& W_q4k, const Tensor* bias,
696 const Tensor& x, Tensor& y) {
697 5 validate_w_q4k(W_q4k, "linear_forward_q4k_fp16");
698 5 const int out = W_q4k.rows;
699 5 const int K = W_q4k.cols;
700
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (x.size() != K) {
701
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_q4k_fp16: x size mismatch");
702 }
703
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
5 if (bias && bias->size() > 0 && bias->size() != out) {
704
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_q4k_fp16: bias size mismatch");
705 }
706
4/8
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
5 if (y.rows != out || y.cols != 1 || (y.dtype != Dtype::FP16 && y.dtype != Dtype::FP32)) {
707 1 y.resize(out, 1, x.dtype == Dtype::FP32 ? Dtype::FP32 : Dtype::FP16);
708 1 }
709
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (out == 0) return;
710
711 5 std::vector<float> x_f32(K);
712
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 if (x.dtype == Dtype::FP16) {
713 4 const uint16_t* xp = static_cast<const uint16_t*>(x.data);
714
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16384 times.
✓ Branch 2 taken 16384 times.
✗ Branch 3 not taken.
16388 for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]);
715
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
5 } else if (x.dtype == Dtype::FP32) {
716 1 std::memcpy(x_f32.data(), x.data, K * sizeof(float));
717 1 } else {
718
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_q4k_fp16: x must be FP16 or FP32");
719 }
720
721
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 std::vector<float> y_f32(out);
722
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 jit::gemv_q4_k(W_q4k.data, x_f32.data(), y_f32.data(), out, K);
723
724
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
5 if (bias && bias->size() > 0) {
725
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (bias->dtype == Dtype::FP16) {
726 ✗ const uint16_t* bp = static_cast<const uint16_t*>(bias->data);
727
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]);
728
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (bias->dtype == Dtype::FP32) {
729 ✗ const float* bp = static_cast<const float*>(bias->data);
730
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < out; ++i) y_f32[i] += bp[i];
731 ✗ }
732 ✗ }
733
734
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 if (y.dtype == Dtype::FP16) {
735 4 uint16_t* yp = static_cast<uint16_t*>(y.data);
736
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 169 times.
✓ Branch 2 taken 169 times.
✗ Branch 3 not taken.
173 for (int i = 0; i < out; ++i) yp[i] = fp32_to_fp16_bits(y_f32[i]);
737 4 } else {
738 1 std::memcpy(y.data, y_f32.data(), out * sizeof(float));
739 }
740 5 }
741
742 1 void linear_forward_batched_q4k_fp16(const Tensor& W_q4k, const Tensor* bias,
743 const Tensor& X_BD, Tensor& Y_BD) {
744 1 validate_w_q4k(W_q4k, "linear_forward_batched_q4k_fp16");
745 1 const int B = X_BD.rows;
746 1 const int K = X_BD.cols;
747 1 const int out = W_q4k.rows;
748
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (W_q4k.cols != K) {
749
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_batched_q4k_fp16: shape mismatch");
750 }
751
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 if (bias && bias->size() > 0 && bias->size() != out) {
752
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_batched_q4k_fp16: bias size mismatch");
753 }
754
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1 if (Y_BD.rows != B || Y_BD.cols != out || (Y_BD.dtype != Dtype::FP16 && Y_BD.dtype != Dtype::FP32)) {
755 1 Y_BD.resize(B, out, X_BD.dtype == Dtype::FP32 ? Dtype::FP32 : Dtype::FP16);
756 1 }
757
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 if (B == 0 || out == 0) return;
758
759
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 for (int b = 0; b < B; ++b) {
760 1 std::vector<float> x_f32(K);
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (X_BD.dtype == Dtype::FP16) {
762 ✗ const uint16_t* xp = static_cast<const uint16_t*>(X_BD.data) + static_cast<size_t>(b) * K;
763
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]);
764
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 } else if (X_BD.dtype == Dtype::FP32) {
765 1 const float* xp = static_cast<const float*>(X_BD.data) + static_cast<size_t>(b) * K;
766 1 std::memcpy(x_f32.data(), xp, K * sizeof(float));
767 1 } else {
768
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ throw std::runtime_error("brotensor: linear_forward_batched_q4k_fp16: X must be FP16 or FP32");
769 }
770
771
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<float> y_f32(out);
772
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 jit::gemv_q4_k(W_q4k.data, x_f32.data(), y_f32.data(), out, K);
773
774
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 if (bias && bias->size() > 0) {
775
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (bias->dtype == Dtype::FP16) {
776 ✗ const uint16_t* bp = static_cast<const uint16_t*>(bias->data);
777
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]);
778
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (bias->dtype == Dtype::FP32) {
779 ✗ const float* bp = static_cast<const float*>(bias->data);
780
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < out; ++i) y_f32[i] += bp[i];
781 ✗ }
782 ✗ }
783
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (Y_BD.dtype == Dtype::FP16) {
785 ✗ uint16_t* yp = static_cast<uint16_t*>(Y_BD.data) + static_cast<size_t>(b) * out;
786
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (int i = 0; i < out; ++i) yp[i] = fp32_to_fp16_bits(y_f32[i]);
787 ✗ } else {
788 1 float* yp = static_cast<float*>(Y_BD.data) + static_cast<size_t>(b) * out;
789 1 std::memcpy(yp, y_f32.data(), out * sizeof(float));
790 }
791 1 }
792 1 }
793
794 } // namespace brotensor::detail::cpu
795