xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/cpu/cpu_instruction_fusion.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
33   using HloPassInterface::Run;
Run(HloModule * module,const absl::flat_hash_set<absl::string_view> & execution_threads)34   StatusOr<bool> Run(HloModule* module,
35                      const absl::flat_hash_set<absl::string_view>&
36                          execution_threads) override {
37     fusion_node_evaluations_.clear();
38     return InstructionFusion::Run(module, execution_threads);
39   }
40 
41  protected:
42   FusionDecision ShouldFuse(HloInstruction* consumer,
43                             int64_t operand_index) override;
44   HloInstruction::FusionKind ChooseKind(
45       const HloInstruction* producer, const HloInstruction* consumer) override;
46 
47  private:
48   HloInstruction* FuseInstruction(HloInstruction* fusion_instruction,
49                                   HloInstruction* producer) override;
50 
51   // Keep track of the number of times each instruction inside a fusion node is
52   // indexed with different index vectors.
53   absl::flat_hash_map<const HloInstruction*, FusionNodeIndexingEvaluation>
54       fusion_node_evaluations_;
55 };
56 
57 }  // namespace cpu
58 }  // namespace xla
59 
60 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_CPU_INSTRUCTION_FUSION_H_
61