concat_compute_test.cc 6.8 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (c) 2019 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.

#include "lite/kernels/arm/concat_compute.h"
#include <gtest/gtest.h>
#include <limits>
#include <string>
#include <vector>
20
#include "lite/backends/arm/math/funcs.h"
Y
Yan Chunwei 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include "lite/core/op_registry.h"
#include "lite/core/tensor.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace arm {

bool infer_shape(const operators::ConcatParam& param) {
  std::vector<lite::DDim> input_dims;
  for (auto p : param.x) {
    input_dims.push_back(p->dims());
  }
  size_t axis = static_cast<size_t>(param.axis);
  const size_t n = input_dims.size();
  CHECK_GT_OR_FALSE(n, 0);
  auto& out_dims = input_dims[0];
  size_t in_zero_dims_size = out_dims.size();
  for (size_t i = 1; i < n; i++) {
    for (size_t j = 0; j < in_zero_dims_size; j++) {
      if (j == axis) {
        out_dims[axis] += input_dims[i][j];
      } else {
        CHECK_EQ_OR_FALSE(out_dims[j], input_dims[i][j]);
      }
    }
  }
  if (out_dims[axis] < 0) {
    out_dims[axis] = -1;
  }
  // Set output dims
  param.output->Resize(lite::DDim(out_dims));
  return true;
}

void concat_compute_ref(const operators::ConcatParam& param) {
  std::vector<lite::Tensor*> input = param.x;
  int axis = param.axis;
  infer_shape(param);

  lite::Tensor* output = param.output;
  int num = input.size();
  int rows = 1;
  auto dim_0 = input[0]->dims();
  for (int i = 0; i < axis; ++i) {
    rows *= dim_0[i];
  }
  int out_rows = rows, out_cols = 0;

  std::vector<int> input_cols(input.size());
  for (int i = 0; i < num; ++i) {
    int input_i_numel = input[i]->dims().size() == 0 ? 0 : 1;
    for (int didx = 0; didx < input[i]->dims().size(); ++didx) {
      input_i_numel *= input[i]->dims()[didx];
    }
    int t_cols = input_i_numel / rows;
    out_cols += t_cols;
    input_cols[i] = t_cols;
  }

  // computation
  auto output_data = output->mutable_data<float>();
  int col_idx = 0;
  for (int j = 0; j < num; ++j) {
    int col_len = input_cols[j];
    auto input_data = input[j]->data<float>();
    for (int k = 0; k < out_rows; ++k) {
      memcpy(output_data + k * out_cols + col_idx,
             input_data + k * col_len,
             sizeof(float) * col_len);
    }
    col_idx += col_len;
  }
}

TEST(concat_arm, init) {
  ConcatCompute concat;
98
  ASSERT_EQ(concat.precision(), PRECISION(kAny));
Y
Yan Chunwei 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  ASSERT_EQ(concat.target(), TARGET(kARM));
}

TEST(concat_arm, compute_input_single) {
  ConcatCompute concat;
  operators::ConcatParam param;

  LOG(INFO) << "test concat start";
  lite::Tensor output;
  lite::Tensor output_ref;
  lite::Tensor tensorA;
  DDimLite ddimA({10, 4, 3, 2});
  tensorA.Resize(ddimA);

  for (int i = 0; i < ddimA.data()[0] * ddimA.data()[1] * ddimA.data()[2] *
                          ddimA.data()[3];
       i++) {
    tensorA.mutable_data<float>()[i] = i;
  }

  param.x.push_back(&tensorA);
  for (int cur_axis : {0, 1}) {
    param.output = &output;
    param.axis = cur_axis;
    CHECK(infer_shape(param));
    concat.SetParam(param);
    LOG(INFO) << "test concat start cur_axis:" << cur_axis;

    concat.Run();
    LOG(INFO) << "concat.Run end";
    param.output = &output_ref;
    LOG(INFO) << "concat_compute_ref start";
    concat_compute_ref(param);
    LOG(INFO) << "concat_compute_ref end";

    auto* output_data = output.data<float>();
    auto* output_ref_data = output_ref.data<float>();
    for (int i = 0; i < (ddimA.data()[0]) * ddimA.data()[1] * ddimA.data()[2] *
                            ddimA.data()[3];
         i++) {
      // LOG(INFO) << "output[" << i << "]:" << output_data[i] << "
      // output_ref_data[" << i << "]:" << output_ref_data[i];
      EXPECT_NEAR(output_data[i], output_ref_data[i], 1e-5);
    }
  }
}

