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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_INSTRUCTION_FUSION_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_INSTRUCTION_FUSION_H_ 18 19 #include "absl/container/flat_hash_map.h" 20 #include "tensorflow/compiler/xla/service/fusion_node_indexing_evaluation.h" 21 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 22 #include "tensorflow/compiler/xla/service/instruction_fusion.h" 23 24 namespace xla { 25 namespace gpu { 26 27 class GpuInstructionFusion : public InstructionFusion { 28 public: GpuInstructionFusion(bool may_duplicate)29 explicit GpuInstructionFusion(bool may_duplicate) 30 : InstructionFusion(GpuInstructionFusion::IsExpensive, may_duplicate) {} 31 32 static bool IsExpensive(const HloInstruction& instruction); 33 34 using HloPassInterface::Run; Run(HloModule * module,const absl::flat_hash_set<absl::string_view> & execution_threads)35 StatusOr<bool> Run(HloModule* module, 36 const absl::flat_hash_set<absl::string_view>& 37 execution_threads) override { 38 fusion_node_evaluations_.clear(); 39 return InstructionFusion::Run(module, execution_threads); 40 } 41 42 protected: 43 FusionDecision ShouldFuse(HloInstruction* consumer, 44 int64_t operand_index) override; 45 46 HloInstruction::FusionKind ChooseKind( 47 const HloInstruction* producer, const HloInstruction* consumer) override; 48 49 private: 50 // This method is called by ShouldFuse() to do all the computationally 51 // inexpensive checks whether we should fuse the operand into 'consumer'. 52 FusionDecision ShouldFuseInexpensiveChecks(HloInstruction* consumer, 53 int64_t operand_index); 54 55 HloInstruction* FuseInstruction(HloInstruction* fusion_instruction, 56 HloInstruction* producer) override; 57 58 // Keep track of the number of times each instruction inside a fusion node is 59 // indexed with different index vectors. 60 absl::flat_hash_map<const HloInstruction*, FusionNodeIndexingEvaluation> 61 fusion_node_evaluations_; 62 }; 63 64 } // namespace gpu 65 } // namespace xla 66 67 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_INSTRUCTION_FUSION_H_ 68