elementwise_div_op.h 2.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
G
gongweibao 已提交
2

L
Luo Tao 已提交
3 4 5
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
G
gongweibao 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
G
gongweibao 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
G
gongweibao 已提交
14

F
fengjiayi 已提交
15 16
#pragma once

17
#include <vector>
18

19 20
#include "paddle/fluid/operators/elementwise/elementwise_mul_op.h"

G
gongweibao 已提交
21 22 23
namespace paddle {
namespace operators {

24 25 26
class ElementwiseDivOpDoubleGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
27
  using Tensor = phi::DenseTensor;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

  void InferShape(framework::InferShapeContext* ctx) const override {
    auto y_grad_name = framework::GradVarName("Y");
    if (ctx->HasOutput("DOut")) {
      ctx->ShareDim("DX", "DOut");
      ctx->ShareLoD("DX", "DOut");
    }
    if (ctx->HasOutput(y_grad_name)) {
      ctx->ShareDim("Y", y_grad_name);
      ctx->ShareLoD("Y", y_grad_name);
    }
    if (ctx->HasOutput("DDOut")) {
      ctx->ShareDim("DX", "DDOut");
      ctx->ShareLoD("DX", "DDOut");
    }
  }

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
C
chentianyu03 已提交
47
    auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "Out");
48 49

#ifdef PADDLE_WITH_MKLDNN
50
    if (this->CanMKLDNNBeUsed(ctx, input_data_type)) {
51 52
      return framework::OpKernelType(input_data_type,
                                     ctx.GetPlace(),
53 54 55 56 57 58
                                     framework::DataLayout::kMKLDNN,
                                     framework::LibraryType::kMKLDNN);
    }
#endif
    return framework::OpKernelType(input_data_type, ctx.GetPlace());
  }
C
chentianyu03 已提交
59 60

  framework::OpKernelType GetKernelTypeForVar(
61
      const std::string& var_name,
62
      const phi::DenseTensor& tensor,
C
chentianyu03 已提交
63 64 65
      const framework::OpKernelType& expected_kernel_type) const {
    if (framework::IsComplexType(expected_kernel_type.data_type_)) {
      // only promote inputs’s types when contains complex input
66
      return framework::OpKernelType(
67 68
          framework::TransToProtoVarType(tensor.dtype()),
          tensor.place(),
69
          tensor.layout());
C
chentianyu03 已提交
70
    } else {
71 72
      return framework::OpKernelType(
          expected_kernel_type.data_type_, tensor.place(), tensor.layout());
C
chentianyu03 已提交
73 74
    }
  }
75 76
};

G
gongweibao 已提交
77 78
}  // namespace operators
}  // namespace paddle