TEST(concat_arm, compute_input_multi) {
  ConcatCompute concat;
  operators::ConcatParam param;

  LOG(INFO) << "test concat start";
  // init param
  // x: tensorA, tensorB, tensorC, tensorD
  // axis: 0
  lite::Tensor output;
  lite::Tensor output_ref;
  lite::Tensor tensorA;
  lite::Tensor tensorB;
  lite::Tensor tensorC;
  lite::Tensor tensorD;

  DDimLite ddimA({10, 4, 3, 2});
  DDimLite ddimB({20, 4, 3, 2});
  DDimLite ddimC({30, 4, 3, 2});
  DDimLite ddimD({40, 4, 3, 2});

  tensorA.Resize(ddimA);
  tensorB.Resize(ddimB);
  tensorC.Resize(ddimC);
  tensorD.Resize(ddimD);

  for (int i = 0; i < ddimA.data()[0] * ddimA.data()[1] * ddimA.data()[2] *
                          ddimA.data()[3];
       i++) {
    tensorA.mutable_data<float>()[i] = i;
  }
  for (int i = 0; i < ddimB.data()[0] * ddimB.data()[1] * ddimB.data()[2] *
                          ddimB.data()[3];
       i++) {
    tensorB.mutable_data<float>()[i] = i + 1;
  }
  for (int i = 0; i < ddimC.data()[0] * ddimC.data()[1] * ddimC.data()[2] *
                          ddimC.data()[3];
       i++) {
    tensorC.mutable_data<float>()[i] = i + 2;
  }
  for (int i = 0; i < ddimD.data()[0] * ddimD.data()[1] * ddimD.data()[2] *
                          ddimD.data()[3];
       i++) {
    tensorD.mutable_data<float>()[i] = i + 3;
  }

  param.x.push_back(&tensorA);
  param.x.push_back(&tensorB);
  param.x.push_back(&tensorC);
  param.x.push_back(&tensorD);
  for (int cur_axis : {0}) {
    param.output = &output;
    param.axis = cur_axis;
    CHECK(infer_shape(param));
    concat.SetParam(param);
    LOG(INFO) << "test concat start cur_axis:" << cur_axis;

    concat.Run();
    LOG(INFO) << "concat.Run end";
    param.output = &output_ref;
    LOG(INFO) << "concat_compute_ref start";
    concat_compute_ref(param);
    LOG(INFO) << "concat_compute_ref end";

    auto* output_data = output.data<float>();
    auto* output_ref_data = output_ref.data<float>();
    int elem_num = (ddimA.data()[0] + ddimB.data()[0] + ddimC.data()[0] +
                    ddimD.data()[0]) *
                   ddimA.data()[1] * ddimA.data()[2] * ddimA.data()[3];
    for (int i = 0; i < elem_num; i++) {
      // LOG(INFO) << "output[" << i << "]:" << output_data[i] << "
      // output_ref_data[" << i << "]:" << output_ref_data[i];
      EXPECT_NEAR(output_data[i], output_ref_data[i], 1e-5);
    }
  }
}

TEST(concat, retrive_op) {
  auto concat =
225
      KernelRegistry::Global().Create<TARGET(kARM), PRECISION(kAny)>("concat");
Y
Yan Chunwei 已提交
226 227 228 229 230 231 232 233 234
  ASSERT_FALSE(concat.empty());
  ASSERT_TRUE(concat.front());
}

}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

235
USE_LITE_KERNEL(concat, kARM, kAny, kNCHW, def);