MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / load_imatrix

Method load_imatrix

tools/imatrix/imatrix.cpp:726–839  ·  view source on GitHub ↗

Using GGUF as the file format, for greater extensibility

Source from the content-addressed store, hash-verified

724
725// Using GGUF as the file format, for greater extensibility
726bool IMatrixCollector::load_imatrix(const char * file_name) {
727 struct ggml_context * ctx = nullptr;
728 struct gguf_init_params meta_gguf_params = {
729 /* .no_alloc = */ false, // the data is needed
730 /* .ctx = */ &ctx,
731 };
732 struct gguf_context * ctx_gguf = gguf_init_from_file(file_name, meta_gguf_params);
733 if (!ctx_gguf) {
734 return this->load_imatrix_legacy(file_name);
735 }
736 const int32_t n_entries = gguf_get_n_tensors(ctx_gguf);
737 if (n_entries < 1) {
738 LOG_ERR("%s: no data in file %s\n", __func__, file_name);
739 gguf_free(ctx_gguf);
740 ggml_free(ctx);
741 return false;
742 }
743
744 const int64_t datasets_key = gguf_find_key(ctx_gguf, LLM_KV_IMATRIX_DATASETS);
745 if (datasets_key != -1 && gguf_get_arr_type(ctx_gguf, datasets_key) == GGUF_TYPE_STRING) {
746 const int64_t n = gguf_get_arr_n(ctx_gguf, datasets_key);
747 m_datasets.reserve(m_datasets.size() + n);
748 for (int64_t i = 0; i < n; ++i) {
749 m_datasets.push_back(gguf_get_arr_str(ctx_gguf, datasets_key, i));
750 }
751 }
752
753 const std::string in_sum2_suffix{ ".in_sum2" };
754 const std::string counts_suffix{ ".counts" };
755
756 // Could re-use m_stats instead, but this allows
757 // checking for completeness of *each* loaded imatrix file
758 // and also makes it easier to re-use a similar implementation in quantize.cpp
759 // Using an ordered map to get a deterministic iteration order.
760 std::map<std::string, std::pair<struct ggml_tensor *, struct ggml_tensor *>> sums_counts_for;
761
762 for (struct ggml_tensor * cur = ggml_get_first_tensor(ctx); cur; cur = ggml_get_next_tensor(ctx, cur)) {
763 std::string name = cur->name;
764
765 if (name.empty()) { continue; }
766
767 if (string_remove_suffix(name, in_sum2_suffix)) {
768 // in_sum2
769 sums_counts_for[std::move(name)].first = cur;
770 } else if (string_remove_suffix(name, counts_suffix)) {
771 // counts
772 sums_counts_for[std::move(name)].second = cur;
773 } else {
774 // ignore other tensors
775 }
776 }
777
778 for (const auto & sc : sums_counts_for) {
779 const std::string & name = sc.first;
780 const struct ggml_tensor * in_sum2 = sc.second.first;
781 const struct ggml_tensor * counts = sc.second.second;
782
783 if (!in_sum2 || !counts) {

Callers 2

show_statisticsFunction · 0.80
mainFunction · 0.80

Calls 15

load_imatrix_legacyMethod · 0.95
gguf_init_from_fileFunction · 0.85
gguf_get_n_tensorsFunction · 0.85
gguf_freeFunction · 0.85
ggml_freeFunction · 0.85
gguf_find_keyFunction · 0.85
gguf_get_arr_typeFunction · 0.85
gguf_get_arr_nFunction · 0.85
gguf_get_arr_strFunction · 0.85
ggml_get_first_tensorFunction · 0.85
ggml_get_next_tensorFunction · 0.85
string_remove_suffixFunction · 0.85

Tested by

no test coverage detected