slice_compute.cc 6.0 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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/slice_compute.h"
15
#include <algorithm>
Y
Yan Chunwei 已提交
16
#include <vector>
17
#include "lite/backends/arm/math/funcs.h"
Y
Yan Chunwei 已提交
18 19 20 21 22 23

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

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
inline std::vector<int32_t> get_new_data_from_tensorlist(
    const std::vector<lite::Tensor*>& list_new_data_tensor) {
  // get tensor
  std::vector<int32_t> vec_new_data;
  for (size_t i = 0; i < list_new_data_tensor.size(); ++i) {
    auto tensor = list_new_data_tensor[i];
    CHECK_EQ(tensor->dims(), DDim({1})) << "shape of dim tensor should be [1]";
    vec_new_data.push_back(static_cast<int32_t>(*tensor->data<int32_t>()));
  }
  return vec_new_data;
}

inline std::vector<int32_t> get_new_data_from_tensor(
    const lite::Tensor* new_data_tensor) {
  std::vector<int32_t> vec_new_data;
  auto* new_data = new_data_tensor->data<int32_t>();
  vec_new_data =
      std::vector<int32_t>(new_data, new_data + new_data_tensor->numel());
  return vec_new_data;
}

Y
Yan Chunwei 已提交
45 46 47 48 49 50
void SliceCompute::PrepareForRun() {}

void SliceCompute::Run() {
  auto& ctx = this->ctx_->template As<ARMContext>();
  auto& param = this->Param<operators::SliceParam>();

51 52 53 54
  auto in = param.X;
  auto in_dims = in->dims();
  auto out = param.Out;
  auto out_dims = out->dims();
Y
Yan Chunwei 已提交
55 56

  std::vector<int> axes = param.axes;
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 98 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
  std::vector<int32_t> starts = param.starts;
  std::vector<int32_t> ends = param.ends;
  std::vector<int> decrease_axis = param.decrease_axis;
  std::vector<int> infer_flags = param.infer_flags;

  auto list_new_ends_tensor = param.EndsTensorList;
  auto list_new_starts_tensor = param.StartsTensorList;

  bool need_infer = false;
  if (param.StartsTensor || param.EndsTensor) {
    need_infer = true;
  }
  if (list_new_starts_tensor.size() > 0 || list_new_ends_tensor.size() > 0) {
    need_infer = true;
  }
  if (need_infer) {
    if (param.StartsTensor) {
      starts = get_new_data_from_tensor(param.StartsTensor);
    } else if (list_new_starts_tensor.size() > 0) {
      starts = get_new_data_from_tensorlist(list_new_starts_tensor);
    }
    CHECK_EQ(starts.size(), axes.size())
        << "The size of starts must be equal to the size of axes.";
    if (param.EndsTensor) {
      ends = get_new_data_from_tensor(param.EndsTensor);
    } else if (list_new_ends_tensor.size() > 0) {
      ends = get_new_data_from_tensorlist(list_new_ends_tensor);
    }
    CHECK_EQ(ends.size(), axes.size())
        << "The size of ends must be equal to the size of axes.";
    out_dims = in_dims;
    int dim_value, start, end;
    for (size_t i = 0; i < axes.size(); ++i) {
      dim_value = out_dims[axes[i]];
      if (dim_value > 0) {
        // when end = start+1 and start == -1
        if (starts[i] == -1 && ends[i] == 0 && infer_flags[i] == -1) {
          auto ret =
              std::find(decrease_axis.begin(), decrease_axis.end(), axes[i]);
          if (ret != decrease_axis.end()) {
            ends[i] = 10000000;
          }
        }

        start = starts[i] < 0 ? (starts[i] + dim_value) : starts[i];
        end = ends[i] < 0 ? (ends[i] + dim_value) : ends[i];
        start = std::max(start, 0);
        end = std::max(end, 0);
        end = std::min(end, dim_value);
        CHECK_GT(end, start) << "end should greater than start";
        out_dims[axes[i]] = end - start;
      }
    }
    out->Resize(out_dims);
    // generate new shape
    if (decrease_axis.size() > 0) {
      std::vector<int64_t> new_out_shape;
      for (size_t i = 0; i < decrease_axis.size(); ++i) {
        CHECK_EQ(out_dims[decrease_axis[i]], 1) << "decrease dim should be 1";
        out_dims[decrease_axis[i]] = 0;
      }

      for (int i = 0; i < out_dims.size(); ++i) {
        if (out_dims[i] != 0) {
          new_out_shape.push_back(out_dims[i]);
        }
      }
      if (new_out_shape.size() == 0) {
        new_out_shape.push_back(1);
      }
      DDim new_dims;
      new_dims.ConstructFrom(new_out_shape);
      out_dims = new_dims;
    }
  }

  // resize out dims
  if (decrease_axis.size() > 0) {
    if (decrease_axis.size() == (size_t)in_dims.size()) {
      std::vector<int64_t> vec_origin_out_shape(decrease_axis.size(), 1);
      out->Resize(DDim(vec_origin_out_shape));
    } else {
      std::vector<int64_t> vec_origin_out_shape(
          out_dims.size() + decrease_axis.size(), -1);

      for (size_t i = 0; i < decrease_axis.size(); ++i) {
        vec_origin_out_shape[decrease_axis[i]] = 1;
      }

      int index = 0;
      for (size_t i = 0; i < vec_origin_out_shape.size(); ++i) {
        if (vec_origin_out_shape[i] == -1) {
          vec_origin_out_shape[i] = out_dims[index];
          ++index;
        }
      }

      out->Resize(DDim(vec_origin_out_shape));
    }
  }

  auto new_out_dims = out->dims();
  const auto* x_data = in->data<int>();
  auto* o_data = out->mutable_data<int>();
Y
Yan Chunwei 已提交
161
  lite::arm::math::slice(
162
      x_data, in_dims.data(), axes, starts, ends, o_data, &ctx);
Y
Yan Chunwei 已提交
163 164 165 166 167 168 169 170 171 172
}

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

REGISTER_LITE_KERNEL(
    slice, kARM, kFloat, kNCHW, paddle::lite::kernels::arm::SliceCompute, def)
    .BindInput("Input", {LiteType::GetTensorTy(TARGET(kARM))})
173 174 175 176
    .BindInput("StartsTensor", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("EndsTensor", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("StartsTensorList", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("EndsTensorList", {LiteType::GetTensorTy(TARGET(kARM))})
Y
Yan Chunwei 已提交
177 178
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
    .Finalize();