resnet_unit_op.cc 20.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* Copyright (c) 2021 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/framework/op_registry.h"
#include "paddle/fluid/platform/float16.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

// Shape of bitmask
static framework::DDim GetBitmaskDims(std::vector<int> out_shape) {
  int c = out_shape.back();
26 27 28 29
  int64_t nhw =
      std::accumulate(
          out_shape.begin(), out_shape.end(), 1, std::multiplies<int>()) /
      c;
30 31 32
  int32_t c_int32_elems = ((c + 63) & ~63) / 32;
  int32_t nhw_int32_elems = ((nhw + 31) & ~31);
  std::vector<int> bitmask_shape = {nhw_int32_elems, c_int32_elems, 1};
33
  return phi::make_ddim(bitmask_shape);
34 35 36 37 38 39 40 41 42
}

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

  void InferShape(framework::InferShapeContext* ctx) const {
    // Check input
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "ResNetUnitOp");
43 44
    OP_INOUT_CHECK(
        ctx->HasInput("FilterX"), "Input", "FilterX", "ResNetUnitOp");
45 46 47 48 49 50 51 52 53 54 55
    OP_INOUT_CHECK(ctx->HasInput("ScaleX"), "Input", "ScaleX", "ResNetUnitOp");
    OP_INOUT_CHECK(ctx->HasInput("BiasX"), "Input", "BiasX", "ResNetUnitOp");
    OP_INOUT_CHECK(ctx->HasInput("MeanX"), "Input", "MeanX", "ResNetUnitOp");
    OP_INOUT_CHECK(ctx->HasInput("VarX"), "Input", "VarX", "ResNetUnitOp");

    bool fuse_add = ctx->Attrs().Get<bool>("fuse_add");
    bool has_shortcut = ctx->Attrs().Get<bool>("has_shortcut");
    if (fuse_add || has_shortcut) {
      OP_INOUT_CHECK(ctx->HasInput("Z"), "Input", "Z", "ResNetUnitOp");
    }
    if (has_shortcut) {
56 57 58 59
      OP_INOUT_CHECK(
          ctx->HasInput("FilterZ"), "Input", "FilterZ", "ResNetUnitOp");
      OP_INOUT_CHECK(
          ctx->HasInput("ScaleZ"), "Input", "ScaleZ", "ResNetUnitOp");
60 61 62 63 64 65 66
      OP_INOUT_CHECK(ctx->HasInput("BiasZ"), "Input", "BiasZ", "ResNetUnitOp");
      OP_INOUT_CHECK(ctx->HasInput("MeanZ"), "Input", "MeanZ", "ResNetUnitOp");
      OP_INOUT_CHECK(ctx->HasInput("VarZ"), "Input", "VarZ", "ResNetUnitOp");
    }

    // Check output
    OP_INOUT_CHECK(ctx->HasOutput("Y"), "Output", "Y", "ResNetUnitOp");
67 68
    OP_INOUT_CHECK(
        ctx->HasOutput("BitMask"), "Output", "BitMask", "ResNetUnitOp");
69
    OP_INOUT_CHECK(ctx->HasOutput("ConvX"), "Output", "ConvX", "ResNetUnitOp");
70 71 72 73 74
    OP_INOUT_CHECK(
        ctx->HasOutput("SavedMeanX"), "Output", "SavedMeanX", "ResNetUnitOp");
    OP_INOUT_CHECK(ctx->HasOutput("SavedInvstdX"),
                   "Output",
                   "SavedInvstdX",
75
                   "ResNetUnitOp");
76 77 78
    OP_INOUT_CHECK(ctx->HasOutput("RunningMeanX"),
                   "Output",
                   "RunningMeanX",
79
                   "ResNetUnitOp");
80 81
    OP_INOUT_CHECK(
        ctx->HasOutput("RunningVarX"), "Output", "RunningVarX", "ResNetUnitOp");
82
    if (has_shortcut) {
83 84 85 86 87 88 89
      OP_INOUT_CHECK(
          ctx->HasOutput("ConvZ"), "Output", "ConvZ", "ResNetUnitOp");
      OP_INOUT_CHECK(
          ctx->HasOutput("SavedMeanZ"), "Output", "SavedMeanZ", "ResNetUnitOp");
      OP_INOUT_CHECK(ctx->HasOutput("SavedInvstdZ"),
                     "Output",
                     "SavedInvstdZ",
90
                     "ResNetUnitOp");
91 92 93
      OP_INOUT_CHECK(ctx->HasOutput("RunningMeanZ"),
                     "Output",
                     "RunningMeanZ",
94
                     "ResNetUnitOp");
95 96 97
      OP_INOUT_CHECK(ctx->HasOutput("RunningVarZ"),
                     "Output",
                     "RunningVarZ",
98 99 100 101 102
                     "ResNetUnitOp");
    }

    // make sure Mean/RunningMean and Var/RunningVar share memory
    PADDLE_ENFORCE_EQ(
103 104
        ctx->Inputs("MeanX")[0],
        ctx->Outputs("RunningMeanX")[0],
105 106
        platform::errors::InvalidArgument(
            "MeanX and RunningMeanX should share the same memory"));
