aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/tests/floor_ceil_test.cc
blob: 30dc639f117b9871238f0bf1628502cf8bef2e0c (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
/* 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.
==============================================================================*/

#include <limits>
#include <string>

#include "tensorflow/compiler/xla/client/local_client.h"
#include "tensorflow/compiler/xla/client/xla_client/xla_builder.h"
#include "tensorflow/compiler/xla/tests/client_library_test_base.h"
#include "tensorflow/compiler/xla/tests/literal_test_util.h"
#include "tensorflow/compiler/xla/tests/test_macros.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/test.h"

namespace xla {
namespace {

class FloorCeilTest : public ClientLibraryTestBase {
 public:
  enum Function {
    kFloor,
    kCeil,
  };

  // Runs a computation and comparison on expected vs f(input)
  void TestR1F32(tensorflow::gtl::ArraySlice<float> input,
                 tensorflow::gtl::ArraySlice<float> expected, Function f) {
    LOG(INFO) << "input: {" << tensorflow::str_util::Join(expected, ", ")
              << "}";
    XlaBuilder builder(TestName());
    auto c = ConstantR1<float>(&builder, input);
    if (f == kCeil) {
      Ceil(c);
    } else {
      ASSERT_EQ(kFloor, f);
      Floor(c);
    }
    ComputeAndCompareR1<float>(&builder, expected, /*arguments=*/{});
  }

  void TestR0F32(float input, float expected, Function f) {
    LOG(INFO) << "input: " << expected;
    XlaBuilder builder(TestName());
    auto c = ConstantR0<float>(&builder, input);
    if (f == kCeil) {
      Ceil(c);
    } else {
      ASSERT_EQ(kFloor, f);
      Floor(c);
    }
    ComputeAndCompareR0<float>(&builder, expected, /*arguments=*/{});
  }

  const ErrorSpec error_spec_{0.0001};

  float infinity_ = std::numeric_limits<float>::infinity();
  float minus_infinity_ = -std::numeric_limits<float>::infinity();
};

// Interesting notes:
// * if you pass snan the CPU doesn't canonicalize it to qnan.
// * passing x86-based CPU's qnan to the GPU makes a different nan
//   "7fc00000=nan=nan vs 7fffffff=nan=nan"

XLA_TEST_F(FloorCeilTest, R1S0Floor) { TestR1F32({}, {}, kFloor); }

TEST_F(FloorCeilTest, R1Floor) {
  TestR1F32({0.0, -0.0, infinity_, minus_infinity_, 1.1, -0.1},
            {0.0, -0.0, infinity_, minus_infinity_, 1.0, -1.0}, kFloor);
}

TEST_F(FloorCeilTest, R1Ceil) {
  TestR1F32({0.0, -0.0, infinity_, minus_infinity_, 1.1, -0.1},
            {0.0, -0.0, infinity_, minus_infinity_, 2.0, -0.0}, kCeil);
}

TEST_F(FloorCeilTest, R0Floor) {
  TestR0F32(0.0, 0.0, kFloor);
  TestR0F32(-0.0, -0.0, kFloor);
  TestR0F32(infinity_, infinity_, kFloor);
  TestR0F32(minus_infinity_, minus_infinity_, kFloor);
  TestR0F32(1.1, 1.0, kFloor);
  TestR0F32(-0.1, -1.0, kFloor);
}

TEST_F(FloorCeilTest, R0Ceil) {
  TestR0F32(0.0, 0.0, kCeil);
  TestR0F32(-0.0, -0.0, kCeil);
  TestR0F32(infinity_, infinity_, kCeil);
  TestR0F32(minus_infinity_, minus_infinity_, kCeil);
  TestR0F32(1.1, 2.0, kCeil);
  TestR0F32(-0.1, -0.0, kCeil);
}

}  // namespace
}  // namespace xla