Return the lhs (incoming gradient) if the rhs (input feature) > 0, otherwise return lhs * (1 + rhs).
| 89 | // Return the lhs (incoming gradient) if the rhs (input feature) > 0, |
| 90 | // otherwise return lhs * (1 + rhs). |
| 91 | void Compile(XlaOpKernelContext* ctx) override { |
| 92 | xla::XlaBuilder* b = ctx->builder(); |
| 93 | const auto zero = XlaHelpers::Zero(b, input_type(0)); |
| 94 | const auto scale = XlaHelpers::FloatLiteral(b, input_type(0), |
| 95 | 1.0507009873554804934193349852946); |
| 96 | const auto scale_alpha = XlaHelpers::FloatLiteral(b, input_type(0), |
| 97 | 1.7580993408473768599402175208123); |
| 98 | const auto grad = ctx->Input(0); |
| 99 | const auto activation = ctx->Input(1); |
| 100 | const auto lin_grad = xla::Mul(grad, scale); |
| 101 | const auto exp_grad = xla::Mul(grad, xla::Add(activation, scale_alpha)); |
| 102 | const auto pred = xla::Gt(activation, zero); |
| 103 | ctx->SetOutput(0, xla::Select(pred, lin_grad, exp_grad)); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | REGISTER_XLA_OP(Name("Selu"), SeluOp); |