space_to_depth_op.cc 7.5 KB
Newer Older
J
JiabinYang 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
J
JiabinYang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

J
JiabinYang 已提交
15
#include "paddle/fluid/operators/space_to_depth_op.h"
H
Huihuang Zheng 已提交
16 17

#include <memory>
J
JiabinYang 已提交
18 19 20
#include <string>
#include <vector>

H
Huihuang Zheng 已提交
21 22
#include "paddle/fluid/framework/no_need_buffer_vars_inference.h"

J
JiabinYang 已提交
23 24 25
namespace paddle {
namespace operators {

H
Huihuang Zheng 已提交
26 27
using Tensor = framework::Tensor;

J
JiabinYang 已提交
28
class SpaceToDepthOp : public framework::OperatorWithKernel {
J
JiabinYang 已提交
29 30 31 32 33
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
J
JiabinYang 已提交
34
                   "Input(X) of SpaceToDepthOp should not be null.");
J
JiabinYang 已提交
35
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
J
JiabinYang 已提交
36
                   "Output(Out) of SpaceToDepthOp should not be null.");
J
JiabinYang 已提交
37 38 39

    auto x_dims = ctx->GetInputDim("X");
    PADDLE_ENFORCE_EQ(x_dims.size(), 4, "input should be a 4D tensor");
J
JiabinYang 已提交
40
    auto blocksize = ctx->Attrs().Get<int64_t>("blocksize");
J
JiabinYang 已提交
41

J
JiabinYang 已提交
42
    PADDLE_ENFORCE_GT(blocksize, 1, "The blocksize should be Greater than 1");
P
phlrain 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    if (ctx->IsRuntime()) {
      PADDLE_ENFORCE_GT(x_dims[1], 0, "input channel should be Greater than 0");
      PADDLE_ENFORCE_GT(x_dims[2], 0, "input Height should be Greater than 0");
      PADDLE_ENFORCE_GT(x_dims[3], 0, "input Width should be Greater than 0");

      PADDLE_ENFORCE_EQ(x_dims[1] % (blocksize * blocksize), 0,
                        "input channel should be divisible of the square of "
                        "SpaceToDepthOp blocksize");
      PADDLE_ENFORCE_EQ(x_dims[2] % (blocksize), 0,
                        "input Height should be divisible of the square of "
                        "SpaceToDepthOp blocksize");
      PADDLE_ENFORCE_EQ(x_dims[3] % (blocksize), 0,
                        "input Width should be divisible of the square of "
                        "SpaceToDepthOp blocksize");
    } else {
      if (x_dims[1] != -1) {
        PADDLE_ENFORCE_GT(x_dims[1], 0,
                          "input channel should be Greater than 0");
        PADDLE_ENFORCE_EQ(x_dims[1] % (blocksize * blocksize), 0,
                          "input channel should be divisible of the square of "
                          "SpaceToDepthOp blocksize");
      }
      if (x_dims[2] != -1) {
        PADDLE_ENFORCE_GT(x_dims[2], 0,
                          "input Height should be Greater than 0");
        PADDLE_ENFORCE_EQ(x_dims[2] % (blocksize), 0,
                          "input Height should be divisible of the square of "
                          "SpaceToDepthOp blocksize");
      }

      if (x_dims[3] != -1) {
        PADDLE_ENFORCE_GT(x_dims[3], 0, "input Width should be Greater than 0");

        PADDLE_ENFORCE_EQ(x_dims[3] % (blocksize), 0,
                          "input Width should be divisible of the square of "
                          "SpaceToDepthOp blocksize");
      }
    }
J
JiabinYang 已提交
81

J
JiabinYang 已提交
82
    VLOG(3) << "SpaceToDepthOp operator x.shape=" << x_dims
J
JiabinYang 已提交
83
            << "Attribute blocksize" << blocksize << std::endl;
J
JiabinYang 已提交
84 85 86

    std::vector<int64_t> output_shape(4, 0);  // [B,C,H,W]
    output_shape[0] = x_dims[0];
J
JiabinYang 已提交
87 88 89
    output_shape[1] = x_dims[1] * blocksize * blocksize;
    output_shape[2] = x_dims[2] / blocksize;
    output_shape[3] = x_dims[3] / blocksize;
J
JiabinYang 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102

    auto out_dims = framework::make_ddim(output_shape);

    ctx->SetOutputDim("Out", out_dims);

    if (x_dims[0] == out_dims[0]) {
      // Only pass LoD when the first dimension of output and Input(X)
      // are the same.
      ctx->ShareLoD("X", /*->*/ "Out");
    }
  }
};

