1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16// Native XLA implementations of XLA Relu 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
27namespace tensorflow {
28namespace {
29
30class ReluOp : public XlaOpKernel {
31 public:
32  explicit ReluOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
33  // Computes the max of the scalar input x and 0.
34  void Compile(XlaOpKernelContext* ctx) override {
35    xla::ComputationBuilder* builder = ctx->builder();
36    auto zero = XlaHelpers::Zero(builder, input_type(0));
37    ctx->SetOutput(0, builder->Max(zero, ctx->Input(0)));
38  }
39};
40
41class Relu6Op : public XlaOpKernel {
42 public:
43  explicit Relu6Op(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
44  // Clamp the scalar input between 0 and 6.
45  void Compile(XlaOpKernelContext* ctx) override {
46    xla::ComputationBuilder* builder = ctx->builder();
47    auto zero = XlaHelpers::Zero(builder, input_type(0));
48    auto six = XlaHelpers::IntegerLiteral(builder, input_type(0), 6);
49    ctx->SetOutput(0, builder->Clamp(zero, ctx->Input(0), six));
50  }
51};
52
53class ReluGradOp : public XlaOpKernel {
54 public:
55  explicit ReluGradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
56  // Return the lhs (incoming gradient) if the rhs (input feature) > 0,
57  // otherwise return 0.
58  void Compile(XlaOpKernelContext* ctx) override {
59    xla::ComputationBuilder* b = ctx->builder();
60    const TensorShape shape = ctx->InputShape(0);
61    const auto zero =
62        b->Broadcast(XlaHelpers::Zero(b, input_type(0)), shape.dim_sizes());
63    const auto pred = b->Gt(ctx->Input(1), zero);
64    ctx->SetOutput(0, b->Select(pred, ctx->Input(0), zero));
65  }
66};
67
68class Relu6GradOp : public XlaOpKernel {
69 public:
70  explicit Relu6GradOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
71  // Return the lhs (incoming gradient) if the rhs (input feature) > 0,
72  // otherwise return 0.
73  void Compile(XlaOpKernelContext* ctx) override {
74    xla::ComputationBuilder* b = ctx->builder();
75    const TensorShape shape = ctx->InputShape(0);
76    const auto zero =
77        b->Broadcast(XlaHelpers::Zero(b, input_type(0)), shape.dim_sizes());
78    const auto six = b->Broadcast(
79        XlaHelpers::IntegerLiteral(b, input_type(0), 6), shape.dim_sizes());
80    auto out =
81        b->Select(b->And(b->Lt(ctx->Input(1), six), b->Gt(ctx->Input(1), zero)),
82                  ctx->Input(0), zero);
83    ctx->SetOutput(0, out);
84  }
85};
86
87REGISTER_XLA_OP(Name("Relu"), ReluOp);
88REGISTER_XLA_OP(Name("Relu6"), Relu6Op);
89REGISTER_XLA_OP(Name("ReluGrad"), ReluGradOp);
90REGISTER_XLA_OP(Name("Relu6Grad"), Relu6GradOp);
91
92}  // namespace
93}  // namespace tensorflow
94