1 /* Copyright 2018 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 #include "tensorflow/compiler/xla/service/llvm_ir/math_ops.h"
17 #include "tensorflow/compiler/xla/service/llvm_ir/llvm_util.h"
18 
19 namespace xla {
20 namespace llvm_ir {
21 
EmitFastTanh(llvm::IRBuilder<> * b,llvm::Value * input)22 llvm::Value* EmitFastTanh(llvm::IRBuilder<>* b, llvm::Value* input) {
23   llvm::Type* type = input->getType();
24 
25   // For small values of x, we can approximate tanh(x)=x. For extremely small
26   // values of x (|x| < 1e-37), the other approximation evaluates tanh(x) = 0.
27   const auto kCanUseApprox = 0.0004;
28   auto abs_x =
29       llvm_ir::EmitCallToIntrinsic(llvm::Intrinsic::fabs, {input}, {type}, b);
30   auto use_aprox =
31       b->CreateFCmpOLT(abs_x, llvm::ConstantFP::get(type, kCanUseApprox));
32 
33   // Clamp the input to [-9, 9].
34   //
35   // To simplify the code base until it's an issue, don't have a slow min/max in
36   // this approximation.
37   llvm::Value* input_clamped = llvm_ir::EmitFloatMin(
38       llvm_ir::EmitFloatMax(input, llvm::ConstantFP::get(type, -9.0), b,
39                             /*enable_fast_min_max=*/true),
40       llvm::ConstantFP::get(type, 9.0), b, /*enable_fast_min_max=*/true);
41 
42   static constexpr std::array<float, 7> numerator_coeffs{
43       -2.76076847742355e-16f, 2.00018790482477e-13f, -8.60467152213735e-11f,
44       5.12229709037114e-08f,  1.48572235717979e-05f, 6.37261928875436e-04f,
45       4.89352455891786e-03f};
46 
47   static constexpr std::array<float, 4> denominator_coeffs{
48       1.19825839466702e-06f, 1.18534705686654e-04f, 2.26843463243900e-03f,
49       4.89352518554385e-03f};
50 
51   llvm::Value* input_squared = b->CreateFMul(input_clamped, input_clamped);
52   llvm::Value* numerator = llvm::ConstantFP::get(type, numerator_coeffs[0]);
53   for (int i = 1; i < numerator_coeffs.size(); i++) {
54     numerator = b->CreateFAdd(b->CreateFMul(input_squared, numerator),
55                               llvm::ConstantFP::get(type, numerator_coeffs[i]));
56   }
57 
58   numerator = b->CreateFMul(input_clamped, numerator);
59 
60   llvm::Value* denominator = llvm::ConstantFP::get(type, denominator_coeffs[0]);
61   for (int i = 1; i < denominator_coeffs.size(); i++) {
62     denominator =
63         b->CreateFAdd(b->CreateFMul(input_squared, denominator),
64                       llvm::ConstantFP::get(type, denominator_coeffs[i]));
65   }
66 
67   return b->CreateSelect(use_aprox, input,
68                          b->CreateFDiv(numerator, denominator));
69 }
70 
71 }  // namespace llvm_ir
72 }  // namespace xla
73