107 108
    PADDLE_ENFORCE_EQ(ctx->Inputs("VarX")[0],
                      ctx->Outputs("RunningVarX")[0],
109 110 111 112
                      platform::errors::InvalidArgument(
                          "VarX and RunningVarX should share the same memory"));
    if (has_shortcut) {
      PADDLE_ENFORCE_EQ(
113 114
          ctx->Inputs("MeanZ")[0],
          ctx->Outputs("RunningMeanZ")[0],
115 116 117
          platform::errors::InvalidArgument(
              "MeanZ and RunningMeanZ should share the same memory"));
      PADDLE_ENFORCE_EQ(
118 119
          ctx->Inputs("VarZ")[0],
          ctx->Outputs("RunningVarZ")[0],
120 121 122 123 124 125 126
          platform::errors::InvalidArgument(
              "VarZ and RunningVarZ should share the same memory"));
    }

    // Check dims of inputs
    const auto x_dims = ctx->GetInputDim("X");
    const auto w_dims = ctx->GetInputDim("FilterX");
W
wuhuanzhou 已提交
127
    std::vector<int64_t> bn_param_shape =
128
        phi::vectorize(ctx->GetInputDim("ScaleX"));
W
wuhuanzhou 已提交
129 130 131
    if (1 == bn_param_shape.size()) {
      bn_param_shape = {1, 1, 1, bn_param_shape[0]};
    }
132
    framework::DDim bn_param_dims = phi::make_ddim(bn_param_shape);
133
    PADDLE_ENFORCE_EQ(
134 135
        x_dims.size(),
        4,
136 137 138 139 140
        platform::errors::InvalidArgument("The dimensions of input "
                                          "must equal to 4."
                                          "But received: the shape of input "
                                          "= [%s], the dimension of input = "
                                          "[%d]",
141 142 143 144
                                          x_dims,
                                          x_dims.size()));
    PADDLE_ENFORCE_EQ(w_dims.size(),
                      4,
145 146 147 148 149
                      platform::errors::InvalidArgument(
                          "The dimensions of filter "
                          "must equal to 4."
                          "But received: the shape of filter "
                          "= [%s], the dimension of filter = [%d] ",
150 151 152 153
                          w_dims,
                          w_dims.size()));
    PADDLE_ENFORCE_EQ(bn_param_dims.size(),
                      4,
154 155 156 157 158
                      platform::errors::InvalidArgument(
                          "The dimensions of bn param "
                          "must equal to 4."
                          "But received: the shape of bn param "
                          "= [%s], the dimension of bn param = [%d] ",
159 160
                          bn_param_dims,
                          bn_param_dims.size()));
161
    auto data_format = ctx->Attrs().Get<std::string>("data_format");
Q
QingshuChen 已提交
162
    bool is_nchw = (data_format == "NCHW");
163 164 165 166 167 168
    // Calculate the dims of outputs
    int batch = x_dims[0];
    int output_channel = w_dims[0];
    int filter_size = w_dims[2];
    int stride = ctx->Attrs().Get<int>("stride");
    int padding = ctx->Attrs().Get<int>("padding");
Q
QingshuChen 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    std::vector<int> out_shape;
    out_shape.push_back(batch);
    if (is_nchw) {
      int out_h = (x_dims[2] + padding * 2 - filter_size) / stride + 1;
      int out_w = (x_dims[3] + padding * 2 - filter_size) / stride + 1;
      out_shape.push_back(output_channel);
      out_shape.push_back(out_h);
      out_shape.push_back(out_w);
    } else {
      int out_h = (x_dims[1] + padding * 2 - filter_size) / stride + 1;
      int out_w = (x_dims[2] + padding * 2 - filter_size) / stride + 1;
      out_shape.push_back(out_h);
      out_shape.push_back(out_w);
      out_shape.push_back(output_channel);
    }
184

