• 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 #include "tensorflow/compiler/xla/service/hlo_module_group.h"
17 
18 #include "tensorflow/core/lib/hash/hash.h"
19 
20 namespace xla {
21 
HloModuleGroup(std::unique_ptr<HloModule> module)22 HloModuleGroup::HloModuleGroup(std::unique_ptr<HloModule> module)
23     : name_(module->name()) {
24   push_back(std::move(module));
25 }
26 
HloModuleGroup(absl::string_view name,absl::Span<std::unique_ptr<HloModule>> modules)27 HloModuleGroup::HloModuleGroup(absl::string_view name,
28                                absl::Span<std::unique_ptr<HloModule>> modules)
29     : name_(name) {
30   for (auto& module : modules) {
31     push_back(std::move(module));
32   }
33 }
34 
HloModuleGroup(absl::string_view name,std::vector<std::unique_ptr<HloModule>> && modules)35 HloModuleGroup::HloModuleGroup(
36     absl::string_view name, std::vector<std::unique_ptr<HloModule>>&& modules)
37     : name_(name) {
38   for (auto& module : modules) {
39     push_back(std::move(module));
40   }
41 }
42 
ConsumeModules()43 std::vector<std::unique_ptr<HloModule>> HloModuleGroup::ConsumeModules() {
44   std::vector<std::unique_ptr<HloModule>> ret_modules = std::move(modules_);
45 
46   // Clear everything so the object state is in a known (empty) state.
47   modules_.clear();
48   module_ptrs_.clear();
49   return ret_modules;
50 }
51 
ToString() const52 string HloModuleGroup::ToString() const {
53   std::ostringstream s;
54   s << "HloModuleGroup " << name() << "\n\n";
55   for (const HloModule* module : modules()) {
56     s << module->ToString() << "\n";
57   }
58   return s.str();
59 }
60 
ToProto() const61 HloModuleGroupProto HloModuleGroup::ToProto() const {
62   HloModuleGroupProto proto;
63   proto.set_name(name());
64   for (const HloModule* module : modules()) {
65     *proto.add_hlo_modules() = module->ToProto();
66   }
67   return proto;
68 }
69 
Hash() const70 uint64 HloModuleGroup::Hash() const {
71   uint64 result = 0;
72   for (auto& module : modules_) {
73     result = tensorflow::Hash64Combine(result, module->Hash());
74   }
75   return result;
76 }
77 
CreateFromProto(const HloModuleGroupProto & proto,absl::Span<const HloModuleConfig> module_configs)78 /* static */ StatusOr<HloModuleGroup> HloModuleGroup::CreateFromProto(
79     const HloModuleGroupProto& proto,
80     absl::Span<const HloModuleConfig> module_configs) {
81   TF_RET_CHECK(!proto.name().empty()) << "Module group name cannot be empty";
82   TF_RET_CHECK(proto.hlo_modules_size() > 0)
83       << "Module group must have at least one HLO module";
84   TF_RET_CHECK(proto.hlo_modules_size() == module_configs.size());
85 
86   std::vector<std::unique_ptr<HloModule>> modules;
87   for (int i = 0; i < proto.hlo_modules_size(); ++i) {
88     const HloModuleProto& module_proto = proto.hlo_modules(i);
89     TF_ASSIGN_OR_RETURN(
90         std::unique_ptr<HloModule> module,
91         HloModule::CreateFromProto(module_proto, module_configs[i]));
92     modules.push_back(std::move(module));
93   }
94 
95   return HloModuleGroup(proto.name(), absl::MakeSpan(modules));
96 }
97 
push_back(std::unique_ptr<HloModule> module)98 void HloModuleGroup::push_back(std::unique_ptr<HloModule> module) {
99   module->metadata()->set_module_group_name(name());
100   modules_.push_back(std::move(module));
101   module_ptrs_.push_back(modules_.back().get());
102 }
103 
ReplaceModule(int index,std::unique_ptr<HloModule> module)104 void HloModuleGroup::ReplaceModule(int index,
105                                    std::unique_ptr<HloModule> module) {
106   modules_.at(index)->MoveMetadataToModule(module.get());
107   modules_.at(index) = std::move(module);
108   module_ptrs_.at(index) = modules_.at(index).get();
109 }
110 
operator <<(std::ostream & out,const HloModuleGroup & group)111 std::ostream& operator<<(std::ostream& out, const HloModuleGroup& group) {
112   out << group.ToString();
113   return out;
114 }
115 
116 }  // namespace xla
117