|
| 1 | + |
| 2 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. */ |
| 15 | + |
| 16 | +#include "paddle/operators/reshape_op.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | + |
| 21 | +class ReshapeOp : public framework::OperatorWithKernel { |
| 22 | + public: |
| 23 | + ReshapeOp(const std::string &type, const framework::VariableNameMap &inputs, |
| 24 | + const framework::VariableNameMap &outputs, |
| 25 | + const framework::AttributeMap &attrs) |
| 26 | + : OperatorWithKernel(type, inputs, outputs, attrs) {} |
| 27 | + |
| 28 | + protected: |
| 29 | + void InferShape(const framework::InferShapeContext &ctx) const override { |
| 30 | + // input check |
| 31 | + PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"), "Input(X) shouldn't be null"); |
| 32 | + auto shape = ctx.Attr<std::vector<int>>("shape"); |
| 33 | + PADDLE_ENFORCE(shape.size() > 0, "Attr(shape) shouldn't be empty."); |
| 34 | + for (auto dim : shape) { |
| 35 | + PADDLE_ENFORCE(dim > 0, "Each dimension of shape must be positive."); |
| 36 | + } |
| 37 | + // capacity check |
| 38 | + int64_t capacity = |
| 39 | + std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<int>()); |
| 40 | + auto *in = ctx.Input<framework::Tensor>("X"); |
| 41 | + int64_t in_size = framework::product(in->dims()); |
| 42 | + PADDLE_ENFORCE_EQ(capacity, in_size, |
| 43 | + "The size of Input(X) mismatches with Attr(shape)."); |
| 44 | + // resize output |
| 45 | + std::vector<int64_t> shape_int64(shape.size(), 0); |
| 46 | + std::transform(shape.begin(), shape.end(), shape_int64.begin(), |
| 47 | + [](int a) { return static_cast<int64_t>(a); }); |
| 48 | + auto out_dims = framework::make_ddim(shape_int64); |
| 49 | + ctx.Output<framework::Tensor>("Out")->Resize(out_dims); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +class ReshapeOpMaker : public framework::OpProtoAndCheckerMaker { |
| 54 | + public: |
| 55 | + ReshapeOpMaker(framework::OpProto *proto, |
| 56 | + framework::OpAttrChecker *op_checker) |
| 57 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 58 | + AddInput("X", "The input tensor of reshape operator."); |
| 59 | + AddOutput("Out", "The output tensor of reshape operator."); |
| 60 | + AddAttr<std::vector<int>>("shape", "Target shape of reshape operator."); |
| 61 | + AddComment(R"DOC(Reshape operator |
| 62 | +
|
| 63 | +Reshape Input(X) into the shape specified by Attr(shape). |
| 64 | +
|
| 65 | +An example: |
| 66 | +Given a 2-D tensor X with 2 rows and 2 columns |
| 67 | +
|
| 68 | + [[1, 2], [3, 4]] |
| 69 | +
|
| 70 | +with target shape = [1, 4], the reshape operator will transform |
| 71 | +the tensor X into a 1-D tensor: |
| 72 | +
|
| 73 | + [1, 2, 3, 4] |
| 74 | +
|
| 75 | +)DOC"); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +class ReshapeGradOp : public framework::OperatorWithKernel { |
| 80 | + public: |
| 81 | + ReshapeGradOp(const std::string &type, |
| 82 | + const framework::VariableNameMap &inputs, |
| 83 | + const framework::VariableNameMap &outputs, |
| 84 | + const framework::AttributeMap &attrs) |
| 85 | + : OperatorWithKernel(type, inputs, outputs, attrs) {} |
| 86 | + |
| 87 | + protected: |
| 88 | + void InferShape(const framework::InferShapeContext &ctx) const override { |
| 89 | + PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"), "Input(X) shouldn't be null."); |
| 90 | + PADDLE_ENFORCE_NOT_NULL(ctx.InputVar(framework::GradVarName("Out")), |
| 91 | + "Input(Out@GRAD) shouldn't be null."); |
| 92 | + auto dims = ctx.Input<framework::Tensor>("X")->dims(); |
| 93 | + auto *d_in = ctx.Output<framework::Tensor>(framework::GradVarName("X")); |
| 94 | + d_in->Resize(dims); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +} // namespace operators |
| 99 | +} // namespace paddle |
| 100 | +namespace ops = paddle::operators; |
| 101 | + |
| 102 | +REGISTER_OP(reshape, ops::ReshapeOp, ops::ReshapeOpMaker, reshape_grad, |
| 103 | + ops::ReshapeGradOp); |
| 104 | +REGISTER_OP_CPU_KERNEL(reshape, |
| 105 | + ops::ReshapeKernel<paddle::platform::CPUPlace, float>); |
| 106 | +REGISTER_OP_CPU_KERNEL( |
| 107 | + reshape_grad, ops::ReshapeGradKernel<paddle::platform::CPUPlace, float>); |
0 commit comments