mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-18 14:40:44 +08:00
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:
@@ -104,7 +104,9 @@ struct EigenMatrix : public EigenTensor<T, 2, MajorType, IndexType> {
|
|||||||
int num_col_dims) {
|
int num_col_dims) {
|
||||||
int rank = tensor.shape.size();
|
int rank = tensor.shape.size();
|
||||||
FDASSERT((num_col_dims > 0 && num_col_dims < rank),
|
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 n = SizeToAxis(num_col_dims, tensor.shape);
|
||||||
const int d = SizeFromAxis(num_col_dims, tensor.shape);
|
const int d = SizeFromAxis(num_col_dims, tensor.shape);
|
||||||
return EigenMatrix::From(tensor, {n, d});
|
return EigenMatrix::From(tensor, {n, d});
|
||||||
@@ -114,7 +116,9 @@ struct EigenMatrix : public EigenTensor<T, 2, MajorType, IndexType> {
|
|||||||
int num_col_dims) {
|
int num_col_dims) {
|
||||||
int rank = tensor.shape.size();
|
int rank = tensor.shape.size();
|
||||||
FDASSERT((num_col_dims > 0 && num_col_dims < rank),
|
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 n = SizeToAxis(num_col_dims, tensor.shape);
|
||||||
const int d = SizeFromAxis(num_col_dims, tensor.shape);
|
const int d = SizeFromAxis(num_col_dims, tensor.shape);
|
||||||
return EigenMatrix::From(tensor, {n, d});
|
return EigenMatrix::From(tensor, {n, d});
|
||||||
|
@@ -114,8 +114,11 @@ void SoftmaxKernel(const FDTensor& x, FDTensor* out, int axis) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Softmax(const FDTensor& x, FDTensor* out, int axis) {
|
void Softmax(const FDTensor& x, FDTensor* out, int axis) {
|
||||||
FDASSERT(std::abs(axis) < x.shape.size(),
|
FDASSERT(
|
||||||
"The given axis should be smaller than the input's dimension");
|
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",
|
FD_VISIT_FLOAT_TYPES(x.dtype, "SoftmaxKernel",
|
||||||
([&] { SoftmaxKernel<data_t>(x, out, axis); }));
|
([&] { SoftmaxKernel<data_t>(x, out, axis); }));
|
||||||
}
|
}
|
||||||
|
@@ -94,10 +94,14 @@ void Transpose(const FDTensor& x, FDTensor* out,
|
|||||||
const std::vector<int64_t>& dims) {
|
const std::vector<int64_t>& dims) {
|
||||||
size_t dims_size = dims.size();
|
size_t dims_size = dims.size();
|
||||||
FDASSERT(dims_size == x.shape.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);
|
std::vector<int> count(dims_size, 0);
|
||||||
for (size_t i = 0; i < dims_size; i++) {
|
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,
|
FDASSERT(dims[i] < static_cast<int>(dims_size) && ++count[dims[i]] == 1,
|
||||||
"Each element of Attribute axis should be a unique value range "
|
"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 "
|
"from 0 to (dims - 1), where the dims is the axis's size, unique "
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -85,10 +86,14 @@ FASTDEPLOY_DECL bool ReadBinaryFromFile(const std::string& file,
|
|||||||
FDLogger(true, "[INFO]") << __REL_FILE__ << "(" << __LINE__ \
|
FDLogger(true, "[INFO]") << __REL_FILE__ << "(" << __LINE__ \
|
||||||
<< ")::" << __FUNCTION__ << "\t"
|
<< ")::" << __FUNCTION__ << "\t"
|
||||||
|
|
||||||
#define FDASSERT(condition, message) \
|
#define FDASSERT(condition, format, ...) \
|
||||||
if (!(condition)) { \
|
if (!(condition)) { \
|
||||||
FDERROR << message << std::endl; \
|
std::string format_string(format); \
|
||||||
std::abort(); \
|
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(); \
|
||||||
}
|
}
|
||||||
|
|
||||||
///////// Basic Marco ///////////
|
///////// Basic Marco ///////////
|
||||||
@@ -119,41 +124,46 @@ FASTDEPLOY_DECL bool ReadBinaryFromFile(const std::string& file,
|
|||||||
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::FP64, double, \
|
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::FP64, double, \
|
||||||
__VA_ARGS__) \
|
__VA_ARGS__) \
|
||||||
default: \
|
default: \
|
||||||
FDASSERT(false, \
|
FDASSERT( \
|
||||||
"Invalid enum data type. Only accept data type BOOL, INT32, " \
|
false, \
|
||||||
"INT64, FP32, FP64.") \
|
"Invalid enum data type. Expect to accept data type BOOL, INT32, " \
|
||||||
|
"INT64, FP32, FP64, but receive type %s.", \
|
||||||
|
Str(__dtype__)); \
|
||||||
} \
|
} \
|
||||||
}()
|
}()
|
||||||
|
|
||||||
#define FD_VISIT_FLOAT_TYPES(TYPE, NAME, ...) \
|
#define FD_VISIT_FLOAT_TYPES(TYPE, NAME, ...) \
|
||||||
|
[&] { \
|
||||||
|
const auto& __dtype__ = TYPE; \
|
||||||
|
switch (__dtype__) { \
|
||||||
|
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::FP32, float, \
|
||||||
|
__VA_ARGS__) \
|
||||||
|
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::FP64, double, \
|
||||||
|
__VA_ARGS__) \
|
||||||
|
default: \
|
||||||
|
FDASSERT(false, \
|
||||||
|
"Invalid enum data type. Expect to accept data type FP32, " \
|
||||||
|
"FP64, but receive type %s.", \
|
||||||
|
Str(__dtype__)); \
|
||||||
|
} \
|
||||||
|
}()
|
||||||
|
|
||||||
|
#define FD_VISIT_INT_TYPES(TYPE, NAME, ...) \
|
||||||
[&] { \
|
[&] { \
|
||||||
const auto& __dtype__ = TYPE; \
|
const auto& __dtype__ = TYPE; \
|
||||||
switch (__dtype__) { \
|
switch (__dtype__) { \
|
||||||
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::FP32, float, \
|
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::INT32, int32_t, \
|
||||||
__VA_ARGS__) \
|
__VA_ARGS__) \
|
||||||
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::FP64, double, \
|
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::INT64, int64_t, \
|
||||||
__VA_ARGS__) \
|
__VA_ARGS__) \
|
||||||
default: \
|
default: \
|
||||||
FDASSERT(false, \
|
FDASSERT(false, \
|
||||||
"Invalid enum data type. Only accept data type FP32, FP64.") \
|
"Invalid enum data type. Expect to accept data type INT32, " \
|
||||||
|
"INT64, but receive type %s.", \
|
||||||
|
Str(__dtype__)); \
|
||||||
} \
|
} \
|
||||||
}()
|
}()
|
||||||
|
|
||||||
#define FD_VISIT_INT_TYPES(TYPE, NAME, ...) \
|
|
||||||
[&] { \
|
|
||||||
const auto& __dtype__ = TYPE; \
|
|
||||||
switch (__dtype__) { \
|
|
||||||
FD_PRIVATE_CASE_TYPE(NAME, ::fastdeploy::FDDataType::INT32, int32_t, \
|
|
||||||
__VA_ARGS__) \
|
|
||||||
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.") \
|
|
||||||
} \
|
|
||||||
}()
|
|
||||||
|
|
||||||
FASTDEPLOY_DECL std::vector<int64_t> GetStride(
|
FASTDEPLOY_DECL std::vector<int64_t> GetStride(
|
||||||
const std::vector<int64_t>& dims);
|
const std::vector<int64_t>& dims);
|
||||||
|
|
||||||
|
@@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "fastdeploy/core/fd_tensor.h"
|
#include "fastdeploy/core/fd_tensor.h"
|
||||||
#include "fastdeploy/function/reduce.h"
|
#include "fastdeploy/function/reduce.h"
|
||||||
|
Reference in New Issue
Block a user