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 #include <string>
20
21 #include "absl/strings/string_view.h"
22 #include "tensorflow/core/platform/types.h"
23 #include "tensorflow/core/profiler/protobuf/kernel_stats.pb.h"
24 #include "tensorflow/core/profiler/protobuf/xplane.pb.h"
25 #include "tensorflow/core/profiler/utils/gpu_event_stats.h"
26 #include "tensorflow/core/profiler/utils/kernel_stats_utils.h"
27 #include "tensorflow/core/profiler/utils/tf_op_utils.h"
28 #include "tensorflow/core/profiler/utils/tf_xplane_visitor.h"
29 #include "tensorflow/core/profiler/utils/trace_utils.h"
30 #include "tensorflow/core/profiler/utils/xplane_visitor.h"
31
32 namespace tensorflow {
33 namespace profiler {
34
ConvertDeviceTraceXPlaneToKernelReports(const XPlane & device_trace,const std::function<void (const GpuEventStats &,KernelReport *)> & on_kernel_fn,KernelReportMap * reports)35 void ConvertDeviceTraceXPlaneToKernelReports(
36 const XPlane& device_trace,
37 const std::function<void(const GpuEventStats&, KernelReport*)>&
38 on_kernel_fn,
39 KernelReportMap* reports) {
40 XPlaneVisitor plane = CreateTfXPlaneVisitor(&device_trace);
41 plane.ForEachLine([&](const XLineVisitor& line) {
42 if (IsDerivedThreadId(line.Id())) {
43 return;
44 }
45 line.ForEachEvent([&](const XEventVisitor& event) {
46 if (event.DurationNs() == 0) return;
47 KernelReport kernel;
48 GpuEventStats stats(&event);
49 if (!stats.IsKernel()) return;
50
51 kernel.set_name(std::string(event.Name()));
52 kernel.set_is_kernel_using_tensor_core(
53 IsKernelUsingTensorCore(event.Name()));
54 kernel.set_total_duration_ns(event.DurationNs());
55 kernel.set_min_duration_ns(event.DurationNs());
56 kernel.set_max_duration_ns(event.DurationNs());
57 ParseKernelLaunchParams(stats.kernel_details, &kernel);
58
59 if (stats.IsTfOp()) {
60 TfOp tf_op = ParseTfOpFullname(stats.tf_op_fullname);
61 kernel.set_op_name(std::string(tf_op.name));
62 bool tensor_core_eligible =
63 IsEinsumTensorCoreEligible(stats.equation) ||
64 IsOpTensorCoreEligible(kernel.op_name());
65 if (!tensor_core_eligible && kernel.is_kernel_using_tensor_core()) {
66 VLOG(1) << "Detected new Op using TensorCores: " << kernel.op_name()
67 << std::endl;
68 tensor_core_eligible = true;
69 }
70 kernel.set_is_op_tensor_core_eligible(tensor_core_eligible);
71 }
72
73 if (on_kernel_fn) {
74 on_kernel_fn(stats, &kernel);
75 }
76
77 KernelReportValue value;
78 value.total_duration_ns = event.DurationNs();
79 value.min_duration_ns = event.DurationNs();
80 value.max_duration_ns = event.DurationNs();
81 value.occurrences = 1;
82 InsertOrUpdateKernelReport(kernel, value, reports);
83 });
84 });
85 }
86
87 } // namespace profiler
88 } // namespace tensorflow
89