| 505 | } |
| 506 | |
| 507 | void IMatrixCollector::save_imatrix(int32_t n_chunk) const { |
| 508 | auto fname = m_params.out_file; |
| 509 | int8_t use_legacy_format = m_params.imat_dat; |
| 510 | |
| 511 | if (use_legacy_format > 0) { |
| 512 | this->save_imatrix_legacy(n_chunk); |
| 513 | return; |
| 514 | } |
| 515 | // only warn when `--output-format gguf` is not specified |
| 516 | if (use_legacy_format == 0 && !string_ends_with(fname, ".gguf")) { |
| 517 | LOG_WRN("\n%s: saving imatrix using GGUF format with a different suffix than .gguf\n", __func__); |
| 518 | LOG_WRN("%s: if you want the previous imatrix format, use --output-format dat\n", __func__); |
| 519 | } |
| 520 | |
| 521 | if (n_chunk > 0) { |
| 522 | fname += ".at_"; |
| 523 | fname += std::to_string(n_chunk); |
| 524 | } |
| 525 | |
| 526 | // write imatrix entries even if they don't have full data. (can be corrected when reading) |
| 527 | // this can happen with MoE models where some of the experts end up not being exercised by the provided training data |
| 528 | |
| 529 | std::vector<std::string> to_store; |
| 530 | size_t data_size = 0; |
| 531 | |
| 532 | bool is_first = true; // for printing |
| 533 | for (const auto & kv : m_stats) { |
| 534 | const int n_all = kv.second.counts.size(); |
| 535 | |
| 536 | int n_zeros = 0; |
| 537 | for (const auto c : kv.second.counts) { |
| 538 | if (c == 0) { |
| 539 | n_zeros++; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (n_zeros != 0 && is_first) { |
| 544 | LOG_INF("\n"); |
| 545 | is_first = false; |
| 546 | } |
| 547 | |
| 548 | if (n_zeros > 0) { |
| 549 | LOG_WRN("%s: entry '%40s' has partial data (%.2f%%)\n", __func__, kv.first.c_str(), 100.0f * (n_all - n_zeros) / n_all); |
| 550 | } |
| 551 | |
| 552 | to_store.push_back(kv.first); |
| 553 | data_size += GGML_PAD(ggml_tensor_overhead() + sizeof(float) * kv.second.values.size(), GGML_MEM_ALIGN); |
| 554 | data_size += GGML_PAD(ggml_tensor_overhead() + sizeof(float) * kv.second.counts.size(), GGML_MEM_ALIGN); |
| 555 | } |
| 556 | |
| 557 | // deterministic tensor name order |
| 558 | std::sort(to_store.begin(), to_store.end()); |
| 559 | |
| 560 | struct ggml_init_params params = { |
| 561 | /* .mem_size = */ data_size, |
| 562 | /* .mem_buffer = */ NULL, |
| 563 | /* .no_alloc = */ false, |
| 564 | }; |
no test coverage detected