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 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 mindspore.profiler.common.util import get_file_join_name, fwrite_format
  18. from mindspore import log as logger
  19. from mindspore.profiler.common.validator.validate_path import \
  20. validate_and_normalize_path
  21. class MinddataParser:
  22. """Minddata Aicpu Parser."""
  23. @staticmethod
  24. def parse_step_minddata_aicpu_data(one_step, result):
  25. """
  26. Parse step mind_data ai_cpu data.
  27. Args:
  28. one_step (str): The mind_data step info text, it is one of two structures.
  29. Type queue: node_name,queue_size,run_start,run_end
  30. Type run: node_name,run_start,run_end,queue_size
  31. result ([[node_name, node_start, node_end, queue_size]]): Step info list.
  32. """
  33. if not one_step:
  34. return
  35. node_info = one_step.split(", ")
  36. node_name, node_start, node_end, queue_size = "", 0, 0, 0
  37. if node_info:
  38. node_name = node_info[0].replace("Node:", "")
  39. if len(node_info) > 3:
  40. if "queue" in node_info[1]:
  41. queue_size = node_info[1].replace("queue size:", "")
  42. node_start = node_info[2].replace("Run start:", "")
  43. node_end = node_info[3].replace("Run end:", "")
  44. elif "Run" in node_info[1]:
  45. queue_size = node_info[3].replace("queue size:", "")
  46. node_start = node_info[1].replace("Run start:", "")
  47. node_end = node_info[2].replace("Run end:", "")
  48. queue_size = int(queue_size) if queue_size.isdigit() else queue_size
  49. node_start = int(node_start) if node_start.isdigit() else node_start
  50. node_end = int(node_end) if node_end.isdigit() else node_end
  51. one_step_list = [node_name, node_start, node_end, queue_size]
  52. result.append(one_step_list)
  53. @staticmethod
  54. def parse_minddata_aicpu_data(minddata_aicpu_source_path):
  55. """
  56. Parse minddata get_next info which contains queue size and execute time.
  57. Args:
  58. minddata_aicpu_source_path (str): the source file path.
  59. Returns:
  60. list[Union[str, float]], the converted data.
  61. """
  62. result = list()
  63. try:
  64. minddata_aicpu_source_path = validate_and_normalize_path(minddata_aicpu_source_path)
  65. with open(minddata_aicpu_source_path) as source_data_file:
  66. source_data = source_data_file.read()
  67. step_data = source_data.split("\x00")
  68. for one_step in step_data:
  69. MinddataParser.parse_step_minddata_aicpu_data(one_step, result)
  70. except OSError:
  71. logger.error("Open get_next profiling file error.")
  72. return result
  73. @staticmethod
  74. def execute(source_path, output_path, device_id):
  75. """
  76. Execute the parser.
  77. Args:
  78. source_path (str): the source file path.
  79. output_path (str): the output file path.
  80. device_id (str): the device id.
  81. """
  82. col_names = ["node_name", "start_time", "end_time", "queue_size"]
  83. source_path = validate_and_normalize_path(source_path)
  84. minddata_aicpu_source_path = get_file_join_name(
  85. input_path=source_path, file_name='DATA_PREPROCESS.AICPUMI')
  86. if not minddata_aicpu_source_path:
  87. minddata_aicpu_source_path = get_file_join_name(
  88. input_path=source_path, file_name='DATA_PREPROCESS.dev.AICPUMI')
  89. if not minddata_aicpu_source_path:
  90. minddata_aicpu_source_path = get_file_join_name(
  91. input_path=os.path.join(source_path, "data"), file_name='DATA_PREPROCESS.AICPUMI')
  92. if not minddata_aicpu_source_path:
  93. minddata_aicpu_source_path = get_file_join_name(
  94. input_path=os.path.join(source_path, "data"), file_name='DATA_PREPROCESS.dev.AICPUMI')
  95. if not minddata_aicpu_source_path:
  96. return
  97. minddata_aicpu_output_path = os.path.join(output_path, "minddata_aicpu_" + device_id + ".txt")
  98. minddata_aicpu_data = MinddataParser.parse_minddata_aicpu_data(minddata_aicpu_source_path)
  99. if minddata_aicpu_data:
  100. fwrite_format(minddata_aicpu_output_path, " ".join(col_names), is_start=True)
  101. fwrite_format(minddata_aicpu_output_path, minddata_aicpu_data, is_start=True)