Return the lhs (incoming gradient) if the rhs (input feature) > 0, otherwise return lhs * (1 + rhs).
| 60 | // Return the lhs (incoming gradient) if the rhs (input feature) > 0, |
| 61 | // otherwise return lhs * (1 + rhs). |
| 62 | void Compile(XlaOpKernelContext* ctx) override { |
| 63 | xla::XlaBuilder* b = ctx->builder(); |
| 64 | const auto zero = XlaHelpers::Zero(b, input_type(0)); |
| 65 | const auto one = XlaHelpers::One(b, input_type(0)); |
| 66 | const auto grad = ctx->Input(0); |
| 67 | const auto activation = ctx->Input(1); |
| 68 | const auto exp_grad = xla::Mul(grad, xla::Add(activation, one)); |
| 69 | const auto pred = xla::Gt(activation, zero); |
| 70 | ctx->SetOutput(0, xla::Select(pred, grad, exp_grad)); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | REGISTER_XLA_OP(Name("Elu"), EluOp); |