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.

aicpu_data_parser.py 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. """
  16. The parser for AI CPU preprocess data.
  17. """
  18. import os
  19. import stat
  20. from mindspore.profiler.common.util import fwrite_format, get_file_join_name
  21. from mindspore import log as logger
  22. class DataPreProcessParser:
  23. """
  24. The Parser for AI CPU preprocess data.
  25. Args:
  26. input_path(str): The profiling job path.
  27. output_filename(str): The output data path and name.
  28. """
  29. _source_file_target_old = 'DATA_PREPROCESS.dev.AICPU.'
  30. _source_file_target = 'DATA_PREPROCESS.AICPU.'
  31. _dst_file_title = 'title:DATA_PREPROCESS AICPU'
  32. _dst_file_column_title = ['serial_number', 'node_type_name', 'total_time(ms)',
  33. 'dispatch_time(ms)', 'run_start', 'run_end']
  34. _ms_unit = 1000
  35. def __init__(self, input_path, output_filename):
  36. self._input_path = input_path
  37. self._output_filename = output_filename
  38. self._source_file_name = self._get_source_file()
  39. self._ms_kernel_flag = 3
  40. self._other_kernel_flag = 6
  41. self._thread_flag = 7
  42. self._ms_kernel_run_end_index = 2
  43. self._other_kernel_run_end_index = 5
  44. self._result_list = []
  45. self._min_cycle_counter = float('inf')
  46. def _get_source_file(self):
  47. """Get log file name, which was created by ada service."""
  48. file_name = get_file_join_name(self._input_path, self._source_file_target)
  49. if not file_name:
  50. file_name = get_file_join_name(self._input_path, self._source_file_target_old)
  51. if not file_name:
  52. data_path = os.path.join(self._input_path, "data")
  53. file_name = get_file_join_name(data_path, self._source_file_target)
  54. if not file_name:
  55. file_name = get_file_join_name(data_path, self._source_file_target_old)
  56. return file_name
  57. def _get_kernel_result(self, number, node_list, thread_list):
  58. """Get the profiling data form different aicpu kernel"""
  59. try:
  60. if len(node_list) == self._ms_kernel_flag and len(thread_list) == self._thread_flag:
  61. node_type_name = node_list[0].split(':')[-1]
  62. run_end_index = self._ms_kernel_run_end_index
  63. elif len(node_list) == self._other_kernel_flag and len(thread_list) == self._thread_flag:
  64. node_type_name = node_list[0].split(':')[-1].split('/')[-1].split('-')[0]
  65. run_end_index = self._other_kernel_run_end_index
  66. else:
  67. logger.warning("the data format can't support 'node_list':%s", str(node_list))
  68. return None
  69. run_start = node_list[1].split(':')[-1].split(' ')[0]
  70. run_end = node_list[run_end_index].split(':')[-1].split(' ')[0]
  71. total_time = float(thread_list[-1].split('=')[-1].split()[0]) / self._ms_unit
  72. dispatch_time = float(thread_list[-2].split('=')[-1].split()[0]) / self._ms_unit
  73. return [number, node_type_name, total_time, dispatch_time,
  74. run_start, run_end]
  75. except IndexError as e:
  76. logger.error(e)
  77. return None
  78. def execute(self):
  79. """Execute the parser, get result data, and write it to the output file."""
  80. if not os.path.exists(self._source_file_name):
  81. logger.info("Did not find the aicpu profiling source file")
  82. return
  83. with open(self._source_file_name, 'rb') as ai_cpu_data:
  84. ai_cpu_str = str(ai_cpu_data.read().replace(b'\n\x00', b' ___ ')
  85. .replace(b'\x00', b' ___ '))[2:-1]
  86. ai_cpu_lines = ai_cpu_str.split(" ___ ")
  87. os.chmod(self._source_file_name, stat.S_IREAD | stat.S_IWRITE)
  88. result_list = list()
  89. ai_cpu_total_time_summary = 0
  90. # Node serial number.
  91. serial_number = 1
  92. for i in range(len(ai_cpu_lines) - 1):
  93. node_line = ai_cpu_lines[i]
  94. thread_line = ai_cpu_lines[i + 1]
  95. if "Node" in node_line and "Thread" in thread_line:
  96. # Get the node data from node_line
  97. node_list = node_line.split(',')
  98. thread_list = thread_line.split(',')
  99. result = self._get_kernel_result(serial_number, node_list, thread_list)
  100. if result is None:
  101. continue
  102. result_list.append(result)
  103. # Calculate the total time.
  104. total_time = result[2]
  105. ai_cpu_total_time_summary += total_time
  106. # Increase node serial number.
  107. serial_number += 1
  108. elif "Node" in node_line and "Thread" not in thread_line:
  109. node_type_name = node_line.split(',')[0].split(':')[-1]
  110. logger.warning("The node type:%s cannot find thread data", node_type_name)
  111. if result_list:
  112. ai_cpu_total_time = format(ai_cpu_total_time_summary, '.6f')
  113. result_list.append(["AI CPU Total Time(ms):", ai_cpu_total_time])
  114. fwrite_format(self._output_filename, " ".join(self._dst_file_column_title), is_start=True, is_print=True)
  115. fwrite_format(self._output_filename, result_list, is_print=True)
  116. # For timeline display.
  117. self._result_list = result_list
  118. def query_aicpu_data(self):
  119. """
  120. Get execution time of AI CPU operator.
  121. Returns:
  122. a dict, the metadata of AI CPU operator execution time.
  123. """
  124. stream_id = 0 # Default stream id for AI CPU.
  125. pid = 9000 # Default pid for AI CPU.
  126. factor = 1000 # Convert time unit from 1us to 1ms
  127. total_time = 0
  128. min_cycle_counter = float('inf')
  129. aicpu_info = []
  130. op_count_list = []
  131. for aicpu_item in self._result_list:
  132. if "AI CPU Total Time(ms):" in aicpu_item:
  133. total_time = aicpu_item[-1]
  134. continue
  135. op_name = aicpu_item[1]
  136. start_time = float(aicpu_item[4]) / factor
  137. min_cycle_counter = min(min_cycle_counter, start_time)
  138. end_time = float(aicpu_item[5]) / factor
  139. duration = end_time - start_time
  140. aicpu_info.append([op_name, stream_id, start_time, duration, pid])
  141. # Record the number of operator types.
  142. if op_name not in op_count_list:
  143. op_count_list.append(op_name)
  144. self._min_cycle_counter = min_cycle_counter
  145. aicpu_dict = {
  146. 'info': aicpu_info,
  147. 'total_time': float(total_time),
  148. 'op_exe_times': len(aicpu_info),
  149. 'num_of_ops': len(op_count_list),
  150. 'num_of_streams': 1
  151. }
  152. return aicpu_dict
  153. @property
  154. def min_cycle_counter(self):
  155. """Get minimum cycle counter in AI CPU."""
  156. return self._min_cycle_counter