slice_op.cpp 3.6 KB
Newer Older
T
Tian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2018 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. */

#ifdef SLICE_OP

#include "operators/slice_op.h"
Y
Yanzhan Yang 已提交
18
#include <algorithm>
T
Tian 已提交
19
#include <vector>
Y
Yanzhan Yang 已提交
20

T
Tian 已提交
21
namespace paddle_mobile {
I
itminner 已提交
22 23 24 25
namespace operators {

template <typename Dtype, typename T>
void SliceOp<Dtype, T>::InferShape() const {
Z
zp7 已提交
26 27 28
  auto axes = this->param_.axes_;
  auto input = this->param_.input_;
  auto output = this->param_.output_;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#ifdef PADDLE_MOBILE_CL
  auto output_dims = output->dims();
  auto output_dims_size = output_dims.size();
  bool should_resize = true;
  if (output_dims_size > 4) {
    for (int i = 0; i < output_dims_size - 4; ++i) {
      if (output_dims[i] != 0 && output_dims[i] != 1) {
        should_resize = false;
        break;
      }
    }
    if (should_resize) {
      std::vector<int64_t> temp_output_dims;
      temp_output_dims.reserve(static_cast<size_t>(4));
      for (int i = output_dims_size - 4; i < output_dims_size; ++i) {
        temp_output_dims.push_back(output_dims[i]);
      }
      framework::DDim temp_ddim = framework::make_ddim(temp_output_dims);
      this->param_.output_->Resize(temp_ddim);
    }
  }
#endif
Z
zp7 已提交
51 52 53
  PADDLE_MOBILE_ENFORCE(axes.size() == 1, "axes size should equals 1");
  PADDLE_MOBILE_ENFORCE(input->dims().size() == output->dims().size(),
                        "input dim size should equals output dim size");
54
  PADDLE_MOBILE_ENFORCE(
Y
Yanzhan Yang 已提交
55
      output->dims().size() -
56 57 58 59
              (axes[0] - (this->param_.original_output_dims_size_ -
                          this->param_.output_->dims().size())) ==
          3,
      "op only support slice channel now");
60 61 62 63 64 65 66 67 68
  auto starts = this->param_.starts_;
  auto ends = this->param_.ends_;
  framework::DDim out_dims(input->dims());
  PADDLE_MOBILE_ENFORCE(starts.size() == ends.size(),
                        "starts.size should equal ends.size");
  PADDLE_MOBILE_ENFORCE(axes.size() == starts.size(),
                        "axes.size should equal starts.size");
  int dim_value, start, end;
  for (size_t i = 0; i < axes.size(); ++i) {
Y
Yanzhan Yang 已提交
69 70 71
    int axis = axes[i] - (this->param_.original_output_dims_size_ -
                          this->param_.output_->dims().size());
    dim_value = out_dims[axis];
72 73 74 75 76 77 78 79 80
    if (dim_value > 0) {
      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);
      // start = std::min(start, dim_value);
      end = std::min(end, dim_value);
      // start = std::min(start, end);
      PADDLE_MOBILE_ENFORCE(end > start, "end should greater than start");
Y
Yanzhan Yang 已提交
81
      out_dims[axis] = end - start;
82 83 84
    }
  }
  output->Resize(out_dims);
85
#if !defined(PADDLE_MOBILE_CL) && defined(PADDLE_MOBILE_CPU)
86 87 88
  if (axes[0] != 0) {
    output->set_lod(input->lod());
  }
Z
zp7 已提交
89
#endif
I
itminner 已提交
90
}
L
liuruilong 已提交
91

I
itminner 已提交
92
}  // namespace operators
T
Tian 已提交
93 94 95 96 97 98
}  // namespace paddle_mobile

namespace ops = paddle_mobile::operators;
#ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU(slice, ops::SliceOp);
#endif
Z
zhangyang 已提交
99 100 101
#ifdef PADDLE_MOBILE_FPGA
REGISTER_OPERATOR_FPGA(slice, ops::SliceOp);
#endif
102 103
#ifdef PADDLE_MOBILE_CL
REGISTER_OPERATOR_CL(slice, ops::SliceOp);
T
Tian 已提交
104
#endif
105
#endif  // SLICE_OP