aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/tf2xla/kernels/unary_ops.cc
blob: e6ec794cfd4103f622f64a113464c2f4cbfd4215 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* 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 simple unary Ops

#include "tensorflow/compiler/tf2xla/kernels/cwise_ops.h"
#include "tensorflow/compiler/tf2xla/type_util.h"
#include "tensorflow/compiler/tf2xla/xla_helpers.h"
#include "tensorflow/compiler/tf2xla/xla_op_registry.h"
#include "tensorflow/compiler/xla/client/client_library.h"
#include "tensorflow/compiler/xla/client/lib/arithmetic.h"
#include "tensorflow/compiler/xla/client/lib/constants.h"
#include "tensorflow/compiler/xla/client/lib/math.h"
#include "tensorflow/compiler/xla/client/xla_client/xla_builder.h"
#include "tensorflow/core/framework/kernel_def_builder.h"

namespace tensorflow {
namespace {

#define XLAJIT_MAKE_UNARY(NAME, COMPUTATION)                           \
  class NAME##Op : public XlaOpKernel {                                \
   public:                                                             \
    explicit NAME##Op(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} \
    void Compile(XlaOpKernelContext* ctx) {                            \
      xla::XlaBuilder* b = ctx->builder();                             \
      (void)b;                                                         \
      xla::XlaOp x = ctx->Input(0);                                    \
      xla::XlaOp y = COMPUTATION;                                      \
      ctx->SetOutput(0, y);                                            \
    }                                                                  \
  };                                                                   \
  REGISTER_XLA_OP(Name(#NAME), NAME##Op);

XLAJIT_MAKE_UNARY(ComplexAbs, xla::Abs(x));

XLAJIT_MAKE_UNARY(Angle, xla::Atan2(xla::Imag(x), xla::Real(x)));

XLAJIT_MAKE_UNARY(Conj, xla::Conj(x));

// Return x if x>0, otherwise -x.
XLAJIT_MAKE_UNARY(Abs, xla::Abs(x));
XLAJIT_MAKE_UNARY(Acos, xla::Acos(x));
XLAJIT_MAKE_UNARY(Acosh, xla::Acosh(x));
XLAJIT_MAKE_UNARY(Asin, xla::Asin(x))
XLAJIT_MAKE_UNARY(Asinh, xla::Asinh(x));
XLAJIT_MAKE_UNARY(Atan, xla::Atan(x));
XLAJIT_MAKE_UNARY(Atanh, xla::Atanh(x));
XLAJIT_MAKE_UNARY(Ceil, xla::Ceil(x));
XLAJIT_MAKE_UNARY(Cos, xla::Cos(x));
XLAJIT_MAKE_UNARY(Cosh, xla::Cosh(x));
XLAJIT_MAKE_UNARY(Sin, xla::Sin(x));
XLAJIT_MAKE_UNARY(Exp, xla::Exp(x));
XLAJIT_MAKE_UNARY(Expm1, xla::Expm1(x));
XLAJIT_MAKE_UNARY(Floor, xla::Floor(x));
XLAJIT_MAKE_UNARY(IsFinite, xla::IsFinite(x));
XLAJIT_MAKE_UNARY(
    IsInf,
    xla::Eq(xla::Abs(x),
            xla::ScalarLike(x, std::numeric_limits<double>::infinity())));
XLAJIT_MAKE_UNARY(IsNan, xla::Ne(x, x));
// Return 1/x
XLAJIT_MAKE_UNARY(Inv, xla::ScalarLike(x, 1.0) / x);
XLAJIT_MAKE_UNARY(Reciprocal, xla::ScalarLike(x, 1.0) / x);
XLAJIT_MAKE_UNARY(Log, xla::Log(x));
XLAJIT_MAKE_UNARY(Log1p, xla::Log1p(x));

XLAJIT_MAKE_UNARY(Invert, xla::Not(x));
XLAJIT_MAKE_UNARY(LogicalNot, xla::Not(x));
XLAJIT_MAKE_UNARY(Neg, -x);

// Implements Banker's rounding: numbers that are equidistant between two
// integers are rounded towards even.
xla::XlaOp RoundToEven(xla::XlaOp x) {
  auto half = xla::ScalarLike(x, 0.5);
  auto one = xla::ScalarLike(x, 1.0);
  auto two = xla::ScalarLike(x, 2.0);

  auto round_val = xla::Floor(x);
  auto fraction = x - round_val;
  auto nearest_even_int = round_val - two * xla::Floor(half * x);
  auto is_odd = xla::Eq(nearest_even_int, one);
  return xla::Select(xla::Or(xla::Gt(fraction, half),
                             xla::And(xla::Eq(fraction, half), is_odd)),
                     round_val + one, round_val);
}

XLAJIT_MAKE_UNARY(Rint, RoundToEven(x));
XLAJIT_MAKE_UNARY(Round, RoundToEven(x));

XLAJIT_MAKE_UNARY(Rsqrt, xla::Rsqrt(x));

// Expresses sigmoid as a rescaled tanh: sigmoid(x) == (tanh(x/2) + 1) / 2.
xla::XlaOp Sigmoid(xla::XlaOp x) {
  auto half = xla::ScalarLike(x, 0.5);
  return half + half * xla::Tanh(half * x);
}
XLAJIT_MAKE_UNARY(Sigmoid, Sigmoid(x));

// Returns 0 if x is 0, -1 if x < 0 and 1 if x > 0.
XLAJIT_MAKE_UNARY(Sign, xla::Sign(x));
XLAJIT_MAKE_UNARY(Sinh, xla::Sinh(x));

// softplus(x) = log(1 + exp(x))
//
// This is not numerically stable when x is large, it can easily overflow.
// However, we can compute it as LogSumExp(x, 0):
//   max(x, 0) + log(exp(x - max(x, 0)) + exp(0 - max(x, 0)))
//
// This is equivalent to:
//   max(x, 0) + log1p(exp(-abs(x)))
XLAJIT_MAKE_UNARY(Softplus, xla::Max(x, xla::ScalarLike(x, 0.0)) +
                                xla::Log1p(xla::Exp(-xla::Abs(x))));

// softsign(x) = x / (abs(x) + 1)
XLAJIT_MAKE_UNARY(Softsign, x / (xla::Abs(x) + xla::ScalarLike(x, 1.0)));
XLAJIT_MAKE_UNARY(Sqrt, xla::Sqrt(x));
XLAJIT_MAKE_UNARY(Square, x* x);
XLAJIT_MAKE_UNARY(Tan, xla::Tan(x));
XLAJIT_MAKE_UNARY(Tanh, xla::Tanh(x));

XLAJIT_MAKE_UNARY(Real, xla::Real(x));
XLAJIT_MAKE_UNARY(Imag, xla::Imag(x));

#undef XLAJIT_MAKE_UNARY

// Erf/Erfc.  For x in (-1, 1), the erf approximation is used; erfc polynomial
// is used outside of this range.
class ErfOp : public XlaOpKernel {
 public:
  explicit ErfOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
  void Compile(XlaOpKernelContext* ctx) override {
    xla::XlaOp x = ctx->Input(0);
    xla::XlaOp one = xla::ScalarLike(x, 1.0);
    auto y =
        xla::Select(xla::Gt(xla::Abs(x), one), one - xla::Erfc(x), xla::Erf(x));
    ctx->SetOutput(0, y);
  }
};
REGISTER_XLA_OP(Name("Erf"), ErfOp);

class ErfcOp : public XlaOpKernel {
 public:
  explicit ErfcOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
  void Compile(XlaOpKernelContext* ctx) override {
    xla::XlaOp x = ctx->Input(0);
    xla::XlaOp one = xla::ScalarLike(x, 1.0);
    auto y =
        xla::Select(xla::Lt(xla::Abs(x), one), one - xla::Erf(x), xla::Erfc(x));
    ctx->SetOutput(0, y);
  }
};
REGISTER_XLA_OP(Name("Erfc"), ErfcOp);

class LgammaOp : public XlaOpKernel {
 public:
  explicit LgammaOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
  // Calculate lgamma using the Lanczos approximation
  // (https://en.wikipedia.org/wiki/Lanczos_approximation).
  void Compile(XlaOpKernelContext* ctx) override {
    xla::XlaOp input = ctx->Input(0);
    xla::PrimitiveType input_type = ctx->input_xla_type(0);

    if (input_type == xla::F16 || input_type == xla::BF16) {
      // The approximation works better with at least 32-bits of accuracy.
      xla::XlaOp input_f32 = xla::ConvertElementType(input, xla::F32);
      xla::XlaOp result_f32 = xla::Lgamma(input_f32);
      xla::XlaOp result_x16 = xla::ConvertElementType(result_f32, input_type);
      ctx->SetOutput(0, result_x16);
    } else {
      xla::XlaOp result = xla::Lgamma(input);
      ctx->SetOutput(0, result);
    }
  }
};  // namespace
REGISTER_XLA_OP(Name("Lgamma"), LgammaOp);

class DigammaOp : public XlaOpKernel {
 public:
  explicit DigammaOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
  // Calculate lgamma using the Lanczos approximation
  // (https://en.wikipedia.org/wiki/Lanczos_approximation).
  void Compile(XlaOpKernelContext* ctx) override {
    xla::XlaOp input = ctx->Input(0);
    xla::PrimitiveType input_type = ctx->input_xla_type(0);

    if (input_type == xla::F16 || input_type == xla::BF16) {
      // The approximation works better with at least 32-bits of accuracy.
      xla::XlaOp input_f32 = xla::ConvertElementType(input, xla::F32);
      xla::XlaOp result_f32 = xla::Digamma(input_f32);
      xla::XlaOp result_x16 = xla::ConvertElementType(result_f32, input_type);
      ctx->SetOutput(0, result_x16);
    } else {
      xla::XlaOp result = xla::Digamma(input);
      ctx->SetOutput(0, result);
    }
  }
};  // namespace
REGISTER_XLA_OP(Name("Digamma"), DigammaOp);

}  // namespace
}  // namespace tensorflow