Skip to content

Commit 1fc25ba

Browse files
[XLA] Add a way to enable propagations on trivial window dilations
PiperOrigin-RevId: 368063527 Change-Id: Icffb166a4d65f68de74cddbd577d6d66876fdff5
1 parent fa75751 commit 1fc25ba

3 files changed

Lines changed: 33 additions & 51 deletions

File tree

‎tensorflow/compiler/xla/service/space_to_batch_converter.cc‎

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ class ConvolutionVisitor {
9191
// Function that determines if space-to-batch can be propagated into the
9292
// consumer. Such propagation is only possible when all required operands are
9393
// space-to-batch'ed.
94-
bool CanPropagate(HloInstruction* consumer, HloInstruction* producer,
95-
bool last_try = false);
94+
bool CanPropagate(HloInstruction* consumer, HloInstruction* producer);
9695

9796
// Returns true if the op has all its direct and indirect operands being
9897
// created via broadcasts. Consumer uses op, and is space-to-batched.
@@ -205,9 +204,7 @@ class ConvolutionVisitor {
205204

206205
~ConvolutionVisitor() = default;
207206

208-
explicit ConvolutionVisitor(bool enable_propagations_on_base_dilations,
209-
bool enable_propagations_on_window_dilations,
210-
int64 limit_on_batch_size,
207+
explicit ConvolutionVisitor(SpaceToBatchController ctrl,
211208
HloComputation* computation);
212209

213210
int64 get_chosen_spatial_dim(HloInstruction* convolution) {
@@ -261,32 +258,22 @@ class ConvolutionVisitor {
261258
// Whether rewrite has occurred.
262259
bool changed_ = false;
263260

264-
// Limit on batch size to apply this technique on.
265-
int64 limit_on_batch_size_;
266-
267-
// Controller flags for backprop space-to-batch propagations.
268-
bool enable_propagations_on_base_dilations_;
269-
bool enable_propagations_on_window_dilations_;
270-
271261
// We choose the new batch size to be kNumSplits times that of the old batch
272262
// so that space-to-batch propagation through several convolutional layers is
273263
// consistent.
274264
static constexpr int64 kNumSplits = 8;
275265

276266
// Depth for searching reduce window
277267
static constexpr int64 kReduceWindowSearchDepth = 10;
268+
269+
// Controller for various knobs.
270+
SpaceToBatchController ctrl_;
278271
};
279272

280-
ConvolutionVisitor::ConvolutionVisitor(
281-
bool enable_propagations_on_base_dilations,
282-
bool enable_propagations_on_window_dilations, int64 limit_on_batch_size,
283-
HloComputation* computation) {
273+
ConvolutionVisitor::ConvolutionVisitor(SpaceToBatchController ctrl,
274+
HloComputation* computation) {
275+
ctrl_ = ctrl;
284276
computation_ = computation;
285-
limit_on_batch_size_ = limit_on_batch_size;
286-
enable_propagations_on_base_dilations_ =
287-
enable_propagations_on_base_dilations;
288-
enable_propagations_on_window_dilations_ =
289-
enable_propagations_on_window_dilations;
290277
for (HloInstruction* inst : computation->MakeInstructionPostOrder()) {
291278
if (inst->opcode() != HloOpcode::kConvolution) {
292279
continue;
@@ -336,7 +323,7 @@ bool ConvolutionVisitor::IsConvSuitableForSpaceToBatch(
336323

337324
// TODO(b/168316428): Support base dilations more generically.
338325
if (c.base_dilation_factor != 1) {
339-
if (!enable_propagations_on_base_dilations_) {
326+
if (!ctrl_.enable_propagations_on_base_dilations) {
340327
return false;
341328
}
342329
if (c.stride != 1) {
@@ -361,7 +348,7 @@ bool ConvolutionVisitor::IsConvSuitableForSpaceToBatch(
361348
const int64 old_batch_size =
362349
convolution->operand(0)->shape().dimensions(activations_batch_dim);
363350

364-
if (old_batch_size > limit_on_batch_size_) {
351+
if (old_batch_size > ctrl_.limit_on_batch_size) {
365352
return false;
366353
}
367354

@@ -731,7 +718,7 @@ StatusOr<bool> ConvolutionVisitor::Run() {
731718
producer = instr->mutable_operand(1);
732719
}
733720
if (producer) {
734-
if (CanPropagate(instr, producer, /*last_try=*/true)) {
721+
if (CanPropagate(instr, producer)) {
735722
bool needs_further_propagation;
736723
TF_ASSIGN_OR_RETURN(needs_further_propagation,
737724
Propagate(instr, producer));
@@ -768,7 +755,7 @@ bool IsTrivialElementwise(HloInstruction* hlo) {
768755
}
769756

770757
bool ConvolutionVisitor::CanPropagate(HloInstruction* consumer,
771-
HloInstruction* producer, bool last_try) {
758+
HloInstruction* producer) {
772759
if (IsTrivialElementwise(consumer)) {
773760
VLOG(2) << "Doing propagation check on elementwise op: "
774761
<< consumer->ToString();
@@ -943,7 +930,7 @@ bool ConvolutionVisitor::CanPropagate(HloInstruction* consumer,
943930
return true;
944931
}
945932

946-
if (!enable_propagations_on_window_dilations_) {
933+
if (!ctrl_.enable_propagations_on_window_dilations) {
947934
return false;
948935
}
949936
// Check for space-to-depth readiness here. Note this is not done in
@@ -981,7 +968,9 @@ bool ConvolutionVisitor::CanPropagate(HloInstruction* consumer,
981968
}
982969
// If the rhs_dilation is absent, we want both LHS and RHS to be space-to-
983970
// batched for propagating on backprop convolutions.
984-
if (!last_try || rhs_dilation == 1) {
971+
972+
if (rhs_dilation == 1 &&
973+
!ctrl_.enable_propagations_on_trivial_window_dilations) {
985974
if (!old_to_new_instrs_.contains(kernel) ||
986975
!old_to_new_instrs_.contains(activations)) {
987976
return false;
@@ -3149,9 +3138,7 @@ StatusOr<bool> SpaceToBatchConverter::Run(HloModule* module) {
31493138
bool changed = false;
31503139

31513140
for (auto* comp : module->MakeNonfusionComputations()) {
3152-
ConvolutionVisitor visitor(enable_propagations_on_base_dilations_,
3153-
enable_propagations_on_window_dilations_,
3154-
limit_on_batch_size_, comp);
3141+
ConvolutionVisitor visitor(ctrl_, comp);
31553142
if (visitor.Run().ValueOrDie()) {
31563143
changed = true;
31573144
}

‎tensorflow/compiler/xla/service/space_to_batch_converter.h‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,28 @@ limitations under the License.
2222

2323
namespace xla {
2424

25+
// Controller of various knobs.
26+
struct SpaceToBatchController {
27+
bool enable_propagations_on_base_dilations;
28+
bool enable_propagations_on_window_dilations;
29+
bool enable_propagations_on_trivial_window_dilations;
30+
int64 limit_on_batch_size;
31+
};
32+
2533
// A pass which rewrites convolutions such that space dimension is turned into
2634
// batch.
2735
class SpaceToBatchConverter : public HloModulePass {
2836
public:
29-
explicit SpaceToBatchConverter(bool enable_propagations_on_base_dilations,
30-
bool enable_propagations_on_window_dilations,
31-
int64 limit_on_batch_size = 1)
32-
: limit_on_batch_size_(limit_on_batch_size),
33-
enable_propagations_on_base_dilations_(
34-
enable_propagations_on_base_dilations),
35-
enable_propagations_on_window_dilations_(
36-
enable_propagations_on_window_dilations) {}
37-
38-
explicit SpaceToBatchConverter(int64 limit_on_batch_size = 1)
39-
: SpaceToBatchConverter(/*enable_propagations_on_base_dilations=*/true,
40-
/*enable_propagations_on_window_dilations=*/true,
41-
limit_on_batch_size) {}
37+
explicit SpaceToBatchConverter(SpaceToBatchController ctrl) : ctrl_(ctrl) {}
4238

4339
absl::string_view name() const override { return "space-to-batch-converter"; }
4440

4541
// Run convolution rewriting on the given computation. Returns whether the
4642
// computation was changed.
4743
StatusOr<bool> Run(HloModule* module) override;
4844

49-
int64 limit_on_batch_size_;
50-
bool enable_propagations_on_base_dilations_;
51-
bool enable_propagations_on_window_dilations_;
45+
// Controller for various knobs.
46+
SpaceToBatchController ctrl_;
5247
};
5348

5449
} // namespace xla

‎tensorflow/compiler/xla/service/space_to_batch_converter_test.cc‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ENTRY computation {
4848
ParseAndReturnVerifiedModule(hlo_string));
4949

5050
auto computation = module->entry_computation();
51-
SpaceToBatchConverter converter;
51+
SpaceToBatchConverter converter(SpaceToBatchController{true, true, true, 8});
5252
ASSERT_TRUE(converter.Run(module.get()).ValueOrDie());
5353
HloInstruction* root = computation->root_instruction();
5454
EXPECT_THAT(root, op::Transpose());
@@ -91,7 +91,7 @@ TEST_F(SpaceToBatchConverterTest, SimpleBatch1WithReduceWindow) {
9191
TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
9292
ParseAndReturnVerifiedModule(hlo_string));
9393

94-
SpaceToBatchConverter converter;
94+
SpaceToBatchConverter converter(SpaceToBatchController{true, true, true, 8});
9595
// Test that a reduce window consumer with different rank won't freeze the
9696
// compiler.
9797
ASSERT_TRUE(converter.Run(module.get()).ValueOrDie());
@@ -111,7 +111,7 @@ TEST_F(SpaceToBatchConverterTest, SimpleBatch2) {
111111
TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
112112
ParseAndReturnVerifiedModule(hlo_string));
113113

114-
SpaceToBatchConverter converter;
114+
SpaceToBatchConverter converter(SpaceToBatchController{true, true, true, 1});
115115
ASSERT_FALSE(converter.Run(module.get()).ValueOrDie());
116116
}
117117

@@ -130,7 +130,7 @@ TEST_F(SpaceToBatchConverterTest, Batch1WithStrideAndPad) {
130130
ParseAndReturnVerifiedModule(hlo_string));
131131

132132
auto computation = module->entry_computation();
133-
SpaceToBatchConverter converter(/*limit_on_batch_size=*/4);
133+
SpaceToBatchConverter converter(SpaceToBatchController{true, true, true, 4});
134134
ASSERT_TRUE(converter.Run(module.get()).ValueOrDie());
135135
HloInstruction* root = computation->root_instruction();
136136
EXPECT_THAT(root, op::Transpose());
@@ -163,7 +163,7 @@ ENTRY computation {
163163
ParseAndReturnVerifiedModule(hlo_string));
164164

165165
auto computation = module->entry_computation();
166-
SpaceToBatchConverter converter;
166+
SpaceToBatchConverter converter(SpaceToBatchController{true, true, true, 8});
167167
ASSERT_TRUE(converter.Run(module.get()).ValueOrDie());
168168

169169
HloInstruction* root = computation->root_instruction();

0 commit comments

Comments
 (0)