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/conditional_simplifier.h"
17
18 #include <string>
19 #include <utility>
20 #include <vector>
21
22 #include "absl/strings/str_cat.h"
23 #include "tensorflow/compiler/xla/literal.h"
24 #include "tensorflow/compiler/xla/service/call_inliner.h"
25 #include "tensorflow/compiler/xla/service/hlo_computation.h"
26 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
27 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
28 #include "tensorflow/compiler/xla/status_macros.h"
29 #include "tensorflow/compiler/xla/types.h"
30 #include "tensorflow/compiler/xla/util.h"
31 #include "tensorflow/core/lib/core/errors.h"
32
33 namespace xla {
34
35 // Tries to replace a conditional with a call operation of the corresponding
36 // computation. If the given conditional has a constant branch_index, tries to
37 // replace it with a call to its corresponding branch computation and then
38 // inline that computation.
39 //
40 // Returns true if it made a change to the graph.
TryRemoveConditional(HloInstruction * conditional)41 static StatusOr<bool> TryRemoveConditional(HloInstruction* conditional) {
42 CHECK_EQ(conditional->opcode(), HloOpcode::kConditional);
43 // Do not remove conditionals that contain side-effecting instructions or
44 // have control predecessors/successors in either true/false computation.
45 if (!conditional->parent()->IsRemovable(conditional) ||
46 conditional->HasSideEffect()) {
47 VLOG(2) << "Not attempting to remove conditional as it is not removable or "
48 "has side effect: "
49 << conditional->ToShortString();
50 return false;
51 }
52
53 // We can always inline a 1-branch conditional due to default branch fallback.
54 int branch_index = 0;
55 if (conditional->branch_count() > 1) {
56 if (conditional->operand(0)->opcode() != HloOpcode::kConstant) {
57 VLOG(2) << "Not attempting to remove conditional as its branch_index is "
58 "not a compile-time constant: "
59 << conditional->ToShortString();
60 return false;
61 }
62
63 if (conditional->operand(0)->shape().element_type() == PRED) {
64 branch_index = conditional->operand(0)->literal().Get<bool>({}) ? 0 : 1;
65 } else {
66 branch_index = conditional->operand(0)->literal().Get<int32>({});
67 if (branch_index < 0 || branch_index >= conditional->branch_count()) {
68 branch_index = conditional->branch_count() - 1;
69 }
70 }
71 }
72 auto computation = conditional->parent();
73 HloInstruction* call_op;
74 call_op = computation->AddInstruction(HloInstruction::CreateCall(
75 conditional->shape(), {conditional->mutable_operand(branch_index + 1)},
76 conditional->branch_computation(branch_index)));
77 conditional->SetupDerivedInstruction(call_op);
78 TF_RETURN_IF_ERROR(computation->ReplaceInstruction(conditional, call_op));
79 TF_RETURN_IF_ERROR(CallInliner::Inline(call_op).status());
80
81 return true;
82 }
83
Run(HloModule * module)84 StatusOr<bool> ConditionalSimplifier::Run(HloModule* module) {
85 XLA_VLOG_LINES(
86 3, "ConditionalSimplifier::Run(), before:\n" + module->ToString());
87 bool changed = false;
88
89 // Gather all the conditional ops in our module. We do this ahead of time so
90 // we don't have to worry about mutating the lists of computations or
91 // instructions as we iterate.
92 std::vector<HloInstruction*> conditional_ops;
93 for (auto* comp : module->computations()) {
94 for (auto* instr : comp->instructions()) {
95 if (instr->opcode() == HloOpcode::kConditional) {
96 conditional_ops.push_back(instr);
97 }
98 }
99 }
100
101 for (HloInstruction* conditional_op : conditional_ops) {
102 TF_ASSIGN_OR_RETURN(bool result, TryRemoveConditional(conditional_op));
103 changed |= result;
104 }
105
106 XLA_VLOG_LINES(3,
107 "ConditionalSimplifier::Run(), after:\n" + module->ToString());
108 return changed;
109 }
110
111 } // namespace xla
112