|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/operators/slice_op.h" |
| 16 | +#include <algorithm> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace operators { |
| 21 | + |
| 22 | +using Tensor = framework::Tensor; |
| 23 | + |
| 24 | +class SliceOp : public framework::OperatorWithKernel { |
| 25 | + public: |
| 26 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 27 | + |
| 28 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 29 | + PADDLE_ENFORCE(ctx->HasInput("Input"), |
| 30 | + "Input (Input) of slice op should not be null."); |
| 31 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 32 | + "Output (Out) of slice op should not be null."); |
| 33 | + |
| 34 | + auto in_dims = ctx->GetInputDim("Input"); |
| 35 | + PADDLE_ENFORCE(in_dims.size() < 7, |
| 36 | + "The rank of input should be less than 7."); |
| 37 | + framework::DDim out_dims(in_dims); |
| 38 | + auto axes = ctx->Attrs().Get<std::vector<int>>("axes"); |
| 39 | + auto starts = ctx->Attrs().Get<std::vector<int>>("starts"); |
| 40 | + auto ends = ctx->Attrs().Get<std::vector<int>>("ends"); |
| 41 | + |
| 42 | + PADDLE_ENFORCE_EQ(starts.size(), ends.size()); |
| 43 | + PADDLE_ENFORCE_EQ(starts.size(), axes.size()); |
| 44 | + int dim_value, start, end; |
| 45 | + for (size_t i = 0; i < axes.size(); ++i) { |
| 46 | + dim_value = out_dims[axes[i]]; |
| 47 | + start = starts[i] < 0 ? (starts[i] + dim_value) : starts[i]; |
| 48 | + end = ends[i] < 0 ? (ends[i] + dim_value) : ends[i]; |
| 49 | + start = std::max(start, 0); |
| 50 | + end = std::max(end, 0); |
| 51 | + start = std::min(start, dim_value); |
| 52 | + end = std::min(end, dim_value); |
| 53 | + start = std::min(start, end); |
| 54 | + out_dims[axes[i]] = end - start; |
| 55 | + } |
| 56 | + ctx->SetOutputDim("Out", out_dims); |
| 57 | + } |
| 58 | + |
| 59 | + protected: |
| 60 | + framework::OpKernelType GetExpectedKernelType( |
| 61 | + const framework::ExecutionContext &ctx) const override { |
| 62 | + return framework::OpKernelType( |
| 63 | + framework::ToDataType(ctx.Input<Tensor>("Input")->type()), |
| 64 | + ctx.GetPlace()); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +class SliceOpMaker : public framework::OpProtoAndCheckerMaker { |
| 69 | + public: |
| 70 | + void Make() override { |
| 71 | + AddInput("Input", "Tensor of data to extract slices from."); |
| 72 | + AddOutput("Out", "Sliced data tensor."); |
| 73 | + |
| 74 | + AddAttr<std::vector<int>>( |
| 75 | + "axes", |
| 76 | + "(list<int>) Axes that `starts` and `ends` apply to. It's optional." |
| 77 | + "If not present, will be treated as [0, 1, ..., len(`starts`) - 1]."); |
| 78 | + AddAttr<std::vector<int>>( |
| 79 | + "starts", |
| 80 | + "(list<int>) Starting indices of corresponding axis in `axes`"); |
| 81 | + AddAttr<std::vector<int>>( |
| 82 | + "ends", |
| 83 | + "(list<int>) Starting indices of corresponding axis in `axes`."); |
| 84 | + |
| 85 | + AddComment(R"DOC( |
| 86 | +Slice Operator. |
| 87 | +
|
| 88 | +Produces a slice of the input tensor along multiple axes. Similar to numpy: |
| 89 | +https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html |
| 90 | +Slice uses `axes`, `starts` and `ends` attributes to specify the start and |
| 91 | +end dimension for each axis in the list of axes, it uses this information |
| 92 | +to slice the input data tensor. If a negative value is passed for any of |
| 93 | +the start or end indices, it represents number of elements before the end |
| 94 | +of that dimension. If the value passed to start or end is larger than |
| 95 | +the n (the number of elements in this dimension), it represents n. |
| 96 | +For slicing to the end of a dimension with unknown size, it is recommended |
| 97 | +to pass in INT_MAX. If axes are omitted, they are set to [0, ..., ndim-1]. |
| 98 | +
|
| 99 | + Example 1: |
| 100 | + Given: |
| 101 | + data = [ [1, 2, 3, 4], [5, 6, 7, 8], ] |
| 102 | + axes = [0, 1] |
| 103 | + starts = [1, 0] |
| 104 | + ends = [2, 3] |
| 105 | + Then: |
| 106 | + result = [ [5, 6, 7], ] |
| 107 | +
|
| 108 | + Example 2: |
| 109 | + Given: |
| 110 | + data = [ [1, 2, 3, 4], [5, 6, 7, 8], ] |
| 111 | + starts = [0, 1] |
| 112 | + ends = [-1, 1000] |
| 113 | + Then: |
| 114 | + result = [ [2, 3, 4], ] |
| 115 | +)DOC"); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +} // namespace operators |
| 120 | +} // namespace paddle |
| 121 | + |
| 122 | +namespace ops = paddle::operators; |
| 123 | +REGISTER_OPERATOR(slice, ops::SliceOp, ops::SliceOpMaker, |
| 124 | + paddle::framework::EmptyGradOpMaker); |
| 125 | + |
| 126 | +REGISTER_OP_CPU_KERNEL( |
| 127 | + slice, ops::SliceKernel<paddle::platform::CPUDeviceContext, int>, |
| 128 | + ops::SliceKernel<paddle::platform::CPUDeviceContext, int64_t>, |
| 129 | + ops::SliceKernel<paddle::platform::CPUDeviceContext, float>, |
| 130 | + ops::SliceKernel<paddle::platform::CPUDeviceContext, double>); |
0 commit comments