• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_is_eager(op_metrics->is_eager() || is_eager);
52   op_metrics->set_occurrences(op_metrics->occurrences() + 1);
53   op_metrics->set_time_ps(op_metrics->time_ps() + time_ps);
54   op_metrics->set_self_time_ps(op_metrics->self_time_ps() + self_time_ps);
55   db()->set_total_op_time_ps(db()->total_op_time_ps() + self_time_ps);
56 }
57 
UpdateHostInfeedEnqInfo(uint64 duration_ps,uint64 start_timestamp_ps_diff)58 void HostOpMetricsDbBuilder::UpdateHostInfeedEnqInfo(
59     uint64 duration_ps, uint64 start_timestamp_ps_diff) {
60   db()->set_total_host_infeed_enq_duration_ps(
61       db()->total_host_infeed_enq_duration_ps() + duration_ps);
62   db()->set_total_host_infeed_enq_start_timestamp_ps_diff(
63       db()->total_host_infeed_enq_start_timestamp_ps_diff() +
64       start_timestamp_ps_diff);
65 }
66 
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 flops,int64 bytes_accessed,const protobuf::RepeatedPtrField<OpMetrics::MemoryAccessed> & memory_accessed_breakdown)67 void DeviceOpMetricsDbBuilder::EnterOp(
68     uint64 program_id, absl::string_view name, absl::string_view category,
69     absl::string_view provenance, bool is_eager, uint64 occurrences,
70     uint64 time_ps, uint64 children_time_ps, int64 flops, int64 bytes_accessed,
71     const protobuf::RepeatedPtrField<OpMetrics::MemoryAccessed>&
72         memory_accessed_breakdown) {
73   uint64 self_time_ps = time_ps - children_time_ps;
74   DCHECK_GE(time_ps, self_time_ps);
75   OpMetrics* op_metrics = LookupOrInsertNewOpMetrics(program_id, name);
76   if (op_metrics->category().empty())
77     op_metrics->set_category(category == kUnknownOp ? "unknown"
78                                                     : std::string(category));
79   if (op_metrics->provenance().empty())
80     op_metrics->set_provenance(std::string(provenance));
81   op_metrics->set_is_eager(op_metrics->is_eager() || is_eager);
82   op_metrics->set_occurrences(op_metrics->occurrences() + occurrences);
83   op_metrics->set_time_ps(op_metrics->time_ps() + time_ps);
84   op_metrics->set_self_time_ps(op_metrics->self_time_ps() + self_time_ps);
85   op_metrics->set_flops(op_metrics->flops() + flops * occurrences);
86   op_metrics->set_bytes_accessed(op_metrics->bytes_accessed() +
87                                  bytes_accessed * occurrences);
88   CombineMemoryAccessedBreakdown(
89       memory_accessed_breakdown,
90       op_metrics->mutable_memory_accessed_breakdown());
91   db()->set_total_op_time_ps(db()->total_op_time_ps() + self_time_ps);
92 }
93 
94 }  // namespace profiler
95 }  // namespace tensorflow
96