• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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_metadata.h"
17 
18 #include <algorithm>
19 
20 #include "absl/container/flat_hash_set.h"
21 #include "tensorflow/core/platform/env.h"
22 
23 namespace xla {
24 
GetCurrentHloPassMetadata()25 StatusOr<HloPassMetadata*> HloModuleMetadata::GetCurrentHloPassMetadata() {
26   if (running_passes_.empty()) {
27     return NotFound(
28         "HloPassMetadata for currently running pass not found, either because "
29         "the pass did not call RecordPassStart or because a pass is "
30         "creating/switching modules without using "
31         "HloModuleGroup::ReplaceModule.");
32   }
33   return running_passes_.back();
34 }
35 
MutateCurrentHloPassMetadata(const std::function<void (HloPassMetadata *)> & mutator)36 Status HloModuleMetadata::MutateCurrentHloPassMetadata(
37     const std::function<void(HloPassMetadata*)>& mutator) {
38   TF_ASSIGN_OR_RETURN(HloPassMetadata * pass_metadata,
39                       GetCurrentHloPassMetadata());
40   mutator(pass_metadata);
41   return Status::OK();
42 }
43 
RecordPassStart()44 void HloModuleMetadata::RecordPassStart() {
45   HloPassMetadata* pass_metadata = module_metadata_.add_pass_metadata();
46   pass_metadata->set_pass_id(next_pass_id_++);
47   pass_metadata->set_start_timestamp_usec(env_->NowMicros());
48   running_passes_.push_back(pass_metadata);
49 }
50 
RecordPassEnd()51 Status HloModuleMetadata::RecordPassEnd() {
52   TF_ASSIGN_OR_RETURN(HloPassMetadata * pass_metadata,
53                       GetCurrentHloPassMetadata());
54   pass_metadata->set_end_timestamp_usec(env_->NowMicros());
55   running_passes_.pop_back();
56   return Status::OK();
57 }
58 
set_prepartitioning_metadata(const HloModuleMetadata & prepartitioning_metadata)59 void HloModuleMetadata::set_prepartitioning_metadata(
60     const HloModuleMetadata& prepartitioning_metadata) {
61   module_metadata_.set_original_module_id(
62       prepartitioning_metadata.proto().canonical_module_id());
63   prepartitioning_metadata_ = prepartitioning_metadata.proto();
64   prepartitioning_metadata_->clear_pass_metadata();
65 
66   // Because HloPassMetadata represents the completion of a pass, metadata for
67   // all currently running passes need to be moved over to the new module.
68   absl::flat_hash_set<HloPassMetadata*> running_passes(
69       prepartitioning_metadata.running_passes_.begin(),
70       prepartitioning_metadata.running_passes_.end());
71   for (const HloPassMetadata& pass_metadata :
72        prepartitioning_metadata.proto().pass_metadata()) {
73     if (running_passes.contains(&pass_metadata)) {
74       HloPassMetadata* added_pass_metadata =
75           module_metadata_.add_pass_metadata();
76       *added_pass_metadata = pass_metadata;
77       running_passes_.push_back(added_pass_metadata);
78       next_pass_id_ =
79           std::max(next_pass_id_,
80                    static_cast<int64>(added_pass_metadata->pass_id()) + 1);
81     } else {
82       *prepartitioning_metadata_->add_pass_metadata() = pass_metadata;
83     }
84   }
85 }
86 
87 }  // namespace xla
88