From e1d43202d74eb605275f4bf9eb18de63d61a5845 Mon Sep 17 00:00:00 2001 From: Hardy <100662313+wanghailu0717@users.noreply.github.com> Date: Mon, 5 Sep 2022 15:45:52 +0800 Subject: [PATCH] Verify wanghailu 0902 (#22) * commit for verify, add some difference function * add code for verify * add code for verify Co-authored-by: wanghailu --- include/utils/validation.h | 40 +++++++++++++++++++ src/utils/validation.cc | 78 ++++++++++++++++++++++++++++++++++++++ test/core/test_verify.cc | 30 +++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 include/utils/validation.h create mode 100644 src/utils/validation.cc create mode 100644 test/core/test_verify.cc diff --git a/include/utils/validation.h b/include/utils/validation.h new file mode 100644 index 00000000..4826f961 --- /dev/null +++ b/include/utils/validation.h @@ -0,0 +1,40 @@ +#pragma once + +namespace infini { + +/* + * Range: For int datatype tensor, Such as int,int64,int8 and so on. + * Effect: Count the number of different data in the tensor. + * Warning: Please use it only for int data. + */ +template int computeDifference1(T *baseline, T *test, int num); + +/* + * Range: For float datatype tensor, Such as half,float,double. + * Effect: Find the maximum absolute error. + * Warning: Please use it only for float data. + */ +template double computeDifference2(T *baseline, T *test, int num); + +/* + * Range: For float datatype tensor, Such as half,float,double. + * Effect: Find the maximum relative error. + * Warning: Please use it only for float data. + */ +template double computeDifference3(T *baseline, T *test, int num); + +/* + * Range: For float datatype tensor, Such as half,float,double. + * Effect: Compute the relative error for a tensor. + * Warning: Please use it only for float data. + */ +template double computeDifference4(T *baseline, T *test, int num); + +/* + * Range: For float datatype tensor, Such as half,float,double. + * Effect: Check for deviations in data errors. + * Warning: Please use it only for float data. + */ +template double computeDifference5(T *baseline, T *test, int num); + +} // namespace infini diff --git a/src/utils/validation.cc b/src/utils/validation.cc new file mode 100644 index 00000000..dc312663 --- /dev/null +++ b/src/utils/validation.cc @@ -0,0 +1,78 @@ +#include +#include +#include + +namespace infini { + +const double EPSILON = 1e-9; +const double EPSILON_FLOAT = 1e-6; +const double EPSILON_HALF = 1e-3; + +template int computeDifference1(T *baseline, T *test, int num) { + int error = 0; + for (int i = 0; i < num; ++i) { + error += (baseline[i] != test[i] ? 1 : 0); + } + return error; +} + +template double computeDifference2(T *baseline, T *test, int num) { + double max_error = 0; + for (int i = 0; i < num; ++i) { + double temp_error = fabs((double)baseline[i] - (double)test[i]); + max_error = std::max(max_error, temp_error); + } + return max_error; +} + +template double computeDifference3(T *baseline, T *test, int num) { + double max_error = 0; + for (int i = 0; i < num; ++i) { + double temp_error = fabs((double)baseline[i] - (double)test[i]) / + (fabs((double)baseline[i]) + EPSILON); + max_error = std::max(max_error, temp_error); + } + return max_error; +} + +template double computeDifference4(T *baseline, T *test, int num) { + double up_sum = 0.0; + double down_sum = 0.0; + for (int i = 0; i < num; ++i) { + up_sum += fabs((double)baseline[i] - (double)test[i]); + down_sum += fabs((double)baseline[i]); + } + return up_sum / (down_sum + EPSILON); +} + +template double computeDifference5(T *baseline, T *test, int num) { + int small = 0; + int down = 0; + for (int i = 0; i < num; ++i) { + if (baseline[i] != test[i]) { + down += 1; + small += (test[i] < baseline[i] ? 1 : 0); + } + } + return (double)small / (double)down; +} + +template int computeDifference1(int *baseline, int *test, int num); +template double computeDifference2(float *baseline, float *test, + int num); +template double computeDifference2(double *baseline, double *test, + int num); +template double computeDifference3(float *baseline, float *test, + int num); +template double computeDifference3(double *baseline, double *test, + int num); +template double computeDifference4(float *baseline, float *test, + int num); +template double computeDifference4(double *baseline, double *test, + int num); +template double computeDifference5(float *baseline, float *test, + int num); +template double computeDifference5(double *baseline, double *test, + int num); + +} // namespace infini diff --git a/test/core/test_verify.cc b/test/core/test_verify.cc new file mode 100644 index 00000000..7a893536 --- /dev/null +++ b/test/core/test_verify.cc @@ -0,0 +1,30 @@ +#include "test.h" +#include "utils/validation.h" + +namespace infini { + +TEST(Verify, validation) { + int array1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int array2[10] = {0, 1, 2, 3, 4, 5, 7, 7, 8, 11}; + + int res1 = computeDifference1(array1, array2, 10); + + float array3[10] = {0.001, 0.0034, 0.44567, 1.2326, 4.5678, + 7.657, 4.667, 8.233, 9.456, 10.334}; + float array4[10] = {0.001, 0.0033, 0.44568, 1.2324, 4.56789, + 7.657, 4.667, 8.233, 9.456, 10.334}; + + double res2 = computeDifference2(array3, array4, 10); + double res3 = computeDifference3(array3, array4, 10); + double res4 = computeDifference4(array3, array4, 10); + double res5 = computeDifference5(array3, array4, 10); + + EXPECT_LE(res1, 2); + EXPECT_LE(res2, 0.03); + EXPECT_LE(res3, 0.03); + EXPECT_LE(res4, 0.03); + EXPECT_LE(res5, 0.7); + EXPECT_GE(res5, 0.1); +} + +} // namespace infini