src/cpu/cpu_quant_q6k_int8.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // brotensor/src/cpu/cpu_quant_q6k_int8.cpp — CPU Q6_K and W8A16 (int8w_fp16) quantized ops. | ||
| 2 | |||
| 3 | #include "cpu_jit.h" | ||
| 4 | #include <brotensor/detail/cpu/thread_pool.h> | ||
| 5 | #include <brotensor/tensor.h> | ||
| 6 | |||
| 7 | #include <cstddef> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <cstring> | ||
| 10 | #include <stdexcept> | ||
| 11 | #include <string> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace brotensor::detail::cpu { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | 4 | static void validate_w_q6k(const Tensor& W, const char* op) { | |
| 19 | 4 | if (W.dtype != Dtype::Q6_K) { | |
| 20 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": W must be Dtype::Q6_K"); | |
| 21 | } | ||
| 22 | 4 | if (W.cols % 256 != 0) { | |
| 23 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": W cols must be a multiple of 256"); | |
| 24 | } | ||
| 25 | 4 | } | |
| 26 | |||
| 27 | 3 | static void validate_w_int8(const Tensor& W, const Tensor& scales, const char* op) { | |
| 28 | 3 | if (W.dtype != Dtype::INT8) { | |
| 29 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": W must be Dtype::INT8"); | |
| 30 | } | ||
| 31 | 3 | if (scales.dtype != Dtype::FP32) { | |
| 32 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": scales must be Dtype::FP32"); | |
| 33 | } | ||
| 34 | 3 | if (scales.size() != W.rows) { | |
| 35 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": scales size must match W.rows"); | |
| 36 | } | ||
| 37 | 3 | } | |
| 38 | |||
| 39 | 32 | static inline void local_dequant_q6k_block(const void* blk_ptr, float* out256) { | |
| 40 | 32 | const uint8_t* p = static_cast<const uint8_t*>(blk_ptr); | |
| 41 | 32 | const uint8_t* ql = p; | |
| 42 | 32 | const uint8_t* qh = p + 128; | |
| 43 | 32 | const int8_t* scales = reinterpret_cast<const int8_t*>(p + 192); | |
| 44 | uint16_t d_raw; | ||
| 45 | 32 | std::memcpy(&d_raw, p + 208, 2); | |
| 46 | 32 | const float d = fp16_bits_to_fp32(d_raw); | |
| 47 | |||
| 48 | 8224 | for (int e = 0; e < 256; ++e) { | |
| 49 | 8192 | const int group = e >> 7; | |
| 50 | 8192 | const int local = e & 127; | |
| 51 | 8192 | const int quad = local >> 5; | |
| 52 | 8192 | const int l = local & 31; | |
| 53 | 8192 | const int sb = (group << 3) + (quad << 1) + (l >> 4); | |
| 54 | |||
| 55 | 8192 | const uint8_t ql_b = ql[group * 64 + (quad & 1) * 32 + l]; | |
| 56 | 8192 | const uint8_t qh_b = qh[group * 32 + l]; | |
| 57 | |||
| 58 | 8192 | const int raw4 = (quad < 2) ? (ql_b & 0x0F) : (ql_b >> 4); | |
| 59 | 8192 | const int high2 = (qh_b >> (quad * 2)) & 0x03; | |
| 60 | 8192 | const int val6 = (raw4 | (high2 << 4)) - 32; | |
| 61 | |||
| 62 | 8192 | out256[e] = d * static_cast<float>(scales[sb]) * static_cast<float>(val6); | |
| 63 | 8192 | } | |
| 64 | 32 | } | |
| 65 | |||
| 66 | 48 | static inline float dot_int8_fp32(const int8_t* w_row, const float* x, int K) { | |
| 67 | 48 | float acc0 = 0.0f, acc1 = 0.0f, acc2 = 0.0f, acc3 = 0.0f; | |
| 68 | 48 | int k = 0; | |
| 69 | 1584 | for (; k + 3 < K; k += 4) { | |
| 70 | 1536 | acc0 += static_cast<float>(w_row[k + 0]) * x[k + 0]; | |
| 71 | 1536 | acc1 += static_cast<float>(w_row[k + 1]) * x[k + 1]; | |
| 72 | 1536 | acc2 += static_cast<float>(w_row[k + 2]) * x[k + 2]; | |
| 73 | 1536 | acc3 += static_cast<float>(w_row[k + 3]) * x[k + 3]; | |
| 74 | 1536 | } | |
| 75 | 48 | float sum = (acc0 + acc1) + (acc2 + acc3); | |
| 76 | 48 | for (; k < K; ++k) { | |
| 77 | ✗ | sum += static_cast<float>(w_row[k]) * x[k]; | |
| 78 | ✗ | } | |
| 79 | 48 | return sum; | |
| 80 | } | ||
| 81 | |||
| 82 | } // namespace | ||
| 83 | |||
| 84 | 1 | void dequant_q6k_to_fp16(const Tensor& W_q6k, Tensor& W_fp16) { | |
| 85 | 1 | validate_w_q6k(W_q6k, "dequant_q6k_to_fp16"); | |
| 86 | 1 | const int out = W_q6k.rows; | |
| 87 | 1 | const int K = W_q6k.cols; | |
| 88 | 1 | if (W_fp16.rows != out || W_fp16.cols != K || W_fp16.dtype != Dtype::FP16) { | |
| 89 | 1 | W_fp16.resize(out, K, Dtype::FP16); | |
| 90 | 1 | } | |
| 91 | 1 | if (out == 0 || K == 0) return; | |
| 92 | 1 | const int bpr = K / 256; | |
| 93 | 1 | const char* src = static_cast<const char*>(W_q6k.data); | |
| 94 | 1 | uint16_t* dst = static_cast<uint16_t*>(W_fp16.data); | |
| 95 | |||
| 96 | 17 | auto dequant_row = [&](int r) { | |
| 97 | float buf[256]; | ||
| 98 | 16 | const char* rsrc = src + static_cast<size_t>(r) * bpr * 210; | |
| 99 | 16 | uint16_t* rdst = dst + static_cast<size_t>(r) * K; | |
| 100 | 48 | for (int b = 0; b < bpr; ++b) { | |
| 101 | 32 | local_dequant_q6k_block(rsrc + b * 210, buf); | |
| 102 | 8224 | for (int i = 0; i < 256; ++i) { | |
| 103 | 8192 | rdst[b * 256 + i] = fp32_to_fp16_bits(buf[i]); | |
| 104 | 8192 | } | |
| 105 | 32 | } | |
| 106 | 16 | }; | |
| 107 | |||
| 108 | 1 | if (out >= 8) { | |
| 109 | 17 | detail::cpu::parallel_for(static_cast<std::size_t>(out), [&](std::size_t r) { | |
| 110 | 16 | dequant_row(static_cast<int>(r)); | |
| 111 | 16 | }); | |
| 112 | 1 | } else { | |
| 113 | ✗ | for (int r = 0; r < out; ++r) dequant_row(r); | |
| 114 | } | ||
| 115 | 1 | } | |
| 116 | |||
| 117 | 2 | void linear_forward_q6k_fp16(const Tensor& W_q6k, const Tensor* bias, | |
| 118 | const Tensor& x, Tensor& y) { | ||
| 119 | 2 | validate_w_q6k(W_q6k, "linear_forward_q6k_fp16"); | |
| 120 | 2 | const int out = W_q6k.rows; | |
| 121 | 2 | const int K = W_q6k.cols; | |
| 122 | 2 | if (x.size() != K) { | |
| 123 | ✗ | throw std::runtime_error("brotensor: linear_forward_q6k_fp16: x size mismatch"); | |
| 124 | } | ||
| 125 | 2 | if (bias && bias->size() > 0 && bias->size() != out) { | |
| 126 | ✗ | throw std::runtime_error("brotensor: linear_forward_q6k_fp16: bias size mismatch"); | |
| 127 | } | ||
| 128 | 2 | if (y.rows != out || y.cols != 1 || (y.dtype != Dtype::FP16 && y.dtype != Dtype::FP32)) { | |
| 129 | 2 | y.resize(out, 1, x.dtype == Dtype::FP32 ? Dtype::FP32 : Dtype::FP16); | |
| 130 | 2 | } | |
| 131 | 2 | if (out == 0) return; | |
| 132 | |||
| 133 | 2 | std::vector<float> x_f32(K); | |
| 134 | 2 | if (x.dtype == Dtype::FP16) { | |
| 135 | 1 | const uint16_t* xp = static_cast<const uint16_t*>(x.data); | |
| 136 | 513 | for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]); | |
| 137 | 2 | } else if (x.dtype == Dtype::FP32) { | |
| 138 | 1 | std::memcpy(x_f32.data(), x.data, K * sizeof(float)); | |
| 139 | 1 | } else { | |
| 140 | ✗ | throw std::runtime_error("brotensor: linear_forward_q6k_fp16: x must be FP16 or FP32"); | |
| 141 | } | ||
| 142 | |||
| 143 | 2 | std::vector<float> y_f32(out); | |
| 144 | 2 | jit::gemv_q6_k(W_q6k.data, x_f32.data(), y_f32.data(), out, K); | |
| 145 | |||
| 146 | 2 | if (bias && bias->size() > 0) { | |
| 147 | 1 | if (bias->dtype == Dtype::FP16) { | |
| 148 | ✗ | const uint16_t* bp = static_cast<const uint16_t*>(bias->data); | |
| 149 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]); | |
| 150 | 1 | } else if (bias->dtype == Dtype::FP32) { | |
| 151 | 1 | const float* bp = static_cast<const float*>(bias->data); | |
| 152 | 17 | for (int i = 0; i < out; ++i) y_f32[i] += bp[i]; | |
| 153 | 1 | } | |
| 154 | 1 | } | |
| 155 | |||
| 156 | 2 | if (y.dtype == Dtype::FP16) { | |
| 157 | 1 | uint16_t* yp = static_cast<uint16_t*>(y.data); | |
| 158 | 17 | for (int i = 0; i < out; ++i) yp[i] = fp32_to_fp16_bits(y_f32[i]); | |
| 159 | 1 | } else { | |
| 160 | 1 | std::memcpy(y.data, y_f32.data(), out * sizeof(float)); | |
| 161 | } | ||
| 162 | 2 | } | |
| 163 | |||
| 164 | 1 | void linear_forward_batched_q6k_fp16(const Tensor& W_q6k, const Tensor* bias, | |
| 165 | const Tensor& X_BD, Tensor& Y_BD) { | ||
| 166 | 1 | validate_w_q6k(W_q6k, "linear_forward_batched_q6k_fp16"); | |
| 167 | 1 | const int B = X_BD.rows; | |
| 168 | 1 | const int K = X_BD.cols; | |
| 169 | 1 | const int out = W_q6k.rows; | |
| 170 | 1 | if (W_q6k.cols != K) { | |
| 171 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_q6k_fp16: shape mismatch"); | |
| 172 | } | ||
| 173 | 1 | if (bias && bias->size() > 0 && bias->size() != out) { | |
| 174 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_q6k_fp16: bias size mismatch"); | |
| 175 | } | ||
| 176 | 1 | if (Y_BD.rows != B || Y_BD.cols != out || (Y_BD.dtype != Dtype::FP16 && Y_BD.dtype != Dtype::FP32)) { | |
| 177 | 1 | Y_BD.resize(B, out, X_BD.dtype == Dtype::FP32 ? Dtype::FP32 : Dtype::FP16); | |
| 178 | 1 | } | |
| 179 | 1 | if (B == 0 || out == 0) return; | |
| 180 | |||
| 181 | 3 | for (int b = 0; b < B; ++b) { | |
| 182 | 2 | std::vector<float> x_f32(K); | |
| 183 | 2 | if (X_BD.dtype == Dtype::FP16) { | |
| 184 | ✗ | const uint16_t* xp = static_cast<const uint16_t*>(X_BD.data) + static_cast<size_t>(b) * K; | |
| 185 | ✗ | for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]); | |
| 186 | 2 | } else if (X_BD.dtype == Dtype::FP32) { | |
| 187 | 2 | const float* xp = static_cast<const float*>(X_BD.data) + static_cast<size_t>(b) * K; | |
| 188 | 2 | std::memcpy(x_f32.data(), xp, K * sizeof(float)); | |
| 189 | 2 | } else { | |
| 190 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_q6k_fp16: X must be FP16 or FP32"); | |
| 191 | } | ||
| 192 | |||
| 193 | 2 | std::vector<float> y_f32(out); | |
| 194 | 2 | jit::gemv_q6_k(W_q6k.data, x_f32.data(), y_f32.data(), out, K); | |
| 195 | |||
| 196 | 2 | if (bias && bias->size() > 0) { | |
| 197 | 2 | if (bias->dtype == Dtype::FP16) { | |
| 198 | ✗ | const uint16_t* bp = static_cast<const uint16_t*>(bias->data); | |
| 199 | ✗ | for (int i = 0; i < out; ++i) y_f32[i] += fp16_bits_to_fp32(bp[i]); | |
| 200 | 2 | } else if (bias->dtype == Dtype::FP32) { | |
| 201 | 2 | const float* bp = static_cast<const float*>(bias->data); | |
| 202 | 34 | for (int i = 0; i < out; ++i) y_f32[i] += bp[i]; | |
| 203 | 2 | } | |
| 204 | 2 | } | |
| 205 | |||
| 206 | 2 | if (Y_BD.dtype == Dtype::FP16) { | |
| 207 | ✗ | uint16_t* yp = static_cast<uint16_t*>(Y_BD.data) + static_cast<size_t>(b) * out; | |
| 208 | ✗ | for (int i = 0; i < out; ++i) yp[i] = fp32_to_fp16_bits(y_f32[i]); | |
| 209 | ✗ | } else { | |
| 210 | 2 | float* yp = static_cast<float*>(Y_BD.data) + static_cast<size_t>(b) * out; | |
| 211 | 2 | std::memcpy(yp, y_f32.data(), out * sizeof(float)); | |
| 212 | } | ||
| 213 | 2 | } | |
| 214 | 1 | } | |
| 215 | |||
| 216 | 2 | void matmul_int8w_fp16(const Tensor& W_int8, const Tensor& scales, | |
| 217 | const Tensor& X, Tensor& Y) { | ||
| 218 | 2 | validate_w_int8(W_int8, scales, "matmul_int8w_fp16"); | |
| 219 | 2 | const int out = W_int8.rows; | |
| 220 | 2 | const int K = W_int8.cols; | |
| 221 | 2 | if (X.size() != K) { | |
| 222 | ✗ | throw std::runtime_error("brotensor: matmul_int8w_fp16: X size mismatch"); | |
| 223 | } | ||
| 224 | 2 | if (Y.rows != out || Y.cols != 1 || (Y.dtype != Dtype::FP16 && Y.dtype != Dtype::FP32)) { | |
| 225 | 2 | Y.resize(out, 1, X.dtype == Dtype::FP32 ? Dtype::FP32 : Dtype::FP16); | |
| 226 | 2 | } | |
| 227 | 2 | if (out == 0) return; | |
| 228 | |||
| 229 | 2 | std::vector<float> x_f32(K); | |
| 230 | 2 | if (X.dtype == Dtype::FP16) { | |
| 231 | 1 | const uint16_t* xp = static_cast<const uint16_t*>(X.data); | |
| 232 | 129 | for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]); | |
| 233 | 2 | } else if (X.dtype == Dtype::FP32) { | |
| 234 | 1 | std::memcpy(x_f32.data(), X.data, K * sizeof(float)); | |
| 235 | 1 | } else { | |
| 236 | ✗ | throw std::runtime_error("brotensor: matmul_int8w_fp16: X must be FP16 or FP32"); | |
| 237 | } | ||
| 238 | |||
| 239 | 2 | const int8_t* w_ptr = static_cast<const int8_t*>(W_int8.data); | |
| 240 | 2 | const float* s_ptr = static_cast<const float*>(scales.data); | |
| 241 | 2 | std::vector<float> y_f32(out); | |
| 242 | |||
| 243 | 34 | auto compute_row = [&](int r) { | |
| 244 | 32 | const int8_t* w_row = w_ptr + static_cast<size_t>(r) * K; | |
| 245 | 32 | y_f32[r] = s_ptr[r] * dot_int8_fp32(w_row, x_f32.data(), K); | |
| 246 | 32 | }; | |
| 247 | |||
| 248 | 2 | if (out >= 8) { | |
| 249 | 34 | detail::cpu::parallel_for(static_cast<std::size_t>(out), [&](std::size_t r) { | |
| 250 | 32 | compute_row(static_cast<int>(r)); | |
| 251 | 32 | }); | |
| 252 | 2 | } else { | |
| 253 | ✗ | for (int r = 0; r < out; ++r) compute_row(r); | |
| 254 | } | ||
| 255 | |||
| 256 | 2 | if (Y.dtype == Dtype::FP16) { | |
| 257 | 1 | uint16_t* yp = static_cast<uint16_t*>(Y.data); | |
| 258 | 17 | for (int i = 0; i < out; ++i) yp[i] = fp32_to_fp16_bits(y_f32[i]); | |
| 259 | 1 | } else { | |
| 260 | 1 | std::memcpy(Y.data, y_f32.data(), out * sizeof(float)); | |
| 261 | } | ||
| 262 | 2 | } | |
| 263 | |||
| 264 | 1 | void linear_forward_batched_int8w_fp16(const Tensor& W_int8, const Tensor& scales, | |
| 265 | const Tensor* bias, const Tensor& X_BD, | ||
| 266 | Tensor& Y_BD) { | ||
| 267 | 1 | validate_w_int8(W_int8, scales, "linear_forward_batched_int8w_fp16"); | |
| 268 | 1 | const int B = X_BD.rows; | |
| 269 | 1 | const int K = X_BD.cols; | |
| 270 | 1 | const int out = W_int8.rows; | |
| 271 | 1 | if (W_int8.cols != K) { | |
| 272 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_int8w_fp16: shape mismatch"); | |
| 273 | } | ||
| 274 | 1 | if (bias && bias->size() > 0 && bias->size() != out) { | |
| 275 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_int8w_fp16: bias size mismatch"); | |
| 276 | } | ||
| 277 | 1 | if (Y_BD.rows != B || Y_BD.cols != out || (Y_BD.dtype != Dtype::FP16 && Y_BD.dtype != Dtype::FP32)) { | |
| 278 | 1 | Y_BD.resize(B, out, X_BD.dtype == Dtype::FP32 ? Dtype::FP32 : Dtype::FP16); | |
| 279 | 1 | } | |
| 280 | 1 | if (B == 0 || out == 0) return; | |
| 281 | |||
| 282 | 1 | const int8_t* w_ptr = static_cast<const int8_t*>(W_int8.data); | |
| 283 | 1 | const float* s_ptr = static_cast<const float*>(scales.data); | |
| 284 | |||
| 285 | 1 | std::vector<float> bias_f32; | |
| 286 | 1 | if (bias && bias->size() > 0) { | |
| 287 | 1 | bias_f32.resize(out); | |
| 288 | 1 | if (bias->dtype == Dtype::FP16) { | |
| 289 | ✗ | const uint16_t* bp = static_cast<const uint16_t*>(bias->data); | |
| 290 | ✗ | for (int i = 0; i < out; ++i) bias_f32[i] = fp16_bits_to_fp32(bp[i]); | |
| 291 | 1 | } else if (bias->dtype == Dtype::FP32) { | |
| 292 | 1 | std::memcpy(bias_f32.data(), bias->data, out * sizeof(float)); | |
| 293 | 1 | } else { | |
| 294 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_int8w_fp16: bias must be FP16 or FP32"); | |
| 295 | } | ||
| 296 | 1 | } | |
| 297 | |||
| 298 | 2 | for (int b = 0; b < B; ++b) { | |
| 299 | 1 | std::vector<float> x_f32(K); | |
| 300 | 1 | if (X_BD.dtype == Dtype::FP16) { | |
| 301 | ✗ | const uint16_t* xp = static_cast<const uint16_t*>(X_BD.data) + static_cast<size_t>(b) * K; | |
| 302 | ✗ | for (int i = 0; i < K; ++i) x_f32[i] = fp16_bits_to_fp32(xp[i]); | |
| 303 | 1 | } else if (X_BD.dtype == Dtype::FP32) { | |
| 304 | 1 | const float* xp = static_cast<const float*>(X_BD.data) + static_cast<size_t>(b) * K; | |
| 305 | 1 | std::memcpy(x_f32.data(), xp, K * sizeof(float)); | |
| 306 | 1 | } else { | |
| 307 | ✗ | throw std::runtime_error("brotensor: linear_forward_batched_int8w_fp16: X must be FP16 or FP32"); | |
| 308 | } | ||
| 309 | |||
| 310 | 1 | std::vector<float> y_f32(out); | |
| 311 | 17 | auto compute_row = [&](int r) { | |
| 312 | 16 | const int8_t* w_row = w_ptr + static_cast<size_t>(r) * K; | |
| 313 | 16 | float val = s_ptr[r] * dot_int8_fp32(w_row, x_f32.data(), K); | |
| 314 | 16 | if (!bias_f32.empty()) val += bias_f32[r]; | |
| 315 | 16 | y_f32[r] = val; | |
| 316 | 16 | }; | |
| 317 | |||
| 318 | 1 | if (out >= 8) { | |
| 319 | 17 | detail::cpu::parallel_for(static_cast<std::size_t>(out), [&](std::size_t r) { | |
| 320 | 16 | compute_row(static_cast<int>(r)); | |
| 321 | 16 | }); | |
| 322 | 1 | } else { | |
| 323 | ✗ | for (int r = 0; r < out; ++r) compute_row(r); | |
| 324 | } | ||
| 325 | |||
| 326 | 1 | if (Y_BD.dtype == Dtype::FP16) { | |
| 327 | ✗ | uint16_t* yp = static_cast<uint16_t*>(Y_BD.data) + static_cast<size_t>(b) * out; | |
| 328 | ✗ | for (int i = 0; i < out; ++i) yp[i] = fp32_to_fp16_bits(y_f32[i]); | |
| 329 | ✗ | } else { | |
| 330 | 1 | float* yp = static_cast<float*>(Y_BD.data) + static_cast<size_t>(b) * out; | |
| 331 | 1 | std::memcpy(yp, y_f32.data(), out * sizeof(float)); | |
| 332 | } | ||
| 333 | 1 | } | |
| 334 | 1 | } | |
| 335 | |||
| 336 | } // namespace brotensor::detail::cpu | ||
| 337 |