• 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 #include <limits>
17 #include <string>
18 
19 #include "tensorflow/compiler/xla/client/computation_builder.h"
20 #include "tensorflow/compiler/xla/client/local_client.h"
21 #include "tensorflow/compiler/xla/tests/client_library_test_base.h"
22 #include "tensorflow/compiler/xla/tests/literal_test_util.h"
23 #include "tensorflow/compiler/xla/tests/test_macros.h"
24 #include "tensorflow/core/lib/gtl/array_slice.h"
25 #include "tensorflow/core/lib/strings/str_util.h"
26 #include "tensorflow/core/platform/logging.h"
27 #include "tensorflow/core/platform/test.h"
28 
29 namespace xla {
30 namespace {
31 
32 class FloorCeilTest : public ClientLibraryTestBase {
33  public:
34   enum Function {
35     kFloor,
36     kCeil,
37   };
38 
39   // Runs a computation and comparison on expected vs f(input)
TestR1F32(tensorflow::gtl::ArraySlice<float> input,tensorflow::gtl::ArraySlice<float> expected,Function f)40   void TestR1F32(tensorflow::gtl::ArraySlice<float> input,
41                  tensorflow::gtl::ArraySlice<float> expected, Function f) {
42     LOG(INFO) << "input: {" << tensorflow::str_util::Join(expected, ", ")
43               << "}";
44     ComputationBuilder builder(client_, TestName());
45     auto c = builder.ConstantR1<float>(input);
46     if (f == kCeil) {
47       builder.Ceil(c);
48     } else {
49       ASSERT_EQ(kFloor, f);
50       builder.Floor(c);
51     }
52     ComputeAndCompareR1<float>(&builder, expected, /*arguments=*/{});
53   }
54 
TestR0F32(float input,float expected,Function f)55   void TestR0F32(float input, float expected, Function f) {
56     LOG(INFO) << "input: " << expected;
57     ComputationBuilder builder(client_, TestName());
58     auto c = builder.ConstantR0<float>(input);
59     if (f == kCeil) {
60       builder.Ceil(c);
61     } else {
62       ASSERT_EQ(kFloor, f);
63       builder.Floor(c);
64     }
65     ComputeAndCompareR0<float>(&builder, expected, /*arguments=*/{});
66   }
67 
68   const ErrorSpec error_spec_{0.0001};
69 
70   float infinity_ = std::numeric_limits<float>::infinity();
71   float minus_infinity_ = -std::numeric_limits<float>::infinity();
72 };
73 
74 // Interesting notes:
75 // * if you pass snan the CPU doesn't canonicalize it to qnan.
76 // * passing x86-based CPU's qnan to the GPU makes a different nan
77 //   "7fc00000=nan=nan vs 7fffffff=nan=nan"
78 
XLA_TEST_F(FloorCeilTest,R1S0Floor)79 XLA_TEST_F(FloorCeilTest, R1S0Floor) { TestR1F32({}, {}, kFloor); }
80 
TEST_F(FloorCeilTest,R1Floor)81 TEST_F(FloorCeilTest, R1Floor) {
82   TestR1F32({0.0, -0.0, infinity_, minus_infinity_, 1.1, -0.1},
83             {0.0, -0.0, infinity_, minus_infinity_, 1.0, -1.0}, kFloor);
84 }
85 
TEST_F(FloorCeilTest,R1Ceil)86 TEST_F(FloorCeilTest, R1Ceil) {
87   TestR1F32({0.0, -0.0, infinity_, minus_infinity_, 1.1, -0.1},
88             {0.0, -0.0, infinity_, minus_infinity_, 2.0, -0.0}, kCeil);
89 }
90 
TEST_F(FloorCeilTest,R0Floor)91 TEST_F(FloorCeilTest, R0Floor) {
92   TestR0F32(0.0, 0.0, kFloor);
93   TestR0F32(-0.0, -0.0, kFloor);
94   TestR0F32(infinity_, infinity_, kFloor);
95   TestR0F32(minus_infinity_, minus_infinity_, kFloor);
96   TestR0F32(1.1, 1.0, kFloor);
97   TestR0F32(-0.1, -1.0, kFloor);
98 }
99 
TEST_F(FloorCeilTest,R0Ceil)100 TEST_F(FloorCeilTest, R0Ceil) {
101   TestR0F32(0.0, 0.0, kCeil);
102   TestR0F32(-0.0, -0.0, kCeil);
103   TestR0F32(infinity_, infinity_, kCeil);
104   TestR0F32(minus_infinity_, minus_infinity_, kCeil);
105   TestR0F32(1.1, 2.0, kCeil);
106   TestR0F32(-0.1, -0.0, kCeil);
107 }
108 
109 }  // namespace
110 }  // namespace xla
111