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/convert/xplane_to_kernel_stats_db.h"
17
18 #include <functional>
19
20 #include "absl/container/flat_hash_map.h"
21 #include "absl/strings/string_view.h"
22 #include "absl/types/optional.h"
23 #include "tensorflow/core/platform/logging.h"
24 #include "tensorflow/core/platform/types.h"
25 #include "tensorflow/core/profiler/protobuf/kernel_stats.pb.h"
26 #include "tensorflow/core/profiler/protobuf/xplane.pb.h"
27 #include "tensorflow/core/profiler/utils/kernel_stats_utils.h"
28 #include "tensorflow/core/profiler/utils/tf_op_utils.h"
29 #include "tensorflow/core/profiler/utils/tf_xplane_visitor.h"
30 #include "tensorflow/core/profiler/utils/trace_utils.h"
31 #include "tensorflow/core/profiler/utils/xplane_schema.h"
32 #include "tensorflow/core/profiler/utils/xplane_visitor.h"
33
34 namespace tensorflow {
35 namespace profiler {
36
ConvertDeviceTraceXPlaneToKernelReports(const XPlane & device_trace,const std::function<void (const XEventVisitor &,KernelReport *)> & on_kernel_fn,KernelReportMap * reports)37 void ConvertDeviceTraceXPlaneToKernelReports(
38 const XPlane& device_trace,
39 const std::function<void(const XEventVisitor&, KernelReport*)>&
40 on_kernel_fn,
41 KernelReportMap* reports) {
42 XPlaneVisitor plane = CreateTfXPlaneVisitor(&device_trace);
43 plane.ForEachLine([&](const XLineVisitor& line) {
44 if (IsDerivedThreadId(line.Id())) {
45 return;
46 }
47 line.ForEachEvent([&](const XEventVisitor& event) {
48 absl::string_view tf_op_fullname;
49 KernelReport kernel;
50
51 absl::string_view equation;
52 event.ForEachStat([&](const tensorflow::profiler::XStatVisitor& stat) {
53 if (!stat.Type().has_value()) return;
54 switch (stat.Type().value()) {
55 case StatType::kTfOp:
56 case StatType::kLevel0: // old way to deliver tf_op info.
57 tf_op_fullname = stat.StrOrRefValue();
58 break;
59 case StatType::kKernelDetails: {
60 kernel.set_name(event.Name().data(), event.Name().size());
61 kernel.set_is_kernel_using_tensor_core(
62 IsKernelUsingTensorCore(event.Name()));
63 kernel.set_total_duration_ns(event.DurationNs());
64 kernel.set_min_duration_ns(event.DurationNs());
65 kernel.set_max_duration_ns(event.DurationNs());
66 absl::string_view launch_params = stat.StrOrRefValue();
67 ParseKernelLaunchParams(launch_params, &kernel);
68 break;
69 }
70 case StatType::kEquation:
71 equation = stat.StrOrRefValue();
72 break;
73 }
74 });
75
76 if (!tf_op_fullname.empty()) {
77 tensorflow::profiler::TfOp tf_op = ParseTfOpFullname(tf_op_fullname);
78
79 if (kernel.total_duration_ns()) {
80 kernel.set_op_name(tf_op.name.data(), tf_op.name.size());
81 bool tensor_core_eligible = IsEinsumTensorCoreEligible(equation) ||
82 IsOpTensorCoreEligible(kernel.op_name());
83
84 if (!tensor_core_eligible && kernel.is_kernel_using_tensor_core()) {
85 VLOG(1) << "Detected new Op using TensorCores: " << kernel.op_name()
86 << std::endl;
87 tensor_core_eligible = true;
88 }
89
90 kernel.set_is_op_tensor_core_eligible(tensor_core_eligible);
91 }
92 }
93
94 if (on_kernel_fn) {
95 on_kernel_fn(event, &kernel);
96 }
97
98 if (kernel.total_duration_ns()) {
99 KernelReportValue value;
100 value.total_duration_ns = event.DurationNs();
101 value.min_duration_ns = event.DurationNs();
102 value.max_duration_ns = event.DurationNs();
103 value.occurrences = 1;
104 InsertOrUpdateKernelReport(kernel, value, reports);
105 }
106 });
107 });
108 }
109
110 } // namespace profiler
111 } // namespace tensorflow
112