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_CONVERT_OP_METRICS_TO_RECORD_H_
17 #define TENSORFLOW_CORE_PROFILER_CONVERT_OP_METRICS_TO_RECORD_H_
18 
19 #include <vector>
20 
21 #include "tensorflow/core/profiler/protobuf/op_metrics.pb.h"
22 #include "tensorflow/core/profiler/utils/math_utils.h"
23 #include "tensorflow/core/profiler/utils/time_utils.h"
24 
25 namespace tensorflow {
26 namespace profiler {
27 
28 std::vector<const OpMetrics*> SortedOpMetricsDb(const OpMetricsDb& metrics_db,
29                                                 int max_records = -1);
30 
31 template <typename Record>
SetExecutionTimes(const OpMetrics & metrics,Record * record)32 inline void SetExecutionTimes(const OpMetrics& metrics, Record* record) {
33   record->set_occurrences(metrics.occurrences());
34   record->set_total_time_in_us(PicosToMicros(metrics.time_ps()));
35   record->set_avg_time_in_us(
36       SafeDivide(record->total_time_in_us(), metrics.occurrences()));
37   record->set_total_self_time_in_us(PicosToMicros(metrics.self_time_ps()));
38   record->set_avg_self_time_in_us(
39       SafeDivide(record->total_self_time_in_us(), metrics.occurrences()));
40 }
41 
42 template <typename Record>
SetTpuUnitFractions(const OpMetrics & metrics,Record * record)43 inline void SetTpuUnitFractions(const OpMetrics& metrics, Record* record) {
44   record->set_dma_stall_fraction(
45       SafeDivide(metrics.dma_stall_ps(), metrics.time_ps()));
46 }
47 
48 template <typename Record>
SetRankAndTimeFractions(double total_time_us,const Record & prev_record,Record * record)49 inline void SetRankAndTimeFractions(double total_time_us,
50                                     const Record& prev_record, Record* record) {
51   record->set_rank(prev_record.rank() + 1);
52   record->set_total_self_time_as_fraction(
53       SafeDivide(record->total_self_time_in_us(), total_time_us));
54   record->set_cumulative_total_self_time_as_fraction(
55       prev_record.cumulative_total_self_time_as_fraction() +
56       record->total_self_time_as_fraction());
57 }
58 
59 template <typename Record>
SetRankAndDeviceTimeFractions(double total_time_us,const Record & prev_record,Record * record)60 inline void SetRankAndDeviceTimeFractions(double total_time_us,
61                                           const Record& prev_record,
62                                           Record* record) {
63   record->set_rank(prev_record.rank() + 1);
64   record->set_device_total_self_time_as_fraction(
65       SafeDivide(record->total_self_time_in_us(), total_time_us));
66   record->set_device_cumulative_total_self_time_as_fraction(
67       prev_record.device_cumulative_total_self_time_as_fraction() +
68       record->device_total_self_time_as_fraction());
69 }
70 
71 template <typename Record>
SetRankAndHostTimeFractions(double total_time_us,const Record & prev_record,Record * record)72 inline void SetRankAndHostTimeFractions(double total_time_us,
73                                         const Record& prev_record,
74                                         Record* record) {
75   record->set_rank(prev_record.rank() + 1);
76   record->set_host_total_self_time_as_fraction(
77       SafeDivide(record->total_self_time_in_us(), total_time_us));
78   record->set_host_cumulative_total_self_time_as_fraction(
79       prev_record.host_cumulative_total_self_time_as_fraction() +
80       record->host_total_self_time_as_fraction());
81 }
82 
83 template <typename Record>
SetRooflineMetrics(const OpMetrics & metrics,double ridge_point_operational_intensity,Record * record)84 inline void SetRooflineMetrics(const OpMetrics& metrics,
85                                double ridge_point_operational_intensity,
86                                Record* record) {
87   using ::tensorflow::profiler::PicosToNanos;
88   record->set_measured_flop_rate(
89       SafeDivide(metrics.flops(), PicosToNanos(metrics.time_ps())));
90   record->set_measured_memory_bw(
91       SafeDivide(metrics.bytes_accessed(), PicosToNanos(metrics.time_ps())));
92   record->set_operational_intensity(
93       SafeDivide(metrics.flops(), metrics.bytes_accessed()));
94   record->set_bound_by((metrics.bytes_accessed() != 0)
95                            ? ((record->operational_intensity() >=
96                                ridge_point_operational_intensity)
97                                   ? "Compute"
98                                   : "Memory")
99                            : ((metrics.flops() != 0) ? "Compute" : "Unknown"));
100 }
101 
102 }  // namespace profiler
103 }  // namespace tensorflow
104 
105 #endif  // TENSORFLOW_CORE_PROFILER_CONVERT_OP_METRICS_TO_RECORD_H_
106