1 /* Copyright 2020 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/tests/client_library_test_base.h" 17 #include "tensorflow/compiler/xla/tests/exhaustive_op_test_utils.h" 18 #include "tensorflow/compiler/xla/util.h" 19 20 #ifdef __FAST_MATH__ 21 #error "Can't be compiled with fast math on" 22 #endif 23 24 namespace xla { 25 namespace exhaustive_op_test { 26 27 // Exhaustive test for unary operations for double. 28 // 29 // Test parameter is a tuple containing 30 // - primitive type under test, 31 // - FpValues representing a set of double values. 32 33 class ExhaustiveF64UnaryTest : public ExhaustiveUnaryTest<F64>, 34 public ::testing::WithParamInterface<FpValues> { 35 private: GetInputSize()36 int64_t GetInputSize() override { 37 FpValues values = GetParam(); 38 return values.GetTotalNumValues(); 39 } 40 FillInput(std::array<Literal,1> * input_literal)41 void FillInput(std::array<Literal, 1>* input_literal) override { 42 FpValues fp_values = GetParam(); 43 int64_t input_size = (*input_literal)[0].element_count(); 44 LOG(INFO) << "Checking fp values " << fp_values.ToString() << ", " 45 << input_size; 46 absl::Span<double> input_arr = (*input_literal)[0].data<double>(); 47 48 uint64_t i = 0; 49 for (auto bits : fp_values) { 50 input_arr[i] = this->ConvertAndReplaceKnownIncorrectValueWith(bits, 1); 51 ++i; 52 } 53 CHECK_EQ(i, input_size); 54 } 55 }; 56 57 #define UNARY_TEST_FLOAT_64(test_name, ...) \ 58 XLA_TEST_P(ExhaustiveF64UnaryTest, test_name) \ 59 __VA_ARGS__ 60 61 UNARY_TEST_FLOAT_64(Log, { Run(Log, std::log); }) 62 63 UNARY_TEST_FLOAT_64(Log1p, { Run(Log1p, std::log1p); }) 64 65 UNARY_TEST_FLOAT_64(Exp, { Run(Exp, std::exp); }) 66 67 UNARY_TEST_FLOAT_64(Expm1, { Run(Expm1, std::expm1); }) 68 69 // TODO(b/138385863): Turn on the test for GPU after fixing the bug. 70 UNARY_TEST_FLOAT_64(DISABLED_ON_GPU(PowOneHalf), { __anon8879ffe10102(XlaOp x) 71 Run([](XlaOp x) { return Pow(x, ScalarLike(x, 0.5)); }, __anon8879ffe10202(double x) 72 +[](double x) { return std::pow(x, 0.5); }); 73 }) 74 75 UNARY_TEST_FLOAT_64(Rsqrt, { 76 Run( __anon8879ffe10302(double x) 77 Rsqrt, +[](double x) { return 1 / std::sqrt(x); }); 78 }) 79 80 UNARY_TEST_FLOAT_64(Sqrt, { Run(Sqrt, std::sqrt); }) 81 82 UNARY_TEST_FLOAT_64(Acosh, { Run(Acosh, std::acosh); }) 83 84 UNARY_TEST_FLOAT_64(Asinh, { Run(Asinh, std::asinh); }) 85 86 UNARY_TEST_FLOAT_64(Atanh, { Run(Atanh, std::atanh); }) 87 88 UNARY_TEST_FLOAT_64(Acos, { Run(Acos, std::acos); }) 89 90 UNARY_TEST_FLOAT_64(Asin, { Run(Asin, std::asin); }) 91 92 UNARY_TEST_FLOAT_64(Cosh, { Run(Cosh, std::cosh); }) 93 94 UNARY_TEST_FLOAT_64(Sinh, { Run(Sinh, std::sinh); }) 95 96 UNARY_TEST_FLOAT_64(Tanh, { 97 ErrorSpecGen error_spec_gen = GetDefaultSpecGenerator(); 98 if (platform_ == "CUDA") { __anon8879ffe10402(NativeT x) 99 error_spec_gen = +[](NativeT x) { 100 return x <= static_cast<NativeT>(-20.0) || x >= static_cast<NativeT>(20.0) 101 ? ErrorSpec{0, 0} 102 : GetDefaultSpecGenerator()(x); 103 }; 104 } 105 Run(Tanh, std::tanh, error_spec_gen); 106 }) 107 108 UNARY_TEST_FLOAT_64(Cos, { Run(Cos, std::cos); }) 109 110 UNARY_TEST_FLOAT_64(Sin, { Run(Sin, std::sin); }) 111 112 UNARY_TEST_FLOAT_64(Tan, { Run(Tan, std::tan); }) 113 114 UNARY_TEST_FLOAT_64(Round, { Run(Round, std::round); }) 115 116 UNARY_TEST_FLOAT_64(Erf, { __anon8879ffe10502(NativeT x) 117 Run(Erf, std::erf, [](NativeT x) { return ErrorSpec{1e-20, 1e-20}; }); 118 }) 119 120 UNARY_TEST_FLOAT_64(Erfc, { __anon8879ffe10602(NativeT x) 121 Run(Erfc, std::erfc, [](NativeT x) { return ErrorSpec{1e-20, 1e-20}; }); 122 }) 123 124 INSTANTIATE_TEST_SUITE_P( 125 SpecialValues, ExhaustiveF64UnaryTest, 126 ::testing::ValuesIn(CreateFpValuesForBoundaryTest<double>())); 127 128 INSTANTIATE_TEST_SUITE_P(NormalValues, ExhaustiveF64UnaryTest, 129 ::testing::Values(GetNormals<double>(1000))); 130 131 // Tests a total of 4000000000 inputs, with 16000000 inputs in each sub-test, to 132 // keep the peak memory usage low. 133 INSTANTIATE_TEST_SUITE_P( 134 LargeAndSmallMagnitudeNormalValues, ExhaustiveF64UnaryTest, 135 ::testing::ValuesIn(GetFpValuesForMagnitudeExtremeNormals<double>( 136 4000000000ull, 16000000))); 137 138 } // namespace exhaustive_op_test 139 } // namespace xla 140