185
    auto y_dims = phi::make_ddim(out_shape);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    auto bitmask_dims = GetBitmaskDims(out_shape);
    // Set dims of outputs
    ctx->SetOutputDim("Y", y_dims);
    ctx->SetOutputDim("BitMask", bitmask_dims);
    ctx->SetOutputDim("ConvX", y_dims);
    ctx->SetOutputDim("SavedMeanX", bn_param_dims);
    ctx->SetOutputDim("SavedInvstdX", bn_param_dims);
    ctx->SetOutputDim("RunningMeanX", bn_param_dims);
    ctx->SetOutputDim("RunningVarX", bn_param_dims);
    if (has_shortcut) {
      ctx->SetOutputDim("ConvZ", y_dims);
      ctx->SetOutputDim("SavedMeanZ", bn_param_dims);
      ctx->SetOutputDim("SavedInvstdZ", bn_param_dims);
      ctx->SetOutputDim("RunningMeanZ", bn_param_dims);
      ctx->SetOutputDim("RunningVarZ", bn_param_dims);
    }
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const {
    auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
    // By default, the type of the scale, bias, mean,
    // and var tensors should be float when input tensor's dtype is float16.
    auto bn_param_type = framework::proto::VarType::FP32;

212 213 214 215 216 217 218 219 220 221
    PADDLE_ENFORCE_EQ(
        bn_param_type,
        framework::TransToProtoVarType(ctx.Input<Tensor>("ScaleX")->dtype()),
        platform::errors::InvalidArgument(
            "Scale input should be of float type"));
    PADDLE_ENFORCE_EQ(
        bn_param_type,
        framework::TransToProtoVarType(ctx.Input<Tensor>("BiasX")->dtype()),
        platform::errors::InvalidArgument(
            "Bias input should be of float type"));
222 223
    framework::LibraryType library = framework::LibraryType::kPlain;
    framework::DataLayout layout = framework::DataLayout::kAnyLayout;
224 225
    return framework::OpKernelType(
        input_data_type, ctx.GetPlace(), layout, library);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  }
};

class ResNetUnitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() {
    AddInput("X", "The input 1 tensor");
    AddInput("FilterX", "Filter tensor of input 1");
    AddInput("ScaleX", "Scale tensor of input 1 used in batchnorm");
    AddInput("BiasX", "Bias tensor of input 1 used in batchnorm");
    AddInput("MeanX", "Mean tensor of input 1 used in batchnorm");
    AddInput("VarX", "Variance tensor of input 1 used in batchnorm");
    AddInput("Z", "The input 2 tensor").AsDispensable();
    AddInput("FilterZ", "Filter tensor of input 2").AsDispensable();
    AddInput("ScaleZ", "Scale tensor of input 2").AsDispensable();
    AddInput("BiasZ", "Bias tensor of input 2").AsDispensable();
    AddInput("MeanZ", "Mean tensor of input 2").AsDispensable();
    AddInput("VarZ", "Variance tensor of input 2").AsDispensable();
    AddOutput("Y", "The result of the resnet unit");
    AddOutput("BitMask", "The bitmask generated after relu");
    AddOutput("ConvX", "The output of input 1 after conv");
    AddOutput("SavedMeanX", "Mean of input 1 in the current batch");
    AddOutput("SavedInvstdX", "Invstd of input 1 in the current batch");
    AddOutput("RunningMeanX", "Shared memory with MeanX");
    AddOutput("RunningVarX", "Shared memory with VarX");
    AddOutput("ConvZ", "The output of input 2 after conv").AsDispensable();
    AddOutput("SavedMeanZ", "Mean of input 1 in the current batch")
        .AsDispensable();
    AddOutput("SavedInvstdZ", "Invstd of input 1 in the current batch")
        .AsDispensable();
    AddOutput("RunningMeanZ", "Shared memory with MeanZ").AsDispensable();
    AddOutput("RunningVarZ", "Shared memory with VarZ").AsDispensable();
    AddAttr<int>("stride", "").SetDefault(1);
    AddAttr<int>("stride_z", "").SetDefault(1);
    AddAttr<int>("padding", "").SetDefault(0);
    AddAttr<int>("dilation", "").SetDefault(1);
    AddAttr<int>("group", "").SetDefault(1);
    AddAttr<float>("momentum", "").SetDefault(0.9);
    AddAttr<float>("epsilon", "").SetDefault(1e-5);
    AddAttr<std::string>("data_format", "").SetDefault("NHWC");
    AddAttr<bool>("fuse_add", "").SetDefault(false);
    AddAttr<bool>("has_shortcut", "").SetDefault(false);
    AddAttr<bool>("use_global_stats", "").SetDefault(false);
    AddAttr<bool>("is_test",
                  "(bool, default false) Set to true for inference only, false "
                  "for training. Some layers may run faster when this is true.")
        .SetDefault(false);
Z
Zhang Zheng 已提交
273
    AddAttr<bool>("use_addto", "").SetDefault(false);
274 275 276
    AddAttr<std::string>("act_type", "The activation type to be fused.")
        .SetDefault("relu");
    AddComment(R"DOC(
277
Fusion op of the basic unit of resnet block.
278 279

The implementation is based on the latest fusion op interface in cuDNN v8.0.
280
For more details:
281 282 283 284 285 286 287 288 289 290 291 292 293
https://docs.nvidia.com/deeplearning/cudnn/api/index.html#cudnnFusedOps_t

)DOC");
  }
};

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

  void InferShape(framework::InferShapeContext* ctx) const {
    // check input
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "ResNetUnitGradOp");
294 295 296 297 298 299 300 301 302 303 304 305 306
    OP_INOUT_CHECK(
        ctx->HasInput("FilterX"), "Input", "FilterX", "ResNetUnitGradOp");
    OP_INOUT_CHECK(
        ctx->HasInput("ConvX"), "Input", "ConvX", "ResNetUnitGradOp");
    OP_INOUT_CHECK(
        ctx->HasInput("ScaleX"), "Input", "ScaleX", "ResNetUnitGradOp");
    OP_INOUT_CHECK(
        ctx->HasInput("BiasX"), "Input", "BiasX", "ResNetUnitGradOp");
    OP_INOUT_CHECK(
        ctx->HasInput("SavedMeanX"), "Input", "SavedMeanX", "ResNetUnitGradOp");
    OP_INOUT_CHECK(ctx->HasInput("SavedInvstdX"),
                   "Input",
                   "SavedInvstdX",
307 308 309 310 311 312 313 314
                   "ResNetUnitGradOp");

    bool fuse_add = ctx->Attrs().Get<bool>("fuse_add");
    bool has_shortcut = ctx->Attrs().Get<bool>("has_shortcut");
    if (fuse_add || has_shortcut) {
      OP_INOUT_CHECK(ctx->HasInput("Z"), "Input", "Z", "ResNetUnitGradOp");
    }
    if (has_shortcut) {
315 316 317 318 319 320 321 322 323 324 325
      OP_INOUT_CHECK(
          ctx->HasInput("FilterZ"), "Input", "FilterZ", "ResNetUnitGradOp");
      OP_INOUT_CHECK(
          ctx->HasInput("ConvZ"), "Input", "ConvZ", "ResNetUnitGradOp");
      OP_INOUT_CHECK(
          ctx->HasInput("ScaleZ"), "Input", "ScaleZ", "ResNetUnitGradOp");
      OP_INOUT_CHECK(
          ctx->HasInput("BiasZ"), "Input", "BiasZ", "ResNetUnitGradOp");
      OP_INOUT_CHECK(ctx->HasInput("SavedMeanZ"),
                     "Input",
                     "SavedMeanZ",
326
                     "ResNetUnitGradOp");
327 328 329
      OP_INOUT_CHECK(ctx->HasInput("SavedInvstdZ"),
                     "Input",
                     "SavedInvstdZ",
330 331 332
                     "ResNetUnitGradOp");
    }
    OP_INOUT_CHECK(ctx->HasInput("Y"), "Input", "Y", "ResNetUnitGradOp");
333 334 335 336 337
    OP_INOUT_CHECK(
        ctx->HasInput("BitMask"), "Input", "BitMask", "ResNetUnitGradOp");
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Y")),
                   "Input",
                   framework::GradVarName("Y"),
338 339 340
                   "ResNetUnitGradOp");

    // check output
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("X")),
                   "Output",
                   framework::GradVarName("X"),
                   "ResNetUnitGradOp");
    OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("FilterX")),
                   "Output",
                   framework::GradVarName("FilterX"),
                   "ResNetUnitGradOp");
    OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("ScaleX")),
                   "Output",
                   framework::GradVarName("ScaleX"),
                   "ResNetUnitGradOp");
    OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("BiasX")),
                   "Output",
                   framework::GradVarName("BiasX"),
                   "ResNetUnitGradOp");
357
    if (fuse_add) {
358 359 360 361
      OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("Z")),
                     "Output",
                     framework::GradVarName("Z"),
                     "ResNetUnitGradOp");
362 363 364
    }
    if (has_shortcut) {
      OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("FilterZ")),
