Skip to content

Commit 6e2e646

Browse files
committed
Add cumprod function
1 parent 12af6b4 commit 6e2e646

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
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

tests/function/test_cumprod.cc

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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/core/fd_tensor.h"
16+
#include "fastdeploy/function/cumprod.h"
17+
#include "glog/logging.h"
18+
#include "gtest_utils.h"
19+
#include "gtest/gtest.h"
20+
#include <array>
21+
#include <vector>
22+
23+
namespace fastdeploy {
24+
namespace function {
25+
26+
std::vector<float> CreateTestData() {
27+
// Shape: [2, 3, 4]
28+
std::vector<float> x_data = {
29+
0.8428625, 0.6461913, 0.13740455, 0.11430702, 0.659926, 0.535816,
30+
0.7429162, 0.8456049, 0.21228176, 0.29970083, 0.8621713, 0.40894133,
31+
0.12684688, 0.1566195, 0.42884097, 0.8476526, 0.2458633, 0.669046,
32+
0.87888306, 0.6762589, 0.666453, 0.32523027, 0.4139388, 0.8341406};
33+
return x_data;
34+
}
35+
36+
TEST(fastdeploy, cumprod) {
37+
CheckShape check_shape;
38+
CheckData check_data;
39+
FDTensor x, y;
40+
auto test_data = CreateTestData();
41+
x.SetExternalData({2, 3, 4}, FDDataType::FP32, test_data.data());
42+
43+
std::vector<float> result = {0.842862, 0.646191, 0.137405, 0.114307, 0.659926,
44+
0.535816, 0.742916, 0.845605, 0.212282, 0.299701,
45+
0.862171, 0.408941, 0.106914, 0.101206, 0.058925,
46+
0.096893, 0.162252, 0.358486, 0.652937, 0.571848,
47+
0.141476, 0.097472, 0.356886, 0.341115};
48+
Cumprod(x, &y, 0);
49+
check_shape(y.shape, {2, 3, 4});
50+
check_data(reinterpret_cast<const float*>(y.Data()), result.data(),
51+
result.size());
52+
53+
result = {0.842862, 0.646191, 0.137405, 0.114307, 0.556227, 0.34624,
54+
0.10208, 0.096659, 0.118077, 0.103768, 0.088011, 0.039528,
55+
0.126847, 0.15662, 0.428841, 0.847653, 0.031187, 0.104786,
56+
0.376901, 0.573233, 0.020785, 0.034079, 0.156014, 0.478157};
57+
Cumprod(x, &y, 1);
58+
check_shape(y.shape, {2, 3, 4});
59+
check_data(reinterpret_cast<const float*>(y.Data()), result.data(),
60+
result.size());
61+
62+
result = {0.842862, 0.54465, 0.074837, 0.008554, 0.659926, 0.353599,
63+
0.262694, 0.222136, 0.212282, 0.063621, 0.054852, 0.022431,
64+
0.126847, 0.019867, 0.00852, 0.007222, 0.245863, 0.164494,
65+
0.144571, 0.097767, 0.666453, 0.216751, 0.089722, 0.07484};
66+
Cumprod(x, &y, 2);
67+
check_shape(y.shape, {2, 3, 4});
68+
check_data(reinterpret_cast<const float*>(y.Data()), result.data(),
69+
result.size());
70+
}
71+
72+
} // namespace function
73+
} // namespace fastdeploy

0 commit comments

Comments
 (0)