shuffle_channel_op.cc 4.8 KB
Newer Older
S
shippingwang 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*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. */

#include "paddle/fluid/operators/shuffle_channel_op.h"
S
sneaxiy 已提交
13
#include <memory>
S
shippingwang 已提交
14 15 16 17 18 19 20 21 22

namespace paddle {
namespace operators {

class ShuffleChannelOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
S
shippingwang 已提交
23
    PADDLE_ENFORCE(ctx->HasInput("X"),
S
shippingwang 已提交
24
                   "Input(X) of ShuffleChannelOp should not be null.");
S
shippingwang 已提交
25
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
S
shippingwang 已提交
26 27 28 29 30 31 32
                   "Output(Out) of ShuffleChannelOp should not be null.");

    auto input_dims = ctx->GetInputDim("X");
    PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");

    ctx->SetOutputDim("Out", input_dims);
  }
S
shippingwang 已提交
33 34 35 36 37 38 39

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(ctx.Input<framework::Tensor>("X")->type(),
                                   ctx.device_context());
  }
S
shippingwang 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
};

class ShuffleChannelOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X",
             "(Tensor, default Tensor<float>), "
             "the input feature data of ShuffleChannelOp, the layout is NCHW.");
    AddOutput("Out",
              "(Tensor, default Tensor<float>), the output of "
              "ShuffleChannelOp. The layout is NCHW.");
    AddAttr<int>("group", "the number of groups.")
        .SetDefault(1)
        .AddCustomChecker([](const int& group) {
          PADDLE_ENFORCE_GE(group, 1, "group should be larger than 0.");
        });

    AddComment(R"DOC(
		Shuffle Channel operator
S
shippingwang 已提交
59 60 61
		This opearator shuffles the channels of input x.
		It  divide the input channels in each group into several subgroups,
		and obtain a new order by selecting element from every subgroup one by one.
S
shippingwang 已提交
62 63 64 65 66 67 68 69 70

		Shuffle channel operation makes it possible to build more powerful structures
		with multiple group convolutional layers.
		please get more information from the following paper:
		https://arxiv.org/pdf/1707.01083.pdf
        )DOC");
  }
};

S
shippingwang 已提交
71
class ShuffleChannelGradOp : public framework::OperatorWithKernel {
S
shippingwang 已提交
72 73 74 75 76
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
S
shippingwang 已提交
77
                   "Input(Out@Grad) should not be null");
S
shippingwang 已提交
78 79 80 81
    PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
                   "Output(X@Grad) should not be null");

    auto input_dims = ctx->GetInputDim("X");
S
shippingwang 已提交
82 83
    PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");

S
shippingwang 已提交
84 85
    ctx->SetOutputDim(framework::GradVarName("X"), input_dims);
  }
S
shippingwang 已提交
86 87 88 89 90 91 92

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(ctx.Input<framework::Tensor>("X")->type(),
                                   ctx.device_context());
  }
S
shippingwang 已提交
93 94
};

S
sneaxiy 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
class ShuffleChannelGradDescMaker : public framework::SingleGradOpDescMaker {
 public:
  using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;

 protected:
  std::unique_ptr<framework::OpDesc> Apply() const override {
    std::unique_ptr<framework::OpDesc> op(new framework::OpDesc());
    op->SetType("shuffle_channel_grad");
    op->SetInput("X", Input("X"));
    op->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), InputGrad("X"));
    op->SetAttrMap(Attrs());
    return op;
  }
};

S
shippingwang 已提交
111 112 113 114
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
S
shippingwang 已提交
115
REGISTER_OPERATOR(shuffle_channel, ops::ShuffleChannelOp,
S
sneaxiy 已提交
116
                  ops::ShuffleChannelOpMaker, ops::ShuffleChannelGradDescMaker);
S
shippingwang 已提交
117

S
shippingwang 已提交
118
REGISTER_OPERATOR(shuffle_channel_grad, ops::ShuffleChannelGradOp);
S
shippingwang 已提交
119 120

REGISTER_OP_CPU_KERNEL(
S
shippingwang 已提交
121
    shuffle_channel,
S
shippingwang 已提交
122 123 124 125
    ops::ShuffleChannelOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::ShuffleChannelOpKernel<paddle::platform::CPUDeviceContext, double>);

REGISTER_OP_CPU_KERNEL(
S
shippingwang 已提交
126
    shuffle_channel_grad,
S
shippingwang 已提交
127 128 129
    ops::ShuffleChannelGradOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::ShuffleChannelGradOpKernel<paddle::platform::CPUDeviceContext,
                                    double>);