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