• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2022 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/core/profiler/utils/hlo_proto_to_module.h"
17 
18 #include <memory>
19 #include <utility>
20 
21 #include "tensorflow/compiler/xla/service/hlo.pb.h"
22 #include "tensorflow/compiler/xla/service/hlo_module.h"
23 #include "tensorflow/compiler/xla/service/hlo_module_config.h"
24 #include "tensorflow/compiler/xla/util.h"
25 #include "tensorflow/core/platform/statusor.h"
26 
27 namespace tensorflow {
28 namespace profiler {
29 
ConvertHloProtoToModule(const xla::HloProto & hlo_proto)30 xla::StatusOr<std::unique_ptr<xla::HloModule>> ConvertHloProtoToModule(
31     const xla::HloProto& hlo_proto) {
32   if (!hlo_proto.has_hlo_module()) {
33     return xla::InternalError("No HLO module found in the HLO proto");
34   }
35   const xla::HloModuleProto& module_proto = hlo_proto.hlo_module();
36   TF_ASSIGN_OR_RETURN(auto config, xla::HloModule::CreateModuleConfigFromProto(
37                                        module_proto, xla::DebugOptions()));
38   TF_ASSIGN_OR_RETURN(auto module,
39                       xla::HloModule::CreateFromProto(module_proto, config));
40   return module;
41 }
42 
ConvertHloProtoToModuleIgnoringErrors(const xla::HloProto & hlo_proto)43 std::unique_ptr<xla::HloModule> ConvertHloProtoToModuleIgnoringErrors(
44     const xla::HloProto& hlo_proto) {
45   auto module = ConvertHloProtoToModule(hlo_proto);
46   if (!module.ok()) {
47     LOG(ERROR) << module.status();
48     return nullptr;
49   }
50   return std::move(module).value();
51 }
52 
53 }  // namespace profiler
54 }  // namespace tensorflow
55