MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / compute_cossim

Function compute_cossim

subprojects/llama.cpp/tools/imatrix/imatrix.cpp:194–217  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

192}
193
194static void compute_cossim(std::vector<tensor_statistics> & tstats) {
195 static const std::regex pattern(R"(blk\.(\d+)\.)");
196 for (auto & ts : tstats) {
197 if (std::smatch match; std::regex_search(ts.tensor, match, pattern)) {
198 const int blk = std::stoi(match[1]);
199 std::string tname(ts.tensor);
200 tname.replace(match.position(1), match.length(1), std::to_string(blk-1));
201 auto prev = std::find_if(tstats.begin(), tstats.end(),
202 [tname](const tensor_statistics & t) { return t.tensor == tname; });
203 if (prev != tstats.end()) {
204 const float dp = std::inner_product(ts.stats.values.begin(), ts.stats.values.end(),
205 prev->stats.values.begin(), 0.0f);
206 const float curr_mag = std::sqrt(std::inner_product(ts.stats.values.begin(), ts.stats.values.end(),
207 ts.stats.values.begin(), 0.0f));
208 const float prev_mag = std::sqrt(std::inner_product(prev->stats.values.begin(), prev->stats.values.end(),
209 prev->stats.values.begin(), 0.0f));
210 const float cs = dp / (curr_mag * prev_mag);
211 ts.cossim = cs;
212 }
213 } else {
214 ts.cossim = 0;
215 }
216 }
217}
218
219bool IMatrixCollector::collect_imatrix(struct ggml_tensor * t, bool ask, void * user_data) {
220 GGML_UNUSED(user_data);

Callers 1

show_statisticsFunction · 0.85

Calls 6

to_stringFunction · 0.85
replaceMethod · 0.65
lengthMethod · 0.65
positionMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected