| 707 | // |
| 708 | |
| 709 | 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) { |
| 710 | if (nthread < 2) { |
| 711 | // single-thread |
| 712 | size_t new_size = ggml_quantize_chunk(new_type, f32_data, new_data, 0, nrows, n_per_row, imatrix); |
| 713 | if (!ggml_validate_row_data(new_type, new_data, new_size)) { |
| 714 | throw std::runtime_error("quantized data validation failed"); |
| 715 | } |
| 716 | return new_size; |
| 717 | } |
| 718 | |
| 719 | std::mutex mutex; |
| 720 | int64_t counter = 0; |
| 721 | size_t new_size = 0; |
| 722 | bool valid = true; |
| 723 | auto compute = [&mutex, &counter, &new_size, &valid, new_type, f32_data, new_data, chunk_size, |
| 724 | nrows, n_per_row, imatrix]() { |
| 725 | const int64_t nrows_per_chunk = chunk_size / n_per_row; |
| 726 | size_t local_size = 0; |
| 727 | while (true) { |
| 728 | std::unique_lock<std::mutex> lock(mutex); |
| 729 | int64_t first_row = counter; counter += nrows_per_chunk; |
| 730 | if (first_row >= nrows) { |
| 731 | if (local_size > 0) { |
| 732 | new_size += local_size; |
| 733 | } |
| 734 | break; |
| 735 | } |
| 736 | lock.unlock(); |
| 737 | const int64_t this_nrow = std::min(nrows - first_row, nrows_per_chunk); |
| 738 | size_t this_size = ggml_quantize_chunk(new_type, f32_data, new_data, first_row * n_per_row, this_nrow, n_per_row, imatrix); |
| 739 | local_size += this_size; |
| 740 | |
| 741 | // validate the quantized data |
| 742 | const size_t row_size = ggml_row_size(new_type, n_per_row); |
| 743 | void * this_data = (char *) new_data + first_row * row_size; |
| 744 | if (!ggml_validate_row_data(new_type, this_data, this_size)) { |
| 745 | std::unique_lock<std::mutex> lock(mutex); |
| 746 | valid = false; |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | }; |
| 751 | for (int it = 0; it < nthread - 1; ++it) { |
| 752 | workers.emplace_back(compute); |
| 753 | } |
| 754 | compute(); |
| 755 | for (auto & w : workers) { w.join(); } |
| 756 | workers.clear(); |
| 757 | if (!valid) { |
| 758 | throw std::runtime_error("quantized data validation failed"); |
| 759 | } |
| 760 | return new_size; |
| 761 | } |
| 762 | |
| 763 | // |
| 764 | // imatrix requirement check |
no test coverage detected