@@ -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
770757bool 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 }
0 commit comments