aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/tf2xla/kernels/relu_op.cc
blob: 2dea4032c0f01163405a62aa36f531893a3f91e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// Native XLA implementations of XLA Relu Ops

#include "tensorflow/compiler/tf2xla/kernels/cwise_ops.h"
#include "tensorflow/compiler/tf2xla/xla_helpers.h"
#include "tensorflow/compiler/tf2xla/xla_op_registry.h"
#include "tensorflow/compiler/xla/client/computation_builder.h"
#include "tensorflow/compiler/xla/literal_util.h"
#include "tensorflow/core/framework/kernel_def_builder.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/kernels/no_op.h"

namespace tensorflow {
namespace {

class ReluOp : public XlaOpKernel {
 public:
  explicit ReluOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
  // Computes the max of the scalar input x and 0.
  void Compile(XlaOpKernelContext* ctx) {
    xla::ComputationBuilder* builder = ctx->builder();
    auto zero = XlaHelpers::Zero(builder, input_type(0));
    ctx->SetOutput(0, builder->Max(zero, ctx->Input(0)));
  }
};

class Relu6Op : public XlaOpKernel {
 public:
  explicit Relu6Op(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
  // Clamp the scalar input between 0 and 6.
  void Compile(XlaOpKernelContext* ctx) {
    xla::ComputationBuilder* builder = ctx->builder();
    auto zero = XlaHelpers::Zero(builder, input_type(0));
    auto six = XlaHelpers::IntegerLiteral(builder, input_type(0), 6);
    ctx->SetOutput(0, builder->Clamp(zero, ctx->Input(0), six));
  }
};

// A subclass of a XlaBinaryMapOp must build the lambda computation
// that describes the (scalar,scalar)->scalar function to apply to
// each element of the input. We have to use XlaBinaryMapOp instead of
// XlaBinaryOp here because XLA Select does not do automatic
// broadcasting.
class ReluGradOp : public XlaBinaryMapOp {
 public:
  explicit ReluGradOp(OpKernelConstruction* ctx) : XlaBinaryMapOp(ctx) {}
  // Return the lhs (incoming gradient) if the rhs (input feature) > 0,
  // otherwise return 0.
  void BuildMapLambda(xla::ComputationBuilder* b,
                      const xla::ComputationDataHandle& gradient,
                      const xla::ComputationDataHandle& feature) override {
    const auto zero = XlaHelpers::Zero(b, input_type(0));
    b->Select(b->Gt(feature, zero), gradient, zero);
  }
};

class Relu6GradOp : public XlaBinaryMapOp {
 public:
  explicit Relu6GradOp(OpKernelConstruction* ctx) : XlaBinaryMapOp(ctx) {}
  // Return the lhs (incoming gradient) if the rhs (input feature) > 0,
  // otherwise return 0.
  void BuildMapLambda(xla::ComputationBuilder* b,
                      const xla::ComputationDataHandle& gradient,
                      const xla::ComputationDataHandle& feature) override {
    const auto zero = XlaHelpers::Zero(b, input_type(0));
    auto six = XlaHelpers::IntegerLiteral(b, input_type(0), 6);
    b->Select(b->LogicalAnd(b->Lt(feature, six), b->Gt(feature, zero)),
              gradient, zero);
  }
};

REGISTER_XLA_OP("Relu", ReluOp);
REGISTER_XLA_OP("Relu6", Relu6Op);
REGISTER_XLA_OP("ReluGrad", ReluGradOp);
REGISTER_XLA_OP("Relu6Grad", Relu6GradOp);

}  // namespace
}  // namespace tensorflow