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/tuple_simplifier.h"
17 
18 #include <queue>
19 
20 #include "tensorflow/compiler/xla/service/hlo_computation.h"
21 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
22 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
23 #include "tensorflow/compiler/xla/status_macros.h"
24 #include "tensorflow/compiler/xla/types.h"
25 #include "tensorflow/compiler/xla/util.h"
26 #include "tensorflow/core/lib/core/errors.h"
27 #include "tensorflow/core/lib/core/status.h"
28 #include "tensorflow/core/platform/logging.h"
29 #include "tensorflow/core/platform/types.h"
30 
31 namespace xla {
32 
TupleSimplifier(bool exclude_entry_computation)33 TupleSimplifier::TupleSimplifier(bool exclude_entry_computation) :
34     exclude_entry_computation_(exclude_entry_computation) {}
35 
Run(HloModule * module)36 StatusOr<bool> TupleSimplifier::Run(HloModule* module) {
37   // Initially add all GTE and Tuple instructions to the worklist.
38   std::queue<HloInstruction*> worklist;
39   for (auto* computation : module->computations()) {
40     if (exclude_entry_computation_ &&
41         computation == module->entry_computation()) {
42       continue;
43     }
44     for (auto* instruction : computation->instructions()) {
45       if (instruction->opcode() == HloOpcode::kTuple ||
46           instruction->opcode() == HloOpcode::kGetTupleElement) {
47         worklist.push(instruction);
48       }
49     }
50   }
51 
52   bool changed = false;
53   while (!worklist.empty()) {
54     HloInstruction* instruction = worklist.front();
55     worklist.pop();
56 
57     if (instruction->user_count() == 0 &&
58         instruction != instruction->parent()->root_instruction()) {
59       // Tuple simplification works by replacing users of optimized away
60       // instructions with a simpler form. If there is no user of the
61       // instruction (including being the root), then there is nothing to do.
62       continue;
63     }
64 
65     if (instruction->opcode() == HloOpcode::kTuple) {
66       // Collapse the following structure into just 'Tuple-shaped Op':
67       //
68       //   Tuple-shaped Op
69       //         |
70       //   +-----+-----+
71       //   |     |     |
72       //  GTE   GTE   GTE
73       //   |     |     |
74       //   +-----+-----+
75       //         |
76       //       Tuple
77       //
78       HloInstruction* top_tuple = nullptr;
79       bool can_simplify = true;
80       for (int64 operand_number = 0;
81            operand_number < instruction->operand_count(); ++operand_number) {
82         HloInstruction* operand = instruction->mutable_operand(operand_number);
83         if (operand->opcode() != HloOpcode::kGetTupleElement ||
84             operand->tuple_index() != operand_number) {
85           can_simplify = false;
86           break;
87         }
88         if (top_tuple == nullptr) {
89           top_tuple = operand->mutable_operand(0);
90           if (!ShapeUtil::Compatible(top_tuple->shape(),
91                                      instruction->shape())) {
92             can_simplify = false;
93             break;
94           }
95         } else if (top_tuple != operand->operand(0)) {
96           can_simplify = false;
97           break;
98         }
99       }
100       if (can_simplify && top_tuple != nullptr) {
101         changed = true;
102         TF_RETURN_IF_ERROR(instruction->ReplaceAllUsesWith(top_tuple));
103         // No need to add anything to the worklist.
104       }
105     } else {
106       CHECK_EQ(instruction->opcode(), HloOpcode::kGetTupleElement);
107       // If possible replace a GTE with the operation which produces the
108       // element. For example, replace uses of GTE with below with just 'Op'
109       // (assuming 'Op' is at the index of the GTE instruction):
110       //
111       //     ...  Op ...
112       //       \  |   /
113       //        Tuple
114       //          |
115       //         GTE
116       if (instruction->operand(0)->opcode() == HloOpcode::kTuple) {
117         HloInstruction* element_source =
118             instruction->mutable_operand(0)->mutable_operand(
119                 instruction->tuple_index());
120         changed = true;
121         TF_RETURN_IF_ERROR(instruction->ReplaceAllUsesWith(element_source));
122         for (HloInstruction* user : element_source->users()) {
123           if (user->opcode() == HloOpcode::kTuple ||
124               user->opcode() == HloOpcode::kGetTupleElement) {
125             worklist.push(user);
126           }
127         }
128       }
129     }
130   }
131 
132   return changed;
133 }
134 
135 }  // namespace xla
136