From 8cdef986b44056f1d96b25948e1fba4ea7afcd41 Mon Sep 17 00:00:00 2001 From: sunyiming Date: Mon, 7 Aug 2023 06:41:35 +0000 Subject: [PATCH 1/4] update debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py. Signed-off-by: sunyiming --- .../api_accuracy_checker/compare/algorithm.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py index 0bb30cc72d..855869e495 100644 --- a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py +++ b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py @@ -54,19 +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: + if np.max(np.abs(n_value)) <= np.finfo(float).eps and np.max(np.abs(b_value)) <= np.finfo(float).eps: return cos, True - elif a_norm <= np.finfo(float).eps: + elif np.max(np.abs(n_value)) <= 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 np.max(np.abs(b_value)) <= np.finfo(float).eps: print_warn_log("All the data is Zero in bench dump data. Compare by relative error.") else: + n_value = n_value / np.max(np.abs(n_value)) + b_value = b_value / np.max(np.abs(b_value)) + num = n_value.dot(b_value) + a_norm = np.linalg.norm(n_value) + b_norm = np.linalg.norm(b_value) cos = num / (a_norm * b_norm) if np.isnan(cos): print_warn_log("Dump data has NaN when comparing with Cosine Similarity.") -- Gitee From 3370b2ae7b595169fe91a8733284be94b554806f Mon Sep 17 00:00:00 2001 From: sunyiming Date: Mon, 7 Aug 2023 06:50:32 +0000 Subject: [PATCH 2/4] update debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py. Signed-off-by: sunyiming --- .../api_accuracy_checker/compare/algorithm.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py index 855869e495..320d5f93a5 100644 --- a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py +++ b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py @@ -62,12 +62,9 @@ def cosine_sim(cpu_output, npu_output): elif np.max(np.abs(b_value)) <= np.finfo(float).eps: print_warn_log("All the data is Zero in bench dump data. Compare by relative error.") else: - n_value = n_value / np.max(np.abs(n_value)) - b_value = b_value / np.max(np.abs(b_value)) - num = n_value.dot(b_value) - a_norm = np.linalg.norm(n_value) - b_norm = np.linalg.norm(b_value) - cos = num / (a_norm * b_norm) + n_value /= np.max(np.abs(n_value)) + b_value /= np.max(np.abs(b_value)) + 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 -- Gitee From 8ea31a792e26a1cc27a31fcdb539df97ece95aa0 Mon Sep 17 00:00:00 2001 From: sunyiming Date: Mon, 7 Aug 2023 07:22:56 +0000 Subject: [PATCH 3/4] update debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py. Signed-off-by: sunyiming --- .../api_accuracy_checker/compare/algorithm.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py index 320d5f93a5..ddfcaf8018 100644 --- a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py +++ b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py @@ -54,16 +54,18 @@ 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) - if np.max(np.abs(n_value)) <= np.finfo(float).eps and np.max(np.abs(b_value)) <= 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 np.max(np.abs(n_value)) <= 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 np.max(np.abs(b_value)) <= 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: - n_value /= np.max(np.abs(n_value)) - b_value /= np.max(np.abs(b_value)) + n_value /= n_max + b_value /= 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.") -- Gitee From 6342ef3830fc0f51188dca6290fc4aecd41177a8 Mon Sep 17 00:00:00 2001 From: sunyiming Date: Mon, 7 Aug 2023 08:04:42 +0000 Subject: [PATCH 4/4] update debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py. Signed-off-by: sunyiming --- .../accuracy_tools/api_accuracy_checker/compare/algorithm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py index ddfcaf8018..17243a7415 100644 --- a/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py +++ b/debug/accuracy_tools/api_accuracy_checker/compare/algorithm.py @@ -64,8 +64,8 @@ def cosine_sim(cpu_output, npu_output): elif b_max <= np.finfo(float).eps: print_warn_log("All the data is Zero in bench dump data. Compare by relative error.") else: - n_value /= n_max - b_value /= b_max + 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.") -- Gitee