xref: /aosp_15_r20/external/tensorflow/tensorflow/core/profiler/utils/op_metrics_db_utils.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 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_OP_METRICS_DB_UTILS_H_
17 #define TENSORFLOW_CORE_PROFILER_UTILS_OP_METRICS_DB_UTILS_H_
18 
19 #include <algorithm>
20 #include <string>
21 
22 #include "absl/container/flat_hash_map.h"
23 #include "absl/strings/string_view.h"
24 #include "absl/types/optional.h"
25 #include "tensorflow/core/platform/macros.h"
26 #include "tensorflow/core/platform/types.h"
27 #include "tensorflow/core/profiler/protobuf/op_metrics.pb.h"
28 
29 namespace tensorflow {
30 namespace profiler {
31 
32 // The name of OpMetrics to represent the idle time.
33 TF_CONST_INIT extern const absl::string_view kIdle;
34 
35 // Helps build an op metrics database (borrowed).
36 // Enables fast lookup of existing ops and prevents the creation of duplicate
37 // ops. It is the user's responsibility to ensure an op metrics database
38 // outlives its builder, and that no ops are added to the database outside of
39 // the builder.
40 class OpMetricsDbBuilder {
41  public:
42   // Create with a borrowed op database.
43   // REQUIRED: The op database must be empty.
44   explicit OpMetricsDbBuilder(OpMetricsDb* db);
45 
46  protected:
47   // Looks up the given OP name. If it is already in the database,
48   // return its OpMetrics; otherwise, insert a new one.
49   OpMetrics* LookupOrInsertNewOpMetrics(uint64 hlo_module_id,
50                                         absl::string_view name);
51 
db()52   OpMetricsDb* db() { return db_; }
53 
54  private:
55   // Map op (hlo_module_id, name) to the corresponding metrics in the op
56   // database.
57   absl::flat_hash_map<uint64 /*hlo_module_id*/,
58                       absl::flat_hash_map<std::string /*name*/, OpMetrics*>>
59       op_metrics_map_;
60 
61   // The op database.
62   OpMetricsDb* db_;
63 };
64 
65 // Sets the total time for OpMetricsDb, ensuring idle time is not negative.
SetTotalTimePs(OpMetricsDb & db,uint64_t total_time_ps)66 inline void SetTotalTimePs(OpMetricsDb& db, uint64_t total_time_ps) {
67   db.set_total_time_ps(std::max(db.total_op_time_ps(), total_time_ps));
68 }
69 
70 // Returns the total time in OpMetricsDb, optionally excluding the idle time.
71 inline uint64_t TotalTimePs(const OpMetricsDb& db, bool exclude_idle = false) {
72   return exclude_idle ? db.total_op_time_ps() : db.total_time_ps();
73 }
74 
75 // Returns the ratio of time that is idle (no op execution) over total time.
76 double IdleTimeRatio(const OpMetricsDb& db);
77 
78 // Returns the idle time in picoseconds.
79 uint64 IdleTimePs(const OpMetricsDb& db);
80 
81 // Populates an OpMetrics record representing idle time, i.e., the amount of
82 // time spent without any op execution.
83 void SetIdleOp(uint64_t idle_time_ps, OpMetrics& metrics);
84 
85 // Adds an OpMetrics record representing idle time, i.e., the amount of time
86 // spent without any op execution.
87 // REQUIRED: All ops must have been added to the database and the total time
88 // must have been set.
89 void AddIdleOp(OpMetricsDb& db);
90 
91 // Returns true if the given metrics represents idle time.
IsIdleOp(const OpMetrics & metrics)92 inline bool IsIdleOp(const OpMetrics& metrics) {
93   return metrics.category() == kIdle;
94 }
95 
96 // Returns the time spent in children (nested) ops.
ChildrenTimePs(const OpMetrics & metrics)97 inline uint64_t ChildrenTimePs(const OpMetrics& metrics) {
98   return metrics.time_ps() - metrics.self_time_ps();
99 }
100 
101 // Returns the ratio of time spent sending data from the host to the device
102 // relative to the total time the host was active.
103 absl::optional<double> HostInfeedEnqueueRatio(const OpMetricsDb& db);
104 
105 // Converts from the device op metrics to Tf-op metrics.
106 OpMetricsDb CreateTfMetricsDbFromDeviceOpMetricsDb(
107     const OpMetricsDb& device_op_metrics_db, bool with_idle = true);
108 
109 }  // namespace profiler
110 }  // namespace tensorflow
111 
112 #endif  // TENSORFLOW_CORE_PROFILER_UTILS_OP_METRICS_DB_UTILS_H_
113