xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/loop_schedule_linearizer_test.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/service/loop_schedule_linearizer.h"
17 
18 #include <set>
19 
20 #include "tensorflow/compiler/xla/debug_options_flags.h"
21 #include "tensorflow/compiler/xla/literal.h"
22 #include "tensorflow/compiler/xla/service/copy_insertion.h"
23 #include "tensorflow/compiler/xla/service/hlo_computation.h"
24 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
25 #include "tensorflow/compiler/xla/service/hlo_matchers.h"
26 #include "tensorflow/compiler/xla/service/hlo_module.h"
27 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
28 #include "tensorflow/compiler/xla/service/hlo_runner.h"
29 #include "tensorflow/compiler/xla/shape_util.h"
30 #include "tensorflow/compiler/xla/test.h"
31 #include "tensorflow/compiler/xla/test_helpers.h"
32 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
33 #include "tensorflow/compiler/xla/xla_data.pb.h"
34 #include "tensorflow/core/platform/test_benchmark.h"
35 
36 namespace xla {
37 namespace {
38 
CountCopies(const HloComputation & computation)39 int64_t CountCopies(const HloComputation& computation) {
40   int64_t count = 0;
41   for (const auto& instruction : computation.instructions()) {
42     if (instruction->opcode() == HloOpcode::kCopy) {
43       count++;
44     }
45   }
46   return count;
47 }
48 
CountCopies(const HloModule & module)49 int64_t CountCopies(const HloModule& module) {
50   int64_t count = 0;
51   for (const auto& computation : module.computations()) {
52     count += CountCopies(*computation);
53   }
54   return count;
55 }
56 
CountControlEdges(const HloComputation & computation)57 int64_t CountControlEdges(const HloComputation& computation) {
58   int64_t count = 0;
59   for (const auto& instruction : computation.instructions()) {
60     count += instruction->control_successors().size();
61   }
62   return count;
63 }
64 
CountControlEdges(const HloModule & module)65 int64_t CountControlEdges(const HloModule& module) {
66   int64_t count = 0;
67   for (const auto& computation : module.computations()) {
68     count += CountControlEdges(*computation);
69   }
70   return count;
71 }
72 
73 class LoopScheduleLinearizerTest : public HloTestBase {
74  protected:
InsertCopies(HloModule * module)75   void InsertCopies(HloModule* module) {
76     LoopScheduleLinearizer loop_schedule_linearizer;
77     ASSERT_IS_OK(loop_schedule_linearizer.Run(module).status());
78 
79     CopyInsertion copy_insertion;
80     ASSERT_IS_OK(copy_insertion.Run(module).status());
81   }
82 };
83 
TEST_F(LoopScheduleLinearizerTest,NoExtraCopiesRequired)84 TEST_F(LoopScheduleLinearizerTest, NoExtraCopiesRequired) {
85   absl::string_view hlo_string = R"(
86 HloModule module
87 
88 while_body {
89   input = (s32[], s32[]) parameter(0)
90   counter = s32[] get-tuple-element(input), index=0
91   buffer = s32[] get-tuple-element(input), index=1
92 
93   one = s32[] constant(1)
94 
95   updated_counter = s32[] add(counter, one)
96 
97   updated_buffer = s32[] add(buffer, counter)
98   ROOT out = (s32[], s32[]) tuple(updated_counter, updated_buffer)
99 }
100 
101 while_cond {
102   input = (s32[], s32[]) parameter(0)
103   counter = s32[] get-tuple-element(input), index=0
104   bound = s32[] constant(100)
105   ROOT cmp = pred[] compare(counter, bound), direction=LT
106 }
107 
108 ENTRY entry {
109   zero = s32[] constant(0)
110   buffer = s32[] parameter(0)
111   while_input = (s32[], s32[]) tuple(zero, buffer)
112   ROOT out = (s32[], s32[]) while(while_input), condition=while_cond, body=while_body
113 }
114 
115   )";
116   TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
117                           ParseAndReturnVerifiedModule(hlo_string));
118   InsertCopies(module.get());
119   EXPECT_EQ(CountCopies(
120                 *module->entry_computation()->root_instruction()->while_body()),
121             0);
122   EXPECT_EQ(CountControlEdges(
123                 *module->entry_computation()->root_instruction()->while_body()),
124             1);
125 }
126 
127 }  // namespace
128 }  // namespace xla
129