MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ChooseIdentityValue

Function ChooseIdentityValue

tensorflow/compiler/xla/service/dynamic_padder.cc:50–124  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

48//
49// nullopt is returned if padding doesn't need to be reset.
50StatusOr<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:

Callers 1

RunMethod · 0.85

Calls 13

MaxValueFunction · 0.85
UnimplementedStrCatFunction · 0.85
IsElementwiseMethod · 0.80
opcodeMethod · 0.80
operand_countMethod · 0.80
mutable_operandMethod · 0.80
ZeroFunction · 0.50
parentMethod · 0.45
element_typeMethod · 0.45
shapeMethod · 0.45
AddInstructionMethod · 0.45
operandMethod · 0.45

Tested by

no test coverage detected