Upgrade FDASSERT (#97)

* upgrade FDASSERT

* Optimize error message of FDTensor function

* upgrade FDASSERT

Co-authored-by: Jason <jiangjiajun@baidu.com>
This commit is contained in:
Jack Zhou
2022-08-11 19:42:31 +08:00
committed by GitHub
parent d900e8d35e
commit 5c6b3d6150
5 changed files with 54 additions and 32 deletions

View File

@@ -104,7 +104,9 @@ struct EigenMatrix : public EigenTensor<T, 2, MajorType, IndexType> {
int num_col_dims) {
int rank = tensor.shape.size();
FDASSERT((num_col_dims > 0 && num_col_dims < rank),
"Input dimension number(num_col_dims).");
"Input dimension number(num_col_dims) must be between 0 and %d, "
"but received number is %d.",
rank, num_col_dims);
const int n = SizeToAxis(num_col_dims, tensor.shape);
const int d = SizeFromAxis(num_col_dims, tensor.shape);
return EigenMatrix::From(tensor, {n, d});
@@ -114,7 +116,9 @@ struct EigenMatrix : public EigenTensor<T, 2, MajorType, IndexType> {
int num_col_dims) {
int rank = tensor.shape.size();
FDASSERT((num_col_dims > 0 && num_col_dims < rank),
"Input dimension number(num_col_dims).");
"Input dimension number(num_col_dims) must be between 0 and %d, "
"but received number is %d.",
rank, num_col_dims);
const int n = SizeToAxis(num_col_dims, tensor.shape);
const int d = SizeFromAxis(num_col_dims, tensor.shape);
return EigenMatrix::From(tensor, {n, d});

View File

@@ -114,8 +114,11 @@ void SoftmaxKernel(const FDTensor& x, FDTensor* out, int axis) {
}
void Softmax(const FDTensor& x, FDTensor* out, int axis) {
FDASSERT(std::abs(axis) < x.shape.size(),
"The given axis should be smaller than the input's dimension");
FDASSERT(
std::abs(axis) < x.shape.size(),
"The absolute given axis should be smaller than the input's "
"dimension. Expected absolute axis is smaller than %d, but receive %d.",
x.shape.size(), std::abs(axis));
FD_VISIT_FLOAT_TYPES(x.dtype, "SoftmaxKernel",
([&] { SoftmaxKernel<data_t>(x, out, axis); }));
}

View File

@@ -94,10 +94,14 @@ void Transpose(const FDTensor& x, FDTensor* out,
const std::vector<int64_t>& dims) {
size_t dims_size = dims.size();
FDASSERT(dims_size == x.shape.size(),
"The input tensor's dimension should be equal to the dims's size.");
"The input tensor's dimension should be equal to the dims's size. "
"Expect dims size is %d, but receive %d.",
x.shape.size(), dims_size);
std::vector<int> count(dims_size, 0);
for (size_t i = 0; i < dims_size; i++) {
FDASSERT(dims[i] >= 0, "The dims should be greater than or equal to 0.");
FDASSERT(dims[i] >= 0,
"The dims should be greater than or equal to 0, but receive %d.",
dims[i]);
FDASSERT(dims[i] < static_cast<int>(dims_size) && ++count[dims[i]] == 1,
"Each element of Attribute axis should be a unique value range "
"from 0 to (dims - 1), where the dims is the axis's size, unique "

View File

@@ -15,6 +15,7 @@
#pragma once
#include <stdlib.h>
#include <cstdio>
#include <fstream>
#include <iostream>
@@ -85,9 +86,13 @@ FASTDEPLOY_DECL bool ReadBinaryFromFile(const std::string& file,
FDLogger(true, "[INFO]") << __REL_FILE__ << "(" << __LINE__ \
<< ")::" << __FUNCTION__ << "\t"
#define FDASSERT(condition, message) \
#define FDASSERT(condition, format, ...) \
if (!(condition)) { \
FDERROR << message << std::endl; \
std::string format_string(format); \
int n = std::snprintf(nullptr, 0, format_string.data(), ##__VA_ARGS__); \
std::vector<char> buffer(n + 1); \
std::snprintf(buffer.data(), n + 1, format_string.data(), ##__VA_ARGS__); \
FDERROR << buffer.data() << std::endl; \
std::abort(); \
}
@@ -119,9 +124,11 @@ FASTDEPLOY_DECL bool ReadBinaryFromFile(const std::string& file,
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::FP64, double, \
__VA_ARGS__) \
default: \
FDASSERT(false, \
"Invalid enum data type. Only accept data type BOOL, INT32, " \
"INT64, FP32, FP64.") \
FDASSERT( \
false, \
"Invalid enum data type. Expect to accept data type BOOL, INT32, " \
"INT64, FP32, FP64, but receive type %s.", \
Str(__dtype__)); \
} \
}()
@@ -135,7 +142,9 @@ FASTDEPLOY_DECL bool ReadBinaryFromFile(const std::string& file,
__VA_ARGS__) \
default: \
FDASSERT(false, \
"Invalid enum data type. Only accept data type FP32, FP64.") \
"Invalid enum data type. Expect to accept data type FP32, " \
"FP64, but receive type %s.", \
Str(__dtype__)); \
} \
}()
@@ -148,9 +157,10 @@ FASTDEPLOY_DECL bool ReadBinaryFromFile(const std::string& file,
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::INT64, int64_t, \
__VA_ARGS__) \
default: \
FDASSERT( \
false, \
"Invalid enum data type. Only accept data type INT32, INT64.") \
FDASSERT(false, \
"Invalid enum data type. Expect to accept data type INT32, " \
"INT64, but receive type %s.", \
Str(__dtype__)); \
} \
}()

View File

@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <array>
#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/reduce.h"