| 75 | }; |
| 76 | |
| 77 | static compare_result compare_tensors(const float * a, const float * b, int n, |
| 78 | float atol = 1e-4f) { |
| 79 | compare_result r; |
| 80 | r.n_elements = n; |
| 81 | double sum_diff = 0.0; |
| 82 | double dot_ab = 0.0, dot_aa = 0.0, dot_bb = 0.0; |
| 83 | for (int i = 0; i < n; ++i) { |
| 84 | float diff = fabsf(a[i] - b[i]); |
| 85 | if (diff > r.max_diff) r.max_diff = diff; |
| 86 | if (diff > atol) r.n_bad++; |
| 87 | sum_diff += diff; |
| 88 | dot_ab += (double)a[i] * b[i]; |
| 89 | dot_aa += (double)a[i] * a[i]; |
| 90 | dot_bb += (double)b[i] * b[i]; |
| 91 | } |
| 92 | r.mean_diff = (float)(sum_diff / n); |
| 93 | if (dot_aa > 0 && dot_bb > 0) { |
| 94 | r.cosine_sim = dot_ab / (sqrt(dot_aa) * sqrt(dot_bb)); |
| 95 | } |
| 96 | return r; |
| 97 | } |
| 98 | |
| 99 | static bool check(const std::string & name, const float * got, const ref_tensor & ref, |
| 100 | float atol, int & n_pass, int & n_fail) { |
no outgoing calls
no test coverage detected