2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
|
// TestUtils.h
|
|
|
|
|
// MNN
|
|
|
|
|
//
|
|
|
|
|
// Created by MNN on 2019/01/15.
|
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef TestUtils_h
|
|
|
|
|
#define TestUtils_h
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <string>
|
2019-12-27 22:16:57 +08:00
|
|
|
#include <MNN/MNNForwardType.h>
|
|
|
|
|
#include <MNN/Tensor.hpp>
|
- dynamic computation graph (beta)
- add supports (/express)
- add tests
- add benchmarks with it (/benchmark/exprModels)
- Python
- MNN engine and tools were submitted to pip
- available on Windows/macOS/Linux
- Engine/Converter
- add supports for each op benchmarking
- refactor optimizer by separating steps
- CPU
- add supports for Conv3D, Pool3D, ELU, ReverseSequence
- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
- add half transform in CPU
- add broadcast supports for binary
- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
- add sub, real div supports for binary
- add supports for unary
- optimize Conv2D, Reshape
- Vulkan
- add max supports for eltwise
- Metal
- fix metallib missing problem
- Train/Quantization
- use express to refactor training codes
2019-09-26 21:02:07 +08:00
|
|
|
#include <math.h>
|
2020-03-06 15:31:21 +08:00
|
|
|
#include <iostream>
|
2020-11-05 16:41:56 +08:00
|
|
|
#include "core/Backend.hpp"
|
2021-11-30 10:10:53 +08:00
|
|
|
#include <MNN/expr/Executor.hpp>
|
2020-11-05 16:41:56 +08:00
|
|
|
#include "MNN_generated.h"
|
2019-04-17 10:49:11 +08:00
|
|
|
/**
|
|
|
|
|
* @brief dispatch payload on all available backends
|
|
|
|
|
* @param payload test to perform
|
|
|
|
|
*/
|
|
|
|
|
void dispatch(std::function<void(MNNForwardType)> payload);
|
|
|
|
|
/**
|
|
|
|
|
* @brief dispatch payload on given backend
|
|
|
|
|
* @param payload test to perform
|
|
|
|
|
* @param backend given backend
|
|
|
|
|
*/
|
|
|
|
|
void dispatch(std::function<void(MNNForwardType)> payload, MNNForwardType backend);
|
|
|
|
|
|
- dynamic computation graph (beta)
- add supports (/express)
- add tests
- add benchmarks with it (/benchmark/exprModels)
- Python
- MNN engine and tools were submitted to pip
- available on Windows/macOS/Linux
- Engine/Converter
- add supports for each op benchmarking
- refactor optimizer by separating steps
- CPU
- add supports for Conv3D, Pool3D, ELU, ReverseSequence
- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
- add half transform in CPU
- add broadcast supports for binary
- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
- add sub, real div supports for binary
- add supports for unary
- optimize Conv2D, Reshape
- Vulkan
- add max supports for eltwise
- Metal
- fix metallib missing problem
- Train/Quantization
- use express to refactor training codes
2019-09-26 21:02:07 +08:00
|
|
|
/**
|
|
|
|
|
@brief check the result with the ground truth
|
|
|
|
|
@param result data
|
2020-11-05 16:41:56 +08:00
|
|
|
@param rightData
|
|
|
|
|
@param size
|
- dynamic computation graph (beta)
- add supports (/express)
- add tests
- add benchmarks with it (/benchmark/exprModels)
- Python
- MNN engine and tools were submitted to pip
- available on Windows/macOS/Linux
- Engine/Converter
- add supports for each op benchmarking
- refactor optimizer by separating steps
- CPU
- add supports for Conv3D, Pool3D, ELU, ReverseSequence
- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
- add half transform in CPU
- add broadcast supports for binary
- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
- add sub, real div supports for binary
- add supports for unary
- optimize Conv2D, Reshape
- Vulkan
- add max supports for eltwise
- Metal
- fix metallib missing problem
- Train/Quantization
- use express to refactor training codes
2019-09-26 21:02:07 +08:00
|
|
|
@param threshold
|
|
|
|
|
*/
|
|
|
|
|
template <typename T>
|
|
|
|
|
bool checkVector(const T* result, const T* rightData, int size, T threshold){
|
|
|
|
|
MNN_ASSERT(result != nullptr);
|
|
|
|
|
MNN_ASSERT(rightData != nullptr);
|
|
|
|
|
MNN_ASSERT(size >= 0);
|
|
|
|
|
for(int i = 0; i < size; ++i){
|
|
|
|
|
if(fabs(result[i] - rightData[i]) > threshold){
|
2020-03-09 18:02:01 +08:00
|
|
|
std::cout << "right: " << rightData[i] << ", compute: " << result[i] << std::endl;
|
- dynamic computation graph (beta)
- add supports (/express)
- add tests
- add benchmarks with it (/benchmark/exprModels)
- Python
- MNN engine and tools were submitted to pip
- available on Windows/macOS/Linux
- Engine/Converter
- add supports for each op benchmarking
- refactor optimizer by separating steps
- CPU
- add supports for Conv3D, Pool3D, ELU, ReverseSequence
- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
- add half transform in CPU
- add broadcast supports for binary
- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
- add sub, real div supports for binary
- add supports for unary
- optimize Conv2D, Reshape
- Vulkan
- add max supports for eltwise
- Metal
- fix metallib missing problem
- Train/Quantization
- use express to refactor training codes
2019-09-26 21:02:07 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 22:16:57 +08:00
|
|
|
template <typename T>
|
|
|
|
|
bool checkVectorByRelativeError(const T* result, const T* rightData, int size, float rtol) {
|
|
|
|
|
MNN_ASSERT(result != nullptr);
|
|
|
|
|
MNN_ASSERT(rightData != nullptr);
|
|
|
|
|
MNN_ASSERT(size >= 0);
|
2021-06-11 17:17:13 +08:00
|
|
|
|
2020-11-05 16:41:56 +08:00
|
|
|
float maxValue = 0.0f;
|
2019-12-27 22:16:57 +08:00
|
|
|
for(int i = 0; i < size; ++i){
|
2020-11-05 16:41:56 +08:00
|
|
|
maxValue = fmax(fabs(rightData[i]), maxValue);
|
|
|
|
|
}
|
2021-11-30 10:10:53 +08:00
|
|
|
float reltiveError = maxValue * rtol;
|
2020-11-05 16:41:56 +08:00
|
|
|
for(int i = 0; i < size; ++i){
|
2021-11-30 10:10:53 +08:00
|
|
|
if (fabs(result[i] - rightData[i]) > reltiveError) {
|
2021-06-11 17:17:13 +08:00
|
|
|
std::cout << i << ": right: " << rightData[i] << ", compute: " << result[i] << std::endl;
|
2019-12-27 22:16:57 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-30 10:10:53 +08:00
|
|
|
template <typename T>
|
|
|
|
|
bool checkVectorByRelativeError(const T* result, const T* rightData, const T* alterRightData, int size, float rtol) {
|
|
|
|
|
MNN_ASSERT(result != nullptr);
|
|
|
|
|
MNN_ASSERT(rightData != nullptr);
|
|
|
|
|
MNN_ASSERT(size >= 0);
|
|
|
|
|
|
|
|
|
|
float maxValue = 0.0f;
|
|
|
|
|
for(int i = 0; i < size; ++i) {
|
|
|
|
|
maxValue = fmax(fmax(fabs(rightData[i]), fabs(alterRightData[i])), maxValue);
|
|
|
|
|
}
|
|
|
|
|
float reltiveError = maxValue * rtol;
|
|
|
|
|
for(int i = 0; i < size; ++i) {
|
|
|
|
|
if (fabs(result[i] - rightData[i]) > reltiveError && fabs(result[i] - alterRightData[i]) > reltiveError) {
|
|
|
|
|
std::cout << i << ": right: " << rightData[i] << " or " << alterRightData[i] << ", compute: " << result[i] << std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2021-06-11 17:17:13 +08:00
|
|
|
}
|
|
|
|
|
|
2021-11-30 10:10:53 +08:00
|
|
|
int getTestPrecision(MNNForwardType forwardType, MNN::BackendConfig::PrecisionMode precision, bool isSupportFp16);
|
|
|
|
|
|
|
|
|
|
float convertFP32ToBF16(float fp32Value);
|
|
|
|
|
float convertFP32ToFP16(float fp32Value);
|
2021-06-11 17:17:13 +08:00
|
|
|
|
|
|
|
|
inline float keepFP32Precision(float fp32Value) {
|
|
|
|
|
return fp32Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using ConvertFP32 = float(*)(float fp32Value);
|
2021-11-30 10:10:53 +08:00
|
|
|
|
|
|
|
|
const static ConvertFP32 FP32Converter[MNN::BackendConfig::Precision_Low + 2] = {
|
|
|
|
|
keepFP32Precision,
|
|
|
|
|
keepFP32Precision,
|
|
|
|
|
#ifdef MNN_SUPPORT_BF16
|
|
|
|
|
convertFP32ToBF16,
|
|
|
|
|
#else
|
|
|
|
|
keepFP32Precision,
|
|
|
|
|
#endif
|
|
|
|
|
convertFP32ToFP16
|
|
|
|
|
};
|
2021-06-11 17:17:13 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
#endif /* TestUtils_h */
|