• 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/convert/xplane_to_hlo.h"
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/string_view.h"
24 #include "tensorflow/compiler/xla/service/hlo.pb.h"
25 #include "tensorflow/core/platform/env.h"
26 #include "tensorflow/core/platform/errors.h"
27 #include "tensorflow/core/platform/path.h"
28 #include "tensorflow/core/platform/status.h"
29 #include "tensorflow/core/profiler/protobuf/xplane.pb.h"
30 #include "tensorflow/core/profiler/utils/file_system_utils.h"
31 #include "tensorflow/core/profiler/utils/hlo_proto_map.h"
32 #include "tensorflow/core/protobuf/error_codes.pb.h"
33 
34 namespace tensorflow {
35 namespace profiler {
36 
37 namespace {
38 
39 constexpr char kNoModuleIdentifier[] = "NO_MODULE";
40 constexpr char kHloProtoSuffix[] = ".hlo_proto.pb";
41 
42 }  // namespace
43 
GetHloProtoFileName(const absl::string_view base_dir,const absl::string_view module_name)44 std::string GetHloProtoFileName(const absl::string_view base_dir,
45                                 const absl::string_view module_name) {
46   return ProfilerJoinPath(base_dir, absl::StrCat(module_name, kHloProtoSuffix));
47 }
48 
GetHloProtoFromMultiXSpaceAndSaveToFile(const std::vector<XSpace> & xspaces,const std::vector<std::string> & xspace_file_names)49 Status GetHloProtoFromMultiXSpaceAndSaveToFile(
50     const std::vector<XSpace>& xspaces,
51     const std::vector<std::string>& xspace_file_names) {
52   if (xspace_file_names.empty() || xspaces.empty()) return OkStatus();
53 
54   // Get all HLO protos from XSpaces and deduplicate.
55   HloProtoMap hlo_proto_map;
56   for (const XSpace& xspace : xspaces) {
57     hlo_proto_map.AddHloProtosFromXSpace(xspace);
58   }
59 
60   absl::string_view dir_name = tensorflow::io::Dirname(xspace_file_names[0]);
61   std::vector<absl::string_view> module_list = hlo_proto_map.GetModuleList();
62   // Write an empty identifier if there is no HLO module.
63   if (module_list.empty()) {
64     std::string file_name = ProfilerJoinPath(
65         dir_name, absl::StrCat(kNoModuleIdentifier, kHloProtoSuffix));
66     xla::HloProto empty_hlo;
67     TF_RETURN_IF_ERROR(tensorflow::WriteBinaryProto(tensorflow::Env::Default(),
68                                                     file_name, empty_hlo));
69     return OkStatus();
70   }
71 
72   // Save HLO protos to the same directory as XSpaces.
73   for (const absl::string_view module_name : module_list) {
74     auto hlo_proto_or = hlo_proto_map.GetHloProtoByModuleName(module_name);
75     if (!hlo_proto_or.ok()) {
76       return Status(tensorflow::error::INTERNAL,
77                     hlo_proto_or.status().message());
78     }
79     std::string file_name =
80         ProfilerJoinPath(dir_name, absl::StrCat(module_name, kHloProtoSuffix));
81     TF_RETURN_IF_ERROR(tensorflow::WriteBinaryProto(
82         tensorflow::Env::Default(), file_name, *hlo_proto_or.value()));
83   }
84 
85   return OkStatus();
86 }
87 
88 }  // namespace profiler
89 }  // namespace tensorflow
90