Add softmax function (#93)

* Add softmax function

* Add softmax unittest

* Add Softmax docs

* Add function directory

* Add comment for FD_VISIT_ALL_TYPES macro
This commit is contained in:
Jack Zhou
2022-08-11 12:01:19 +08:00
committed by GitHub
parent 4b67b6e8f9
commit cec0d502e0
10 changed files with 349 additions and 31 deletions

View File

@@ -18,6 +18,7 @@
#include <memory>
#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/utils/axis_utils.h"
#include "unsupported/Eigen/CXX11/Tensor"
namespace fastdeploy {
@@ -96,6 +97,30 @@ struct EigenVector : public EigenTensor<T, 1, MajorType, IndexType> {
}
};
template <typename T, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
struct EigenMatrix : public EigenTensor<T, 2, MajorType, IndexType> {
static typename EigenMatrix::Type Reshape(FDTensor& tensor, // NOLINT
int num_col_dims) {
int rank = tensor.shape.size();
FDASSERT((num_col_dims > 0 && num_col_dims < rank),
"Input dimension number(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});
}
static typename EigenMatrix::ConstType Reshape(const FDTensor& tensor,
int num_col_dims) {
int rank = tensor.shape.size();
FDASSERT((num_col_dims > 0 && num_col_dims < rank),
"Input dimension number(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});
}
};
class EigenDeviceWrapper {
public:
static std::shared_ptr<EigenDeviceWrapper> GetInstance();