1 /* Copyright 2021 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/lite/delegates/gpu/common/task/profiling_info.h" 17 18 #include <map> 19 #include <string> 20 21 namespace tflite { 22 namespace gpu { 23 GetTotalTime() const24absl::Duration ProfilingInfo::GetTotalTime() const { 25 absl::Duration total_time; 26 for (const auto& dispatch : dispatches) { 27 total_time += dispatch.duration; 28 } 29 return total_time; 30 } 31 GetDetailedReport() const32std::string ProfilingInfo::GetDetailedReport() const { 33 std::string result; 34 struct OpStatistic { 35 int count; 36 double total_time; 37 }; 38 std::map<std::string, OpStatistic> statistics; 39 result += 40 "Per kernel timing(" + std::to_string(dispatches.size()) + " kernels):\n"; 41 for (const auto& dispatch : dispatches) { 42 result += " " + dispatch.label + " - " + 43 std::to_string(absl::ToDoubleMilliseconds(dispatch.duration)) + 44 " ms"; 45 const double times_per_sec = 46 1000.0 / absl::ToDoubleMilliseconds(dispatch.duration); 47 if (dispatch.read_mem_size && dispatch.write_mem_size) { 48 const uint64_t total_size = 49 dispatch.read_mem_size + dispatch.write_mem_size; 50 const double giga_bytes = total_size / 1024.0 / 1024.0 / 1024.0; 51 const double giga_bytes_per_sec = times_per_sec * giga_bytes; 52 result += ", " + std::to_string(giga_bytes_per_sec) + " Gb/s"; 53 } 54 if (dispatch.flops) { 55 const double giga_flops = dispatch.flops / 1000.0 / 1000.0 / 1000.0; 56 const double giga_flops_per_sec = times_per_sec * giga_flops; 57 result += ", " + std::to_string(giga_flops_per_sec) + " Gflops"; 58 } 59 result += "\n"; 60 auto name = dispatch.label.substr(0, dispatch.label.find(' ')); 61 if (statistics.find(name) != statistics.end()) { 62 statistics[name].count++; 63 statistics[name].total_time += 64 absl::ToDoubleMilliseconds(dispatch.duration); 65 } else { 66 statistics[name].count = 1; 67 statistics[name].total_time = 68 absl::ToDoubleMilliseconds(dispatch.duration); 69 } 70 } 71 result += "--------------------\n"; 72 result += "Accumulated time per operation type:\n"; 73 for (auto& t : statistics) { 74 auto stat = t.second; 75 result += " " + t.first + "(x" + std::to_string(stat.count) + ") - " + 76 std::to_string(stat.total_time) + " ms\n"; 77 } 78 result += "--------------------\n"; 79 result += "Ideal total time: " + 80 std::to_string(absl::ToDoubleMilliseconds(GetTotalTime())) + "\n"; 81 result += "--------------------\n"; 82 return result; 83 } 84 85 } // namespace gpu 86 } // namespace tflite 87