MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / load_imatrix

Function load_imatrix

smallthinker/tools/quantize/quantize.cpp:143–204  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

141}
142
143static int load_imatrix(const std::string & imatrix_file, std::string & imatrix_dataset, std::unordered_map<std::string, std::vector<float>> & imatrix_data) {
144 std::ifstream in(imatrix_file.c_str(), std::ios::binary);
145 if (!in) {
146 printf("%s: failed to open %s\n",__func__, imatrix_file.c_str());
147 exit(1);
148 }
149 int n_entries;
150 in.read((char *)&n_entries, sizeof(n_entries));
151 if (in.fail() || n_entries < 1) {
152 printf("%s: no data in file %s\n", __func__, imatrix_file.c_str());
153 exit(1);
154 }
155 for (int i = 0; i < n_entries; ++i) {
156 int len; in.read((char *)&len, sizeof(len));
157 std::vector<char> name_as_vec(len+1);
158 in.read((char *)name_as_vec.data(), len);
159 if (in.fail()) {
160 printf("%s: failed reading name for entry %d from %s\n", __func__, i+1, imatrix_file.c_str());
161 exit(1);
162 }
163 name_as_vec[len] = 0;
164 std::string name{name_as_vec.data()};
165 auto & e = imatrix_data[name];
166 int ncall;
167 in.read((char *)&ncall, sizeof(ncall));
168 int nval;
169 in.read((char *)&nval, sizeof(nval));
170 if (in.fail() || nval < 1) {
171 printf("%s: failed reading number of values for entry %d\n", __func__, i);
172 imatrix_data = {};
173 exit(1);
174 }
175 e.resize(nval);
176 in.read((char *)e.data(), nval*sizeof(float));
177 if (in.fail()) {
178 printf("%s: failed reading data for entry %d\n", __func__, i);
179 imatrix_data = {};
180 exit(1);
181 }
182 if (ncall > 0) {
183 for (auto& v : e) v /= ncall;
184 }
185
186 if (getenv("LLAMA_TRACE")) {
187 printf("%s: loaded data (size = %6d, ncall = %6d) for '%s'\n", __func__, int(e.size()), ncall, name.c_str());
188 }
189 }
190
191 // latest imatrix version contains the dataset filename at the end of the file
192 int m_last_call = 0;
193 if (in.peek() != EOF) {
194 in.read((char *)&m_last_call, sizeof(m_last_call));
195 int dataset_len;
196 in.read((char *)&dataset_len, sizeof(dataset_len));
197 std::vector<char> dataset_as_vec(dataset_len);
198 in.read(dataset_as_vec.data(), dataset_len);
199 imatrix_dataset.assign(dataset_as_vec.begin(), dataset_as_vec.end());
200 printf("%s: imatrix dataset='%s'\n", __func__, imatrix_dataset.c_str());

Callers 1

prepare_imatrixFunction · 0.85

Calls 9

printfFunction · 0.85
c_strMethod · 0.45
readMethod · 0.45
dataMethod · 0.45
resizeMethod · 0.45
sizeMethod · 0.45
assignMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected