Skip to content

Commit df92bba

Browse files
committed
[BugFix][ARM][OP] Fix arm op: reduce_mean & sequence_expand_as (PaddlePaddle#4492)
* fix op for gcn model: reduce_mean, sequence_expand_as_compute. test=develop
1 parent 68b3886 commit df92bba

File tree

5 files changed

+54
-19
lines changed

5 files changed

+54
-19
lines changed

lite/kernels/arm/reduce_mean_compute.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ void ReduceMeanCompute::Run() {
3737
}
3838
}
3939
}
40-
int n_in = x_dims[0];
41-
int c_in = x_dims[1];
42-
int h_in = x_dims[2];
43-
int w_in = x_dims[3];
40+
41+
size_t new_dims[] = {1, 1, 1, 1};
42+
for (size_t j = 0; j < x_dims.size(); ++j) {
43+
new_dims[j] = x_dims[j];
44+
}
45+
int n_in = new_dims[0];
46+
int c_in = new_dims[1];
47+
int h_in = new_dims[2];
48+
int w_in = new_dims[3];
4449
if (dim.size() == 0) {
4550
lite::arm::math::reduce_mean_all(input, output, n_in, c_in, h_in, w_in);
4651
} else if (dim.size() == 1) {

lite/kernels/arm/sequence_expand_as_compute.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ void SequenceExpandAsCompute::Run() {
2525
auto* x = param.x;
2626
auto* y = param.y;
2727
auto* out = param.out;
28-
auto x_lod = x->lod();
2928
auto y_lod = y->lod();
29+
CHECK_EQ(y_lod.size(), 1u);
30+
CHECK_GT(y_lod[0].size(), 1u);
31+
3032
auto dims = x->dims();
3133
auto out_data = out->mutable_data<float>();
3234
auto x_data = x->data<float>();
@@ -35,8 +37,8 @@ void SequenceExpandAsCompute::Run() {
3537
std::vector<uint64_t> out_lod;
3638
out_lod.push_back(0);
3739
int sum = 0;
38-
for (int i = 0; i < y_lod[0].size(); i++) {
39-
int repeat_num = y_lod[0][i];
40+
for (int i = 1; i < y_lod[0].size(); i++) {
41+
int repeat_num = y_lod[0][i] - y_lod[0][i - 1];
4042
if (repeat_num == 0) {
4143
continue;
4244
} else {

lite/kernels/x86/reduce_compute.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class ReduceMeanCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
115115
HANDLE_DIM(4, 1, MeanFunctor);
116116
HANDLE_DIM(3, 2, MeanFunctor);
117117
HANDLE_DIM(3, 1, MeanFunctor);
118+
HANDLE_DIM(2, 2, MeanFunctor);
118119
HANDLE_DIM(2, 1, MeanFunctor);
119120
HANDLE_DIM(1, 1, MeanFunctor);
120121
}

lite/tests/kernels/reduce_mean_compute_test.cc

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,14 @@ class ReduceMeanComputeTester : public arena::TestCase {
255255
}
256256

257257
auto* out_data = out->mutable_data<float>();
258-
int in_n = x_dims_[0];
259-
int in_c = x_dims_[1];
260-
int in_h = x_dims_[2];
261-
int in_w = x_dims_[3];
258+
size_t new_dims[] = {1, 1, 1, 1};
259+
for (size_t j = 0; j < x_dims_.size(); ++j) {
260+
new_dims[j] = x_dims_[j];
261+
}
262+
int in_n = new_dims[0];
263+
int in_c = new_dims[1];
264+
int in_h = new_dims[2];
265+
int in_w = new_dims[3];
262266

263267
if (dim_.size() == 0) {
264268
reduce_mean_all(x_data, out_data, in_n, in_c, in_h, in_w);
@@ -318,12 +322,35 @@ void test_reduce_mean(Place place) {
318322
for (auto w : {1, 3}) {
319323
for (bool keep_dim : {false, true}) {
320324
for (auto dim : reduce_dim) {
321-
auto x_dims = DDim(std::vector<int64_t>({n, c, h, w}));
322-
std::unique_ptr<arena::TestCase> tester(
323-
new ReduceMeanComputeTester(
324-
place, "def", dim, keep_dim, x_dims));
325-
arena::Arena arena(std::move(tester), place, 2e-5);
326-
arena.TestPrecision();
325+
DDim x_dims;
326+
for (auto dims : {2, 3, 4}) {
327+
switch (dims) {
328+
case 2:
329+
x_dims = DDim(std::vector<int64_t>({n, c}));
330+
break;
331+
case 3:
332+
x_dims = DDim(std::vector<int64_t>({n, c, h}));
333+
break;
334+
case 4:
335+
x_dims = DDim(std::vector<int64_t>({n, c, h, w}));
336+
break;
337+
default:
338+
x_dims = DDim(std::vector<int64_t>({n, c, h, w}));
339+
}
340+
341+
int last_dim = dim.back();
342+
if (dim.back() < 0) {
343+
last_dim += x_dims.size();
344+
if (last_dim < 1) continue;
345+
}
346+
if (last_dim > x_dims.size() - 1) continue;
347+
348+
std::unique_ptr<arena::TestCase> tester(
349+
new ReduceMeanComputeTester(
350+
place, "def", dim, keep_dim, x_dims));
351+
arena::Arena arena(std::move(tester), place, 2e-5);
352+
arena.TestPrecision();
353+
}
327354
}
328355
}
329356
}

lite/tests/kernels/sequence_expand_as_compute_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ TEST(sequence_expand_as, run_test) {
5656
y_data[i] = static_cast<float>(i);
5757
}
5858

59-
std::vector<std::vector<uint64_t>> lod{{0, 3, 3, 1, 1}};
59+
std::vector<std::vector<uint64_t>> lod{{0, 3, 6, 7, 8}};
6060
y.set_lod(lod);
6161
paddle::lite::kernels::arm::SequenceExpandAsCompute sequence_expand_as;
6262

@@ -76,7 +76,7 @@ TEST(sequence_expand_as, run_test) {
7676

7777
int index = 1;
7878
auto out_lod = param.out->lod()[0];
79-
int lod_sum = out_lod[index];
79+
int lod_sum = out_lod[index] - out_lod[index - 1];
8080
LOG(INFO) << "output: ";
8181
for (int i = 0; i < out.dims().production(); i++) {
8282
LOG(INFO) << out_data[i];

0 commit comments

Comments
 (0)