fusion_conv_inception_op.cc 4.8 KB
Newer Older
Q
qingqing01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 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 <string>
#include <vector>
17

Q
qingqing01 已提交
18
#include "paddle/fluid/framework/op_registry.h"
19
#include "paddle/phi/backends/gpu/cuda/cudnn_workspace_helper.h"
Q
qingqing01 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32

namespace paddle {
namespace operators {

class ConvInceptionFusionOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
    // 1 x
    auto in_dims = ctx->GetInputDim("Input");
    // 4 filters
    auto w_dims = ctx->GetInputsDim("Filter");

33
    PADDLE_ENFORCE_EQ(
34 35
        in_dims.size(),
        4,
36
        platform::errors::InvalidArgument("Conv intput should be 4-D tensor."));
37
    PADDLE_ENFORCE_EQ(
38 39
        w_dims.size(),
        4,
40
        platform::errors::InvalidArgument("There should be 4 filters."));
41 42
    PADDLE_ENFORCE_EQ(w_dims[0][1],
                      in_dims[1],
43 44 45
                      platform::errors::InvalidArgument(
                          "Invalid fileter channel number %d, which should be "
                          "equal to input channel number %d.",
46 47 48 49
                          w_dims[0][1],
                          in_dims[1]));
    PADDLE_ENFORCE_EQ(w_dims[1][1],
                      in_dims[1],
50 51 52
                      platform::errors::InvalidArgument(
                          "Invalid fileter channel number %d, which should be "
                          "equal to input channel number %d.",
53 54
                          w_dims[1][1],
                          in_dims[1]));
Q
qingqing01 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    int n = in_dims[0];
    // compute output channel
    // 1st channel
    int c = w_dims[0][0];
    // add 2nd channel
    c += (w_dims[1][0] - w_dims[2][1] * 2);
    // add 3rd channel
    c += (w_dims[2][0] - w_dims[3][1]);
    // add 4-th channel
    c += w_dims[3][0];

    int h = in_dims[2];
    int w = in_dims[3];

    ctx->SetOutputDim("Output", {n, c, h, w});
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
77 78
        OperatorWithKernel::IndicateVarDataType(ctx, "Input"),
        ctx.device_context());
Q
qingqing01 已提交
79 80 81 82 83 84 85 86
  }
};

class ConvInceptionFusionOpMaker : public framework::OpProtoAndCheckerMaker {
 protected:
  void Make() override {
    AddInput("Input", "(Tensor) NCHW layout.");
    AddInput("Filter", "(vector<Tensor>) 4 aggregated filters").AsDuplicable();
翟飞跃 已提交
87
    AddInput("Bias", "(vector<Tensor>) it's length is equal to Filter")
Q
qingqing01 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101
        .AsDuplicable();
    AddOutput("Output",
              "(Tensor) The output tensor of convolution operator. "
              "The format of output tensor is also NCHW.");
    AddOutput("TempOutput", "").AsDuplicable();
    AddAttr<std::string>(
        "pooling_type",
        "(string), pooling type, can be \"max\" for max-pooling "
        "and \"avg\" for average-pooling.")
        .InEnum({"max", "avg"});
    AddAttr<bool>(
        "exclusive",
        "(bool, default True) When true, will exclude the zero-padding in the "
        "averaging calculating, otherwise, include the zero-padding. Note, it "
翟飞跃 已提交
102
        "is only used when pooling_type is avg. The default is True.")
Q
qingqing01 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
        .SetDefault(true);
    AddAttr<std::string>(
        "activation",
        "The activation type can be 'identity', 'sigmoid', 'relu', 'relu6' "
        "'relux' , 'tanh', 'band_pass'")
        .SetDefault("relu");
    AddAttr<int>("workspace_size_MB",
                 "Only used in cudnn kernel. Need set use_cudnn to true."
                 "workspace size for cudnn, in MB, "
                 "workspace is a section of GPU memory which will be "
                 "allocated/freed each time the operator runs, larger "
                 "workspace size can increase performance but also requires "
                 "better hardware. This size should be chosen carefully.")
116
        .SetDefault(phi::backends::gpu::GetDefaultConvWorkspaceSizeLimitMB());
Q
qingqing01 已提交
117 118 119 120 121 122 123 124 125
    AddComment(R"DOC(
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
126
REGISTER_OPERATOR(
127 128
    conv2d_inception_fusion,
    ops::ConvInceptionFusionOp,
H
hong 已提交
129 130 131
    ops::ConvInceptionFusionOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);