elementwise_div_op.h 2.3 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
    return framework::OpKernelType(input_data_type, ctx.GetPlace());
  }
C
chentianyu03 已提交
50 51

  framework::OpKernelType GetKernelTypeForVar(
52
      const std::string& var_name,
53
      const phi::DenseTensor& tensor,
54
      const framework::OpKernelType& expected_kernel_type) const override {
C
chentianyu03 已提交
55 56
    if (framework::IsComplexType(expected_kernel_type.data_type_)) {
      // only promote inputs’s types when contains complex input
57
      return framework::OpKernelType(
58 59
          framework::TransToProtoVarType(tensor.dtype()),
          tensor.place(),
60
          tensor.layout());
C
chentianyu03 已提交
61
    } else {
62 63
      return framework::OpKernelType(
          expected_kernel_type.data_type_, tensor.place(), tensor.layout());
C
chentianyu03 已提交
64 65
    }
  }
66 67
};

G
gongweibao 已提交
68 69
}  // namespace operators
}  // namespace paddle