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_REMOVER_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_DOMAIN_REMOVER_H_ 18 19 #include "tensorflow/compiler/xla/service/hlo_domain_metadata.h" 20 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 21 #include "tensorflow/compiler/xla/service/hlo_module.h" 22 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h" 23 #include "tensorflow/core/lib/core/status.h" 24 25 namespace xla { 26 27 // Removes all the kDomain instructions of a given kind from the input module, 28 // and calls the normalizer to propagate the properties on the possibly new born 29 // instructions. 30 class HloDomainRemover : public HloModulePass { 31 public: 32 // Creates a new HloDomainRemover object tasked at removing all the kDomain 33 // instructions of a given kind. 34 // In case a reachable set (the set of instructions within a computation, 35 // which are mutually reachable via operand/user pathways) has all the 36 // instructions in it with the same attributes (ie, sharding), a normalizer 37 // function is tasked at applying attribute normalization on the instructions 38 // within such domain. HloDomainRemover(absl::string_view kind,std::function<Status (const DomainMetadata::Domain &,const DomainMetadata * metadata)> normalizer)39 HloDomainRemover(absl::string_view kind, 40 std::function<Status(const DomainMetadata::Domain&, 41 const DomainMetadata* metadata)> 42 normalizer) 43 : kind_(kind), normalizer_(std::move(normalizer)) {} 44 name()45 absl::string_view name() const override { return "domain_remover"; } 46 47 // Remove domains of a given kind which are used as users of a specific 48 // instruction. 49 static StatusOr<int64_t> RemoveExitDomains(HloInstruction* instruction, 50 absl::string_view domain_kind); 51 52 using HloPassInterface::Run; 53 StatusOr<bool> Run( 54 HloModule* module, 55 const absl::flat_hash_set<absl::string_view>& execution_threads) override; 56 57 private: 58 class RunContext; 59 60 std::string kind_; 61 std::function<Status(const DomainMetadata::Domain&, 62 const DomainMetadata* metadata)> 63 normalizer_; 64 }; 65 66 } // namespace xla 67 68 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_DOMAIN_REMOVER_H_ 69