365 366 367 368 369 370 371 372 373 374
                     "Output",
                     framework::GradVarName("FilterZ"),
                     "ResNetUnitGradOp");
      OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("ScaleZ")),
                     "Output",
                     framework::GradVarName("ScaleZ"),
                     "ResNetUnitGradOp");
      OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("BiasZ")),
                     "Output",
                     framework::GradVarName("BiasZ"),
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
                     "ResNetUnitGradOp");
    }
    const auto x_dims = ctx->GetInputDim("X");
    const auto filter_x_dims = ctx->GetInputDim("FilterX");
    const auto param_dims = ctx->GetInputDim("ScaleX");
    ctx->SetOutputDim(framework::GradVarName("X"), x_dims);
    ctx->SetOutputDim(framework::GradVarName("FilterX"), filter_x_dims);
    ctx->SetOutputDim(framework::GradVarName("ScaleX"), param_dims);
    ctx->SetOutputDim(framework::GradVarName("BiasX"), param_dims);
    if (fuse_add || has_shortcut) {
      const auto z_dims = ctx->GetInputDim("Z");
      ctx->SetOutputDim(framework::GradVarName("Z"), z_dims);
    }
    if (has_shortcut) {
      const auto filter_z_dims = ctx->GetInputDim("FilterZ");
      ctx->SetOutputDim(framework::GradVarName("FilterZ"), filter_z_dims);
      ctx->SetOutputDim(framework::GradVarName("ScaleZ"), param_dims);
      ctx->SetOutputDim(framework::GradVarName("BiasZ"), param_dims);
    }
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const {
    PADDLE_ENFORCE_NOT_NULL(
        ctx.InputVar(framework::GradVarName("Y")),
        platform::errors::NotFound(
            "Can not find Y@GRAD in the execution context."));

    framework::LibraryType library = framework::LibraryType::kPlain;
    framework::DataLayout layout = framework::DataLayout::kAnyLayout;

    return framework::OpKernelType(
408 409 410 411
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.GetPlace(),
        layout,
        library);
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
  }
};

template <typename T>
class ResNetUnitGradOpMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("resnet_unit_grad");
    op->SetInput("X", this->Input("X"));
    op->SetInput("FilterX", this->Input("FilterX"));
    op->SetInput("ConvX", this->Output("ConvX"));
    op->SetInput("ScaleX", this->Input("ScaleX"));
    op->SetInput("BiasX", this->Input("BiasX"));
    op->SetInput("SavedMeanX", this->Output("SavedMeanX"));
    op->SetInput("SavedInvstdX", this->Output("SavedInvstdX"));
    op->SetInput("Z", this->Input("Z"));
    op->SetInput("FilterZ", this->Input("FilterZ"));
    op->SetInput("ConvZ", this->Output("ConvZ"));
    op->SetInput("ScaleZ", this->Input("ScaleZ"));
    op->SetInput("BiasZ", this->Input("BiasZ"));
    op->SetInput("SavedMeanZ", this->Output("SavedMeanZ"));
    op->SetInput("SavedInvstdZ", this->Output("SavedInvstdZ"));
    op->SetInput("Y", this->Output("Y"));
    op->SetInput("BitMask", this->Output("BitMask"));
    op->SetInput(framework::GradVarName("Y"), this->OutputGrad("Y"));

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

    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetOutput(framework::GradVarName("FilterX"),
                  this->InputGrad("FilterX"));
    op->SetOutput(framework::GradVarName("ScaleX"), this->InputGrad("ScaleX"));
    op->SetOutput(framework::GradVarName("BiasX"), this->InputGrad("BiasX"));
    op->SetOutput(framework::GradVarName("Z"), this->InputGrad("Z"));
    op->SetOutput(framework::GradVarName("FilterZ"),
                  this->InputGrad("FilterZ"));
    op->SetOutput(framework::GradVarName("ScaleZ"), this->InputGrad("ScaleZ"));
    op->SetOutput(framework::GradVarName("BiasZ"), this->InputGrad("BiasZ"));
  }
};

class ResNetUnitOpInferVarType
    : public framework::PassInDtypeAndVarTypeToOutput {
 protected:
  std::unordered_map<std::string, std::string>& GetInputOutputWithSameType()
      const override {
    static std::unordered_map<std::string, std::string> m{{"X", /*->*/ "Y"}};
    return m;
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
470 471 472
REGISTER_OPERATOR(resnet_unit,
                  ops::ResNetUnitOp,
                  ops::ResNetUnitOpMaker,
473 474 475 476
                  ops::ResNetUnitOpInferVarType,
                  ops::ResNetUnitGradOpMaker<paddle::framework::OpDesc>,
                  ops::ResNetUnitGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(resnet_unit_grad, ops::ResNetUnitGradOp);