J
JiabinYang 已提交
103
class SpaceToDepthOpMaker : public framework::OpProtoAndCheckerMaker {
J
JiabinYang 已提交
104 105 106
 public:
  void Make() override {
    AddInput("X",
J
JiabinYang 已提交
107 108
             "(Tensor). The input should be a 4D tensor B * C * W * H of "
             "SpaceToDepthOp "
J
JiabinYang 已提交
109 110 111
             "operator.");
    AddOutput("Out",
              "(Tensor), The output should be a 4D tensor B * C2 * W2 * H2 of "
J
JiabinYang 已提交
112 113
              "SpaceToDepthOp operator.");
    AddAttr<int64_t>(
J
JiabinYang 已提交
114 115
        "blocksize",
        "(int64_t, default 2) blocksize used to do change Space To Depth.")
J
JiabinYang 已提交
116 117
        .SetDefault(2)
        .GreaterThan(1);
J
JiabinYang 已提交
118 119
    AddComment(R"DOC(
        reorg operator used in Yolo v2.
W
wopeizl 已提交
120
        The equation is: C2 = C1/blocksize * blocksize, W2 = W1 * blocksize + offset % blocksize, H2 = H1 * blocksize + offset / blocksize,
J
JiabinYang 已提交
121

J
JiabinYang 已提交
122
        Reshape Input(X) into the shape according to Attr(blocksize). The
J
JiabinYang 已提交
123 124 125 126
        data in Input(X) are unchanged.

        Examples:

J
JiabinYang 已提交
127
            1. Given a 4-D tensor Input(X) with a shape [128, 2048, 26, 26], and the blocksize is 2, the reorg operator will transform Input(X)
J
JiabinYang 已提交
128
            into a 4-D tensor with shape [128, 2048, 13, 13] and leaving Input(X)'s data unchanged.
J
JiabinYang 已提交
129 130 131 132 133

    )DOC");
  }
};

Z
Zeng Jinle 已提交
134
DECLARE_NO_NEED_BUFFER_VARS_INFERER(SpaceToDepthGradOpNoBuffer, "X");
H
Huihuang Zheng 已提交
135

H
hong 已提交
136 137
template <typename T>
class SpaceToDepthGradOpMaker : public framework::SingleGradOpMaker<T> {
H
Huihuang Zheng 已提交
138
 public:
H
hong 已提交
139
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
H
Huihuang Zheng 已提交
140 141

 protected:
142
  void Apply(GradOpPtr<T> op) const override {
H
Huihuang Zheng 已提交
143 144
    op->SetType("space_to_depth_grad");

H
hong 已提交
145 146
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetInput("X", this->Input("X"));
H
Huihuang Zheng 已提交
147

H
hong 已提交
148
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
H
Huihuang Zheng 已提交
149

H
hong 已提交
150
    op->SetAttrMap(this->Attrs());
H
Huihuang Zheng 已提交
151 152 153
  }
};

J
JiabinYang 已提交
154
class SpaceToDepthGradOp : public framework::OperatorWithKernel {
J
JiabinYang 已提交
155 156 157 158 159 160 161 162 163
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) shouldn't be null.");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
                   "Input(Out@GRAD) shouldn't be null.");
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
  }
H
Huihuang Zheng 已提交
164 165 166 167

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
168 169 170
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.GetPlace());
H
Huihuang Zheng 已提交
171
  }
J
JiabinYang 已提交
172 173 174 175 176 177
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

J
JiabinYang 已提交
178
REGISTER_OPERATOR(space_to_depth, ops::SpaceToDepthOp, ops::SpaceToDepthOpMaker,
H
hong 已提交
179 180
                  ops::SpaceToDepthGradOpMaker<paddle::framework::OpDesc>,
                  ops::SpaceToDepthGradOpMaker<paddle::imperative::OpBase>);
H
Huihuang Zheng 已提交
181 182
REGISTER_OPERATOR(space_to_depth_grad, ops::SpaceToDepthGradOp,
                  ops::SpaceToDepthGradOpNoBuffer);
J
JiabinYang 已提交
183
REGISTER_OP_CPU_KERNEL(
J
JiabinYang 已提交
184 185 186
    space_to_depth,
    ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, float>,
    ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, double>,
187
    ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, int>,
J
JiabinYang 已提交
188
    ops::SpaceToDepthKernel<paddle::platform::CPUDeviceContext, int64_t>);
J
JiabinYang 已提交
189
REGISTER_OP_CPU_KERNEL(
J
JiabinYang 已提交
190 191 192
    space_to_depth_grad,
    ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, float>,
    ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, double>,
193
    ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, int>,
J
JiabinYang 已提交
194
    ops::SpaceToDepthGradKernel<paddle::platform::CPUDeviceContext, int64_t>);