| 2683 | } |
| 2684 | |
| 2685 | Status AlgebraicSimplifierVisitor::HandlePad(HloInstruction* pad) { |
| 2686 | if (ShapeUtil::IsZeroElementArray(pad->operand(0)->shape())) { |
| 2687 | return ReplaceWithNewInstruction( |
| 2688 | pad, HloInstruction::CreateBroadcast(pad->shape(), |
| 2689 | pad->mutable_operand(1), {})); |
| 2690 | } |
| 2691 | |
| 2692 | // Interior padding on one sized dimensions have no effect. As a result it |
| 2693 | // makes other simplifications possible if there is no interior padding. |
| 2694 | if (HasInteriorPadding(pad->padding_config())) { |
| 2695 | PaddingConfig padding_config = pad->padding_config(); |
| 2696 | bool cleared_interior_padding = false; |
| 2697 | for (int64 i = 0; i < pad->shape().rank(); ++i) { |
| 2698 | if (padding_config.dimensions(i).interior_padding() > 0 && |
| 2699 | pad->operand(0)->shape().dimensions(i) == 1) { |
| 2700 | cleared_interior_padding = true; |
| 2701 | padding_config.mutable_dimensions(i)->set_interior_padding(0); |
| 2702 | } |
| 2703 | } |
| 2704 | if (cleared_interior_padding) { |
| 2705 | return ReplaceWithNewInstruction( |
| 2706 | pad, |
| 2707 | HloInstruction::CreatePad(pad->shape(), pad->mutable_operand(0), |
| 2708 | pad->mutable_operand(1), padding_config)); |
| 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | // Eliminate nop pads (padding all zero), and replace a pad with negative |
| 2713 | // padding with a pad with non-negative padding followed by a slice. |
| 2714 | bool all_zero = true; |
| 2715 | bool has_negative = false; |
| 2716 | for (auto& padding_dimension : pad->padding_config().dimensions()) { |
| 2717 | if (padding_dimension.edge_padding_low() < 0 || |
| 2718 | padding_dimension.edge_padding_high() < 0) { |
| 2719 | has_negative = true; |
| 2720 | } |
| 2721 | if (padding_dimension.edge_padding_low() != 0 || |
| 2722 | padding_dimension.edge_padding_high() != 0) { |
| 2723 | all_zero = false; |
| 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | if (all_zero) { |
| 2728 | ReplaceInstructionIfSameShape(pad, pad->mutable_operand(0)); |
| 2729 | return Status::OK(); |
| 2730 | } |
| 2731 | |
| 2732 | if (has_negative) { |
| 2733 | // Pad has negative padding. Replace with a pad with the non-negative |
| 2734 | // padding followed by a slice which effectively performs the negative |
| 2735 | // padding. |
| 2736 | // TODO(b/34628603): Add support for negative padding in the backends, or |
| 2737 | // change kPad semantics to disallow negative padding and use slice |
| 2738 | // instead. |
| 2739 | |
| 2740 | // First construct the padding config with non-negative entries and the |
| 2741 | // compute the shape of this new pad instruction. |
| 2742 | PaddingConfig nonzero_padding = pad->padding_config(); |
nothing calls this directly
no test coverage detected