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

// Shape of bitmask
static framework::DDim GetBitmaskDims(std::vector<int> out_shape) {
  int c = out_shape.back();
24 25 26 27
  int64_t nhw =
      std::accumulate(
          out_shape.begin(), out_shape.end(), 1, std::multiplies<int>()) /
      c;
28 29 30
  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};
31
  return phi::make_ddim(bitmask_shape);
32 33 34 35 36 37 38 39 40
}

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");
41 42
    OP_INOUT_CHECK(
        ctx->HasInput("FilterX"), "Input", "FilterX", "ResNetUnitOp");
43 44 45 46 47 48 49 50 51 52 53
    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) {
54 55 56 57
      OP_INOUT_CHECK(
          ctx->HasInput("FilterZ"), "Input", "FilterZ", "ResNetUnitOp");
      OP_INOUT_CHECK(
          ctx->HasInput("ScaleZ"), "Input", "ScaleZ", "ResNetUnitOp");
58 59 60 61 62 63 64
      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");
65 66
    OP_INOUT_CHECK(
        ctx->HasOutput("BitMask"), "Output", "BitMask", "ResNetUnitOp");
67
    OP_INOUT_CHECK(ctx->HasOutput("ConvX"), "Output", "ConvX", "ResNetUnitOp");
68 69 70 71 72
    OP_INOUT_CHECK(
        ctx->HasOutput("SavedMeanX"), "Output", "SavedMeanX", "ResNetUnitOp");
    OP_INOUT_CHECK(ctx->HasOutput("SavedInvstdX"),
                   "Output",
                   "SavedInvstdX",
73
                   "ResNetUnitOp");
74 75 76
    OP_INOUT_CHECK(ctx->HasOutput("RunningMeanX"),
                   "Output",
                   "RunningMeanX",
77
                   "ResNetUnitOp");
78 79
    OP_INOUT_CHECK(
        ctx->HasOutput("RunningVarX"), "Output", "RunningVarX", "ResNetUnitOp");
80
    if (has_shortcut) {
81 82 83 84 85 86 87
      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",
88
                     "ResNetUnitOp");
89 90 91
      OP_INOUT_CHECK(ctx->HasOutput("RunningMeanZ"),
                     "Output",
                     "RunningMeanZ",
92
                     "ResNetUnitOp");
93 94 95
      OP_INOUT_CHECK(ctx->HasOutput("RunningVarZ"),
                     "Output",
                     "RunningVarZ",
96 97 98 99 100
                     "ResNetUnitOp");
    }

    // make sure Mean/RunningMean and Var/RunningVar share memory
    PADDLE_ENFORCE_EQ(
101 102
        ctx->Inputs("MeanX")[0],
        ctx->Outputs("RunningMeanX")[0],
103 104
        platform::errors::InvalidArgument(
            "MeanX and RunningMeanX should share the same memory"));
105 106
    PADDLE_ENFORCE_EQ(ctx->Inputs("VarX")[0],
                      ctx->Outputs("RunningVarX")[0],
107 108 109 110
                      platform::errors::InvalidArgument(
                          "VarX and RunningVarX should share the same memory"));
    if (has_shortcut) {
      PADDLE_ENFORCE_EQ(
111 112
          ctx->Inputs("MeanZ")[0],
          ctx->Outputs("RunningMeanZ")[0],
113 114 115
          platform::errors::InvalidArgument(
              "MeanZ and RunningMeanZ should share the same memory"));
      PADDLE_ENFORCE_EQ(
116 117
          ctx->Inputs("VarZ")[0],
          ctx->Outputs("RunningVarZ")[0],
118 119 120 121 122 123 124
          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 已提交
125
    std::vector<int64_t> bn_param_shape =
126
        phi::vectorize(ctx->GetInputDim("ScaleX"));
W
wuhuanzhou 已提交
127 128 129
    if (1 == bn_param_shape.size()) {
      bn_param_shape = {1, 1, 1, bn_param_shape[0]};
    }
130
    framework::DDim bn_param_dims = phi::make_ddim(bn_param_shape);
131
    PADDLE_ENFORCE_EQ(
132 133
        x_dims.size(),
        4,
134 135 136 137 138
        platform::errors::InvalidArgument("The dimensions of input "
                                          "must equal to 4."
                                          "But received: the shape of input "
                                          "= [%s], the dimension of input = "
                                          "[%d]",
139 140 141 142
                                          x_dims,
                                          x_dims.size()));
    PADDLE_ENFORCE_EQ(w_dims.size(),
                      4,
143 144 145 146 147
                      platform::errors::InvalidArgument(
                          "The dimensions of filter "
                          "must equal to 4."
                          "But received: the shape of filter "
                          "= [%s], the dimension of filter = [%d] ",
148 149 150 151
                          w_dims,
                          w_dims.size()));
    PADDLE_ENFORCE_EQ(bn_param_dims.size(),
                      4,
152 153 154 155 156
                      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] ",
157 158
                          bn_param_dims,
                          bn_param_dims.size()));
