fsp_op.cc 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2019 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/fsp_op.h"
16
#include <memory>
17 18 19 20 21 22 23 24 25

namespace paddle {
namespace operators {

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

  void InferShape(framework::InferShapeContext* ctx) const override {
26 27 28
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "fsp");
    OP_INOUT_CHECK(ctx->HasInput("Y"), "Input", "Y", "fsp");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "fsp");
29 30 31 32

    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    PADDLE_ENFORCE_EQ(
        x_dims.size(), 4UL,
        platform::errors::InvalidArgument(
            "The Input(X) must have shape [batch_size, channel, height, width]."
            "Now the dimension of 'X' is %d.",
            x_dims.size()));
    PADDLE_ENFORCE_EQ(
        y_dims.size(), 4UL,
        platform::errors::InvalidArgument(
            "The Input(Y) must have shape [batch_size, channel, height, width]."
            "Now the dimension of 'Y' is %d.",
            y_dims.size()));
    PADDLE_ENFORCE_EQ(
        x_dims[2], y_dims[2],
        platform::errors::InvalidArgument(
            "The Input(X)(%d) and Input(Y)(%d) should have the same height.",
            x_dims[2], y_dims[2]));
    PADDLE_ENFORCE_EQ(
        x_dims[3], y_dims[3],
        platform::errors::InvalidArgument(
            "The Input(X)(%d) and Input(Y)(%d) should have the same width.",
            x_dims[3], y_dims[3]));
55 56 57 58 59 60 61 62 63 64

    ctx->SetOutputDim("Out", {x_dims[0], x_dims[1], y_dims[1]});
    ctx->ShareLoD("X", "Out");
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    framework::LibraryType library_{framework::LibraryType::kPlain};
    framework::DataLayout layout_ = framework::DataLayout::kAnyLayout;
65 66 67
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"), ctx.device_context(),
        layout_, library_);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  }
};

class FSPOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X",
             "(Tensor) The input of FSP op with shape [batch_size, x_channel, "
             "height, width]");
    AddInput("Y",
             "(Tensor) The input of FSP op with shape"
             "[batch_size, y_channel, height, width]."
             "The y_channel can be different with the x_channel of Input(X)"
             " while the other dimensions must be the same with Input(X)'s.");
    AddOutput(
        "Out",
        "(Tensor) The output of FSP op with shape "
        "[batch_size, x_channel, y_channel]. The x_channel is the channel "
        "of Input(X) and the y_channel is the channel of Input(Y).");
    AddComment(R"DOC(
    This op is used to calculate the flow of solution procedure (FSP) matrix of two feature maps.
    Given feature map x with shape [x_channel, h, w] and feature map y with shape
    [y_channel, h, w], we can get the fsp matrix of x and y in two steps:

        step 1: reshape x into matrix with shape [x_channel, h * w] and reshape and
                transpose y into matrix with shape [h * w, y_channel]
        step 2: multiply x and y to get fsp matrix with shape [x_channel, y_channel]

    The output is a batch of fsp matrices.
    )DOC");
  }
};

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

  void InferShape(framework::InferShapeContext* ctx) const override {
106 107 108 109 110
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "fsp_grad");
    OP_INOUT_CHECK(ctx->HasInput("Y"), "Input", "Y", "fsp_grad");
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")), "Input",
                   framework::GradVarName("Out"), "fsp_grad");

111 112 113 114 115 116 117 118 119 120 121 122 123 124
    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");
    auto x_grad_name = framework::GradVarName("X");
    auto y_grad_name = framework::GradVarName("Y");
    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, x_dims);
    }
    if (ctx->HasOutput(y_grad_name)) {
      ctx->SetOutputDim(y_grad_name, y_dims);
    }
  }

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
125 126 127
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
128 129 130
  }
};

131 132 133 134 135 136
template <typename T>
class FSPGradOpMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
137
  void Apply(GradOpPtr<T> op) const override {
138 139 140 141 142 143 144 145 146 147 148 149 150
    op->SetType("fsp_grad");

    op->SetInput("X", this->Input("X"));
    op->SetInput("Y", this->Input("Y"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));

    op->SetAttrMap(this->Attrs());

    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetOutput(framework::GradVarName("Y"), this->InputGrad("Y"));
  }
};

151 152 153 154
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
155 156 157
REGISTER_OPERATOR(fsp, ops::FSPOp, ops::FSPOpMaker,
                  ops::FSPGradOpMaker<paddle::framework::OpDesc>,
                  ops::FSPGradOpMaker<paddle::imperative::OpBase>);
158 159 160 161 162 163 164
REGISTER_OPERATOR(fsp_grad, ops::FSPOpGrad);
REGISTER_OP_CPU_KERNEL(
    fsp, ops::FSPOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::FSPOpKernel<paddle::platform::CPUDeviceContext, double>);
REGISTER_OP_CPU_KERNEL(
    fsp_grad, ops::FSPGradOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::FSPGradOpKernel<paddle::platform::CPUDeviceContext, double>);