xref: /aosp_15_r20/external/tensorflow/tensorflow/core/profiler/utils/op_utils.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/core/profiler/utils/op_utils.h"
17 
18 #include <algorithm>
19 #include <string>
20 
21 #include "absl/strings/string_view.h"
22 #include "tensorflow/core/platform/logging.h"
23 #include "tensorflow/core/platform/types.h"
24 #include "tensorflow/core/profiler/convert/op_metrics_db_combiner.h"
25 #include "tensorflow/core/profiler/protobuf/op_metrics.pb.h"
26 #include "tensorflow/core/profiler/utils/tf_op_utils.h"
27 
28 namespace tensorflow {
29 namespace profiler {
30 namespace {
31 
32 // Return capped performance. If time == 0, returns the original perf.
33 // Otherwise, returns the minimum of perf and the product of rate_limit
34 // and time.
GetCappedPerf(double perf,uint64 time,double rate_limit)35 double GetCappedPerf(double perf, uint64 time, double rate_limit) {
36   if (perf <= 0) return 0;
37   if (time == 0) return perf;
38   return std::min(perf, time * rate_limit);
39 }
40 
41 }  // namespace
42 
EnterOp(absl::string_view name,absl::string_view category,bool is_eager,uint64 time_ps,uint64 children_time_ps)43 void HostOpMetricsDbBuilder::EnterOp(absl::string_view name,
44                                      absl::string_view category, bool is_eager,
45                                      uint64 time_ps, uint64 children_time_ps) {
46   uint64 self_time_ps = time_ps - children_time_ps;
47   DCHECK_GE(time_ps, self_time_ps);
48   OpMetrics* op_metrics = LookupOrInsertNewOpMetrics(/*hlo_module_id=*/0, name);
49   if (op_metrics->category().empty())
50     op_metrics->set_category(category.data(), category.size());
51   op_metrics->set_num_cores(1);
52   op_metrics->set_is_eager(op_metrics->is_eager() || is_eager);
53   op_metrics->set_occurrences(op_metrics->occurrences() + 1);
54   op_metrics->set_time_ps(op_metrics->time_ps() + time_ps);
55   op_metrics->set_self_time_ps(op_metrics->self_time_ps() + self_time_ps);
56   db()->set_total_op_time_ps(db()->total_op_time_ps() + self_time_ps);
57 }
58 
EnterHostInfeedEnqueue(Timespan host_infeed_enqueue)59 void HostOpMetricsDbBuilder::EnterHostInfeedEnqueue(
60     Timespan host_infeed_enqueue) {
61   if (!last_host_infeed_enqueue_.Empty()) {
62     // Expect non-overlapping InfeedEnqueue timespans sorted by time.
63     DCHECK_GE(host_infeed_enqueue.end_ps(),
64               last_host_infeed_enqueue_.begin_ps());
65     db()->set_total_host_infeed_enq_duration_ps(
66         db()->total_host_infeed_enq_duration_ps() +
67         last_host_infeed_enqueue_.duration_ps());
68     db()->set_total_host_infeed_enq_start_timestamp_ps_diff(
69         db()->total_host_infeed_enq_start_timestamp_ps_diff() +
70         (host_infeed_enqueue.begin_ps() -
71          last_host_infeed_enqueue_.begin_ps()));
72   }
73   last_host_infeed_enqueue_ = host_infeed_enqueue;
74 }
75 
EnterOp(uint64 program_id,absl::string_view name,absl::string_view category,absl::string_view provenance,bool is_eager,uint64 occurrences,uint64 time_ps,uint64 children_time_ps,int64_t flops,int64_t bytes_accessed,const protobuf::RepeatedPtrField<OpMetrics::MemoryAccessed> & memory_accessed_breakdown)76 void DeviceOpMetricsDbBuilder::EnterOp(
77     uint64 program_id, absl::string_view name, absl::string_view category,
78     absl::string_view provenance, bool is_eager, uint64 occurrences,
79     uint64 time_ps, uint64 children_time_ps, int64_t flops,
80     int64_t bytes_accessed,
81     const protobuf::RepeatedPtrField<OpMetrics::MemoryAccessed>&
82         memory_accessed_breakdown) {
83   uint64 self_time_ps = time_ps - children_time_ps;
84   DCHECK_GE(time_ps, self_time_ps);
85   OpMetrics* op_metrics = LookupOrInsertNewOpMetrics(program_id, name);
86   if (op_metrics->category().empty())
87     op_metrics->set_category(category == kUnknownOp ? "unknown"
88                                                     : std::string(category));
89   if (op_metrics->provenance().empty())
90     op_metrics->set_provenance(std::string(provenance));
91   op_metrics->set_num_cores(1);
92   op_metrics->set_is_eager(op_metrics->is_eager() || is_eager);
93   op_metrics->set_occurrences(op_metrics->occurrences() + occurrences);
94   op_metrics->set_time_ps(op_metrics->time_ps() + time_ps);
95   op_metrics->set_self_time_ps(op_metrics->self_time_ps() + self_time_ps);
96   op_metrics->set_flops(op_metrics->flops() + flops * occurrences);
97   op_metrics->set_bytes_accessed(op_metrics->bytes_accessed() +
98                                  bytes_accessed * occurrences);
99   CombineMemoryAccessedBreakdown(
100       memory_accessed_breakdown,
101       op_metrics->mutable_memory_accessed_breakdown());
102   db()->set_total_op_time_ps(db()->total_op_time_ps() + self_time_ps);
103 }
104 
105 }  // namespace profiler
106 }  // namespace tensorflow
107