From b4bac81bd9da1b2ffdf0e3a1850707bf56ee6d55 Mon Sep 17 00:00:00 2001 From: jackzhang235 Date: Fri, 27 Mar 2020 05:39:33 +0000 Subject: [PATCH] fix some mlu kernel test --- .../kernels/mlu/bridges/batch_norm_op_test.cc | 2 -- lite/kernels/mlu/bridges/concat_op_test.cc | 14 +-------- lite/kernels/mlu/bridges/conv_op_test.cc | 4 --- lite/kernels/mlu/bridges/fc_op_test.cc | 31 +++++++++++++++---- .../mlu/bridges/interpolate_op_test.cc | 2 -- lite/kernels/mlu/bridges/pool_op_test.cc | 5 --- lite/kernels/mlu/bridges/softmax_op_test.cc | 2 -- lite/kernels/mlu/bridges/test_helper.cc | 4 ++- lite/kernels/mlu/bridges/transpose_op.cc | 22 ------------- lite/kernels/mlu/bridges/transpose_op_test.cc | 1 + 10 files changed, 30 insertions(+), 57 deletions(-) diff --git a/lite/kernels/mlu/bridges/batch_norm_op_test.cc b/lite/kernels/mlu/bridges/batch_norm_op_test.cc index 706b0421b3..65b24a0a72 100644 --- a/lite/kernels/mlu/bridges/batch_norm_op_test.cc +++ b/lite/kernels/mlu/bridges/batch_norm_op_test.cc @@ -137,9 +137,7 @@ void test_batch_norm( {bs, ic, ih, iw}, {0, 2, 3, 1}); - out->Resize({bs, ih, iw, ic}); x->CopyDataFrom(input_trans); - x->Resize({bs, ih, iw, ic}); LaunchOp(op, {x_var_name}, {out_var_name}); diff --git a/lite/kernels/mlu/bridges/concat_op_test.cc b/lite/kernels/mlu/bridges/concat_op_test.cc index b75ebc0951..c4b48a9ef4 100644 --- a/lite/kernels/mlu/bridges/concat_op_test.cc +++ b/lite/kernels/mlu/bridges/concat_op_test.cc @@ -113,21 +113,8 @@ void test_concat(std::vector> input, int axis) { static_cast(input[1][2]), static_cast(input[1][3])}, {0, 2, 3, 1}); - auto os = out->dims(); - out->Resize({static_cast(os[0]), - static_cast(os[2]), - static_cast(os[3]), - static_cast(os[1])}); x->CopyDataFrom(input_x); y->CopyDataFrom(input_y); - x->Resize({static_cast(input[0][0]), - static_cast(input[0][2]), - static_cast(input[0][3]), - static_cast(input[0][1])}); - y->Resize({static_cast(input[1][0]), - static_cast(input[1][2]), - static_cast(input[1][3]), - static_cast(input[1][1])}); LaunchOp(op, {x_var_name, y_var_name}, {out_var_name}); @@ -136,6 +123,7 @@ void test_concat(std::vector> input, int axis) { Tensor output_trans; output_trans.Resize(out->dims()); + auto os = out->dims(); transpose(out_data, output_trans.mutable_data(), {static_cast(os[0]), diff --git a/lite/kernels/mlu/bridges/conv_op_test.cc b/lite/kernels/mlu/bridges/conv_op_test.cc index 6155a75018..e34dd7c2a8 100644 --- a/lite/kernels/mlu/bridges/conv_op_test.cc +++ b/lite/kernels/mlu/bridges/conv_op_test.cc @@ -244,10 +244,6 @@ void test_conv(int bs, } } - input->Resize({bs, ih, iw, ic}); - output->Resize( - {output_shape[0], output_shape[2], output_shape[3], output_shape[1]}); - // create and convert op to MLU model, then run it on MLU auto op = CreateOp(opdesc_mlu, &scope); LaunchOp(op, {input_var_name}, {output_var_name}); diff --git a/lite/kernels/mlu/bridges/fc_op_test.cc b/lite/kernels/mlu/bridges/fc_op_test.cc index 79bee35501..8f92b6abad 100644 --- a/lite/kernels/mlu/bridges/fc_op_test.cc +++ b/lite/kernels/mlu/bridges/fc_op_test.cc @@ -139,15 +139,34 @@ void test_fc(const std::vector& input_shape, } auto fc_op_mlu = CreateOp(fc_op_desc_mlu, &scope); - input->Resize({static_cast(input_shape[0]), - static_cast(input_shape[2]), - static_cast(input_shape[3]), - static_cast(input_shape[1])}); - out->Resize({static_cast(input_shape[0]), static_cast(w_shape[1])}); + + Tensor input_tmp, out_tmp; + input_tmp.Resize(input_shape); + transpose(input->mutable_data(), + input_tmp.mutable_data(), + {static_cast(input_shape[0]), + static_cast(input_shape[1]), + static_cast(input_shape[2]), + static_cast(input_shape[3])}, + {0, 2, 3, 1}); + input->CopyDataFrom(input_tmp); + LaunchOp(fc_op_mlu, {input_var_name}, {out_var_name}); - // compare results + auto os = out->dims(); + out_tmp.Resize(os); auto* out_data = out->mutable_data(); + // transpose(out_data, + // out_tmp.mutable_data(), + // {static_cast(os[0]), + // static_cast(os[2]), + // static_cast(os[3]), + // static_cast(os[1])}, + // {0, 3, 1, 2}); + // + // out_data = out_tmp.mutable_data(); + + // compare results auto* out_ref_data = out_ref->mutable_data(); for (int i = 0; i < out->dims().production(); i++) { EXPECT_NEAR(out_data[i], out_ref_data[i], 1e-5); diff --git a/lite/kernels/mlu/bridges/interpolate_op_test.cc b/lite/kernels/mlu/bridges/interpolate_op_test.cc index 29abff819a..0e99da6435 100644 --- a/lite/kernels/mlu/bridges/interpolate_op_test.cc +++ b/lite/kernels/mlu/bridges/interpolate_op_test.cc @@ -237,7 +237,6 @@ class InterpComputeTester { /* printf("----output tensor dims: %ld, %d, %d, %ld\n", dims_[0], out_h, * out_w, dims_[1]); */ std::vector out_shape_nchw = {dims_[0], dims_[1], out_h, out_w}; - out->Resize(DimNCHW2NHWC(out_shape_nchw)); outref->Resize(out_shape_nchw); outsize->Resize({2}); @@ -283,7 +282,6 @@ class InterpComputeTester { {in, ic, ih, iw}, {0, 2, 3, 1}); x->CopyDataFrom(input_trans); - x->Resize(DimNCHW2NHWC(dims_.Vectorize())); if (use_outsize_) { LaunchOp(op, {x_var_name, outsize_var_name}, {out_var_name}); } else { diff --git a/lite/kernels/mlu/bridges/pool_op_test.cc b/lite/kernels/mlu/bridges/pool_op_test.cc index 90e43987e4..8cee8dbe86 100644 --- a/lite/kernels/mlu/bridges/pool_op_test.cc +++ b/lite/kernels/mlu/bridges/pool_op_test.cc @@ -180,12 +180,7 @@ void test_pool(int bs, {0, 2, 3, 1}); auto os = out->dims(); - out->Resize({static_cast(os[0]), - static_cast(os[2]), - static_cast(os[3]), - static_cast(os[1])}); x->CopyDataFrom(input_trans); - x->Resize({bs, ih, iw, ic}); LaunchOp(op, {x_var_name}, {out_var_name}); diff --git a/lite/kernels/mlu/bridges/softmax_op_test.cc b/lite/kernels/mlu/bridges/softmax_op_test.cc index 87f8f589bc..a5251ed43c 100644 --- a/lite/kernels/mlu/bridges/softmax_op_test.cc +++ b/lite/kernels/mlu/bridges/softmax_op_test.cc @@ -110,9 +110,7 @@ void test_softmax(const std::vector& input_shape, int axis) { {bs, ic, ih, iw}, {0, 2, 3, 1}); - out->Resize({bs, ih, iw, ic}); x->CopyDataFrom(input_trans); - x->Resize({bs, ih, iw, ic}); LaunchOp(op, {x_var_name}, {out_var_name}); diff --git a/lite/kernels/mlu/bridges/test_helper.cc b/lite/kernels/mlu/bridges/test_helper.cc index c6c87e42b4..377a00689e 100644 --- a/lite/kernels/mlu/bridges/test_helper.cc +++ b/lite/kernels/mlu/bridges/test_helper.cc @@ -58,7 +58,7 @@ void LaunchOp(const std::shared_ptr op, graph.AddNode(input_name, input_tensor->dims().Vectorize(), CNML_TENSOR, - CNML_NHWC, + CNML_NCHW, graph.FPType(), reinterpret_cast( input_tensor->mutable_data(TARGET(kMLU)))); @@ -68,6 +68,8 @@ void LaunchOp(const std::shared_ptr op, sizeof(float) * input_tensor->dims().production(), CNRT_MEM_TRANS_DIR_HOST2DEV)); } + op->CheckShape(); + op->InferShape(); bridges.Select(op_type, TARGET(kMLU))( reinterpret_cast(&graph), const_cast(op.get()), nullptr); diff --git a/lite/kernels/mlu/bridges/transpose_op.cc b/lite/kernels/mlu/bridges/transpose_op.cc index f9b0caeb3e..5e5c5b79eb 100644 --- a/lite/kernels/mlu/bridges/transpose_op.cc +++ b/lite/kernels/mlu/bridges/transpose_op.cc @@ -21,28 +21,6 @@ namespace lite { namespace subgraph { namespace mlu { -// std::vector axis_to_nhwc4d(const std::vector& axis) { -// CHECK_EQ(axis.size(), 4); -// std::vector new_axis(4, 0); -// const std::vector axis_map1 = {0, 2, 3, 1}; -// const std::vector axis_map2 = {0, 3, 1, 2}; -// for (size_t i = 0; i < new_axis.size(); ++i) { -// new_axis[i] = axis_map2[axis[axis_map1[i]]]; -// } -// return new_axis; -//} -// -// std::vector axis_to_nhw3d(const std::vector& axis) { -// CHECK_EQ(axis.size(), 3); -// std::vector new_axis(3, 0); -// const std::vector axis_map = {0, 2, 1}; -// for (size_t i = 0; i < new_axis.size(); ++i) { -// new_axis[i] = axis_map[axis[axis_map[i]]]; -// } -// new_axis.push_back(3); -// return new_axis; -//} - std::vector axis_to_nhwc(const std::vector& axis) { CHECK_EQ(axis.size(), 4) << "Unsupport dim in mlu transpose"; std::vector new_axis(4, 0); diff --git a/lite/kernels/mlu/bridges/transpose_op_test.cc b/lite/kernels/mlu/bridges/transpose_op_test.cc index 0b2c015975..f10801fbc6 100644 --- a/lite/kernels/mlu/bridges/transpose_op_test.cc +++ b/lite/kernels/mlu/bridges/transpose_op_test.cc @@ -115,6 +115,7 @@ void test_transpose(const std::vector& input_shape, } } +// TODO(pmshst): fix the transpose test TEST(MLUBridges, transpose) { std::vector input_shape = {2, 3, 4, 5}; test_transpose(input_shape, std::vector{0, 1, 3, 2}); -- GitLab