• 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_CPU_CPU_INSTRUCTION_FUSION_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_CPU_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 cpu {
26 
27 class CpuInstructionFusion : public InstructionFusion {
28  public:
CpuInstructionFusion()29   CpuInstructionFusion()
30       : InstructionFusion(CpuInstructionFusion::IsExpensive) {}
31   ~CpuInstructionFusion() override = default;
32 
Run(HloModule * module)33   StatusOr<bool> Run(HloModule* module) override {
34     fusion_node_evaluations_.clear();
35     return InstructionFusion::Run(module);
36   }
37 
38  protected:
39   bool ShouldFuse(HloInstruction* consumer, int64 operand_index) override;
40   HloInstruction::FusionKind ChooseKind(
41       const HloInstruction* producer, const HloInstruction* consumer) override;
42 
43  private:
44   HloInstruction* FuseInstruction(HloInstruction* fusion_instruction,
45                                   HloInstruction* producer) override;
46 
47   // Keep track of the number of times each instruction inside a fusion node is
48   // indexed with different index vectors.
49   absl::flat_hash_map<const HloInstruction*, FusionNodeIndexingEvaluation>
50       fusion_node_evaluations_;
51 };
52 
53 }  // namespace cpu
54 }  // namespace xla
55 
56 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_CPU_INSTRUCTION_FUSION_H_
57