diff --git a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py index 0bb30cc72d7d7f0a2387385084ec0722dc1cbffe..17243a7415881198c1841afa119cc242292bfdc1 100644 --- a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py +++ b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py @@ -54,20 +54,19 @@ def cosine_sim(cpu_output, npu_output): return get_max_rel_err(n_value, b_value) if n_value.dtype == np.uint8: return compare_uint8_data(n_value, b_value) - n_value = n_value / (np.max(np.abs(n_value)) + np.finfo(n_value.dtype).eps) - b_value = b_value / (np.max(np.abs(b_value)) + np.finfo(b_value.dtype).eps) - num = n_value.dot(b_value) - a_norm = np.linalg.norm(n_value) - b_norm = np.linalg.norm(b_value) - if a_norm <= np.finfo(float).eps and b_norm <= np.finfo(float).eps: + n_max = np.max(np.abs(n_value)) + b_max = np.max(np.abs(b_value)) + if n_max <= np.finfo(float).eps and b_max <= np.finfo(float).eps: return cos, True - elif a_norm <= np.finfo(float).eps: + elif n_max <= np.finfo(float).eps: print_warn_log("All the data is Zero in npu dump data. Compare by relative error.") return get_max_rel_err(n_value, b_value) - elif b_norm <= np.finfo(float).eps: + elif b_max <= np.finfo(float).eps: print_warn_log("All the data is Zero in bench dump data. Compare by relative error.") else: - cos = num / (a_norm * b_norm) + n_value = n_value.astype(float) / n_max + b_value = b_value.astype(float) / b_max + cos = np.dot(n_value, b_value) / (np.linalg.norm(n_value) * np.linalg.norm(b_value)) if np.isnan(cos): print_warn_log("Dump data has NaN when comparing with Cosine Similarity.") return cos, cos > 0.99