1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 // Native XLA implementations of XLA Elu Ops 17 18 #include "tensorflow/compiler/tf2xla/kernels/cwise_ops.h" 19 #include "tensorflow/compiler/tf2xla/xla_helpers.h" 20 #include "tensorflow/compiler/tf2xla/xla_op_registry.h" 21 #include "tensorflow/compiler/xla/client/xla_builder.h" 22 #include "tensorflow/compiler/xla/literal.h" 23 #include "tensorflow/core/framework/kernel_def_builder.h" 24 #include "tensorflow/core/framework/types.h" 25 26 namespace tensorflow { 27 namespace { 28 29 class EluOp : public XlaOpKernel { 30 public: EluOp(OpKernelConstruction * ctx)31 explicit EluOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 32 // Computes the max of the scalar input x and 0. Compile(XlaOpKernelContext * ctx)33 void Compile(XlaOpKernelContext* ctx) override { 34 xla::XlaBuilder* b = ctx->builder(); 35 const auto zero = XlaHelpers::Zero(b, input_type(0)); 36 const auto pred = xla::Gt(ctx->Input(0), zero); 37 const auto expm1 = xla::Expm1(ctx->Input(0)); 38 ctx->SetOutput(0, xla::Select(pred, ctx->Input(0), expm1)); 39 } 40 }; 41 42 class EluGradOp : public XlaOpKernel { 43 public: EluGradOp(OpKernelConstruction * ctx)44 explicit EluGradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 45 // Return the lhs (incoming gradient) if the rhs (input feature) > 0, 46 // otherwise return lhs * (1 + rhs). Compile(XlaOpKernelContext * ctx)47 void Compile(XlaOpKernelContext* ctx) override { 48 xla::XlaBuilder* b = ctx->builder(); 49 const auto zero = XlaHelpers::Zero(b, input_type(0)); 50 const auto one = XlaHelpers::One(b, input_type(0)); 51 const auto grad = ctx->Input(0); 52 const auto activation = ctx->Input(1); 53 const auto exp_grad = xla::Mul(grad, xla::Add(activation, one)); 54 const auto pred = xla::Gt(activation, zero); 55 ctx->SetOutput(0, xla::Select(pred, grad, exp_grad)); 56 } 57 }; 58 59 REGISTER_XLA_OP(Name("Elu"), EluOp); 60 REGISTER_XLA_OP(Name("EluGrad"), EluGradOp); 61 62 class SeluOp : public XlaOpKernel { 63 public: SeluOp(OpKernelConstruction * ctx)64 explicit SeluOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 65 // Computes the max of the scalar input x and 0. Compile(XlaOpKernelContext * ctx)66 void Compile(XlaOpKernelContext* ctx) override { 67 xla::XlaBuilder* b = ctx->builder(); 68 const auto zero = XlaHelpers::Zero(b, input_type(0)); 69 const auto scale = XlaHelpers::FloatLiteral(b, input_type(0), 70 1.0507009873554804934193349852946); 71 const auto scale_alpha = XlaHelpers::FloatLiteral(b, input_type(0), 72 1.7580993408473768599402175208123); 73 const auto pred = xla::Gt(ctx->Input(0), zero); 74 const auto expm1 = xla::Expm1(ctx->Input(0)); 75 ctx->SetOutput(0, xla::Select(pred, xla::Mul(scale, ctx->Input(0)), 76 xla::Mul(scale_alpha, expm1))); 77 } 78 }; 79 80 class SeluGradOp : public XlaOpKernel { 81 public: SeluGradOp(OpKernelConstruction * ctx)82 explicit SeluGradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 83 // Return the lhs (incoming gradient) if the rhs (input feature) > 0, 84 // otherwise return lhs * (1 + rhs). Compile(XlaOpKernelContext * ctx)85 void Compile(XlaOpKernelContext* ctx) override { 86 xla::XlaBuilder* b = ctx->builder(); 87 const auto zero = XlaHelpers::Zero(b, input_type(0)); 88 const auto scale = XlaHelpers::FloatLiteral(b, input_type(0), 89 1.0507009873554804934193349852946); 90 const auto scale_alpha = XlaHelpers::FloatLiteral(b, input_type(0), 91 1.7580993408473768599402175208123); 92 const auto grad = ctx->Input(0); 93 const auto activation = ctx->Input(1); 94 const auto lin_grad = xla::Mul(grad, scale); 95 const auto exp_grad = xla::Mul(grad, xla::Add(activation, scale_alpha)); 96 const auto pred = xla::Gt(activation, zero); 97 ctx->SetOutput(0, xla::Select(pred, lin_grad, exp_grad)); 98 } 99 }; 100 101 REGISTER_XLA_OP(Name("Selu"), SeluOp); 102 REGISTER_XLA_OP(Name("SeluGrad"), SeluGradOp); 103 104 } // namespace 105 } // namespace tensorflow 106