split_compute_test.cc 5.7 KB
Newer Older
Z
zhupengyang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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 "paddle/fluid/lite/kernels/arm/split_compute.h"
#include <gtest/gtest.h>
C
Chunwei 已提交
17
#include <cstring>
Z
zhupengyang 已提交
18 19 20 21 22 23 24 25 26 27
#include <limits>
#include <vector>
#include "paddle/fluid/lite/core/op_registry.h"

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

void splite_resize_out(const lite::Tensor* din,
Z
zhupengyang 已提交
28 29
                       const std::vector<lite::Tensor*>& dout, int axis,
                       int num, const std::vector<int>& sections) {
Z
zhupengyang 已提交
30
  auto in_dims = din->dims();
Z
zhupengyang 已提交
31
  int outs_number = dout.size();
Z
zhupengyang 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

  std::vector<lite::DDimLite> outs_dims;
  outs_dims.reserve(outs_number);

  if (num > 0) {
    int out_axis_dim = in_dims[axis] / num;
    for (int i = 0; i < outs_number; ++i) {
      auto dim = in_dims;
      dim[axis] = out_axis_dim;
      outs_dims.push_back(dim);
    }
  } else if (sections.size() > 0) {
    for (size_t i = 0; i < outs_number; ++i) {
      auto dim = in_dims;
      dim[axis] = sections[i];
      outs_dims.push_back(dim);
    }
  }

  for (int j = 0; j < outs_dims.size(); ++j) {
Z
zhupengyang 已提交
52
    dout[j]->Resize(outs_dims[j]);
Z
zhupengyang 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  }
}

template <typename dtype>
void split_compute_ref(const operators::SplitParam& param) {
  const dtype* din = param.x->mutable_data<const dtype>();
  auto& dout = param.output;
  auto in_dim = param.x->dims();
  int axis = param.axis;
  std::vector<int> in_strides(in_dim.size());
  in_strides[in_dim.size() - 1] = in_dim[in_dim.size() - 1];
  for (int i = in_dim.size() - 2; i >= 0; --i) {
    in_strides[i] = in_strides[i + 1] * in_dim[i];
  }

  int input_offset = 0;
Z
zhupengyang 已提交
69
  for (auto out : dout) {
Z
zhupengyang 已提交
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    auto out_dim = out->dims();
    std::vector<int> out_strides(out_dim.size());
    out_strides[out_dim.size() - 1] = out_dim[out_dim.size() - 1];
    for (int i = out_dim.size() - 2; i >= 0; --i) {
      out_strides[i] = out_strides[i + 1] * out_dim[i];
    }

    dtype* out_data = out->mutable_data<dtype>();
    int before = out_strides[0] / out_strides[axis];
    int in_after = in_strides[axis];
    int out_after = out_strides[axis];

    for (int i = 0; i < before; ++i) {
      std::memcpy(out_data + i * out_after, din + input_offset + i * in_after,
                  sizeof(dtype) * out_after);
    }
    input_offset += out_strides[axis];
  }
}

TEST(split_arm, init) {
  SplitCompute split;
  ASSERT_EQ(split.precision(), PRECISION(kFloat));
  ASSERT_EQ(split.target(), TARGET(kARM));
}

TEST(split_arm, compute) {
  SplitCompute split;
  operators::SplitParam param;

  lite::Tensor x;
  std::vector<lite::Tensor*> output;
  std::vector<lite::Tensor*> output_ref;

  for (auto n : {1, 3, 4}) {
    for (auto c : {1, 3, 4}) {
      for (auto h : {1, 3, 4}) {
        for (auto w : {1, 3, 4}) {
          for (auto axis : {0, 1, 2, 3}) {
            for (auto num : {0, 1, 2, 3}) {
              for (auto sections :
                   {std::vector<int>{1, 1, 1}, std::vector<int>{2, 2},
                    std::vector<int>{1, 2}}) {
                auto x_dim = DDim(std::vector<int64_t>({n, c, h, w}));
                x.Resize(x_dim);
                if ((num != 0 && x_dim[axis] % num != 0) ||
                    (num == 0 && x_dim[axis] % sections.size() != 0))
                  continue;
                auto* x_data = x.mutable_data<float>();
                for (int i = 0; i < x.dims().production(); i++) {
                  x_data[i] = i;
                }
Z
zhupengyang 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
                for (auto out : output) delete out;
                for (auto out : output_ref) delete out;
                output.clear();
                output_ref.clear();

                int outs_number;
                if (num > 0) {
                  outs_number = num;
                } else {
                  outs_number = sections.size();
                }
                for (int i = 0; i < outs_number; i++) {
                  output.push_back(new lite::Tensor);
                  output_ref.push_back(new lite::Tensor);
                }
                splite_resize_out(&x, output, axis, num, sections);
                splite_resize_out(&x, output_ref, axis, num, sections);
Z
zhupengyang 已提交
139 140 141
                param.x = &x;
                param.axis = axis;
                param.num = num;
Z
zhupengyang 已提交
142 143
                param.sections = sections;
                param.output = output;
Z
zhupengyang 已提交
144 145
                split.SetParam(param);
                split.Run();
Z
zhupengyang 已提交
146
                param.output = output_ref;
Z
zhupengyang 已提交
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
                split_compute_ref<float>(param);
                for (int i = 0; i < output.size(); i++) {
                  float* output_data = output[i]->mutable_data<float>();
                  float* output_ref_data = output_ref[i]->mutable_data<float>();
                  for (int j = 0; j < output[i]->dims().production(); j++) {
                    EXPECT_NEAR(output_data[j], output_ref_data[j], 1e-5);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

TEST(split, retrive_op) {
  auto split =
      KernelRegistry::Global().Create<TARGET(kARM), PRECISION(kFloat)>("split");
  ASSERT_FALSE(split.empty());
  ASSERT_TRUE(split.front());
}

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

USE_LITE_KERNEL(split, kARM, kFloat, kNCHW, def);