| 64 | } |
| 65 | |
| 66 | Status MapInlinerVisitor::HandleMap(HloInstruction* map) { |
| 67 | HloComputation* function = map->to_apply(); |
| 68 | HloInstruction& root = *function->root_instruction(); |
| 69 | // Only inlining functions that are simply a single operation until a better |
| 70 | // profitability model for inlining is defined. |
| 71 | if (hlo_query::AllOperandsAreParameters(root)) { |
| 72 | if (root.opcode() == HloOpcode::kFusion || |
| 73 | root.opcode() == HloOpcode::kTrace) { |
| 74 | // Cloning not supported for these instructions. |
| 75 | return Status::OK(); |
| 76 | } |
| 77 | VLOG(10) << "inlining map({X ... Y}, op) => : op(X ... Y) with function " |
| 78 | << root.ToShortString(); |
| 79 | if (root.opcode() == HloOpcode::kParameter) { |
| 80 | // If the root is a parameter, then use the corresponding operand as the |
| 81 | // result of the computation. |
| 82 | TF_RETURN_IF_ERROR( |
| 83 | map->ReplaceAllUsesWith(map->operands()[root.parameter_number()])); |
| 84 | TF_RETURN_IF_ERROR(computation_->RemoveInstruction(map)); |
| 85 | } else if (root.opcode() == HloOpcode::kConstant) { |
| 86 | // If the input is a constant then the shape of the constant could be |
| 87 | // different than the map shape. Hence, a broadcast is needed, else the |
| 88 | // cloned operand with new shape and operands work. |
| 89 | // |
| 90 | // The constant is in an embedded computation and needs to be recreated |
| 91 | // as part of the computation that the broadcast is inserted into. |
| 92 | HloInstruction* constant = computation_->AddInstruction(root.Clone()); |
| 93 | HloInstruction* placed_instruction = computation_->AddInstruction( |
| 94 | HloInstruction::CreateBroadcast(map->shape(), constant, {})); |
| 95 | TF_RETURN_IF_ERROR( |
| 96 | computation_->ReplaceInstruction(map, placed_instruction)); |
| 97 | } else { |
| 98 | std::vector<HloInstruction*> params; |
| 99 | for (int64 o = 0; o < root.operands().size(); o++) { |
| 100 | params.push_back(map->operands()[root.operand(o)->parameter_number()]); |
| 101 | } |
| 102 | HloInstruction* placed_instruction = computation_->AddInstruction( |
| 103 | root.CloneWithNewOperands(map->shape(), params)); |
| 104 | TF_RETURN_IF_ERROR( |
| 105 | computation_->ReplaceInstruction(map, placed_instruction)); |
| 106 | } |
| 107 | changed_ = true; |
| 108 | return Status::OK(); |
| 109 | } |
| 110 | |
| 111 | return Status::OK(); |
| 112 | } |
| 113 | |
| 114 | StatusOr<bool> MapInliner::Run(HloModule* module) { |
| 115 | MapInlinerVisitor visitor(/*computation=*/nullptr); |
no test coverage detected