Skip to content

Commit 124f9f8

Browse files
authored
Merge pull request #687 from joey12300/add_tile_linespace_functions
[Functions] Add fdtensor functions
2 parents 5af28c1 + 97165fa commit 124f9f8

File tree

18 files changed

+887
-11
lines changed

18 files changed

+887
-11
lines changed

fastdeploy/function/cumprod.cc

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "fastdeploy/function/cumprod.h"
16+
17+
namespace fastdeploy {
18+
namespace function {
19+
20+
void GetCumprodDimInfo(const std::vector<int64_t>& dim, int cumprod_dim,
21+
size_t* outer_dim, size_t* mid_dim, size_t* inner_dim) {
22+
int dim_size = dim.size();
23+
FDASSERT(cumprod_dim >= -dim_size,
24+
"The input dim of CumprodOp should be larger than the opposite "
25+
"rank of input x which is %d. But received dim = %d",
26+
-dim_size, cumprod_dim);
27+
FDASSERT(cumprod_dim < dim_size,
28+
"The input dim of CumprodOp should be smaller than the "
29+
"rank of input x which is %d. But received dim = %d",
30+
dim_size, cumprod_dim);
31+
if (cumprod_dim < 0)
32+
cumprod_dim += dim_size;
33+
34+
*outer_dim = 1;
35+
for (int i = 0; i < cumprod_dim; ++i) {
36+
*outer_dim *= dim[i];
37+
}
38+
*mid_dim = dim[cumprod_dim];
39+
*inner_dim = 1;
40+
for (int i = cumprod_dim + 1; i < dim_size; ++i) {
41+
*inner_dim *= dim[i];
42+
}
43+
}
44+
45+
template <typename T>
46+
void CumprodKernel(const FDTensor& x, FDTensor* out, int axis) {
47+
auto* x_data = reinterpret_cast<const T*>(x.Data());
48+
auto shape = x.Shape();
49+
50+
size_t outer_dim = 1;
51+
size_t mid_dim = 1;
52+
size_t inner_dim = 1;
53+
GetCumprodDimInfo(shape, axis, &outer_dim, &mid_dim, &inner_dim);
54+
55+
out->Allocate(x.Shape(), x.Dtype());
56+
auto* out_data = reinterpret_cast<T*>(out->Data());
57+
58+
for (size_t i = 0; i < outer_dim; i++) {
59+
for (size_t j = 0; j < mid_dim; j++) {
60+
for (size_t k = 0; k < inner_dim; k++) {
61+
size_t pos = i * mid_dim * inner_dim + j * inner_dim + k;
62+
if (j == 0) {
63+
out_data[pos] = x_data[pos];
64+
} else {
65+
out_data[pos] = out_data[pos - inner_dim] * x_data[pos];
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+
void Cumprod(const FDTensor& x, FDTensor* out, int axis) {
73+
FD_VISIT_INT_FLOAT_TYPES(x.dtype, "CumprodKernel",
74+
([&] { CumprodKernel<data_t>(x, out, axis); }));
75+
}
76+
77+
} // namespace function
78+
} // namespace fastdeploy

fastdeploy/function/cumprod.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include "fastdeploy/core/fd_tensor.h"
18+
19+
namespace fastdeploy {
20+
namespace function {
21+
22+
/** Excute the concatenate operation for input FDTensor along given axis.
23+
@param x The input tensor.
24+
@param out The output tensor which stores the result.
25+
@param axisi Axis which will be concatenated.
26+
*/
27+
28+
FASTDEPLOY_DECL void Cumprod(const FDTensor& x, FDTensor* out, int axis = 0);
29+
30+
} // namespace function
31+
} // namespace fastdeploy

fastdeploy/function/linspace.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "fastdeploy/function/linspace.h"
16+
17+
namespace fastdeploy {
18+
namespace function {
19+
20+
template <typename T>
21+
void LinspaceKernel(double start, double end, int num, FDTensor* out) {
22+
FDASSERT(
23+
num > 0,
24+
"The num of linspace op should be larger than 0, but received num is %d",
25+
num);
26+
out->Allocate({num}, TypeToDataType<T>::dtype);
27+
T* out_data = reinterpret_cast<T*>(out->Data());
28+
if (num > 1) {
29+
// step should be of double type for all types
30+
double step = (static_cast<double>(end - start)) / (num - 1);
31+
int half_num = num / 2;
32+
for (int i = 0; i < num; ++i) {
33+
if (i < half_num) {
34+
out_data[i] = static_cast<T>(start + step * i);
35+
} else {
36+
out_data[i] = static_cast<T>(end - step * (num - i - 1));
37+
}
38+
}
39+
} else {
40+
out_data[0] = static_cast<T>(start);
41+
}
42+
}
43+
44+
void Linspace(double start, double end, int num, FDTensor* out,
45+
FDDataType dtype) {
46+
FD_VISIT_INT_FLOAT_TYPES(dtype, "LinspaceKernel", ([&] {
47+
LinspaceKernel<data_t>(start, end, num, out);
48+
}));
49+
}
50+
51+
} // namespace function
52+
} // namespace fastdeploy

fastdeploy/function/linspace.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include "fastdeploy/core/fd_tensor.h"
18+
19+
namespace fastdeploy {
20+
namespace function {
21+
22+
/** Return fixed number of evenly spaced values within a given interval.
23+
@param start The input start is start variable of range.
24+
@param end The input stop is start variable of range.
25+
@param num The input num is given num of the sequence.
26+
@param out The output tensor which stores the result.
27+
@param dtype The data type of output tensor, default to float32.
28+
*/
29+
FASTDEPLOY_DECL void Linspace(double start, double end, int num, FDTensor* out,
30+
FDDataType dtype = FDDataType::FP32);
31+
32+
} // namespace function
33+
} // namespace fastdeploy

fastdeploy/function/math.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ DEFINE_ACTIVATION_KERNEL(Sqrt, SqrtFunctor)
3939
DEFINE_ACTIVATION_KERNEL(Log, LogFunctor)
4040
DEFINE_ACTIVATION_KERNEL(Round, RoundFunctor)
4141
DEFINE_ACTIVATION_KERNEL(Exp, ExpFunctor)
42+
DEFINE_ACTIVATION_KERNEL(Abs, AbsFunctor)
4243

4344
void Sqrt(const FDTensor& x, FDTensor* out) {
4445
FD_VISIT_FLOAT_TYPES(x.dtype, "SqrtKernel",
@@ -60,5 +61,10 @@ void Exp(const FDTensor& x, FDTensor* out) {
6061
([&] { ExpKernel<data_t>(x, out); }));
6162
}
6263

64+
void Abs(const FDTensor& x, FDTensor* out) {
65+
FD_VISIT_FLOAT_TYPES(x.dtype, "AbsKernel",
66+
([&] { AbsKernel<data_t>(x, out); }));
67+
}
68+
6369
} // namespace function
6470
} // namespace fastdeploy

fastdeploy/function/math.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@ FASTDEPLOY_DECL void Round(const FDTensor& x, FDTensor* out);
4343
*/
4444
FASTDEPLOY_DECL void Exp(const FDTensor& x, FDTensor* out);
4545

46+
/** This operator is used to perform elementwise abs for input X. Only for float type FDTensor
47+
@param x The input tensor.
48+
@param out The output tensor which stores the result.
49+
*/
50+
FASTDEPLOY_DECL void Abs(const FDTensor& x, FDTensor* out);
51+
4652
} // namespace function
4753
} // namespace fastdeploy

fastdeploy/function/math_functor.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,14 @@ template <typename T> struct SqrtFunctor {
5252
}
5353
};
5454

55+
// abs(x) = x if x > 0 else -x
56+
template <typename T> struct AbsFunctor {
57+
template <typename Device, typename X, typename Out>
58+
void operator()(Device d, X x, Out out) const {
59+
out.device(d) =
60+
x.unaryExpr([](T v) { return v > static_cast<T>(0) ? v : -v; });
61+
}
62+
};
63+
5564
} // namespace function
5665
} // namespace fastdeploy

0 commit comments

Comments
 (0)