| 5216 | bool run_whole_graph() override { return ties; } |
| 5217 | |
| 5218 | double err(const float * a, const float * b, size_t n) override { |
| 5219 | // When there are no ties, we expect the exact same set of indices, |
| 5220 | // but possibly in a different order. When there are ties, the indices |
| 5221 | // can be different but the input values they correspond to should be |
| 5222 | // the same. The logic for ties could work for non-ties, but only for |
| 5223 | // the output tensor, not for the sentinel tensors. |
| 5224 | if (ties) { |
| 5225 | std::vector<float> src(ggml_nelements(input)); |
| 5226 | |
| 5227 | ggml_backend_tensor_get(input, src.data(), 0, ggml_nelements(input) * ggml_type_size(type)); |
| 5228 | |
| 5229 | double diff = 0.0f; |
| 5230 | |
| 5231 | GGML_ASSERT(n == (size_t)(ggml_nrows(input) * k)); |
| 5232 | int64_t cols = input->ne[0]; |
| 5233 | std::vector<int32_t> ia(k); |
| 5234 | std::vector<int32_t> ib(k); |
| 5235 | std::vector<float> asrc(k); |
| 5236 | std::vector<float> bsrc(k); |
| 5237 | for (int64_t r = 0; r < ggml_nrows(input); r++) { |
| 5238 | // Convert indices for the row back to integer |
| 5239 | for (int64_t c = 0; c < k; c++) { |
| 5240 | ia[c] = (int32_t)a[r * k + c]; |
| 5241 | ib[c] = (int32_t)b[r * k + c]; |
| 5242 | } |
| 5243 | // The src values for each row should match. |
| 5244 | for (int64_t c = 0; c < k; c++) { |
| 5245 | asrc[c] = src[r * cols + ia[c]]; |
| 5246 | bsrc[c] = src[r * cols + ib[c]]; |
| 5247 | } |
| 5248 | diff += jdst(asrc.data(), bsrc.data(), k); |
| 5249 | // There should be no duplicate indices |
| 5250 | std::sort(ia.begin(), ia.end()); |
| 5251 | std::sort(ib.begin(), ib.end()); |
| 5252 | if (std::adjacent_find(ia.begin(), ia.end()) != ia.end()) { |
| 5253 | diff += 1; |
| 5254 | } |
| 5255 | if (std::adjacent_find(ib.begin(), ib.end()) != ib.end()) { |
| 5256 | diff += 1; |
| 5257 | } |
| 5258 | } |
| 5259 | return diff; |
| 5260 | } else { |
| 5261 | std::vector<int32_t> ia(n); |
| 5262 | std::vector<int32_t> ib(n); |
| 5263 | |
| 5264 | double diff = 0.0f; |
| 5265 | |
| 5266 | for (size_t i = 0; i < n; i++) { |
| 5267 | ia[i] = (int32_t) a[i]; |
| 5268 | ib[i] = (int32_t) b[i]; |
| 5269 | |
| 5270 | // penalize the result if the data is not integer valued |
| 5271 | diff += std::fabs(a[i] - ia[i]); |
| 5272 | diff += std::fabs(b[i] - ib[i]); |
| 5273 | } |
| 5274 | |
| 5275 | return diff + jdst(ia.data(), ib.data(), n); |
nothing calls this directly
no test coverage detected