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 "tensorflow/compiler/xla/service/while_loop_analysis.h"
17 
18 #include "tensorflow/compiler/xla/service/hlo_parser.h"
19 #include "tensorflow/compiler/xla/test.h"
20 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
21 #include "tensorflow/core/lib/core/status_test_util.h"
22 
23 namespace xla {
24 namespace {
25 
26 class WhileLoopAnalysisTest : public HloTestBase {};
27 
TEST_F(WhileLoopAnalysisTest,SingleIterationUpperBound)28 TEST_F(WhileLoopAnalysisTest, SingleIterationUpperBound) {
29   const char* const kHloModule = R"(
30     HloModule ModuleWithWhile
31 
32     body {
33       p_body = (f32[2], s32[]) parameter(0)
34       val = f32[2] get-tuple-element(p_body), index=0
35       const = s32[] constant(-1)
36       ROOT root = (f32[2], s32[]) tuple(val, const)
37     }
38 
39     condition {
40       p_cond = (f32[2], s32[]) parameter(0)
41       gte = s32[] get-tuple-element(p_cond), index=1
42       const = s32[] constant(42)
43       ROOT result = pred[] compare(gte, const), direction=EQ
44     }
45 
46     ENTRY entry {
47       param.0 = f32[2] parameter(0)
48       param.1 = s32[] parameter(1)
49       while_init = (f32[2], s32[]) tuple(param.0, param.1)
50       ROOT while = (f32[2], s32[]) while(while_init), condition=condition, body=body
51     })";
52   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
53                           ParseAndReturnVerifiedModule(kHloModule));
54 
55   HloInstruction* while_op = module->entry_computation()->root_instruction();
56   EXPECT_EQ(*ComputeWhileLoopTripCountUpperBound(while_op), 1);
57 }
58 
TEST_F(WhileLoopAnalysisTest,NoUpperBound)59 TEST_F(WhileLoopAnalysisTest, NoUpperBound) {
60   const char* const kHloModule = R"(
61     HloModule ModuleWithWhile
62 
63     body {
64       p_body = (f32[2], s32[]) parameter(0)
65       val = f32[2] get-tuple-element(p_body), index=0
66       const = s32[] constant(42)
67       ROOT root = (f32[2], s32[]) tuple(val, const)
68     }
69 
70     condition {
71       p_cond = (f32[2], s32[]) parameter(0)
72       gte = s32[] get-tuple-element(p_cond), index=1
73       const = s32[] constant(42)
74       ROOT result = pred[] compare(gte, const), direction=EQ
75     }
76 
77     ENTRY entry {
78       param.0 = f32[2] parameter(0)
79       param.1 = s32[] parameter(1)
80       while_init = (f32[2], s32[]) tuple(param.0, param.1)
81       ROOT while = (f32[2], s32[]) while(while_init), condition=condition, body=body
82     })";
83   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
84                           ParseAndReturnVerifiedModule(kHloModule));
85 
86   HloInstruction* while_op = module->entry_computation()->root_instruction();
87   EXPECT_EQ(ComputeWhileLoopTripCountUpperBound(while_op), absl::nullopt);
88 }
89 
TEST_F(WhileLoopAnalysisTest,ExactBound)90 TEST_F(WhileLoopAnalysisTest, ExactBound) {
91   const char* const kHloModule = R"(
92     HloModule ModuleWithWhile
93 
94     body {
95       p_body = (f32[2], s32[]) parameter(0)
96       val = f32[2] get-tuple-element(p_body), index=0
97       index = s32[] get-tuple-element(p_body), index=1
98       one = s32[] constant(1)
99       inc = s32[] add(index, one)
100       ROOT root = (f32[2], s32[]) tuple(val, inc)
101     }
102 
103     condition {
104       p_cond = (f32[2], s32[]) parameter(0)
105       gte = s32[] get-tuple-element(p_cond), index=1
106       const = s32[] constant(42)
107       ROOT result = pred[] compare(gte, const), direction=LT
108     }
109 
110     ENTRY entry {
111       param.0 = f32[2] parameter(0)
112       param.1 = s32[] constant(0)
113       while_init = (f32[2], s32[]) tuple(param.0, param.1)
114       ROOT while = (f32[2], s32[]) while(while_init), condition=condition, body=body
115     })";
116   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
117                           ParseAndReturnVerifiedModule(kHloModule));
118 
119   HloInstruction* while_op = module->entry_computation()->root_instruction();
120   EXPECT_EQ(*ComputeWhileLoopTripCountUpperBound(while_op), 42);
121 }
122 
123 }  // namespace
124 }  // namespace xla
125