reshape_utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (c) Microsoft Corporation.
  2. # SPDX-License-Identifier: Apache-2.0
  3. # DeepSpeed Team
  4. import os
  5. import torch
  6. from collections import OrderedDict
  7. from .constants import (ZERO_FILE_PREFIX, FP16_ZERO_FILE_PREFIX, BF16_ZERO_FILE_PREFIX)
  8. def basic_folder_validation(dir):
  9. assert os.path.exists(dir), f'{dir} path does not exist'
  10. assert os.path.isdir(dir), f'{dir} is not a folder'
  11. def get_files_with_prefix(all_files, prefix):
  12. file_list = []
  13. for file_path in all_files:
  14. _, fname = os.path.split(file_path)
  15. if fname.startswith(prefix):
  16. file_list.append(file_path)
  17. return sorted(file_list)
  18. def validate_files(file_list):
  19. for file in file_list:
  20. if not os.path.isfile(file):
  21. print(f'Error: {file} is not existent')
  22. def get_files(dir):
  23. file_list = []
  24. for root, _, files in os.walk(dir):
  25. for file in files:
  26. file_list.append(os.path.join(root, file))
  27. return file_list
  28. def get_zero_files(dir):
  29. file_list = get_files(dir)
  30. for prefix in [ZERO_FILE_PREFIX, FP16_ZERO_FILE_PREFIX, BF16_ZERO_FILE_PREFIX]:
  31. zero_files = get_files_with_prefix(file_list, prefix)
  32. if len(zero_files) > 0:
  33. return zero_files
  34. return []
  35. def partition_data(data_list, num_partitions):
  36. num_elems = len(data_list)
  37. assert num_elems % num_partitions == 0
  38. partition_size = num_elems // num_partitions
  39. partitions_list = [data_list[i:i + partition_size] for i in range(0, num_elems, partition_size)]
  40. return partitions_list
  41. def _key_list_to_string(key_list):
  42. return '.'.join(key_list)
  43. def merge_state_dict(dict_a, dict_b, key_list):
  44. merged_dict = type(dict_a)({})
  45. for key, value in dict_b.items():
  46. if key in dict_a.keys():
  47. merged_dict[key] = merge_state(dict_a[key], dict_b[key], [str(key)])
  48. else:
  49. merged_dict[key] = value
  50. return merged_dict
  51. def merge_state_list(list_a, list_b, key_list):
  52. if len(list_a) != len(list_b):
  53. print(f'{_key_list_to_string(key_list)}')
  54. raise ValueError(f'Cannot merge lists of different lengths, a = {len(list_a)} b = {len(list_b)}')
  55. return [merge_state(a, b, key_list) for a, b in zip(list_a, list_b)]
  56. def merge_state(state_a, state_b, key_list=[]):
  57. if type(state_a) != type(state_b):
  58. key_list_string = _key_list_to_string(key_list)
  59. print(f'key_list = {key_list_string}')
  60. raise ValueError(f'Cannot merge two states of types {type(state_a)} and type {type(state_b)}')
  61. if type(state_a) in (dict, OrderedDict):
  62. return merge_state_dict(state_a, state_b, key_list)
  63. elif type(state_a) in (list, tuple):
  64. return type(state_a)(merge_state_list(state_a, state_b, key_list))
  65. elif torch.is_tensor(state_a):
  66. return torch.cat([state_a, state_b], 0)
  67. else:
  68. return state_a