• 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 simple unary Ops
17 
18 #include "tensorflow/compiler/tf2xla/kernels/cwise_ops.h"
19 #include "tensorflow/compiler/tf2xla/type_util.h"
20 #include "tensorflow/compiler/tf2xla/xla_helpers.h"
21 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
22 #include "tensorflow/compiler/xla/client/client_library.h"
23 #include "tensorflow/compiler/xla/client/lib/arithmetic.h"
24 #include "tensorflow/compiler/xla/client/lib/constants.h"
25 #include "tensorflow/compiler/xla/client/lib/math.h"
26 #include "tensorflow/compiler/xla/client/xla_builder.h"
27 #include "tensorflow/core/framework/kernel_def_builder.h"
28 
29 namespace tensorflow {
30 namespace {
31 
32 #define XLAJIT_MAKE_UNARY(NAME, COMPUTATION)                           \
33   class NAME##Op : public XlaOpKernel {                                \
34    public:                                                             \
35     explicit NAME##Op(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} \
36     void Compile(XlaOpKernelContext* ctx) {                            \
37       xla::XlaBuilder* b = ctx->builder();                             \
38       (void)b;                                                         \
39       xla::XlaOp x = ctx->Input(0);                                    \
40       xla::XlaOp y = COMPUTATION;                                      \
41       ctx->SetOutput(0, y);                                            \
42     }                                                                  \
43   };                                                                   \
44   REGISTER_XLA_OP(Name(#NAME), NAME##Op);
45 
46 XLAJIT_MAKE_UNARY(ComplexAbs, xla::Abs(x));
47 
48 XLAJIT_MAKE_UNARY(Angle, xla::Atan2(xla::Imag(x), xla::Real(x)));
49 
50 XLAJIT_MAKE_UNARY(Conj, xla::Conj(x));
51 
52 // Return x if x>0, otherwise -x.
53 XLAJIT_MAKE_UNARY(Abs, xla::Abs(x));
54 XLAJIT_MAKE_UNARY(Acos, xla::Acos(x));
55 XLAJIT_MAKE_UNARY(Acosh, xla::Acosh(x));
56 XLAJIT_MAKE_UNARY(Asin, xla::Asin(x))
57 XLAJIT_MAKE_UNARY(Asinh, xla::Asinh(x));
58 XLAJIT_MAKE_UNARY(Atan, xla::Atan(x));
59 XLAJIT_MAKE_UNARY(Atanh, xla::Atanh(x));
60 XLAJIT_MAKE_UNARY(Ceil, xla::Ceil(x));
61 XLAJIT_MAKE_UNARY(Cos, xla::Cos(x));
62 XLAJIT_MAKE_UNARY(Cosh, xla::Cosh(x));
63 XLAJIT_MAKE_UNARY(Sin, xla::Sin(x));
64 XLAJIT_MAKE_UNARY(Exp, xla::Exp(x));
65 XLAJIT_MAKE_UNARY(Expm1, xla::Expm1(x));
66 XLAJIT_MAKE_UNARY(Floor, xla::Floor(x));
67 XLAJIT_MAKE_UNARY(IsFinite, xla::IsFinite(x));
68 XLAJIT_MAKE_UNARY(IsInf, xla::IsInf(x));
69 XLAJIT_MAKE_UNARY(IsNan, xla::IsNan(x));
70 // Return 1/x
71 XLAJIT_MAKE_UNARY(Inv, xla::ScalarLike(x, 1.0) / x);
72 XLAJIT_MAKE_UNARY(Reciprocal, xla::ScalarLike(x, 1.0) / x);
73 XLAJIT_MAKE_UNARY(Log, xla::Log(x));
74 XLAJIT_MAKE_UNARY(Log1p, xla::Log1p(x));
75 
76 XLAJIT_MAKE_UNARY(Invert, xla::Not(x));
77 XLAJIT_MAKE_UNARY(LogicalNot, xla::Not(x));
78 XLAJIT_MAKE_UNARY(Neg, -x);
79 
80 XLAJIT_MAKE_UNARY(Rint, xla::RoundToEven(x));
81 XLAJIT_MAKE_UNARY(Round, xla::RoundToEven(x));
82 
83 XLAJIT_MAKE_UNARY(Rsqrt, xla::Rsqrt(x));
84 
85 // Expresses sigmoid as a rescaled tanh: sigmoid(x) == (tanh(x/2) + 1) / 2.
Sigmoid(xla::XlaOp x)86 xla::XlaOp Sigmoid(xla::XlaOp x) {
87   auto half = xla::ScalarLike(x, 0.5);
88   return half + half * xla::Tanh(half * x);
89 }
90 XLAJIT_MAKE_UNARY(Sigmoid, Sigmoid(x));
91 
92 // Returns 0 if x is NaN, 0 if x is 0, -1 if x < 0 and 1 if x > 0.
93 XLAJIT_MAKE_UNARY(Sign,
94                   xla::Select(xla::Ne(x, x), xla::ZerosLike(x), xla::Sign(x)));
95 XLAJIT_MAKE_UNARY(Sinh, xla::Sinh(x));
96 
97 // softplus(x) = log(1 + exp(x))
98 //
99 // This is not numerically stable when x is large, it can easily overflow.
100 // However, we can compute it as LogSumExp(x, 0):
101 //   max(x, 0) + log(exp(x - max(x, 0)) + exp(0 - max(x, 0)))
102 //
103 // This is equivalent to:
104 //   max(x, 0) + log1p(exp(-abs(x)))
105 XLAJIT_MAKE_UNARY(Softplus, xla::Max(x, xla::ScalarLike(x, 0.0)) +
106                                 xla::Log1p(xla::Exp(-xla::Abs(x))));
107 
108 // softsign(x) = x / (abs(x) + 1)
109 XLAJIT_MAKE_UNARY(Softsign, x / (xla::Abs(x) + xla::ScalarLike(x, 1.0)));
110 XLAJIT_MAKE_UNARY(Sqrt, xla::Sqrt(x));
111 XLAJIT_MAKE_UNARY(Square, x* x);
112 XLAJIT_MAKE_UNARY(Tan, xla::Tan(x));
113 XLAJIT_MAKE_UNARY(Tanh, xla::Tanh(x));
114 
115 XLAJIT_MAKE_UNARY(Real, xla::Real(x));
116 XLAJIT_MAKE_UNARY(Imag, xla::Imag(x));
117 XLAJIT_MAKE_UNARY(Erf, xla::Erf(x));
118 XLAJIT_MAKE_UNARY(Erfc, xla::Erfc(x));
119 XLAJIT_MAKE_UNARY(Lgamma, xla::Lgamma(x));
120 XLAJIT_MAKE_UNARY(Digamma, xla::Digamma(x));
121 
122 }  // namespace
123 }  // namespace tensorflow
124