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_STATS_TO_INPUT_PIPELINE_ANALYSIS_H_
17 #define TENSORFLOW_CORE_PROFILER_CONVERT_OP_STATS_TO_INPUT_PIPELINE_ANALYSIS_H_
18 
19 #include <string>
20 
21 #include "google/protobuf/any.pb.h"
22 #include "absl/strings/string_view.h"
23 #include "tensorflow/core/platform/protobuf.h"
24 #include "tensorflow/core/platform/types.h"
25 #include "tensorflow/core/profiler/protobuf/hardware_types.pb.h"
26 #include "tensorflow/core/profiler/protobuf/input_pipeline.pb.h"
27 #include "tensorflow/core/profiler/protobuf/op_metrics.pb.h"
28 #include "tensorflow/core/profiler/protobuf/op_stats.pb.h"
29 #include "tensorflow/core/profiler/protobuf/steps_db.pb.h"
30 
31 namespace tensorflow {
32 namespace profiler {
33 
34 // If the percent of input-time spent on host-to-device transfer is greater than
35 // kHostToDeviceTimePercentAsSignificant, we should advise the
36 // user to optimize this transfer.
37 constexpr double kHostToDeviceTimePercentAsSignificant = 10.0;
38 
39 // If the percent of input-time spent on host-to-device transfer is greater than
40 // kHostToDeviceTimePercentAsDominant, we should ONLY advise the
41 // user to optimize this transfer; we won't bother to suggest optimization for
42 // tf.data.
43 constexpr double kHostToDeviceTimePercentAsDominant = 90.0;
44 
45 // Computes the summary of step time in milliseconds.
46 StepSummary ComputeStepTimeSummaryInMs(
47     const ::tensorflow::protobuf::RepeatedPtrField<PerCoreStepInfo>&
48         grouped_by_step);
49 
50 void GenerateHostResult(const OpMetricsDb& host_tf_metrics_db,
51                         InputPipelineAnalysisResult* result);
52 
53 InputPipelineAnalysisRecommendation GenerateRecommendation();
54 
55 // Returns the performance bottleneck of the program executed.
56 BottleneckAnalysis ComputeBottleneckAnalysis(
57     const InputTimeBreakdown& input_time_breakdown,
58     const ::tensorflow::protobuf::RepeatedPtrField<::google::protobuf::Any>&
59         any_step_details);
60 
61 InputPipelineAnalysisResult ConvertOpStatsToInputPipelineAnalysis(
62     const OpStats& op_stats);
63 
64 // Returns true if explanation for "All Others" time is also included in
65 // input_statement.
66 bool InputAnalysis(double input_percent, double all_other_percent,
67                    std::string* input_classification,
68                    std::string* input_statement);
69 
70 void OutputAnalysis(double output_percent, std::string* output_classification,
71                     std::string* output_statement);
72 
73 string GetSummaryNextStep(absl::string_view input_classification,
74                           const InputTimeBreakdown& breakdown);
75 
76 // Returns the percentage of the input time that is spent on transferring the
77 // data from host to device.
78 double HostToDeviceTransferAsPercentOfInputTime(
79     const InputTimeBreakdown& breakdown);
80 
81 void AddErrorMessages(const OpStats& op_stats,
82                       InputPipelineAnalysisResult* result);
83 
84 }  // namespace profiler
85 }  // namespace tensorflow
86 
87 #endif  // TENSORFLOW_CORE_PROFILER_CONVERT_OP_STATS_TO_INPUT_PIPELINE_ANALYSIS_H_
88