Home | History | Annotate | Download | only in service
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/compiler/xla/service/defuser.h"
     17 
     18 #include <algorithm>
     19 #include <memory>
     20 #include <numeric>
     21 #include <string>
     22 #include <utility>
     23 #include <vector>
     24 
     25 #include "tensorflow/compiler/xla/service/call_graph.h"
     26 #include "tensorflow/compiler/xla/service/hlo_computation.h"
     27 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
     28 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
     29 #include "tensorflow/compiler/xla/status_macros.h"
     30 #include "tensorflow/compiler/xla/types.h"
     31 #include "tensorflow/compiler/xla/util.h"
     32 #include "tensorflow/core/lib/core/errors.h"
     33 #include "tensorflow/core/lib/core/status.h"
     34 #include "tensorflow/core/platform/logging.h"
     35 #include "tensorflow/core/platform/types.h"
     36 
     37 namespace xla {
     38 
     39 namespace {
     40 
     41 // Copy all the instructions in the given fusion instruction into the fusion
     42 // instruction's parent computation and replace the use of the fusion
     43 // instruction with the copy of the fusion expression root.
     44 Status Defuse(HloInstruction* fusion_instruction) {
     45   VLOG(2) << "Defusing instruction: " << fusion_instruction->ToString();
     46 
     47   HloComputation* fused_computation =
     48       fusion_instruction->fused_instructions_computation();
     49 
     50   // A map from fused instruction to its defused clone.
     51   tensorflow::gtl::FlatMap<const HloInstruction*, HloInstruction*>
     52       defused_instructions;
     53   // Initialize map to contain the fusion instruction parameters mapping
     54   // to the operands of the fusion instruction.
     55   for (int64 i = 0; i < fusion_instruction->operand_count(); ++i) {
     56     defused_instructions[fused_computation->parameter_instruction(i)] =
     57         fusion_instruction->mutable_operand(i);
     58   }
     59 
     60   // Create a clone of each instruction of the fused computation in the same
     61   // computation as the fusion instruction itself.
     62   // TODO(b/68227302): Moving instruction to new computation rather than
     63   // cloning and deleting.
     64   for (HloInstruction* fused_instruction :
     65        fused_computation->MakeInstructionPostOrder()) {
     66     if (fused_instruction->opcode() == HloOpcode::kParameter) {
     67       continue;
     68     }
     69     std::vector<HloInstruction*> new_operands;
     70     for (HloInstruction* operand : fused_instruction->operands()) {
     71       new_operands.push_back(defused_instructions.at(operand));
     72     }
     73     HloInstruction* defused_instruction =
     74         fusion_instruction->parent()->AddInstruction(
     75             fused_instruction->CloneWithNewOperands(fused_instruction->shape(),
     76                                                     new_operands));
     77     defused_instructions[fused_instruction] = defused_instruction;
     78   }
     79 
     80   TF_RETURN_IF_ERROR(fusion_instruction->ReplaceAllUsesWith(
     81       defused_instructions.at(fusion_instruction->fused_expression_root())));
     82 
     83   HloModule* module = fusion_instruction->parent()->parent();
     84   TF_RETURN_IF_ERROR(
     85       fusion_instruction->parent()->RemoveInstruction(fusion_instruction));
     86   return module->RemoveEmbeddedComputation(fused_computation);
     87 }
     88 
     89 }  // namespace
     90 
     91 StatusOr<bool> Defuser::Run(HloModule* module) {
     92   VLOG(1) << "Defusing module " << module->name();
     93   XLA_VLOG_LINES(2, "Before defusion:\n" + module->ToString());
     94 
     95   bool changed = false;
     96   std::unique_ptr<CallGraph> call_graph = CallGraph::Build(module);
     97   TF_RETURN_IF_ERROR(call_graph->VisitNodes(
     98       [&](const CallGraphNode& call_graph_node) -> Status {
     99         if (call_graph_node.computation()->IsFusionComputation()) {
    100           TF_RET_CHECK(call_graph_node.caller_callsites().size() == 1);
    101           HloInstruction* fusion_instruction =
    102               call_graph_node.caller_callsites()[0].instruction();
    103           TF_RETURN_IF_ERROR(Defuse(fusion_instruction));
    104           changed = true;
    105         }
    106         return Status::OK();
    107       },
    108       /*visit_unreachable_nodes=*/true));
    109 
    110   XLA_VLOG_LINES(2, "After defusion:\n" + module->ToString());
    111 
    112   return changed;
    113 }
    114 
    115 }  // namespace xla
    116