• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Relu Ops
17 
18 #include "tensorflow/compiler/tf2xla/xla_helpers.h"
19 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
20 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
21 #include "tensorflow/compiler/xla/client/lib/constants.h"
22 #include "tensorflow/compiler/xla/client/xla_builder.h"
23 #include "tensorflow/compiler/xla/literal.h"
24 
25 namespace tensorflow {
26 namespace {
27 
28 class ReluOp : public XlaOpKernel {
29  public:
ReluOp(OpKernelConstruction * ctx)30   explicit ReluOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
31   // Computes the max of the scalar input x and 0.
Compile(XlaOpKernelContext * ctx)32   void Compile(XlaOpKernelContext* ctx) override {
33     xla::XlaBuilder* builder = ctx->builder();
34     auto zero = XlaHelpers::Zero(builder, input_type(0));
35     ctx->SetOutput(0, xla::Max(zero, ctx->Input(0)));
36   }
37 };
38 REGISTER_XLA_OP(Name("Relu"), ReluOp);
39 
40 class Relu6Op : public XlaOpKernel {
41  public:
Relu6Op(OpKernelConstruction * ctx)42   explicit Relu6Op(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
43   // Clamp the scalar input between 0 and 6.
Compile(XlaOpKernelContext * ctx)44   void Compile(XlaOpKernelContext* ctx) override {
45     xla::XlaBuilder* builder = ctx->builder();
46     auto zero = XlaHelpers::Zero(builder, input_type(0));
47     auto six = XlaHelpers::IntegerLiteral(builder, input_type(0), 6);
48     ctx->SetOutput(0, xla::Clamp(zero, ctx->Input(0), six));
49   }
50 };
51 REGISTER_XLA_OP(Name("Relu6"), Relu6Op);
52 
53 class LeakyReluOp : public XlaOpKernel {
54  public:
LeakyReluOp(OpKernelConstruction * ctx)55   explicit LeakyReluOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
56     OP_REQUIRES_OK(ctx, ctx->GetAttr("alpha", &alpha_));
57   }
Compile(XlaOpKernelContext * ctx)58   void Compile(XlaOpKernelContext* ctx) override {
59     auto features = ctx->Input("features");
60     auto output =
61         xla::Max(features, features * xla::ScalarLike(features, alpha_));
62     ctx->SetOutput(0, output);
63   }
64   float alpha_;
65 };
66 REGISTER_XLA_OP(Name("LeakyRelu"), LeakyReluOp);
67 
68 class ReluGradOp : public XlaOpKernel {
69  public:
ReluGradOp(OpKernelConstruction * ctx)70   explicit ReluGradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
71   // Return the lhs (incoming gradient) if the rhs (input feature) > 0,
72   // otherwise return 0.
Compile(XlaOpKernelContext * ctx)73   void Compile(XlaOpKernelContext* ctx) override {
74     xla::XlaBuilder* b = ctx->builder();
75     const TensorShape shape = ctx->InputShape(0);
76     const auto zero =
77         xla::Broadcast(XlaHelpers::Zero(b, input_type(0)), shape.dim_sizes());
78     const auto pred = xla::Gt(ctx->Input(1), zero);
79     ctx->SetOutput(0, xla::Select(pred, ctx->Input(0), zero));
80   }
81 };
82 REGISTER_XLA_OP(Name("ReluGrad"), ReluGradOp);
83 
84 class Relu6GradOp : public XlaOpKernel {
85  public:
Relu6GradOp(OpKernelConstruction * ctx)86   explicit Relu6GradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
87   // Return the lhs (incoming gradient) if the rhs (input feature) > 0,
88   // otherwise return 0.
Compile(XlaOpKernelContext * ctx)89   void Compile(XlaOpKernelContext* ctx) override {
90     xla::XlaBuilder* b = ctx->builder();
91     const TensorShape shape = ctx->InputShape(0);
92     const auto zero =
93         xla::Broadcast(XlaHelpers::Zero(b, input_type(0)), shape.dim_sizes());
94     const auto six = xla::Broadcast(
95         XlaHelpers::IntegerLiteral(b, input_type(0), 6), shape.dim_sizes());
96     auto out = xla::Select(
97         xla::And(xla::Lt(ctx->Input(1), six), xla::Gt(ctx->Input(1), zero)),
98         ctx->Input(0), zero);
99     ctx->SetOutput(0, out);
100   }
101 };
102 REGISTER_XLA_OP(Name("Relu6Grad"), Relu6GradOp);
103 
104 class LeakyReluGradOp : public XlaOpKernel {
105  public:
LeakyReluGradOp(OpKernelConstruction * ctx)106   explicit LeakyReluGradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
107     OP_REQUIRES_OK(ctx, ctx->GetAttr("alpha", &alpha_));
108   }
Compile(XlaOpKernelContext * ctx)109   void Compile(XlaOpKernelContext* ctx) override {
110     auto gradients = ctx->Input("gradients");
111     auto features = ctx->Input("features");
112     auto output =
113         xla::Select(xla::Gt(features, xla::ScalarLike(features, 0)), gradients,
114                     gradients * xla::ScalarLike(gradients, alpha_));
115     ctx->SetOutput(0, output);
116   }
117   float alpha_;
118 };
119 REGISTER_XLA_OP(Name("LeakyReluGrad"), LeakyReluGradOp);
120 
121 }  // namespace
122 }  // namespace tensorflow
123