|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 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/operators/pool_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +int OutputSizePool(int input_size, int filter_size, int padding, int stride) { |
| 21 | + int output_size = (input_size - filter_size + 2 * padding) / stride + 1; |
| 22 | + return output_size; |
| 23 | +} |
| 24 | + |
| 25 | +class PoolOp : public framework::OperatorWithKernel { |
| 26 | + public: |
| 27 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 28 | + |
| 29 | + protected: |
| 30 | + void InferShape(framework::InferShapeContextBase *ctx) const override { |
| 31 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 32 | + "X(Input) of Pooling should not be null."); |
| 33 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 34 | + "Out(Output) of Pooling should not be null."); |
| 35 | + |
| 36 | + auto in_x_dims = ctx->GetInputDim("X"); |
| 37 | + |
| 38 | + std::string pooling_type = ctx->Attrs().Get<std::string>("poolingType"); |
| 39 | + std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize"); |
| 40 | + std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides"); |
| 41 | + std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings"); |
| 42 | + |
| 43 | + PADDLE_ENFORCE(pooling_type == "max" || pooling_type == "avg", |
| 44 | + "pooling_type should be 'max' or 'avg'"); |
| 45 | + PADDLE_ENFORCE(in_x_dims.size() == 4 || in_x_dims.size() == 5, |
| 46 | + "Pooling intput should be 4-D or 5-D"); |
| 47 | + |
| 48 | + if (ctx->Attrs().Get<bool>("globalPooling")) { |
| 49 | + ksize.resize(static_cast<size_t>(in_x_dims.size()) - 2); |
| 50 | + for (size_t i = 0; i < ksize.size(); ++i) |
| 51 | + ksize[i] = static_cast<int>(in_x_dims[i + 2]); |
| 52 | + } |
| 53 | + |
| 54 | + PADDLE_ENFORCE(in_x_dims.size() - ksize.size() == 2U, |
| 55 | + "Input size and Pooling size should be consistent."); |
| 56 | + PADDLE_ENFORCE(ksize.size() == 2 || ksize.size() == 3, |
| 57 | + "Pooling size should be 2 elements. or 3 elements."); |
| 58 | + PADDLE_ENFORCE_EQ(ksize.size(), strides.size(), |
| 59 | + "strides size and pooling size should be the same."); |
| 60 | + PADDLE_ENFORCE_EQ(ksize.size(), paddings.size(), |
| 61 | + "paddings size and pooling size should be the same."); |
| 62 | + |
| 63 | + std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]}); |
| 64 | + for (size_t i = 0; i < ksize.size(); ++i) { |
| 65 | + output_shape.push_back( |
| 66 | + OutputSizePool(in_x_dims[i + 2], ksize[i], paddings[i], strides[i])); |
| 67 | + } |
| 68 | + ctx->SetOutputDim("Out", framework::make_ddim(output_shape)); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +class PoolOpGrad : public framework::OperatorWithKernel { |
| 73 | + public: |
| 74 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 75 | + |
| 76 | + protected: |
| 77 | + void InferShape(framework::InferShapeContextBase *ctx) const override { |
| 78 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 79 | + "X(Input) of Pooling should not be null."); |
| 80 | + PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")), |
| 81 | + "Input@Grad of Pooling should not be null."); |
| 82 | + ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker { |
| 87 | + public: |
| 88 | + Pool2dOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) |
| 89 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 90 | + AddInput( |
| 91 | + "X", |
| 92 | + "The input tensor of pooling operator. " |
| 93 | + "The format of input tensor is NCHW. Where N is batch size, C is the " |
| 94 | + "number of channels, H and W is the height and width of feature."); |
| 95 | + AddOutput("Out", |
| 96 | + "The output tensor of pooling operator." |
| 97 | + "The format of output tensor is also NCHW."); |
| 98 | + |
| 99 | + AddAttr<std::string>("poolingType", |
| 100 | + "PoolingType of pooling operator." |
| 101 | + "Str constant equal to 'max' or 'avg'.") |
| 102 | + .InEnum({"max", "avg"}); |
| 103 | + AddAttr<std::vector<int>>( |
| 104 | + "ksize", |
| 105 | + "Pooling size(depth, height, width) of pooling operator." |
| 106 | + "If globalPooling = true, ksize is ignored and need not be " |
| 107 | + "specified."); // TODO(Add checker) |
| 108 | + AddAttr<bool>( |
| 109 | + "globalPooling", |
| 110 | + "Whether to use the globalPooling." |
| 111 | + "Bool constant equal to false or true." |
| 112 | + "Default false." |
| 113 | + "If globalPooling = true, ksize is ignored and need not be specified.") |
| 114 | + .SetDefault(false); |
| 115 | + AddAttr<std::vector<int>>("strides", |
| 116 | + "Strides(height, width) of pooling operator." |
| 117 | + "Default {1,1}") |
| 118 | + .SetDefault({1, 1}); // TODO(Add checker) |
| 119 | + AddAttr<std::vector<int>>("paddings", |
| 120 | + "Paddings(height, width) of pooling operator." |
| 121 | + "Default {0,0}.") |
| 122 | + .SetDefault({0, 0}); // TODO(Add checker) |
| 123 | + AddComment(R"DOC( |
| 124 | +The pooling2d operation calculates the output based on |
| 125 | +the input, poolingType and ksize, strides, paddings parameters. |
| 126 | +)DOC"); |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker { |
| 131 | + public: |
| 132 | + Pool3dOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) |
| 133 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 134 | + AddInput("X", |
| 135 | + "The input tensor of pooling operator. " |
| 136 | + "The format of input tensor is NCDHW. Where N is batch size, C is " |
| 137 | + "the " |
| 138 | + "number of channels, D, H and W is the depth, height and width of " |
| 139 | + "feature."); |
| 140 | + AddOutput("Out", |
| 141 | + "The output tensor of pooling operator." |
| 142 | + "The format of output tensor is also NCDHW."); |
| 143 | + |
| 144 | + AddAttr<std::string>("poolingType", |
| 145 | + "PoolingType of pooling operator." |
| 146 | + "str constant equal to 'max' or 'avg'.") |
| 147 | + .InEnum({"max", "avg"}); |
| 148 | + AddAttr<std::vector<int>>( |
| 149 | + "ksize", |
| 150 | + "Pooling size(depth, height, width) of pooling operator." |
| 151 | + "If globalPooling = true, ksize is ignored and need not be " |
| 152 | + "specified."); // TODO(Add checker) |
| 153 | + AddAttr<bool>( |
| 154 | + "globalPooling", |
| 155 | + "Whether to use the globalPooling." |
| 156 | + "Bool constant equal to false or true." |
| 157 | + "Default false." |
| 158 | + "If globalPooling = true, ksize is ignored and need not be specified.") |
| 159 | + .SetDefault(false); |
| 160 | + AddAttr<std::vector<int>>( |
| 161 | + "strides", |
| 162 | + "Strides(depth, height, width) of pooling operator." |
| 163 | + "Default {1,1,1}.") |
| 164 | + .SetDefault({1, 1, 1}); // TODO(Add checker) |
| 165 | + AddAttr<std::vector<int>>( |
| 166 | + "paddings", |
| 167 | + "Paddings(depth, height, width) of pooling operator." |
| 168 | + "Default {0,0,0}.") |
| 169 | + .SetDefault({0, 0, 0}); // TODO(Add checker) |
| 170 | + AddComment(R"DOC( |
| 171 | +The pooling3d operation calculates the output based on |
| 172 | +the input, poolingType and ksize, strides, paddings parameters. |
| 173 | +)DOC"); |
| 174 | + } |
| 175 | +}; |
| 176 | +} // namespace operators |
| 177 | +} // namespace paddle |
| 178 | + |
| 179 | +namespace ops = paddle::operators; |
| 180 | + |
| 181 | +REGISTER_OP(pool2d, ops::PoolOp, ops::Pool2dOpMaker, pool2d_grad, |
| 182 | + ops::PoolOpGrad); |
| 183 | + |
| 184 | +REGISTER_OP_CPU_KERNEL(pool2d, |
| 185 | + ops::PoolKernel<paddle::platform::CPUPlace, float>); |
| 186 | +REGISTER_OP_CPU_KERNEL(pool2d_grad, |
| 187 | + ops::PoolGradKernel<paddle::platform::CPUPlace, float>) |
| 188 | + |
| 189 | +REGISTER_OP(pool3d, ops::PoolOp, ops::Pool3dOpMaker, pool3d_grad, |
| 190 | + ops::PoolOpGrad); |
| 191 | + |
| 192 | +REGISTER_OP_CPU_KERNEL(pool3d, |
| 193 | + ops::PoolKernel<paddle::platform::CPUPlace, float>); |
| 194 | +REGISTER_OP_CPU_KERNEL(pool3d_grad, |
| 195 | + ops::PoolGradKernel<paddle::platform::CPUPlace, float>); |
0 commit comments