xref: /aosp_15_r20/external/tensorflow/tensorflow/core/profiler/utils/hlo_proto_map.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_CORE_PROFILER_UTILS_HLO_PROTO_MAP_H_
17 #define TENSORFLOW_CORE_PROFILER_UTILS_HLO_PROTO_MAP_H_
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/string_view.h"
28 #include "tensorflow/compiler/xla/service/hlo.pb.h"
29 #include "tensorflow/core/profiler/protobuf/xplane.pb.h"
30 
31 namespace tensorflow {
32 namespace profiler {
33 
34 std::vector<std::pair<uint64_t /*program_id*/, std::unique_ptr<xla::HloProto>>>
35 ParseHloProtosFromXSpace(const XSpace& space);
36 
37 class HloProtoMap {
38  public:
39   void AddHloProtosFromXSpace(const XSpace& space);
40 
41   void AddHloProto(uint64_t program_id,
42                    std::unique_ptr<const xla::HloProto> hlo_proto);
43   // Returns whether <hlo_proto> is new to HloProtoMap.
44   bool AddHloProto(uint64_t program_id, const xla::HloProto* hlo_proto);
45 
begin()46   auto begin() const { return hlo_protos_by_program_id_.begin(); }
end()47   auto end() const { return hlo_protos_by_program_id_.end(); }
48 
contains(absl::string_view name)49   bool contains(absl::string_view name) const {
50     return hlo_protos_by_name_.contains(name);
51   }
52 
53   // Returns a list of module names (not sorted).
54   std::vector<absl::string_view> GetModuleList() const;
55 
56   // Returns a list of module names sorted alphabetically.
57   std::vector<absl::string_view> GetSortedModuleList() const;
58 
59   // Returns a list of hlo module names sorted first by heap trace size and then
60   // by hlo module name alphabetically.
61   std::vector<absl::string_view> GetSortedModuleListByHeapTraceSize() const;
62 
63   absl::StatusOr<const xla::HloProto*> GetHloProtoByModuleName(
64       absl::string_view module_name) const;
65 
66  private:
67   absl::flat_hash_map<int64_t, const xla::HloProto*> hlo_protos_by_program_id_;
68   absl::flat_hash_map<std::string, const xla::HloProto*> hlo_protos_by_name_;
69   std::vector<std::unique_ptr<const xla::HloProto>> owned_hlo_protos_;
70 };
71 
72 }  // namespace profiler
73 }  // namespace tensorflow
74 
75 #endif  // TENSORFLOW_CORE_PROFILER_UTILS_HLO_PROTO_MAP_H_
76