• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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_HLO_CLONE_CONTEXT_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_CLONE_CONTEXT_H_
18 
19 #include <string>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "tensorflow/compiler/xla/map_util.h"
23 
24 namespace xla {
25 
26 class HloInstruction;
27 class HloComputation;
28 class HloModule;
29 
30 // Data structure used to track the cloning of HloInstruction and HloComputation
31 // objects.
32 class HloCloneContext {
33  public:
34   // Creates a new HloCloneContext object to clone HloInstruction and
35   // HloComputation objects to be added to the module specified as argument.
36   // The suffix string will be appended to computation names.
37   explicit HloCloneContext(HloModule* module, const string& suffix = "")
module_(module)38       : module_(module), suffix_(suffix) {}
39 
module()40   HloModule* module() const { return module_; }
41 
suffix()42   const string& suffix() const { return suffix_; }
43 
MapInstruction(const HloInstruction * old_instruction,HloInstruction * new_instruction)44   void MapInstruction(const HloInstruction* old_instruction,
45                       HloInstruction* new_instruction) {
46     instructions_[old_instruction] = new_instruction;
47   }
48 
MapComputation(const HloComputation * old_computation,HloComputation * new_computation)49   void MapComputation(const HloComputation* old_computation,
50                       HloComputation* new_computation) {
51     computations_[old_computation] = new_computation;
52   }
53 
54   // Finds the new instruction mapped to its old copy, or return nullptr in case
55   // it is not found.
FindInstruction(const HloInstruction * old_instruction)56   HloInstruction* FindInstruction(const HloInstruction* old_instruction) const {
57     return FindOrDefault(instructions_, old_instruction, nullptr);
58   }
59 
60   // Finds the new computation mapped to its old copy, or return nullptr in case
61   // it is not found.
FindComputation(const HloComputation * old_computation)62   HloComputation* FindComputation(const HloComputation* old_computation) const {
63     return FindOrDefault(computations_, old_computation, nullptr);
64   }
65 
66   // Retrieves the new instruction mapped to its old copy, or fail if not found.
GetInstruction(const HloInstruction * old_instruction)67   HloInstruction* GetInstruction(const HloInstruction* old_instruction) const {
68     return FindOrDie(instructions_, old_instruction);
69   }
70 
71   // Retrieves the new computation mapped to its old copy, or fail if not found.
GetComputation(const HloComputation * old_computation)72   HloComputation* GetComputation(const HloComputation* old_computation) const {
73     return FindOrDie(computations_, old_computation);
74   }
75 
76   const absl::flat_hash_map<const HloInstruction*, HloInstruction*>&
cloned_instructions()77   cloned_instructions() const {
78     return instructions_;
79   }
80 
81   const absl::flat_hash_map<const HloComputation*, HloComputation*>&
cloned_computations()82   cloned_computations() const {
83     return computations_;
84   }
85 
86  private:
87   HloModule* module_;
88   string suffix_;
89   absl::flat_hash_map<const HloInstruction*, HloInstruction*> instructions_;
90   absl::flat_hash_map<const HloComputation*, HloComputation*> computations_;
91 };
92 
93 }  // namespace xla
94 
95 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_CLONE_CONTEXT_H_
96