Verify wanghailu 0902 (#22)

* commit for verify, add some difference function

* add code for verify

* add code for verify

Co-authored-by: wanghailu <wanghailu@qiyuanlab.com>
This commit is contained in:
Hardy 2022-09-05 15:45:52 +08:00 committed by GitHub
parent c3bc278c12
commit e1d43202d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 148 additions and 0 deletions

View File

@ -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 <typename T> 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 <typename T> 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 <typename T> 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 <typename T> 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 <typename T> double computeDifference5(T *baseline, T *test, int num);
} // namespace infini

78
src/utils/validation.cc Normal file
View File

@ -0,0 +1,78 @@
#include <algorithm>
#include <math.h>
#include <utils/validation.h>
namespace infini {
const double EPSILON = 1e-9;
const double EPSILON_FLOAT = 1e-6;
const double EPSILON_HALF = 1e-3;
template <typename T> 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 <typename T> 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 <typename T> 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 <typename T> 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 <typename T> 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>(int *baseline, int *test, int num);
template double computeDifference2<float>(float *baseline, float *test,
int num);
template double computeDifference2<double>(double *baseline, double *test,
int num);
template double computeDifference3<float>(float *baseline, float *test,
int num);
template double computeDifference3<double>(double *baseline, double *test,
int num);
template double computeDifference4<float>(float *baseline, float *test,
int num);
template double computeDifference4<double>(double *baseline, double *test,
int num);
template double computeDifference5<float>(float *baseline, float *test,
int num);
template double computeDifference5<double>(double *baseline, double *test,
int num);
} // namespace infini

30
test/core/test_verify.cc Normal file
View File

@ -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