activation_sig.cc 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2022 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/phi/core/compat/op_utils.h"

namespace phi {

Y
YuanRisheng 已提交
19 20 21
#define DEFINE_ACT_GRAD_DEPX_OP_ARGMAP(func_name, op_name, attrs) \
  KernelSignature func_name##GradOpArgumentMapping(               \
      const ArgumentMappingContext& ctx) {                        \
22 23
    return KernelSignature(                                       \
        op_name "_grad", {"X", "Out@GRAD"}, {attrs}, {"X@GRAD"}); \
24 25
  }

Y
YuanRisheng 已提交
26 27 28
#define DEFINE_ACT_GRAD_DEPOUT_OP_ARGMAP(func_name, op_name, attrs) \
  KernelSignature func_name##GradOpArgumentMapping(                 \
      const ArgumentMappingContext& ctx) {                          \
29 30
    return KernelSignature(                                         \
        op_name "_grad", {"Out", "Out@GRAD"}, {attrs}, {"X@GRAD"}); \
31 32
  }

33 34 35 36 37
#define DEFINE_ACT_GRAD_NODEP_OP_ARGMAP(func_name, op_name, attrs) \
  KernelSignature func_name##GradOpArgumentMapping(                \
      const ArgumentMappingContext& ctx) {                         \
    return KernelSignature(                                        \
        op_name "_grad", {"Out@GRAD"}, {attrs}, {"X@GRAD"});       \
Y
YuanRisheng 已提交
38 39
  }

40 41
#define comma ,

42
DEFINE_ACT_GRAD_DEPX_OP_ARGMAP(HardTanh, "hard_tanh", "t_min" comma "t_max");
43
DEFINE_ACT_GRAD_DEPX_OP_ARGMAP(Mish, "mish", "threshold");
Y
YuanRisheng 已提交
44 45 46 47 48
DEFINE_ACT_GRAD_DEPX_OP_ARGMAP(HardSwish,
                               "hard_swish",
                               "threshold" comma "scale" comma
                               "offset");                // NOLINT
DEFINE_ACT_GRAD_DEPX_OP_ARGMAP(Swish, "swish", "beta");  // NOLINT
Y
YuanRisheng 已提交
49

50 51 52 53
DEFINE_ACT_GRAD_DEPX_OP_ARGMAP(STanh,
                               "stanh",
                               "scale_a" comma "scale_b");  // NOLINT

54
DEFINE_ACT_GRAD_DEPOUT_OP_ARGMAP(Relu6, "relu6", "threshold");  // NOLINT
55

56 57 58 59 60 61 62 63 64 65 66 67 68
KernelSignature HardSwishOpArgumentMapping(const ArgumentMappingContext& ctx) {
  return KernelSignature(
      "hard_swish_raw", {"X"}, {"threshold", "scale", "offset"}, {"Out"});
}

KernelSignature SwishOpArgumentMapping(const ArgumentMappingContext& ctx) {
  return KernelSignature("swish_raw", {"X"}, {"beta"}, {"Out"});
}

KernelSignature Relu6OpArgumentMapping(const ArgumentMappingContext& ctx) {
  return KernelSignature("relu6_raw", {"X"}, {"threshold"}, {"Out"});
}

Y
YuanRisheng 已提交
69 70 71 72 73 74 75 76 77 78
KernelSignature PowOpArgumentMapping(const ArgumentMappingContext& ctx) {
  if (ctx.HasInput("FactorTensor")) {
    return KernelSignature("pow", {"X"}, {"FactorTensor"}, {"Out"});
  } else {
    return KernelSignature("pow", {"X"}, {"factor"}, {"Out"});
  }
}

KernelSignature PowGradOpArgumentMapping(const ArgumentMappingContext& ctx) {
  if (ctx.HasInput("FactorTensor")) {
79 80
    return KernelSignature(
        "pow_grad", {"X", "Out@GRAD"}, {"FactorTensor"}, {"X@GRAD"});
Y
YuanRisheng 已提交
81 82
  } else {
    return KernelSignature(
83
        "pow_grad", {"X", "Out@GRAD"}, {"factor"}, {"X@GRAD"});
Y
YuanRisheng 已提交
84 85 86
  }
}

C
Charles-hit 已提交
87 88 89 90 91 92 93 94 95 96 97 98
KernelSignature PowDoubleGradOpArgumentMapping(
    const ArgumentMappingContext& ctx) {
  if (ctx.HasInput("FactorTensor")) {
    return KernelSignature("pow_double_grad",
                           {"X", "DOut", "DDX"},
                           {"FactorTensor"},
                           {"DX", "DDOut"});
  } else {
    return KernelSignature(
        "pow_double_grad", {"X", "DOut", "DDX"}, {"factor"}, {"DX", "DDOut"});
  }
}
C
Charles-hit 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

KernelSignature PowTripleGradOpArgumentMapping(
    const ArgumentMappingContext& ctx) {
  if (ctx.HasInput("FactorTensor")) {
    return KernelSignature("pow_triple_grad",
                           {"X", "DOut", "DDX", "D_DX", "D_DDOut"},
                           {"FactorTensor"},
                           {"D_X", "D_DOut", "D_DDX"});
  } else {
    return KernelSignature("pow_triple_grad",
                           {"X", "DOut", "DDX", "D_DX", "D_DDOut"},
                           {"factor"},
                           {"D_X", "D_DOut", "D_DDX"});
  }
}
114 115
}  // namespace phi

116 117
PD_REGISTER_BASE_KERNEL_NAME(brelu, hard_tanh);
PD_REGISTER_BASE_KERNEL_NAME(brelu_grad, hard_tanh_grad);
118

119 120 121
PD_REGISTER_ARG_MAPPING_FN(mish_grad, phi::MishGradOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(stanh_grad, phi::STanhGradOpArgumentMapping);

122
PD_REGISTER_ARG_MAPPING_FN(brelu_grad, phi::HardTanhGradOpArgumentMapping);
123
PD_REGISTER_ARG_MAPPING_FN(relu6_grad, phi::Relu6GradOpArgumentMapping);
124
PD_REGISTER_ARG_MAPPING_FN(relu6, phi::Relu6OpArgumentMapping);
Y
YuanRisheng 已提交
125 126
PD_REGISTER_ARG_MAPPING_FN(hard_swish_grad,
                           phi::HardSwishGradOpArgumentMapping);
127
PD_REGISTER_ARG_MAPPING_FN(hard_swish, phi::HardSwishOpArgumentMapping);
Y
YuanRisheng 已提交
128
PD_REGISTER_ARG_MAPPING_FN(swish_grad, phi::SwishGradOpArgumentMapping);
129
PD_REGISTER_ARG_MAPPING_FN(swish, phi::SwishOpArgumentMapping);
Y
YuanRisheng 已提交
130
PD_REGISTER_ARG_MAPPING_FN(pow_grad, phi::PowGradOpArgumentMapping);
C
Charles-hit 已提交
131 132
PD_REGISTER_ARG_MAPPING_FN(pow_double_grad,
                           phi::PowDoubleGradOpArgumentMapping);
C
Charles-hit 已提交
133 134
PD_REGISTER_ARG_MAPPING_FN(pow_triple_grad,
                           phi::PowTripleGradOpArgumentMapping);
Y
YuanRisheng 已提交
135
PD_REGISTER_ARG_MAPPING_FN(pow, phi::PowOpArgumentMapping);