1 /* Copyright 2021 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/literal_util.h"
17 #include "tensorflow/compiler/xla/service/gpu/tests/gpu_codegen_test.h"
18
19 namespace xla {
20 namespace gpu {
21
22 namespace {
23
24 class PredArithmeticTest : public GpuCodegenTest {
25 protected:
RunAndCompareTruthTable(const char * const hlo_text)26 void RunAndCompareTruthTable(const char* const hlo_text) {
27 auto false_literal = LiteralUtil::CreateR0(false);
28 auto true_literal = LiteralUtil::CreateR0(true);
29
30 TF_ASSERT_OK_AND_ASSIGN(auto module,
31 ParseAndReturnVerifiedModule(hlo_text));
32
33 // Each call to RunAndCompareNoHloPasses insists on taking ownership and
34 // deleting the module, so just Clone it.
35
36 EXPECT_TRUE(RunAndCompareNoHloPasses(
37 module->Clone(), {&false_literal, &false_literal}, std::nullopt));
38
39 EXPECT_TRUE(RunAndCompareNoHloPasses(
40 module->Clone(), {&false_literal, &true_literal}, std::nullopt));
41
42 EXPECT_TRUE(RunAndCompareNoHloPasses(
43 module->Clone(), {&true_literal, &false_literal}, std::nullopt));
44
45 EXPECT_TRUE(RunAndCompareNoHloPasses(
46 module->Clone(), {&true_literal, &true_literal}, std::nullopt));
47 }
48 };
49
TEST_F(PredArithmeticTest,AddPreds)50 TEST_F(PredArithmeticTest, AddPreds) {
51 const char* hlo_text = R"(
52 HloModule TestModule
53
54 ENTRY compare {
55 x = pred[] parameter(0)
56 y = pred[] parameter(1)
57 ROOT z = pred[] add(x, y)
58 }
59
60 )";
61
62 RunAndCompareTruthTable(hlo_text);
63 }
64
TEST_F(PredArithmeticTest,MulPreds)65 TEST_F(PredArithmeticTest, MulPreds) {
66 const char* hlo_text = R"(
67 HloModule TestModule
68
69 ENTRY compare {
70 x = pred[] parameter(0)
71 y = pred[] parameter(1)
72 ROOT z = pred[] multiply(x, y)
73 }
74
75 )";
76
77 RunAndCompareTruthTable(hlo_text);
78 }
79
TEST_F(PredArithmeticTest,MaxPreds)80 TEST_F(PredArithmeticTest, MaxPreds) {
81 const char* hlo_text = R"(
82 HloModule TestModule
83
84 ENTRY compare {
85 x = pred[] parameter(0)
86 y = pred[] parameter(1)
87 ROOT z = pred[] maximum(x, y)
88 }
89
90 )";
91
92 RunAndCompareTruthTable(hlo_text);
93 }
94
TEST_F(PredArithmeticTest,MinPreds)95 TEST_F(PredArithmeticTest, MinPreds) {
96 const char* hlo_text = R"(
97 HloModule TestModule
98
99 ENTRY compare {
100 x = pred[] parameter(0)
101 y = pred[] parameter(1)
102 ROOT z = pred[] minimum(x, y)
103 }
104
105 )";
106
107 RunAndCompareTruthTable(hlo_text);
108 }
109
110 } // namespace
111 } // namespace gpu
112 } // namespace xla
113