-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_instruction_fusion.cc
214 lines (185 loc) · 8.45 KB
/
cpu_instruction_fusion.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Copyright 2017 The OpenXLA Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "xla/service/cpu/cpu_instruction_fusion.h"
#include "xla/hlo/ir/hlo_opcode.h"
#include "xla/service/fusion_node_indexing_evaluation.h"
#include "xla/service/instruction_fusion.h"
#include "xla/service/llvm_ir/fused_ir_emitter.h"
namespace xla {
namespace cpu {
namespace {
bool CanBeLoopFused(const HloInstruction& hlo) {
// These are the only ones we fuse since we rely on effective elemental IR
// generation.
return hlo.IsElementwise() || //
hlo.opcode() == HloOpcode::kBitcast ||
hlo.opcode() == HloOpcode::kBroadcast ||
hlo.opcode() == HloOpcode::kConcatenate ||
hlo.opcode() == HloOpcode::kDynamicSlice ||
hlo.opcode() == HloOpcode::kDynamicUpdateSlice ||
hlo.opcode() == HloOpcode::kGather ||
hlo.opcode() == HloOpcode::kIota || hlo.opcode() == HloOpcode::kPad ||
hlo.opcode() == HloOpcode::kReduce ||
hlo.opcode() == HloOpcode::kReshape ||
hlo.opcode() == HloOpcode::kReverse ||
hlo.opcode() == HloOpcode::kSlice ||
hlo.opcode() == HloOpcode::kTranspose;
}
bool IsNonComplexNonBatchedMatrixVectorDot(const HloInstruction* hlo) {
const Shape& hlo_shape = hlo->shape();
return !ShapeUtil::ElementIsComplex(hlo_shape) &&
hlo->opcode() == HloOpcode::kDot && hlo_shape.dimensions_size() <= 1 &&
hlo->dot_dimension_numbers().lhs_batch_dimensions_size() == 0;
}
bool HasExactlyOneUse(const HloInstruction& hlo_instr) {
return hlo_instr.user_count() == 1 &&
absl::c_count(hlo_instr.users().front()->operands(), &hlo_instr) == 1;
}
bool CanBeOutputFused(const HloInstruction* producer,
const HloInstruction* consumer) {
return consumer->opcode() == HloOpcode::kAdd &&
IsNonComplexNonBatchedMatrixVectorDot(producer) &&
HasExactlyOneUse(*producer) == 1;
}
bool CanBeOutputFusedIntoSomeOperand(const HloInstruction* consumer) {
return consumer->opcode() == HloOpcode::kAdd &&
(CanBeOutputFused(consumer->operand(0), consumer) ||
CanBeOutputFused(consumer->operand(1), consumer));
}
} // namespace
FusionDecision CpuInstructionFusion::ShouldFuse(HloInstruction* consumer,
int64_t operand_index) {
HloInstruction* producer = consumer->mutable_operand(operand_index);
VLOG(2) << "Considering for fusion: operand " << operand_index << " of "
<< consumer->ToString();
constexpr int kFusionThresholdBytes = 16 * 1024;
if (CanBeOutputFused(producer, consumer)) {
VLOG(2) << "Fusion OK: Can create output fusion.";
return {};
}
if (CanBeOutputFusedIntoSomeOperand(producer)) {
return "Bailing because producer can be output-fused into some operand.";
}
if (!CanBeLoopFused(*producer)) {
return "Producer is not loop-fusible.";
}
// Cost condition: not fuse (simple, expensive producers) and (consumers who
// reuse operand elements).
if (producer->opcode() != HloOpcode::kFusion && is_expensive(*producer) &&
ReusesOperandElements(consumer, operand_index)) {
return "Fusion is not profitable.";
}
RETURN_IF_NOT_FUSIBLE(InstructionFusion::ShouldFuse(consumer, operand_index));
// Fuse constants in general but avoid creating 2-instruction fusions with
// just a constant and another node.
if (producer->opcode() == HloOpcode::kConstant &&
consumer->opcode() != HloOpcode::kFusion) {
return "Not fusing: insufficient non-constant nodes.";
}
// Output fusion is not currently supported on CPUs.
if (producer->opcode() == HloOpcode::kFusion) {
return "Not fusing: producer is itself a fusion node.";
}
// Don't fuse if fusing would cause too much code duplication because of
// inefficiencies in the fusion emitter.
// TODO(b/119692968): Remove this once the fusion emitter can handle
// arbitrary fusion nodes.
if (consumer->opcode() == HloOpcode::kFusion) {
if (fusion_node_evaluations_.find(consumer) ==
fusion_node_evaluations_.end()) {
// We have no cached results for this fusion node yet. This can happen
// when we run the InstructionFusion pass more than once. We can only
// cache the results within one run.
fusion_node_evaluations_.emplace(consumer,
FusionNodeIndexingEvaluation(consumer));
}
if (fusion_node_evaluations_.at(consumer).CodeDuplicationTooHigh(
producer)) {
return "Code duplication too high";
}
}
if (consumer->opcode() == HloOpcode::kDot) {
// In the general case we call out to optimized "black box" GEMM routines
// for Dot, which precludes fusion. However, in very specific cases, we try
// to fuse Dot operations by generating an elemental dot implementation.
//
// We need to be careful and conservative here since any benefit we get from
// fusion can easily be overshadowed by the overhead of a naive GEMM
// algorithm in the IR.
const Shape& output_shape = consumer->shape();
if (output_shape.dimensions_size() <= 1) {
// We fuse in cases where we have a matrix*vector or vector*matrix dot and
// fusion can get rid of the larger tensor. We assume that a naive
// traversal of a small enough (to fit in L1) column or row tensor is
// "good enough" from the perspective of cache management; and calling out
// to an optimized GEMM kernel is not a huge win.
if (consumer->operand(0)->shape().rank() == 1 && operand_index == 1 &&
ShapeUtil::ByteSizeOfElements(consumer->operand(0)->shape()) <
kFusionThresholdBytes) {
VLOG(2) << "Fusing small matrix-vector product.";
return {};
} else if (consumer->operand(1)->shape().rank() == 1 &&
operand_index == 0 &&
ShapeUtil::ByteSizeOfElements(consumer->operand(1)->shape()) <
kFusionThresholdBytes) {
VLOG(2) << "Fusing small matrix-vector product.";
return {};
}
}
}
// Don't fuse reductions over the major dimensions. These have an efficient
// lowering that's only implemented for the unfused case.
if (consumer->opcode() == HloOpcode::kReduce &&
!absl::c_linear_search(
consumer->dimensions(),
LayoutUtil::Minor(consumer->operand(0)->shape().layout(), 0))) {
return "Not fusing reductions over major dimensions";
}
if (producer->opcode() == HloOpcode::kReduce &&
!absl::c_linear_search(
producer->dimensions(),
LayoutUtil::Minor(producer->operand(0)->shape().layout(), 0))) {
return "Not fusing reductions over major dimensions";
}
if (consumer->IsLoopFusion()) {
VLOG(2) << "Fusing: consumer is a fusion node.";
return {};
}
if (CanBeLoopFused(*consumer)) {
VLOG(2) << "Fusing: consumer is elementwise or fusible.";
return {};
}
return "Not fusing: not found a fusible case";
}
HloInstruction::FusionKind CpuInstructionFusion::ChooseKind(
const HloInstruction* producer, const HloInstruction* consumer) {
return CanBeOutputFused(producer, consumer)
? HloInstruction::FusionKind::kOutput
: HloInstruction::FusionKind::kLoop;
}
HloInstruction* CpuInstructionFusion::FuseInstruction(
HloInstruction* fusion_instruction, HloInstruction* producer) {
auto evaluation = fusion_node_evaluations_.find(fusion_instruction);
if (evaluation == fusion_node_evaluations_.end()) {
evaluation = fusion_node_evaluations_
.emplace(fusion_instruction,
FusionNodeIndexingEvaluation(fusion_instruction))
.first;
}
auto indexing_users = evaluation->second.RemoveFusionOperand(producer);
HloInstruction* new_producer =
InstructionFusion::FuseInstruction(fusion_instruction, producer);
evaluation->second.UpdateEvaluationCache(new_producer, indexing_users);
return new_producer;
}
} // namespace cpu
} // namespace xla