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 | 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 | 778127 | for (int j = 0; j < 8; ++j) { | |
| 42 | 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 | 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 | 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 | 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 | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | 3 | BasicBlock* entry = kb.builder().append_block("entry"); | |
| 179 | 3 | kb.position_at_end(entry); | |
| 180 | 3 | Value* w_blk = kb.builder().add_block_param(entry, Type::ptr()); | |
| 181 | 3 | Value* x_blk = kb.builder().add_block_param(entry, Type::ptr()); | |
| 182 | 3 | Value* acc8 = kb.builder().add_block_param(entry, Type::ptr()); | |
| 183 | |||
| 184 | 3 | Value* vsum = kb.vload_f32x8(acc8, 0); | |
| 185 | 15 | for (int c = 0; c < 4; ++c) { | |
| 186 | 12 | Value* vw = kb.vload_f32x8(w_blk, c * 32); | |
| 187 | 12 | Value* vx = kb.vload_f32x8(x_blk, c * 32); | |
| 188 | 12 | vsum = kb.vfma(vw, vx, vsum); | |
| 189 | 12 | } | |
| 190 | 3 | kb.vstore_f32x8(acc8, vsum, 0); | |
| 191 | 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 | 3 | BasicBlock* entry = kb.builder().append_block("entry"); | |
| 201 | 3 | kb.position_at_end(entry); | |
| 202 | 3 | Value* w_blk = kb.builder().add_block_param(entry, Type::ptr()); | |
| 203 | 3 | Value* x_blk = kb.builder().add_block_param(entry, Type::ptr()); | |
| 204 | 3 | Value* acc8 = kb.builder().add_block_param(entry, Type::ptr()); | |
| 205 | |||
| 206 | 3 | Value* vsum = kb.vload_f32x8(acc8, 0); | |
| 207 | 99 | for (int c = 0; c < 32; ++c) { | |
| 208 | 96 | Value* vw = kb.vload_f32x8(w_blk, c * 32); | |
| 209 | 96 | Value* vx = kb.vload_f32x8(x_blk, c * 32); | |
| 210 | 96 | vsum = kb.vfma(vw, vx, vsum); | |
| 211 | 96 | } | |
| 212 | 3 | kb.vstore_f32x8(acc8, vsum, 0); | |
| 213 | 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 | 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 | 5 | if (!s_quant_jit_ready.load(std::memory_order_relaxed)) { | |
| 251 | try { | ||
| 252 | 5 | auto& cache = brotensor::jit::KernelDiskCache::instance(); | |
| 253 | 5 | brotensor::jit::KernelDiskCache::CpuCompileOptions opts; | |
| 254 | |||
| 255 | 10 | s_quant_kernels.q8_0_acc_fn = cache.compile_or_load_cpu_fn<BlockAccFn>( | |
| 256 | 5 | "cpu_quant_gemv_q8_0_v1", "gemv_q8_0_acc_block", | |
| 257 | populate_gemv_q8_0_acc_block, opts); | ||
| 258 | 10 | s_quant_kernels.q4_k_acc_fn = cache.compile_or_load_cpu_fn<BlockAccFn>( | |
| 259 | 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 | 5 | (s_quant_kernels.q8_0_acc_fn != nullptr && s_quant_kernels.q4_k_acc_fn != nullptr); | |
| 264 | 5 | } catch (const std::exception& e) { | |
| 265 | ✗ | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 132 | if (N <= 0 || K <= 0) return; | |
| 338 | 132 | const auto& k = get_quant_kernels(); | |
| 339 | 132 | if (k.available && k.q8_0_acc_fn != nullptr) { | |
| 340 | 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 | 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 | 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 | 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 | 134 | if (N <= 0 || K <= 0) return; | |
| 368 | 134 | const auto& k = get_quant_kernels(); | |
| 369 | 134 | if (k.available && k.q4_k_acc_fn != nullptr) { | |
| 370 | 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 | 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 | 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 | 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 | 5 | if (N <= 0 || K <= 0) return; | |
| 398 | 5 | const auto& k = get_quant_kernels(); | |
| 399 | 5 | if (k.available && k.q4_k_acc_fn != nullptr) { | |
| 400 | 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 | 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 | 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 | 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 | 5 | if (W.dtype != Dtype::Q8_0) { | |
| 462 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": W must be Dtype::Q8_0"); | |
| 463 | } | ||
| 464 | 5 | if (W.cols % 32 != 0) { | |
| 465 | ✗ | 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 | 7 | if (W.dtype != Dtype::Q4_K) { | |
| 471 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": W must be Dtype::Q4_K"); | |
| 472 | } | ||
| 473 | 7 | if (W.cols % 256 != 0) { | |
| 474 | ✗ | 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 | 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 | 144 | for (int j = 0; j < 8; ++j) { | |
| 501 | 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 | 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 | 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 | 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 | 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 | 131 | for (int b = 0; b < bpr; ++b) { | |
| 547 | 123 | local_dequant_q8_0_block(rsrc + b * 34, buf); | |
| 548 | 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 | if (out >= 8) { | |
| 555 | 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 | ✗ | 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 | 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 | 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 | 24 | for (int b = 0; b < bpr; ++b) { | |
| 580 | 16 | local_dequant_q4k_block(rsrc + b * 144, buf); | |
| 581 | 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 | if (out >= 8) { | |
| 588 | 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 | ✗ | 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 | 4 | if (x.size() != K) { | |
| 602 | ✗ | throw std::runtime_error("brotensor: linear_forward_q8_0_fp16: x size mismatch"); | |
| 603 | } | ||
| 604 | 4 | if (bias && bias->size() > 0 && bias->size() != out) { | |
| 605 | ✗ | throw std::runtime_error("brotensor: linear_forward_q8_0_fp16: bias size mismatch"); | |
| 606 | } | ||
| 607 | 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 | 4 | if (out == 0) return; | |
| 611 | |||
| 612 | 4 | std::vector<float> x_f32(K); | |
| 613 | 4 | if (x.dtype == Dtype::FP16) { | |
| 614 | 4 | const uint16_t* xp = static_cast<const uint16_t*>(x.data); | |
| 615 | 16388 | for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]); | |
| 616 | 4 | } else if (x.dtype == Dtype::FP32) { | |
| 617 | ✗ | std::memcpy(x_f32.data(), x.data, K * sizeof(float)); | |
| 618 | ✗ | } else { | |
| 619 | ✗ | throw std::runtime_error("brotensor: linear_forward_q8_0_fp16: x must be FP16 or FP32"); | |
| 620 | } | ||
| 621 | |||
| 622 | 4 | std::vector<float> y_f32(out); | |
| 623 | 4 | jit::gemv_q8_0(W_q8.data, x_f32.data(), y_f32.data(), out, K); | |
| 624 | |||
| 625 | 4 | if (bias && bias->size() > 0) { | |
| 626 | ✗ | if (bias->dtype == Dtype::FP16) { | |
| 627 | ✗ | const uint16_t* bp = static_cast<const uint16_t*>(bias->data); | |
| 628 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]); | |
| 629 | ✗ | } else if (bias->dtype == Dtype::FP32) { | |
| 630 | ✗ | const float* bp = static_cast<const float*>(bias->data); | |
| 631 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += bp[i]; | |
| 632 | ✗ | } | |
| 633 | ✗ | } | |
| 634 | |||
| 635 | 4 | if (y.dtype == Dtype::FP16) { | |
| 636 | 4 | uint16_t* yp = static_cast<uint16_t*>(y.data); | |
| 637 | 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 | ✗ | if (W_q8.cols != K) { | |
| 650 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_q8_0_fp16: shape mismatch"); | |
| 651 | } | ||
| 652 | ✗ | if (bias && bias->size() > 0 && bias->size() != out) { | |
| 653 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_q8_0_fp16: bias size mismatch"); | |
| 654 | } | ||
| 655 | ✗ | 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 | ✗ | if (B == 0 || out == 0) return; | |
| 659 | |||
| 660 | ✗ | for (int b = 0; b < B; ++b) { | |
| 661 | ✗ | std::vector<float> x_f32(K); | |
| 662 | ✗ | 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 | ✗ | for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]); | |
| 665 | ✗ | } 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 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_q8_0_fp16: X must be FP16 or FP32"); | |
| 670 | } | ||
| 671 | |||
| 672 | ✗ | std::vector<float> y_f32(out); | |
| 673 | ✗ | jit::gemv_q8_0(W_q8.data, x_f32.data(), y_f32.data(), out, K); | |
| 674 | |||
| 675 | ✗ | if (bias && bias->size() > 0) { | |
| 676 | ✗ | if (bias->dtype == Dtype::FP16) { | |
| 677 | ✗ | const uint16_t* bp = static_cast<const uint16_t*>(bias->data); | |
| 678 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]); | |
| 679 | ✗ | } else if (bias->dtype == Dtype::FP32) { | |
| 680 | ✗ | const float* bp = static_cast<const float*>(bias->data); | |
| 681 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += bp[i]; | |
| 682 | ✗ | } | |
| 683 | ✗ | } | |
| 684 | |||
| 685 | ✗ | if (Y_BD.dtype == Dtype::FP16) { | |
| 686 | ✗ | uint16_t* yp = static_cast<uint16_t*>(Y_BD.data) + static_cast<size_t>(b) * out; | |
| 687 | ✗ | 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 | 5 | if (x.size() != K) { | |
| 701 | ✗ | throw std::runtime_error("brotensor: linear_forward_q4k_fp16: x size mismatch"); | |
| 702 | } | ||
| 703 | 5 | if (bias && bias->size() > 0 && bias->size() != out) { | |
| 704 | ✗ | throw std::runtime_error("brotensor: linear_forward_q4k_fp16: bias size mismatch"); | |
| 705 | } | ||
| 706 | 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 | 5 | if (out == 0) return; | |
| 710 | |||
| 711 | 5 | std::vector<float> x_f32(K); | |
| 712 | 5 | if (x.dtype == Dtype::FP16) { | |
| 713 | 4 | const uint16_t* xp = static_cast<const uint16_t*>(x.data); | |
| 714 | 16388 | for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]); | |
| 715 | 5 | } else if (x.dtype == Dtype::FP32) { | |
| 716 | 1 | std::memcpy(x_f32.data(), x.data, K * sizeof(float)); | |
| 717 | 1 | } else { | |
| 718 | ✗ | throw std::runtime_error("brotensor: linear_forward_q4k_fp16: x must be FP16 or FP32"); | |
| 719 | } | ||
| 720 | |||
| 721 | 5 | std::vector<float> y_f32(out); | |
| 722 | 5 | jit::gemv_q4_k(W_q4k.data, x_f32.data(), y_f32.data(), out, K); | |
| 723 | |||
| 724 | 5 | if (bias && bias->size() > 0) { | |
| 725 | ✗ | if (bias->dtype == Dtype::FP16) { | |
| 726 | ✗ | const uint16_t* bp = static_cast<const uint16_t*>(bias->data); | |
| 727 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]); | |
| 728 | ✗ | } else if (bias->dtype == Dtype::FP32) { | |
| 729 | ✗ | const float* bp = static_cast<const float*>(bias->data); | |
| 730 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += bp[i]; | |
| 731 | ✗ | } | |
| 732 | ✗ | } | |
| 733 | |||
| 734 | 5 | if (y.dtype == Dtype::FP16) { | |
| 735 | 4 | uint16_t* yp = static_cast<uint16_t*>(y.data); | |
| 736 | 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 | if (W_q4k.cols != K) { | |
| 749 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_q4k_fp16: shape mismatch"); | |
| 750 | } | ||
| 751 | 1 | if (bias && bias->size() > 0 && bias->size() != out) { | |
| 752 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_q4k_fp16: bias size mismatch"); | |
| 753 | } | ||
| 754 | 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 | 1 | if (B == 0 || out == 0) return; | |
| 758 | |||
| 759 | 2 | for (int b = 0; b < B; ++b) { | |
| 760 | 1 | std::vector<float> x_f32(K); | |
| 761 | 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 | ✗ | for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]); | |
| 764 | 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 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_q4k_fp16: X must be FP16 or FP32"); | |
| 769 | } | ||
| 770 | |||
| 771 | 1 | std::vector<float> y_f32(out); | |
| 772 | 1 | jit::gemv_q4_k(W_q4k.data, x_f32.data(), y_f32.data(), out, K); | |
| 773 | |||
| 774 | 1 | if (bias && bias->size() > 0) { | |
| 775 | ✗ | if (bias->dtype == Dtype::FP16) { | |
| 776 | ✗ | const uint16_t* bp = static_cast<const uint16_t*>(bias->data); | |
| 777 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]); | |
| 778 | ✗ | } else if (bias->dtype == Dtype::FP32) { | |
| 779 | ✗ | const float* bp = static_cast<const float*>(bias->data); | |
| 780 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += bp[i]; | |
| 781 | ✗ | } | |
| 782 | ✗ | } | |
| 783 | |||
| 784 | 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 | ✗ | 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 |