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_DOMAIN_ISOLATOR_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_DOMAIN_ISOLATOR_H_
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
23 #include "tensorflow/compiler/xla/service/hlo_module.h"
24 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h"
25 
26 namespace xla {
27 
28 // Domain isolation is the task of placing kDomain instructions between HLO
29 // instructions having different sharding. A kDomain instruction is essentially
30 // used to break an HLO graph edge connecting two instructions with different
31 // sharding. If a set of connected instructions have all the same sharding, no
32 // kDomain instruction will be placed.
33 class HloDomainIsolator : public HloModulePass {
34  public:
35   // Creates a new kDomain instruction for the edge between the use instruction
36   // (the first HloInstruction argument), and the operand instruction (the
37   // third HloInstruction argument) if the interesting attribute of the
38   // instruction differes from the attribute of the root (the second
39   // HloInstruction argument).
40   // Returns nullptr in case no domain separation is necessary.
41   using DomainCreator = std::function<HloInstruction*(
42       HloInstruction*, HloInstruction*, HloInstruction*)>;
43   using DomainCreatorFactory = std::function<DomainCreator()>;
44   explicit HloDomainIsolator(DomainCreatorFactory creator_factory_);
45 
name()46   absl::string_view name() const override { return "domain_isolator"; }
47 
48   StatusOr<bool> Run(HloModule* module) override;
49 
50  private:
51   DomainCreatorFactory creator_factory_;
52 };
53 
54 }  // namespace xla
55 
56 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_DOMAIN_ISOLATOR_H_
57