| 164 | } |
| 165 | |
| 166 | QTensor QTensor::from_codec_tensor(const Tensor& tensor, Quant weight_quant, std::array<int, 4> shape, const int debug_line) { |
| 167 | QTensor qtensor; |
| 168 | CodecDType expected_dtype = quant_to_codec_dtype(weight_quant); |
| 169 | std::array<int, 4> expected_shape = shape; |
| 170 | if (is_k_quant(weight_quant)) { |
| 171 | size_t numel = 1; |
| 172 | for (int i = 0; i < 4; i++) { |
| 173 | if (shape[i] > 0) { |
| 174 | numel *= shape[i]; |
| 175 | } |
| 176 | } |
| 177 | size_t block_size = sizeof(block_q2_K); |
| 178 | switch (weight_quant) { |
| 179 | case Quant::Q2_K: { |
| 180 | block_size = sizeof(block_q2_K); |
| 181 | break; |
| 182 | } |
| 183 | case Quant::Q3_K: { |
| 184 | block_size = sizeof(block_q3_K); |
| 185 | break; |
| 186 | } |
| 187 | default: {} |
| 188 | } |
| 189 | size_t total_blocks = numel / QK_K; |
| 190 | size_t total_bytes = total_blocks * block_size; |
| 191 | if (tensor.dtype != expected_dtype || tensor.size != total_bytes) { |
| 192 | std::cerr << "FATAL: tensor mismatch for " << tensor.name << std::endl; |
| 193 | std::cerr |
| 194 | << fmt::format( |
| 195 | "expected: dtype={}, size={}", |
| 196 | codec_dtype_to_string(expected_dtype), |
| 197 | total_bytes |
| 198 | ) |
| 199 | << std::endl; |
| 200 | std::cerr |
| 201 | << fmt::format( |
| 202 | "got: dtype={}, size={}", |
| 203 | codec_dtype_to_string(tensor.dtype), |
| 204 | tensor.size |
| 205 | ) << std::endl; |
| 206 | assert(false); |
| 207 | } |
| 208 | } else if (tensor.dtype != expected_dtype || tensor.shape != expected_shape) { |
| 209 | std::cerr << "FATAL: tensor mismatch for " << tensor.name << std::endl; |
| 210 | std::cerr |
| 211 | << fmt::format( |
| 212 | "expected: dtype={}, shape=[{},{},{},{}]", |
| 213 | codec_dtype_to_string(expected_dtype), |
| 214 | expected_shape[0], |
| 215 | expected_shape[1], |
| 216 | expected_shape[2], |
| 217 | expected_shape[3] |
| 218 | ) |
| 219 | << std::endl; |
| 220 | std::cerr |
| 221 | << fmt::format( |
| 222 | "got: dtype={}, shape=[{},{},{},{}]", |
| 223 | codec_dtype_to_string(tensor.dtype), |
nothing calls this directly
no test coverage detected