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.

convert_async.py 4.4 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2021 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. """Module to provide conversion capabalities from .timestamp async dump files to .npy."""
  16. import site
  17. import os
  18. DIR_PATH = "/usr/local/Ascend/toolkit/tools/operator_cmp/compare/"
  19. if not os.path.exists(DIR_PATH):
  20. raise ValueError("Directory " + DIR_PATH + " does not exist. Please install Ascend toolkit.")
  21. site.addsitedir(DIR_PATH)
  22. #pylint: disable=wrong-import-position
  23. import argparse
  24. import csv
  25. from dump_data_parser import DumpDataParser
  26. from shape_conversion import FormatConversionMain
  27. import utils
  28. #pylint: enable=wrong-import-position
  29. def handle_multi_process(convert_obj, files):
  30. """Convert async format files to npy in a multithreaded manner"""
  31. #pylint: disable=W0212
  32. return_code = utils.VECTOR_COMPARISON_NONE_ERROR
  33. convert_obj.progress = utils.Progress(len(files))
  34. multi_process_file_list = []
  35. big_file_list = []
  36. max_file_size = convert_obj._get_max_file_size()
  37. for cur_file in files:
  38. cur_path = cur_file
  39. if os.path.isfile(cur_path):
  40. if os.path.getsize(cur_path) > max_file_size:
  41. big_file_list.append(cur_path)
  42. else:
  43. multi_process_file_list.append(cur_path)
  44. if multi_process_file_list:
  45. ret = convert_obj._do_multi_process(multi_process_file_list)
  46. if ret != utils.VECTOR_COMPARISON_NONE_ERROR:
  47. return_code = ret
  48. for big_file in big_file_list:
  49. ret, _ = convert_obj.convert_format_for_one_file(big_file)
  50. convert_obj._handle_result_callback([ret, big_file])
  51. if ret != utils.VECTOR_COMPARISON_NONE_ERROR:
  52. return_code = ret
  53. if return_code != utils.VECTOR_COMPARISON_NONE_ERROR:
  54. error_file_path = os.path.join(
  55. convert_obj.output_path, utils.CONVERT_FAILED_FILE_LIST_NAME)
  56. if os.path.exists(error_file_path):
  57. utils.print_info_log(
  58. 'The list of file that failed to convert has been written to "' + error_file_path + '".')
  59. # pylint: enable=W0212
  60. return return_code
  61. convert_parser = argparse.ArgumentParser()
  62. convert_parser.add_argument(
  63. '-d', '--dump_file', dest='dump_path', default='', required=True)
  64. convert_parser.add_argument(
  65. '-l', '--file_list', nargs="*", dest='file_list', default='')
  66. convert_parser.add_argument('-f', '--format', dest='format', default=None)
  67. convert_parser.add_argument(
  68. '-v', '--version', dest='dump_version', choices=[1, 2], type=int, default=2)
  69. convert_parser.add_argument('-s', '--shape', dest='shape', default=None)
  70. convert_parser.add_argument('-o', '--output_tensor',
  71. dest='output', default=None)
  72. convert_parser.add_argument('-i', '--input_tensor', dest='input', default=None)
  73. convert_parser.add_argument(
  74. '-c', '--custom_script_path', dest='custom_script_path', default=None)
  75. convert_parser.add_argument('-out', '--output', dest='output_path', default='')
  76. convert_parser.add_argument(
  77. '-t', '--type', dest='output_file_type', choices=['npy', 'bin'], default='npy')
  78. args = convert_parser.parse_args()
  79. dump_failed = os.path.abspath(args.dump_path) + "/convert_failed_file_list.txt"
  80. if os.path.exists(dump_failed):
  81. os.remove(dump_failed)
  82. file_list = args.file_list
  83. if args.format is not None:
  84. convert = FormatConversionMain(args)
  85. else:
  86. convert = DumpDataParser(args)
  87. if args.file_list == "":
  88. file_list = os.listdir(args.dump_path)
  89. handle_multi_process(convert, file_list)
  90. if os.path.exists(dump_failed):
  91. with open(dump_failed, newline='') as failed_ops:
  92. file_reader = csv.reader(failed_ops, delimiter=',')
  93. file_list = [os.path.abspath(row[0]) for row in file_reader]
  94. args.format = None
  95. convert = DumpDataParser(args)
  96. handle_multi_process(convert, file_list)