chore: 更新fastdeploy

83824f7969
This commit is contained in:
MistEO
2022-12-08 21:00:10 +08:00
parent 41ed6eec9a
commit ed4e6bcf6a
44 changed files with 1469 additions and 99 deletions

View File

@@ -0,0 +1,31 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Cast x to output data type element-wise. Only for float type FDTensor
@param x The input tensor.
@param out The output tensor which stores the result.
@param output_dtype The type of output tensor.
*/
FASTDEPLOY_DECL void Cast(const FDTensor& x, FDTensor* out,
FDDataType output_dtype);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,32 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** This operator clip all elements in input into the range [ min, max ]. Support float32, float64, int32, int64
@param x The input tensor.
@param min The lower bound
@param max The uppper bound
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Clip(const FDTensor& x, double min, double max,
FDTensor* out);
} // namespace function
} // namespace fastdeploy

View File

@@ -22,7 +22,7 @@ namespace function {
/** Excute the concatenate operation for input FDTensor along given axis.
@param x The input tensor.
@param out The output tensor which stores the result.
@param axisi Axis which will be concatenated.
@param axis Axis which will be concatenated.
*/
FASTDEPLOY_DECL void Concat(const std::vector<FDTensor>& x, FDTensor* out,

View File

@@ -0,0 +1,31 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Excute the concatenate operation for input FDTensor along given axis.
@param x The input tensor.
@param out The output tensor which stores the result.
@param axisi Axis which will be concatenated.
*/
FASTDEPLOY_DECL void Cumprod(const FDTensor& x, FDTensor* out, int axis = 0);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,105 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_scalar.h"
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Excute the add operation for input FDTensors. *out = x + y.
@param x The input tensor.
@param y The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Add(const FDTensor& x, const FDTensor& y, FDTensor* out);
/** Excute the subtract operation for input FDTensors. *out = x - y.
@param x The input tensor.
@param y The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Subtract(const FDTensor& x, const FDTensor& y,
FDTensor* out);
/** Excute the multiply operation for input FDTensors. *out = x * y.
@param x The input tensor.
@param y The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Multiply(const FDTensor& x, const FDTensor& y,
FDTensor* out);
/** Excute the divide operation for input FDTensors. *out = x / y.
@param x The input tensor.
@param y The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Divide(const FDTensor& x, const FDTensor& y,
FDTensor* out);
/** Excute the maximum operation for input FDTensors. *out = max(x, y).
@param x The input tensor.
@param y The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Maximum(const FDTensor& x, const FDTensor& y,
FDTensor* out);
} // namespace function
FASTDEPLOY_DECL FDTensor operator+(const FDTensor& x, const FDTensor& y);
template <typename T> FDTensor operator+(const FDTensor& x, T y) {
return x + FDTensor(Scalar(y));
}
template <typename T> FDTensor operator+(T x, const FDTensor& y) {
return FDTensor(Scalar(x)) + y;
}
FASTDEPLOY_DECL FDTensor operator-(const FDTensor& x, const FDTensor& y);
template <typename T> FDTensor operator-(const FDTensor& x, T y) {
return x - FDTensor(Scalar(y));
}
template <typename T> FDTensor operator-(T x, const FDTensor& y) {
return FDTensor(Scalar(x)) - y;
}
FASTDEPLOY_DECL FDTensor operator*(const FDTensor& x, const FDTensor& y);
template <typename T> FDTensor operator*(const FDTensor& x, T y) {
return x * FDTensor(Scalar(y));
}
template <typename T> FDTensor operator*(T x, const FDTensor& y) {
return FDTensor(Scalar(x)) * y;
}
FASTDEPLOY_DECL FDTensor operator/(const FDTensor& x, const FDTensor& y);
template <typename T> FDTensor operator/(const FDTensor& x, T y) {
return x / FDTensor(Scalar(y));
}
template <typename T> FDTensor operator/(T x, const FDTensor& y) {
return FDTensor(Scalar(x)) / y;
}
} // namespace fastdeploy

View File

@@ -0,0 +1,265 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <algorithm>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/eigen.h"
namespace fastdeploy {
namespace function {
#define DEFINE_ELEMENTWISE_OP(name) \
template <typename T> struct name##RawKernel { \
void operator()(const FDTensor& x, const FDTensor& y, int axis, \
FDTensor* out) { \
if (x.Shape() == y.Shape()) { \
SameDimsElementwiseCompute<SameDims##name##Functor<T>>()(x, y, out); \
} else { \
auto x_dims = x.Shape(); \
auto y_dims = y.Shape(); \
if (x_dims.size() >= y_dims.size()) { \
ElementwiseCompute<name##Functor<T>, T>(x, y, axis, \
name##Functor<T>(), out); \
} else { \
ElementwiseCompute<Inverse##name##Functor<T>, T>( \
x, y, axis, Inverse##name##Functor<T>(), out); \
} \
} \
} \
}
inline void GetMidDims(const std::vector<int64_t>& x_dims,
const std::vector<int64_t>& y_dims, const int axis,
int* pre, int* n, int* post,
int* is_run_common_broadcast) {
*pre = 1;
*n = 1;
*post = 1;
*is_run_common_broadcast = 0;
for (int i = 0; i < axis; ++i) {
(*pre) *= x_dims[i];
}
for (int i = 0; i < y_dims.size(); ++i) {
if (x_dims[i + axis] != y_dims[i]) {
FDASSERT(y_dims[i] == 1 || x_dims[i + axis] == 1,
"Broadcast dimension mismatch. Operands "
"could not be broadcast together with the shape of "
"X = [%s] and the shape of Y = [%s]. Received [%d] "
"in X is not equal to [%d] in Y.",
Str(x_dims).c_str(), Str(y_dims).c_str(), x_dims[i + axis],
y_dims[i]);
*is_run_common_broadcast = 1;
return;
}
(*n) *= y_dims[i];
}
for (int i = axis + y_dims.size(); i < x_dims.size(); ++i) {
(*post) *= x_dims[i];
}
}
inline std::vector<int64_t>
TrimTrailingSingularDims(const std::vector<int64_t>& dims) {
// Remove trailing dimensions of size 1 for y
auto actual_dims_size = dims.size();
for (; actual_dims_size != 0; --actual_dims_size) {
if (dims[actual_dims_size - 1] != 1)
break;
}
if (actual_dims_size == dims.size())
return dims;
std::vector<int64_t> trim_dims;
trim_dims.resize(actual_dims_size);
for (int i = 0; i < actual_dims_size; ++i) {
trim_dims[i] = dims[i];
}
return trim_dims;
}
inline int GetElementwiseIndex(const int64_t* x_dims_array, const int max_dim,
const int64_t* index_array) {
int index_ = 0;
for (int i = 0; i < max_dim; i++) {
if (x_dims_array[i] > 1) {
index_ = index_ * x_dims_array[i] + index_array[i];
}
}
return index_;
}
inline void UpdateElementwiseIndexArray(const int64_t* out_dims_array,
const int max_dim,
int64_t* index_array) {
for (int i = max_dim - 1; i >= 0; --i) {
++index_array[i];
if (index_array[i] >= out_dims_array[i]) {
index_array[i] -= out_dims_array[i];
} else {
break;
}
}
}
inline void GetBroadcastDimsArrays(const std::vector<int64_t>& x_dims,
const std::vector<int64_t>& y_dims,
int64_t* x_dims_array, int64_t* y_dims_array,
int64_t* out_dims_array, const int max_dim,
const int axis) {
FDASSERT(axis >= 0,
"Axis should be great than or equal to 0, but received axis is %d.",
axis);
FDASSERT(axis < max_dim,
"Axis should be less than %d, but received axis is %d.", max_dim,
axis);
if (x_dims.size() > y_dims.size()) {
std::fill(y_dims_array, y_dims_array + axis, 1);
if (axis + y_dims.size() < max_dim) {
std::fill(y_dims_array + axis + y_dims.size(), y_dims_array + max_dim, 1);
}
std::copy(x_dims.data(), x_dims.data() + x_dims.size(), x_dims_array);
std::copy(y_dims.data(), y_dims.data() + y_dims.size(),
y_dims_array + axis);
} else {
std::fill(x_dims_array, x_dims_array + axis, 1);
if (axis + x_dims.size() < max_dim) {
std::fill(x_dims_array + axis + x_dims.size(), x_dims_array + max_dim, 1);
}
std::copy(x_dims.data(), x_dims.data() + x_dims.size(),
x_dims_array + axis);
std::copy(y_dims.data(), y_dims.data() + y_dims.size(), y_dims_array);
}
for (int i = 0; i < max_dim; i++) {
FDASSERT(x_dims_array[i] == y_dims_array[i] || x_dims_array[i] <= 1 ||
y_dims_array[i] <= 1,
"Broadcast dimension mismatch. Operands "
"could not be broadcast together with the shape of "
"X = [%s] and the shape of Y = [%s]. Received [%d] "
"in X is not equal to [%d] in Y.",
Str(x_dims).c_str(), Str(y_dims).c_str(), x_dims[i + axis],
y_dims[i]);
if ((x_dims_array[i] > 1 || y_dims_array[i] > 1) ||
(x_dims_array[i] == 1 && y_dims_array[i] == 1)) {
out_dims_array[i] = (std::max)(x_dims_array[i], y_dims_array[i]);
} else {
out_dims_array[i] = -1;
}
}
}
template <typename Functor, typename T, typename OutType = T>
void CommonForwardBroadcastCPU(const FDTensor& x, const FDTensor& y,
FDTensor* z, int64_t* x_dims_array,
int64_t* y_dims_array, int64_t* out_dims_array,
int max_dim, Functor func,
const bool is_xsize_larger = true) {
std::vector<int64_t> index_array(max_dim, 0);
const T* x_data = reinterpret_cast<const T*>(x.Data());
const T* y_data = reinterpret_cast<const T*>(y.Data());
FDASSERT(x_data != nullptr, "The input X should not be empty.");
FDASSERT(y_data != nullptr, "The input X should not be empty.");
OutType* out_data = reinterpret_cast<OutType*>(z->Data());
const int out_size = std::accumulate(out_dims_array, out_dims_array + max_dim,
1, std::multiplies<int64_t>());
int x_index, y_index;
for (int out_index = 0; out_index < out_size; ++out_index) {
x_index = GetElementwiseIndex(x_dims_array, max_dim, index_array.data());
y_index = GetElementwiseIndex(y_dims_array, max_dim, index_array.data());
if (is_xsize_larger) {
out_data[out_index] = func(x_data[x_index], y_data[y_index]);
} else {
out_data[out_index] = func(y_data[y_index], x_data[x_index]);
}
UpdateElementwiseIndexArray(out_dims_array, max_dim, index_array.data());
}
}
template <typename Functor, typename T, typename OutType = T>
void CommonElementwiseBroadcastForward(const FDTensor& x, const FDTensor& y,
FDTensor* z,
const std::vector<int64_t>& x_dims,
const std::vector<int64_t>& y_dims,
Functor func, int axis,
const bool is_xsize_larger = true) {
int x_dims_size = x_dims.size();
int y_dims_size = y_dims.size();
int max_dim = (std::max)(x_dims_size, y_dims_size);
axis = (axis == -1 ? std::abs(x_dims_size - y_dims_size) : axis);
FDASSERT(axis >= 0,
"Axis should be great than or equal to 0, but received axis is %d.",
axis);
FDASSERT(axis < max_dim,
"Axis should be less than %d, but received axis is %d.", max_dim,
axis);
std::vector<int64_t> x_dims_array(max_dim);
std::vector<int64_t> y_dims_array(max_dim);
std::vector<int64_t> out_dims_array(max_dim);
GetBroadcastDimsArrays(x_dims, y_dims, x_dims_array.data(),
y_dims_array.data(), out_dims_array.data(), max_dim,
axis);
FDTensor tmp;
tmp.Allocate(out_dims_array, TypeToDataType<OutType>::dtype);
CommonForwardBroadcastCPU<Functor, T, OutType>(
x, y, &tmp, x_dims_array.data(), y_dims_array.data(),
out_dims_array.data(), max_dim, func, is_xsize_larger);
*z = std::move(tmp);
}
template <typename Functor, typename T, typename OutType = T>
void ElementwiseCompute(const FDTensor& x, const FDTensor& y, int axis,
Functor func, FDTensor* z) {
auto x_dims = x.Shape();
auto y_dims = y.Shape();
bool is_xsize_larger = true;
int max_dim = x_dims.size();
if (x_dims.size() < y_dims.size()) {
is_xsize_larger = false;
max_dim = y_dims.size();
}
int diff_size = x_dims.size() - y_dims.size();
axis = (axis == -1 ? std::abs(diff_size) : axis);
FDASSERT(axis >= 0,
"Axis should be great than or equal to 0, but received axis is %d.",
axis);
FDASSERT(axis < max_dim,
"Axis should be less than %d, but received axis is %d.", max_dim,
axis);
int pre, n, post, is_run_common_broadcast, axis_trim = 0;
if (is_xsize_larger) {
auto y_dims_trimed = TrimTrailingSingularDims(y_dims);
axis_trim = (y_dims_trimed.size() == 0) ? x_dims.size() : axis;
GetMidDims(x_dims, y_dims_trimed, axis_trim, &pre, &n, &post,
&is_run_common_broadcast);
} else {
auto x_dims_trimed = TrimTrailingSingularDims(x_dims);
axis_trim = (x_dims_trimed.size() == 0) ? y_dims.size() : axis;
GetMidDims(y_dims, x_dims_trimed, axis_trim, &pre, &n, &post,
&is_run_common_broadcast);
}
// special case for common implementation.
// case 1: x=[2,3,1,5], y=[2,1,4,1]
// case 2: x=[2,3,4], y=[1,1,4]
CommonElementwiseBroadcastForward<Functor, T, OutType>(
x, y, z, x_dims, y_dims, func, axis, is_xsize_larger);
}
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,131 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/function/eigen.h"
#include "fastdeploy/function/elementwise.h"
#include "fastdeploy/function/elementwise_base.h"
#include <algorithm>
namespace fastdeploy {
namespace function {
template <typename Functor> struct SameDimsElementwiseCompute {
void operator()(const FDTensor& x, const FDTensor& y, FDTensor* z) {
z->Allocate(x.Shape(), x.Dtype());
Functor()(x, y, z);
}
};
template <typename T> struct SameDimsAddFunctor {
void operator()(const FDTensor& x, const FDTensor& y, FDTensor* z) {
const auto& dev = *EigenDeviceWrapper::GetInstance()->GetDevice();
auto eigen_x = EigenVector<T>::Flatten(x);
auto eigen_y = EigenVector<T>::Flatten(y);
auto eigen_z = EigenVector<T>::Flatten(*z);
eigen_z.device(dev) = eigen_x + eigen_y;
}
};
template <typename T> struct SameDimsSubtractFunctor {
void operator()(const FDTensor& x, const FDTensor& y, FDTensor* z) {
const auto& dev = *EigenDeviceWrapper::GetInstance()->GetDevice();
auto eigen_x = EigenVector<T>::Flatten(x);
auto eigen_y = EigenVector<T>::Flatten(y);
auto eigen_z = EigenVector<T>::Flatten(*z);
eigen_z.device(dev) = eigen_x - eigen_y;
}
};
template <typename T> struct SameDimsMultiplyFunctor {
void operator()(const FDTensor& x, const FDTensor& y, FDTensor* z) {
const auto& dev = *EigenDeviceWrapper::GetInstance()->GetDevice();
auto eigen_x = EigenVector<T>::Flatten(x);
auto eigen_y = EigenVector<T>::Flatten(y);
auto eigen_z = EigenVector<T>::Flatten(*z);
eigen_z.device(dev) = eigen_x * eigen_y;
}
};
template <typename T> struct SameDimsDivideFunctor {
void operator()(const FDTensor& x, const FDTensor& y, FDTensor* z) {
const auto& dev = *EigenDeviceWrapper::GetInstance()->GetDevice();
auto eigen_x = EigenVector<T>::Flatten(x);
auto eigen_y = EigenVector<T>::Flatten(y);
auto eigen_z = EigenVector<T>::Flatten(*z);
eigen_z.device(dev) = eigen_x / eigen_y;
}
};
// Add
template <typename T> struct AddFunctor {
inline T operator()(const T a, const T b) const { return a + b; }
};
template <typename T> struct InverseAddFunctor {
inline T operator()(const T a, const T b) const { return b + a; }
};
// Subtract
template <typename T> struct SubtractFunctor {
inline T operator()(const T a, const T b) const { return a - b; }
};
template <typename T> struct InverseSubtractFunctor {
inline T operator()(const T a, const T b) const { return b - a; }
};
// Multiply
template <typename T> struct MultiplyFunctor {
inline T operator()(const T a, const T b) const { return a * b; }
};
template <> struct MultiplyFunctor<bool> {
inline bool operator()(const bool a, const bool b) const { return a && b; }
};
template <typename T> struct InverseMultiplyFunctor {
inline T operator()(const T a, const T b) const { return b * a; }
};
template <> struct InverseMultiplyFunctor<bool> {
inline bool operator()(const bool a, const bool b) const { return b && a; }
};
// Divide
#define DIV_ERROR_INFO \
"InvalidArgumentError: Integer division by zero encountered in " \
"(floor) divide. Please check the input value."
template <typename T, typename Enable = void> struct DivideFunctor {
inline T operator()(const T a, const T b) const { return a / b; }
};
template <typename T>
struct DivideFunctor<
T, typename std::enable_if<std::is_integral<T>::value>::type> {
inline T operator()(const T a, const T b) const {
// For int32/int64, need to check whether the divison is zero.
FDASSERT(b != 0, DIV_ERROR_INFO);
return a / b;
}
};
template <typename T, typename Enable = void> struct InverseDivideFunctor {
inline T operator()(const T a, const T b) const { return b / a; }
};
// Maximum
template <typename T> struct MaximumFunctor {
inline T operator()(const T a, const T b) const { return a > b ? a : b; }
};
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,44 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_scalar.h"
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Fill the value to tensor
@param value The value to be filled in tensor
@param shape The shape of output tensor.
@param out The output tensor which stores the result.
@param dtype The data type of output tensor. Default to float32
*/
FASTDEPLOY_DECL void Full(const Scalar& value,
const std::vector<int64_t>& shape, FDTensor* out,
FDDataType dtype = FDDataType::FP32);
/** Fill the value to tensor
@param x The input tensor.
@param value The value to be filled in tensor
@param out The output tensor which stores the result.
@param dtype The data type of output tensor. Default to float32
*/
FASTDEPLOY_DECL void FullLike(const FDTensor& x, const Scalar& value,
FDTensor* out,
FDDataType dtype = FDDataType::FP32);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,35 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/function/cast.h"
#include "fastdeploy/function/clip.h"
#include "fastdeploy/function/concat.h"
#include "fastdeploy/function/cumprod.h"
#include "fastdeploy/function/elementwise.h"
#include "fastdeploy/function/full.h"
#include "fastdeploy/function/gather_scatter_along_axis.h"
#include "fastdeploy/function/isfinite.h"
#include "fastdeploy/function/linspace.h"
#include "fastdeploy/function/math.h"
#include "fastdeploy/function/pad.h"
#include "fastdeploy/function/quantile.h"
#include "fastdeploy/function/reduce.h"
#include "fastdeploy/function/slice.h"
#include "fastdeploy/function/softmax.h"
#include "fastdeploy/function/sort.h"
#include "fastdeploy/function/split.h"
#include "fastdeploy/function/tile.h"
#include "fastdeploy/function/transpose.h"

View File

@@ -0,0 +1,33 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Output is obtained by gathering entries of axis of x indexed by index and
* concatenate them together.
@param x The input tensor.
@param index The index of a tensor to gather.
@param out The output tensor which stores the result.
@param axis Axis which will be gathered.
*/
void GatherAlongAxis(const FDTensor& x, const FDTensor& index, FDTensor* result,
int axis);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,47 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Return whether every element of input tensor is NaN or not.
@param x The input tensor.
@param out The output tensor which stores the result.
@param dtype The output data type
*/
FASTDEPLOY_DECL void IsNan(const FDTensor& x, FDTensor* out,
FDDataType dtype = FDDataType::BOOL);
/** Return whether every element of input tensor is Inf or not.
@param x The input tensor.
@param out The output tensor which stores the result.
@param dtype The output data type
*/
FASTDEPLOY_DECL void IsInf(const FDTensor& x, FDTensor* out,
FDDataType dtype = FDDataType::BOOL);
/** Return whether every element of input tensor is finite or not.
@param x The input tensor.
@param out The output tensor which stores the result.
@param dtype The output data type
*/
FASTDEPLOY_DECL void IsFinite(const FDTensor& x, FDTensor* out,
FDDataType dtype = FDDataType::BOOL);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,33 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Return fixed number of evenly spaced values within a given interval.
@param start The input start is start variable of range.
@param end The input stop is start variable of range.
@param num The input num is given num of the sequence.
@param out The output tensor which stores the result.
@param dtype The data type of output tensor, default to float32.
*/
FASTDEPLOY_DECL void Linspace(double start, double end, int num, FDTensor* out,
FDDataType dtype = FDDataType::FP32);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,65 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Calculates the sqrt of the given input Tensor, element-wise. Only for float type FDTensor
@param x The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Sqrt(const FDTensor& x, FDTensor* out);
/** Calculates the natural log of the given input Tensor, element-wise. Only for float type FDTensor
@param x The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Log(const FDTensor& x, FDTensor* out);
/** Rounds the values in the input to the nearest integer value, element-wise. Only for float type FDTensor
@param x The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Round(const FDTensor& x, FDTensor* out);
/** Computes exp of x element-wise with a natural number e as the base, element-wise. Only for float type FDTensor
@param x The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Exp(const FDTensor& x, FDTensor* out);
/** This operator is used to perform elementwise abs for input X. Only for float type FDTensor
@param x The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Abs(const FDTensor& x, FDTensor* out);
/** Computes ceil of x element-wise. Only for float type FDTensor
@param x The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Ceil(const FDTensor& x, FDTensor* out);
/** Computes floor of x element-wise. Only for float type FDTensor
@param x The input tensor.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Floor(const FDTensor& x, FDTensor* out);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,81 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/function/eigen.h"
namespace fastdeploy {
namespace function {
// log(x) = natural logarithm of x
template <typename T> struct LogFunctor {
template <typename Device, typename X, typename Out>
void operator()(Device d, X x, Out out) const {
out.device(d) = x.log();
}
};
// exp functor
// exp(x) = e^x
template <typename T> struct ExpFunctor {
template <typename Device, typename X, typename Out>
void operator()(Device d, X x, Out out) const {
out.device(d) = x.exp();
}
};
// round(x) = [x]
template <typename T> struct RoundFunctor {
template <typename Device, typename X, typename Out>
void operator()(Device d, X x, Out out) const {
out.device(d) = x.round();
}
};
// sqrt(x) = x^(1/2)
template <typename T> struct SqrtFunctor {
template <typename Device, typename X, typename Out>
void operator()(Device d, X x, Out out) const {
out.device(d) = x.sqrt();
}
};
// abs(x) = x if x > 0 else -x
template <typename T> struct AbsFunctor {
template <typename Device, typename X, typename Out>
void operator()(Device d, X x, Out out) const {
out.device(d) =
x.unaryExpr([](T v) { return v > static_cast<T>(0) ? v : -v; });
}
};
// ceil(x) = ceiling(x)
template <typename T> struct CeilFunctor {
template <typename Device, typename X, typename Out>
void operator()(Device d, X x, Out out) const {
out.device(d) = x.ceil();
}
};
// floor(x) = flooring(x)
template <typename T> struct FloorFunctor {
template <typename Device, typename X, typename Out>
void operator()(Device d, X x, Out out) const {
out.device(d) = x.floor();
}
};
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,34 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Compute the quantile of the input along the specified axis. If any values
** in a reduced row are NaN, then the quantiles for that reduction will be NaN.
@param x The input tensor.
@param q The q for calculate quantile, which should be in range [0, 1].
@param axis The axis along which to calculate quantile. axis should be int
or list of int.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Quantile(const FDTensor& x, const std::vector<double>& q,
const std::vector<int>& axis, FDTensor* out);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,44 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** This operator produces a slice of input along multiple axes.
@param x The input tensor.
@param axes Axes that starts and ends apply to.
@param starts If starts is a list or tuple, the elements of it should be
integers or Tensors with shape [1]. If starts is an Tensor, it should
be an 1-D Tensor. It represents starting indices of corresponding axis
in axes
@param ends If ends is a list or tuple, the elements of it should be
integers or Tensors with shape [1]. If ends is an Tensor, it should
be an 1-D Tensor . It represents ending indices of corresponding axis
in axes.
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Slice(const FDTensor& x, const std::vector<int64_t>& axes,
const std::vector<int64_t>& starts,
const std::vector<int64_t>& ends, FDTensor* out);
FASTDEPLOY_DECL void Slice(const FDTensor& x, const std::vector<int64_t>& axes,
const std::vector<int64_t>& index, FDTensor* out);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,47 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/**
* @brief Performs sorting on the input tensor along the given axis and outputs
* two tensors, Output(Out) and Output(Indices). They reserve the same
* shape with Input(X), and Output(Out) represents the sorted tensor
* while Output(Indices) gives the sorted order along the given axis
* Attr(axis).
* @param x The input of sort
* @param out The sorted tensor of sort op, with the same shape as
* x
* @param indices The indices of a tensor giving the sorted order, with
* the same shape as x
* @param axis The axis along which to sort the tensor.
* When axis < 0, the actual axis will be the |axis|'th
* counting backwards
* @param descending The descending attribute is a flag to tell
* algorithm how to sort the input data.
* If descending is true, will sort by descending order,
* else if false, sort by ascending order
* @param indices_type The data type of indices, default to int64
*/
FASTDEPLOY_DECL void Sort(const FDTensor& x, FDTensor* out, FDTensor* indices,
int axis = 0, bool descending = false,
FDDataType indices_type = FDDataType::INT64);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,36 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Split the input tensor into multiple sub-Tensors.
@param x The input tensor.
@param num_or_sections f num_or_sections is an int, then num_or_sections
indicates the number of equal sized sub-Tensors that the x will
be divided into.
@param out The output vector tensor which stores the result.
@param axis Axis which will be splitted.
*/
FASTDEPLOY_DECL void Split(const FDTensor& x,
const std::vector<int>& num_or_sections,
std::vector<FDTensor>* out, int axis = 0);
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,36 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/core/fd_tensor.h"
namespace fastdeploy {
namespace function {
/** Construct a new Tensor by repeating x the number of times given by
** repeat_times. After tiling, the value of the ith dimension of the
** output is equal to x.shape[i]*repeat_times[i]. Both the number of
** dimensions of x and the number of elements in repeat_times should
** be less than or equal to 6.Support all data types.
@param x The input tensor.
@param repeat_times The lower bound
@param out The output tensor which stores the result.
*/
FASTDEPLOY_DECL void Tile(const FDTensor& x,
const std::vector<int64_t>& repeat_times,
FDTensor* out);
} // namespace function
} // namespace fastdeploy