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.

dump_test_utils.py 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. """
  16. Utils for testing offline debugger.
  17. """
  18. import os
  19. import tempfile
  20. import bisect
  21. import csv
  22. import numpy as np
  23. def build_dump_structure(path, tensor_name_list, tensor_list, net_name, tensor_info_list):
  24. """Build dump file structure from tensor_list."""
  25. ranks_run_history = {}
  26. temp_dir = tempfile.mkdtemp(prefix=net_name, dir=path)
  27. for tensor_name, tensor, tensor_info in zip(tensor_name_list, tensor_list, tensor_info_list):
  28. slot = str(tensor_info.slot)
  29. iteration = str(tensor_info.iteration)
  30. rank_id = str(tensor_info.rank_id)
  31. root_graph_id = str(tensor_info.root_graph_id)
  32. is_output = str(tensor_info.is_output)
  33. graphs_run_history = ranks_run_history.get(rank_id)
  34. if graphs_run_history is None:
  35. graphs_run_history = {}
  36. ranks_run_history[rank_id] = graphs_run_history
  37. if root_graph_id not in graphs_run_history:
  38. graphs_run_history[root_graph_id] = [iteration]
  39. if iteration not in graphs_run_history[root_graph_id]:
  40. bisect.insort(graphs_run_history[root_graph_id], iteration)
  41. path = os.path.join(temp_dir, "rank_" + rank_id, net_name, root_graph_id, iteration)
  42. os.makedirs(path, exist_ok=True)
  43. if is_output == "True":
  44. file_name = f'{tensor_name}.output.{slot}.DefaultFormat.npy'
  45. else:
  46. file_name = f'{tensor_name}.input.{slot}.DefaultFormat.npy'
  47. full_path = os.path.join(path, file_name)
  48. np.save(full_path, tensor)
  49. build_global_execution_order(temp_dir, ranks_run_history)
  50. return temp_dir
  51. def build_global_execution_order(path, ranks_run_history):
  52. """Build global execution order."""
  53. for rank_id in ranks_run_history.keys():
  54. exec_order_path = path + "/rank_" + rank_id + "/" + "execution_order"
  55. os.makedirs(exec_order_path, exist_ok=True)
  56. for graph in ranks_run_history[rank_id].keys():
  57. full_path = os.path.join(exec_order_path, "ms_global_execution_order_graph_" + graph + ".csv")
  58. with open(full_path, 'w+', newline='') as csv_file:
  59. write = csv.writer(csv_file)
  60. write.writerows(ranks_run_history[rank_id][graph])