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.

minddata_parser.py 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. """Minddata aicpu parser."""
  16. import os
  17. from tabulate import tabulate
  18. from mindinsight.profiler.common._utils import get_file_join_name, fwrite_format
  19. from mindinsight.profiler.common.log import logger
  20. class MinddataParser:
  21. """Minddata Aicpu Parser."""
  22. @staticmethod
  23. def parse_minddata_aicpu_data(minddata_aicpu_source_path):
  24. """
  25. Parse minddata get_next info which contains queue size and execute time.
  26. Args:
  27. minddata_aicpu_source_path (str): the source file path.
  28. Returns:
  29. list[Union[str, float]], the converted data.
  30. """
  31. result = list()
  32. try:
  33. with open(minddata_aicpu_source_path) as source_data_file:
  34. source_data = source_data_file.read()
  35. step_data = source_data.split("\x00")
  36. for one_step in step_data:
  37. if one_step:
  38. node_info = one_step.split(", ")
  39. node_name, node_start, node_end, queue_size = "", 0, 0, 0
  40. if node_info:
  41. node_name = node_info[0].replace("Node:", "")
  42. if len(node_info) > 2:
  43. node_start = node_info[1].replace("Run start:", "")
  44. if node_start.isdigit():
  45. node_start = int(node_start)
  46. node_end = node_info[2].replace("Run end:", "")
  47. if node_end.isdigit():
  48. node_end = int(node_end)
  49. if len(node_info) > 3:
  50. queue_size = node_info[3].replace("queue size:", "")
  51. if queue_size.isdigit():
  52. queue_size = int(queue_size)
  53. one_step_list = [node_name, node_start, node_end, queue_size]
  54. result.append(one_step_list)
  55. except OSError:
  56. logger.error("Open get_next profiling file error.")
  57. return result
  58. @staticmethod
  59. def execute(source_path, output_path, device_id):
  60. """
  61. Execute the parser.
  62. Args:
  63. source_path (str): the source file path.
  64. output_path (str): the output file path.
  65. device_id (str): the device id.
  66. """
  67. col_names = ["node_name", "start_time", "end_time", "queue_size"]
  68. minddata_aicpu_source_path = get_file_join_name(
  69. input_path=source_path, file_name='DATA_PREPROCESS.dev.AICPUMI')
  70. if not minddata_aicpu_source_path:
  71. minddata_aicpu_source_path = get_file_join_name(
  72. input_path=os.path.join(source_path, "data"), file_name='DATA_PREPROCESS.dev.AICPUMI')
  73. if not minddata_aicpu_source_path:
  74. return
  75. minddata_aicpu_output_path = os.path.join(output_path, "minddata_aicpu_" + device_id + ".txt")
  76. minddata_aicpu_data = MinddataParser.parse_minddata_aicpu_data(minddata_aicpu_source_path)
  77. if minddata_aicpu_data:
  78. fwrite_format(
  79. minddata_aicpu_output_path,
  80. tabulate(minddata_aicpu_data, col_names, tablefmt='simple'),
  81. is_start=True
  82. )