159
    auto data_format = ctx->Attrs().Get<std::string>("data_format");
Q
QingshuChen 已提交
160
    bool is_nchw = (data_format == "NCHW");
161 162 163 164 165 166
    // 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 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    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);
    }
182

183
    auto y_dims = phi::make_ddim(out_shape);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    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;

210 211 212 213 214 215 216 217 218 219
    PADDLE_ENFORCE_EQ(bn_param_type,
                      framework::TransToProtoVarType(
                          ctx.Input<phi::DenseTensor>("ScaleX")->dtype()),
                      platform::errors::InvalidArgument(
                          "Scale input should be of float type"));
    PADDLE_ENFORCE_EQ(bn_param_type,
                      framework::TransToProtoVarType(
                          ctx.Input<phi::DenseTensor>("BiasX")->dtype()),
                      platform::errors::InvalidArgument(
                          "Bias input should be of float type"));
220
    framework::LibraryType library = framework::LibraryType::kPlain;
221
    phi::DataLayout layout = phi::DataLayout::kAnyLayout;
222 223
    return framework::OpKernelType(
        input_data_type, ctx.GetPlace(), layout, library);
224 225 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
  }
};

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 已提交
271
    AddAttr<bool>("use_addto", "").SetDefault(false);
272 273 274
    AddAttr<std::string>("act_type", "The activation type to be fused.")
        .SetDefault("relu");
    AddComment(R"DOC(
275
Fusion op of the basic unit of resnet block.
276 277

The implementation is based on the latest fusion op interface in cuDNN v8.0.
278
For more details:
279 280 281 282 283 284 285 286 287 288 289 290 291
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");
292 293 294 295 296 297 298 299 300 301 302 303 304
    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",
305 306 307 308 309 310 311 312
                   "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) {
313 314 315 316 317 318 319 320 321 322 323
      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",
324
                     "ResNetUnitGradOp");
325 326 327
      OP_INOUT_CHECK(ctx->HasInput("SavedInvstdZ"),
                     "Input",
                     "SavedInvstdZ",
328 329 330
                     "ResNetUnitGradOp");
    }
    OP_INOUT_CHECK(ctx->HasInput("Y"), "Input", "Y", "ResNetUnitGradOp");
331 332 333 334 335
    OP_INOUT_CHECK(
        ctx->HasInput("BitMask"), "Input", "BitMask", "ResNetUnitGradOp");
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Y")),
                   "Input",
                   framework::GradVarName("Y"),
336 337 338
                   "ResNetUnitGradOp");

    // check output
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    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");
355
    if (fuse_add) {
356 357 358 359
      OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("Z")),
                     "Output",
                     framework::GradVarName("Z"),
                     "ResNetUnitGradOp");
360 361 362
    }
    if (has_shortcut) {
      OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("FilterZ")),
363 364 365 366 367 368 369 370 371 372
                     "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"),
373 374 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
                     "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;
403
    phi::DataLayout layout = phi::DataLayout::kAnyLayout;
404 405

    return framework::OpKernelType(
406 407 408 409
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.GetPlace(),
        layout,
        library);
410 411 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
  }
};

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;
468 469 470
REGISTER_OPERATOR(resnet_unit,
                  ops::ResNetUnitOp,
                  ops::ResNetUnitOpMaker,
471 472 473 474
                  ops::ResNetUnitOpInferVarType,
                  ops::ResNetUnitGradOpMaker<paddle::framework::OpDesc>,
                  ops::ResNetUnitGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(resnet_unit_grad, ops::ResNetUnitGradOp);