aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/hlo_module_group.cc
blob: f9b56ef4643f2ca88e56456ae6c990161adb5085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/hlo_module_group.h"

namespace xla {

HloModuleGroup::HloModuleGroup(absl::string_view name,
                               std::unique_ptr<HloModule> module)
    : name_(name) {
  push_back(std::move(module));
}

HloModuleGroup::HloModuleGroup(absl::string_view name,
                               absl::Span<std::unique_ptr<HloModule>> modules)
    : name_(name) {
  for (auto& module : modules) {
    push_back(std::move(module));
  }
}

std::vector<std::unique_ptr<HloModule>> HloModuleGroup::ConsumeModules() {
  std::vector<std::unique_ptr<HloModule>> ret_modules = std::move(modules_);

  // Clear everything so the object state is in a known (empty) state.
  modules_.clear();
  module_ptrs_.clear();
  return ret_modules;
}

string HloModuleGroup::ToString() const {
  std::ostringstream s;
  s << "HloModuleGroup " << name() << "\n\n";
  for (const HloModule* module : modules()) {
    s << module->ToString() << "\n";
  }
  return s.str();
}

HloModuleGroupProto HloModuleGroup::ToProto() const {
  HloModuleGroupProto proto;
  proto.set_name(name());
  for (const HloModule* module : modules()) {
    *proto.add_hlo_modules() = module->ToProto();
  }
  return proto;
}

/* static */ StatusOr<HloModuleGroup> HloModuleGroup::CreateFromProto(
    const HloModuleGroupProto& proto,
    absl::Span<const HloModuleConfig> module_configs) {
  TF_RET_CHECK(!proto.name().empty()) << "Module group name cannot be empty";
  TF_RET_CHECK(proto.hlo_modules_size() > 0)
      << "Module group must have at least one HLO module";
  TF_RET_CHECK(proto.hlo_modules_size() == module_configs.size());

  std::vector<std::unique_ptr<HloModule>> modules;
  for (int i = 0; i < proto.hlo_modules_size(); ++i) {
    const HloModuleProto& module_proto = proto.hlo_modules(i);
    TF_ASSIGN_OR_RETURN(
        std::unique_ptr<HloModule> module,
        HloModule::CreateFromProto(module_proto, module_configs[i]));
    modules.push_back(std::move(module));
  }

  return HloModuleGroup(proto.name(), absl::MakeSpan(modules));
}

void HloModuleGroup::push_back(std::unique_ptr<HloModule> module) {
  modules_.push_back(std::move(module));
  module_ptrs_.push_back(modules_.back().get());
}

std::ostream& operator<<(std::ostream& out, const HloModuleGroup& group) {
  out << group.ToString();
  return out;
}

}  // namespace xla