| 417 | } |
| 418 | |
| 419 | static size_t llama_tensor_quantize_impl(enum ggml_type new_type, const float * f32_data, void * new_data, const int64_t chunk_size, int64_t nrows, int64_t n_per_row, const float * imatrix, std::vector<std::thread> & workers, const int nthread) { |
| 420 | if (nthread < 2) { |
| 421 | // single-thread |
| 422 | size_t new_size = ggml_quantize_chunk(new_type, f32_data, new_data, 0, nrows, n_per_row, imatrix); |
| 423 | if (!ggml_validate_row_data(new_type, new_data, new_size)) { |
| 424 | throw std::runtime_error("quantized data validation failed"); |
| 425 | } |
| 426 | return new_size; |
| 427 | } |
| 428 | |
| 429 | std::mutex mutex; |
| 430 | int64_t counter = 0; |
| 431 | size_t new_size = 0; |
| 432 | bool valid = true; |
| 433 | auto compute = [&mutex, &counter, &new_size, &valid, new_type, f32_data, new_data, chunk_size, |
| 434 | nrows, n_per_row, imatrix]() { |
| 435 | const int64_t nrows_per_chunk = chunk_size / n_per_row; |
| 436 | size_t local_size = 0; |
| 437 | while (true) { |
| 438 | std::unique_lock<std::mutex> lock(mutex); |
| 439 | int64_t first_row = counter; counter += nrows_per_chunk; |
| 440 | if (first_row >= nrows) { |
| 441 | if (local_size > 0) { |
| 442 | new_size += local_size; |
| 443 | } |
| 444 | break; |
| 445 | } |
| 446 | lock.unlock(); |
| 447 | const int64_t this_nrow = std::min(nrows - first_row, nrows_per_chunk); |
| 448 | size_t this_size = ggml_quantize_chunk(new_type, f32_data, new_data, first_row * n_per_row, this_nrow, n_per_row, imatrix); |
| 449 | local_size += this_size; |
| 450 | |
| 451 | // validate the quantized data |
| 452 | const size_t row_size = ggml_row_size(new_type, n_per_row); |
| 453 | void * this_data = (char *) new_data + first_row * row_size; |
| 454 | if (!ggml_validate_row_data(new_type, this_data, this_size)) { |
| 455 | std::unique_lock<std::mutex> lock(mutex); |
| 456 | valid = false; |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | }; |
| 461 | for (int it = 0; it < nthread - 1; ++it) { |
| 462 | workers.emplace_back(compute); |
| 463 | } |
| 464 | compute(); |
| 465 | for (auto & w : workers) { w.join(); } |
| 466 | workers.clear(); |
| 467 | if (!valid) { |
| 468 | throw std::runtime_error("quantized data validation failed"); |
| 469 | } |
| 470 | return new_size; |
| 471 | } |
| 472 | |
| 473 | static void llama_model_quantize_impl(const std::string & fname_inp, const std::string & fname_out, const llama_model_quantize_params * params) { |
| 474 | ggml_type default_type; |
no test coverage detected