You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

optime_parser.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. """Op compute time files parser."""
  16. from tabulate import tabulate
  17. from mindinsight.profiler.common._utils import fwrite_format
  18. class OPComputeTimeParser:
  19. """
  20. Join hwts info and framework info, get op time info, and output to the result file.
  21. Args:
  22. hwts_output_file (str): The file path of hwts_output_file. Such as: './output_format_data_hwts_0.txt".
  23. output_filename (str): The output data file path and name. Such as: './output_op_compute_time_0.txt'.
  24. op_task_info (dict): The task and op relation info. The format: {task_id, [opname, stream_id, block dim]}.
  25. """
  26. _dst_file_title = 'title:op compute time'
  27. _dst_file_column_title = ['op_name', 'compute_time(ms)', 'stream_id']
  28. def __init__(self, hwts_output_file, output_filename, op_task_info):
  29. self._hwts_output_file = hwts_output_file
  30. self._output_filename = output_filename
  31. self._op_task_info = op_task_info
  32. def _get_op_task_id_map(self):
  33. """
  34. Read hwts data file, get the task time info.
  35. Returns:
  36. list: all hwts task time info.
  37. """
  38. op_map_result = []
  39. hwts_list = []
  40. with(open(self._hwts_output_file, 'r')) as data_file:
  41. lines = data_file.readlines()
  42. for line in lines:
  43. if line.startswith("Start of task"):
  44. line_split = line.split()
  45. hwts_list.append([line_split[0], line_split[6], line_split[7], line_split[8]])
  46. if line.startswith('End of task'):
  47. line_split = line.split()
  48. hwts_list.append([line_split[0], line_split[6], line_split[7], line_split[8]])
  49. # hwts op map by taskId
  50. for hwts in hwts_list:
  51. if hwts[1] in self._op_task_info.keys():
  52. op_map_result.append([self._op_task_info[hwts[1]], hwts[0], hwts[1], hwts[2], hwts[3]])
  53. return op_map_result
  54. def execute(self):
  55. """Execute the parser, compute all op, get op time, and write it to the output file."""
  56. result_data = []
  57. tmp_result_data = []
  58. op_map_list = self._get_op_task_id_map()
  59. cur_index = 0
  60. length = len(op_map_list)
  61. while cur_index < length:
  62. if cur_index + 1 == length:
  63. break
  64. op_start = op_map_list[cur_index]
  65. op_end = op_map_list[cur_index+1]
  66. if op_start[1] == "Start" and op_end[1] == "End"\
  67. and op_start[0] == op_end[0]:
  68. # op_name, task_id, cycle counter, stream_id
  69. tmp_result_data.append([op_start[0], op_start[2], int(op_end[3]) - int(op_start[3]), op_start[4]])
  70. cur_index += 2
  71. else:
  72. cur_index += 1
  73. op_name_time_dict = {}
  74. op_name_steamid_dict = {}
  75. op_name_count_dict = {}
  76. op_name_task_dict = {}
  77. # compute all op
  78. for item in tmp_result_data:
  79. if item[0] in op_name_time_dict.keys():
  80. op_name_time_dict[item[0]] += float(item[2])/1e5 # cycle counter/1*10^5 ms
  81. if item[1] == op_name_task_dict[item[0]]:
  82. op_name_count_dict[item[0]] += 1
  83. else:
  84. op_name_time_dict[item[0]] = float(item[2])/1e5
  85. op_name_steamid_dict[item[0]] = item[-1]
  86. op_name_task_dict[item[0]] = item[1]
  87. op_name_count_dict[item[0]] = 1
  88. for op_name, time in op_name_time_dict.items():
  89. if op_name in op_name_steamid_dict.keys():
  90. stream_id = op_name_steamid_dict[op_name]
  91. avg_time = time / op_name_count_dict[op_name]
  92. result_data.append([op_name, avg_time, stream_id])
  93. result_data.sort(key=lambda x: x[0])
  94. total_time = 0
  95. for item in result_data:
  96. total_time += item[1]
  97. result_data.append(["total op", total_time, 0])
  98. fwrite_format(self._output_filename, data_source=self._dst_file_title, is_start=True)
  99. fwrite_format(self._output_filename, data_source=tabulate(result_data,
  100. self._dst_file_column_title,
  101. tablefmt='simple'))