ChooseIdentityValue looks at the instruction's operand, returns a identity value which, when padded, doesn't change the result of the instruction. nullopt is returned if padding doesn't need to be reset.
| 48 | // |
| 49 | // nullopt is returned if padding doesn't need to be reset. |
| 50 | StatusOr<HloInstruction*> ChooseIdentityValue(HloInstruction* inst, |
| 51 | int64 operand_number) { |
| 52 | HloComputation* comp = inst->parent(); |
| 53 | // Padding on elementwise operation doesn't affect the result of the effective |
| 54 | // data. |
| 55 | if (inst->IsElementwise()) { |
| 56 | return nullptr; |
| 57 | } |
| 58 | |
| 59 | switch (inst->opcode()) { |
| 60 | case HloOpcode::kReduce: { |
| 61 | TF_RET_CHECK(operand_number < inst->operand_count() / 2) |
| 62 | << "Only data operand with dynamic dimension is valid."; |
| 63 | // Variadic reduce has different init value for different operand, given a |
| 64 | // data operand number, find the init value index. |
| 65 | int64 init_value_index = inst->operand_count() / 2 + operand_number; |
| 66 | return inst->mutable_operand(init_value_index); |
| 67 | } |
| 68 | case HloOpcode::kReduceWindow: { |
| 69 | // Because of the way we do reduce, we already require the `init` operand |
| 70 | // of hlo reduce instruction to be identity value. Here we reuse the |
| 71 | // operand. |
| 72 | return inst->mutable_operand(1); |
| 73 | } |
| 74 | |
| 75 | case HloOpcode::kConvolution: |
| 76 | case HloOpcode::kDot: { |
| 77 | // Use 0 as padding value for convolution and dot. |
| 78 | PrimitiveType ptype = inst->shape().element_type(); |
| 79 | return comp->AddInstruction( |
| 80 | HloInstruction::CreateConstant(LiteralUtil::Zero(ptype))); |
| 81 | } |
| 82 | |
| 83 | case HloOpcode::kPad: { |
| 84 | return inst->mutable_operand(1); |
| 85 | } |
| 86 | |
| 87 | case HloOpcode::kSelectAndScatter: { |
| 88 | return inst->mutable_operand(2); |
| 89 | } |
| 90 | case HloOpcode::kScatter: { |
| 91 | if (operand_number != 1) { |
| 92 | return nullptr; |
| 93 | } |
| 94 | PrimitiveType indices_ptype = |
| 95 | inst->operand(operand_number)->shape().element_type(); |
| 96 | |
| 97 | return comp->AddInstruction( |
| 98 | HloInstruction::CreateConstant(LiteralUtil::MaxValue(indices_ptype))); |
| 99 | } |
| 100 | case HloOpcode::kParameter: |
| 101 | case HloOpcode::kGather: |
| 102 | case HloOpcode::kDynamicSlice: |
| 103 | case HloOpcode::kDynamicUpdateSlice: |
| 104 | case HloOpcode::kGetDimensionSize: |
| 105 | case HloOpcode::kSetDimensionSize: |
| 106 | case HloOpcode::kConcatenate: |
| 107 | case HloOpcode::kReshape: |
no test coverage detected