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 #include "tensorflow/compiler/xla/service/hlo_domain_remover.h"
17
18 #include "tensorflow/compiler/xla/service/hlo_computation.h"
19 #include "tensorflow/compiler/xla/service/hlo_domain_map.h"
20 #include "tensorflow/compiler/xla/service/hlo_domain_verifier.h"
21 #include "tensorflow/compiler/xla/service/hlo_graph_dumper.h"
22 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
23 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
24 #include "tensorflow/compiler/xla/types.h"
25
26 namespace xla {
27
28 class HloDomainRemover::RunContext {
29 public:
RunContext(HloModule * module,HloDomainRemover * remover)30 RunContext(HloModule* module, HloDomainRemover* remover)
31 : module_(module), remover_(remover) {}
32
33 StatusOr<bool> Run();
34
35 private:
36 // Verifies the consistency of the domain, and normalizes the instructions
37 // within it.
38 Status VerifyAndNormalizeDomain(const DomainMetadata::Domain& domain);
39
40 HloModule* module_;
41 HloDomainRemover* remover_;
42 };
43
VerifyAndNormalizeDomain(const DomainMetadata::Domain & domain)44 Status HloDomainRemover::RunContext::VerifyAndNormalizeDomain(
45 const DomainMetadata::Domain& domain) {
46 TF_ASSIGN_OR_RETURN(const DomainMetadata* ref_metadata,
47 HloDomainVerifier::VerifyDomain(domain));
48 if (ref_metadata != nullptr) {
49 VLOG(4) << "Applying domain normalization: " << ref_metadata->ToString();
50 TF_RETURN_IF_ERROR(remover_->normalizer_(domain, ref_metadata));
51 } else {
52 // No kDomain instruction was present within this domain, so call the
53 // generic normalization functions and have them apply their heuristic.
54 VLOG(2) << "Applying domain-less normalization";
55 TF_RETURN_IF_ERROR(remover_->normalizer_(domain, nullptr));
56 }
57 return Status::OK();
58 }
59
Run()60 StatusOr<bool> HloDomainRemover::RunContext::Run() {
61 VLOG(4) << "Processing metadata domain: '" << remover_->kind_ << "'";
62 int64 removed_domains = 0;
63 for (HloComputation* computation : module_->computations()) {
64 // First create the domain instruciton sets. A domain instruction set is
65 // the set of instructions whose edges never cross a kDomain instruction.
66 TF_ASSIGN_OR_RETURN(std::unique_ptr<HloDomainMap> domain_map,
67 HloDomainMap::Create(computation, remover_->kind_));
68 // Verify and normalize every domain populated within the map.
69 for (auto& domain : domain_map->GetDomains()) {
70 TF_RETURN_IF_ERROR(VerifyAndNormalizeDomain(*domain));
71 }
72
73 // Now remove all the kDomain instructions of the kind specified by the
74 // remover, that are within the currently processed computation from the
75 // graph.
76 for (HloInstruction* instruction :
77 computation->MakeInstructionPostOrder()) {
78 for (HloInstruction* operand : instruction->unique_operands()) {
79 if (domain_map->IsDomainInstruction(operand)) {
80 VLOG(5) << "Removing " << operand->name();
81 TF_RETURN_IF_ERROR(
82 operand->ReplaceAllUsesWith(operand->mutable_operand(0)));
83 TF_RETURN_IF_ERROR(computation->RemoveInstruction(operand));
84 ++removed_domains;
85 }
86 }
87 }
88 HloInstruction* root = computation->root_instruction();
89 if (root != nullptr && domain_map->IsDomainInstruction(root)) {
90 VLOG(5) << "Removing " << root->name();
91 computation->set_root_instruction(root->mutable_operand(0));
92 TF_RETURN_IF_ERROR(computation->RemoveInstruction(root));
93 ++removed_domains;
94 }
95 }
96 VLOG(3) << "Removed " << removed_domains << " kDomain instructions of '"
97 << remover_->kind_ << "' kind";
98 return removed_domains > 0;
99 }
100
Run(HloModule * module)101 StatusOr<bool> HloDomainRemover::Run(HloModule* module) {
102 RunContext run_context(module, this);
103 return run_context.Run();
104 }
105
106 } // namespace xla
107