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.

launch.py 6.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. """launch train script"""
  16. import os
  17. import sys
  18. import json
  19. import subprocess
  20. from argparse import ArgumentParser
  21. def parse_args():
  22. """
  23. parse args .
  24. Args:
  25. Returns:
  26. args.
  27. Examples:
  28. >>> parse_args()
  29. """
  30. parser = ArgumentParser(description="mindspore distributed training launch "
  31. "helper utilty that will spawn up "
  32. "multiple distributed processes")
  33. parser.add_argument("--nproc_per_node", type=int, default=1,
  34. help="The number of processes to launch on each node, "
  35. "for D training, this is recommended to be set "
  36. "to the number of D in your system so that "
  37. "each process can be bound to a single D.")
  38. parser.add_argument("--visible_devices", type=str, default="0,1,2,3,4,5,6,7",
  39. help="will use the visible devices sequentially")
  40. parser.add_argument("--server_id", type=str, default="",
  41. help="server ip")
  42. parser.add_argument("--training_script", type=str,
  43. help="The full path to the single D training "
  44. "program/script to be launched in parallel, "
  45. "followed by all the arguments for the "
  46. "training script")
  47. # rest from the training program
  48. args, unknown = parser.parse_known_args()
  49. args.training_script_args = unknown
  50. return args
  51. def main():
  52. print("start", __file__)
  53. args = parse_args()
  54. print(args)
  55. visible_devices = args.visible_devices.split(',')
  56. assert os.path.isfile(args.training_script)
  57. assert len(visible_devices) >= args.nproc_per_node
  58. print('visible_devices:{}'.format(visible_devices))
  59. if not args.server_id:
  60. print('pleaser input server ip!!!')
  61. exit(0)
  62. print('server_id:{}'.format(args.server_id))
  63. # construct hccn_table
  64. hccn_configs = open('/etc/hccn.conf', 'r').readlines()
  65. device_ips = {}
  66. for hccn_item in hccn_configs:
  67. hccn_item = hccn_item.strip()
  68. if hccn_item.startswith('address_'):
  69. device_id, device_ip = hccn_item.split('=')
  70. device_id = device_id.split('_')[1]
  71. device_ips[device_id] = device_ip
  72. print('device_id:{}, device_ip:{}'.format(device_id, device_ip))
  73. hccn_table = {}
  74. hccn_table['board_id'] = '0x0000'
  75. hccn_table['chip_info'] = '910'
  76. hccn_table['deploy_mode'] = 'lab'
  77. hccn_table['group_count'] = '1'
  78. hccn_table['group_list'] = []
  79. instance_list = []
  80. usable_dev = ''
  81. for instance_id in range(args.nproc_per_node):
  82. instance = {}
  83. instance['devices'] = []
  84. device_id = visible_devices[instance_id]
  85. device_ip = device_ips[device_id]
  86. usable_dev += str(device_id)
  87. instance['devices'].append({
  88. 'device_id': device_id,
  89. 'device_ip': device_ip,
  90. })
  91. instance['rank_id'] = str(instance_id)
  92. instance['server_id'] = args.server_id
  93. instance_list.append(instance)
  94. hccn_table['group_list'].append({
  95. 'device_num': str(args.nproc_per_node),
  96. 'server_num': '1',
  97. 'group_name': '',
  98. 'instance_count': str(args.nproc_per_node),
  99. 'instance_list': instance_list,
  100. })
  101. hccn_table['para_plane_nic_location'] = 'device'
  102. hccn_table['para_plane_nic_name'] = []
  103. for instance_id in range(args.nproc_per_node):
  104. eth_id = visible_devices[instance_id]
  105. hccn_table['para_plane_nic_name'].append('eth{}'.format(eth_id))
  106. hccn_table['para_plane_nic_num'] = str(args.nproc_per_node)
  107. hccn_table['status'] = 'completed'
  108. # save hccn_table to file
  109. table_path = os.getcwd()
  110. if not os.path.exists(table_path):
  111. os.mkdir(table_path)
  112. table_fn = os.path.join(table_path,
  113. 'rank_table_{}p_{}_{}.json'.format(args.nproc_per_node, usable_dev, args.server_id))
  114. with open(table_fn, 'w') as table_fp:
  115. json.dump(hccn_table, table_fp, indent=4)
  116. sys.stdout.flush()
  117. # spawn the processes
  118. processes = []
  119. cmds = []
  120. for rank_id in range(0, args.nproc_per_node):
  121. device_id = visible_devices[rank_id]
  122. device_dir = os.path.join(os.getcwd(), 'device{}'.format(rank_id))
  123. rank_process = 'export RANK_SIZE={} && export RANK_ID={} && export DEVICE_ID={} && '.format(args.nproc_per_node,
  124. rank_id, device_id)
  125. if args.nproc_per_node > 1:
  126. rank_process += 'export MINDSPORE_HCCL_CONFIG_PATH={} && '.format(table_fn)
  127. rank_process += 'export RANK_TABLE_FILE={} && '.format(table_fn)
  128. rank_process += 'rm -rf {dir} && mkdir {dir} && cd {dir} && python {script} '.format(dir=device_dir,
  129. script=args.training_script
  130. )
  131. rank_process += ' '.join(args.training_script_args) + ' > log{}.log 2>&1 &'.format(rank_id)
  132. process = subprocess.Popen(rank_process, shell=True)
  133. processes.append(process)
  134. cmds.append(rank_process)
  135. for process, cmd in zip(processes, cmds):
  136. process.wait()
  137. if process.returncode != 0:
  138. raise subprocess.CalledProcessError(returncode=process, cmd=cmd)
  139. if __name__ == "__main__":
  140. main()