Home | History | Annotate | Download | only in kernels
      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/computation_builder.h"
     22 #include "tensorflow/compiler/xla/literal_util.h"
     23 #include "tensorflow/core/framework/kernel_def_builder.h"
     24 #include "tensorflow/core/framework/types.h"
     25 #include "tensorflow/core/kernels/no_op.h"
     26 
     27 namespace tensorflow {
     28 namespace {
     29 
     30 class EluOp : public XlaOpKernel {
     31  public:
     32   explicit EluOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
     33   // Computes the max of the scalar input x and 0.
     34   void Compile(XlaOpKernelContext* ctx) override {
     35     xla::ComputationBuilder* b = ctx->builder();
     36     const auto zero = XlaHelpers::Zero(b, input_type(0));
     37     const auto one = XlaHelpers::One(b, input_type(0));
     38     const auto pred = b->Gt(ctx->Input(0), zero);
     39     const auto expm1 = b->Sub(b->Exp(ctx->Input(0)), one);
     40     ctx->SetOutput(0, b->Select(pred, ctx->Input(0), expm1));
     41   }
     42 };
     43 
     44 class EluGradOp : public XlaOpKernel {
     45  public:
     46   explicit EluGradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
     47   // Return the lhs (incoming gradient) if the rhs (input feature) > 0,
     48   // otherwise return lhs * (1 + rhs).
     49   void Compile(XlaOpKernelContext* ctx) override {
     50     xla::ComputationBuilder* b = ctx->builder();
     51     const auto zero = XlaHelpers::Zero(b, input_type(0));
     52     const auto one = XlaHelpers::One(b, input_type(0));
     53     const auto grad = ctx->Input(0);
     54     const auto activation = ctx->Input(1);
     55     const auto exp_grad = b->Mul(grad, b->Add(activation, one));
     56     const auto pred = b->Gt(activation, zero);
     57     ctx->SetOutput(0, b->Select(pred, grad, exp_grad));
     58   }
     59 };
     60 
     61 REGISTER_XLA_OP(Name("Elu"), EluOp);
     62 REGISTER_XLA_OP(Name("EluGrad"), EluGradOp);
     63 
     64 class SeluOp : public XlaOpKernel {
     65  public:
     66   explicit SeluOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
     67   // Computes the max of the scalar input x and 0.
     68   void Compile(XlaOpKernelContext* ctx) override {
     69     xla::ComputationBuilder* b = ctx->builder();
     70     const auto zero = XlaHelpers::Zero(b, input_type(0));
     71     const auto one = XlaHelpers::One(b, input_type(0));
     72     const auto scale = XlaHelpers::FloatLiteral(b, input_type(0),
     73             1.0507009873554804934193349852946);
     74     const auto scale_alpha = XlaHelpers::FloatLiteral(b, input_type(0),
     75             1.7580993408473768599402175208123);
     76     const auto pred = b->Gt(ctx->Input(0), zero);
     77     const auto expm1 = b->Sub(b->Exp(ctx->Input(0)), one);
     78     ctx->SetOutput(0, b->Select(pred, b->Mul(scale, ctx->Input(0)),
     79                                       b->Mul(scale_alpha, expm1)));
     80   }
     81 };
     82 
     83 class SeluGradOp : public XlaOpKernel {
     84  public:
     85   explicit SeluGradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
     86   // Return the lhs (incoming gradient) if the rhs (input feature) > 0,
     87   // otherwise return lhs * (1 + rhs).
     88   void Compile(XlaOpKernelContext* ctx) override {
     89     xla::ComputationBuilder* b = ctx->builder();
     90     const auto zero = XlaHelpers::Zero(b, input_type(0));
     91     const auto one = XlaHelpers::One(b, input_type(0));
     92     const auto scale = XlaHelpers::FloatLiteral(b, input_type(0),
     93             1.0507009873554804934193349852946);
     94     const auto scale_alpha = XlaHelpers::FloatLiteral(b, input_type(0),
     95             1.7580993408473768599402175208123);
     96     const auto grad = ctx->Input(0);
     97     const auto activation = ctx->Input(1);
     98     const auto lin_grad = b->Mul(grad, scale);
     99     const auto exp_grad = b->Mul(grad, b->Add(activation, scale_alpha));
    100     const auto pred = b->Gt(activation, zero);
    101     ctx->SetOutput(0, b->Select(pred, lin_grad, exp_grad));
    102   }
    103 };
    104 
    105 REGISTER_XLA_OP(Name("Selu"), SeluOp);
    106 REGISTER_XLA_OP(Name("SeluGrad"), SeluGradOp);
    107 
    108 }  // namespace
    109 }  // namespace tensorflow
    110