| 210 | // |
| 211 | |
| 212 | static void llama_tensor_dequantize_impl( |
| 213 | ggml_tensor * tensor, std::vector<no_init<float>> & output, std::vector<std::thread> & workers, |
| 214 | const size_t nelements, const int nthread |
| 215 | ) { |
| 216 | if (output.size() < nelements) { |
| 217 | output.resize(nelements); |
| 218 | } |
| 219 | float * f32_output = (float *) output.data(); |
| 220 | |
| 221 | const ggml_type_traits * qtype = ggml_get_type_traits(tensor->type); |
| 222 | if (ggml_is_quantized(tensor->type)) { |
| 223 | if (qtype->to_float == NULL) { |
| 224 | throw std::runtime_error(format("type %s unsupported for integer quantization: no dequantization available", ggml_type_name(tensor->type))); |
| 225 | } |
| 226 | } else if (tensor->type != GGML_TYPE_F16 && |
| 227 | tensor->type != GGML_TYPE_BF16) { |
| 228 | throw std::runtime_error(format("cannot dequantize/convert tensor type %s", ggml_type_name(tensor->type))); |
| 229 | } |
| 230 | |
| 231 | if (nthread < 2) { |
| 232 | if (tensor->type == GGML_TYPE_F16) { |
| 233 | ggml_fp16_to_fp32_row((ggml_fp16_t *)tensor->data, f32_output, nelements); |
| 234 | } else if (tensor->type == GGML_TYPE_BF16) { |
| 235 | ggml_bf16_to_fp32_row((ggml_bf16_t *)tensor->data, f32_output, nelements); |
| 236 | } else if (ggml_is_quantized(tensor->type)) { |
| 237 | qtype->to_float(tensor->data, f32_output, nelements); |
| 238 | } else { |
| 239 | GGML_ABORT("fatal error"); // unreachable |
| 240 | } |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | size_t block_size; |
| 245 | if (tensor->type == GGML_TYPE_F16 || |
| 246 | tensor->type == GGML_TYPE_BF16) { |
| 247 | block_size = 1; |
| 248 | } else { |
| 249 | block_size = (size_t)ggml_blck_size(tensor->type); |
| 250 | } |
| 251 | |
| 252 | size_t block_size_bytes = ggml_type_size(tensor->type); |
| 253 | |
| 254 | GGML_ASSERT(nelements % block_size == 0); |
| 255 | size_t nblocks = nelements / block_size; |
| 256 | size_t blocks_per_thread = nblocks / nthread; |
| 257 | size_t spare_blocks = nblocks - (blocks_per_thread * nthread); // if blocks aren't divisible by thread count |
| 258 | |
| 259 | size_t in_buff_offs = 0; |
| 260 | size_t out_buff_offs = 0; |
| 261 | |
| 262 | for (int tnum = 0; tnum < nthread; tnum++) { |
| 263 | size_t thr_blocks = blocks_per_thread + (tnum == nthread - 1 ? spare_blocks : 0); // num blocks for this thread |
| 264 | size_t thr_elems = thr_blocks * block_size; // number of elements for this thread |
| 265 | size_t thr_block_bytes = thr_blocks * block_size_bytes; // number of input bytes for this thread |
| 266 | |
| 267 | auto compute = [qtype] (ggml_type typ, uint8_t * inbuf, float * outbuf, int nels) { |
| 268 | if (typ == GGML_TYPE_F16) { |
| 269 | ggml_fp16_to_fp32_row((ggml_fp16_t *)inbuf, outbuf, nels); |
no test coverage detected