• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   bool ShouldFuse(HloInstruction* consumer, int64 operand_index) override;
35 
36   bool ShouldFuseIntoMultiOutput(HloInstruction* consumer,
37                                  int64 operand_index) override;
38 
39   HloInstruction::FusionKind ChooseKind(
40       const HloInstruction* producer, const HloInstruction* consumer) override;
41 
Run(HloModule * module)42   StatusOr<bool> Run(HloModule* module) override {
43     fusion_node_evaluations_.clear();
44     return InstructionFusion::Run(module);
45   }
46 
47  private:
48   // This method is called by ShouldFuse() to do all the computationally
49   // inexpensive checks whether we should fuse the operand into 'consumer'.
50   bool ShouldFuseInexpensiveChecks(HloInstruction* consumer,
51                                    int64 operand_index);
52 
53   HloInstruction* FuseInstruction(HloInstruction* fusion_instruction,
54                                   HloInstruction* producer) override;
55 
56   // Keep track of the number of times each instruction inside a fusion node is
57   // indexed with different index vectors.
58   absl::flat_hash_map<const HloInstruction*, FusionNodeIndexingEvaluation>
59       fusion_node_evaluations_;
60 };
61 
62 }  // namespace gpu
63 }  // namespace xla
64 
65 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_INSTRUCTION_FUSION_H_
66