|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from argparse import Namespace |
| 6 | +from bobber.lib.exit_codes import SBATCH_CALL_ERROR, SLURM_QUEUE_ERROR |
| 7 | +from typing import NoReturn |
| 8 | + |
| 9 | + |
| 10 | +def _slurm_scripts_path() -> str: |
| 11 | + """ |
| 12 | + Find the absolute path to the slurm_scripts directory. |
| 13 | +
|
| 14 | + The slurm_scripts directory contains several *.sub files which are required |
| 15 | + to launch test commands via SLURM. Depending on how and where Bobber is |
| 16 | + installed on a system, the absolute path to this directory may change, but |
| 17 | + the relative path is easy to find compared to this module. By allowing |
| 18 | + Python to determine the absolute path to this module, the absolute path to |
| 19 | + slurm_scripts can be found by combining the absolute path of this module |
| 20 | + and the relative path to the slurm_scripts directory. |
| 21 | +
|
| 22 | + Returns |
| 23 | + ------- |
| 24 | + str |
| 25 | + Returns a ``string`` of the absolute path to the slurm_scripts |
| 26 | + directory. |
| 27 | + """ |
| 28 | + directory = os.path.dirname(os.path.realpath(__file__)) |
| 29 | + directory = os.path.join(directory, '../../slurm_scripts') |
| 30 | + return directory |
| 31 | + |
| 32 | + |
| 33 | +def _sbatch_path() -> str: |
| 34 | + """ |
| 35 | + Find the full path to the sbatch script. |
| 36 | +
|
| 37 | + While launching a Python process without "shell=True" as is done for the |
| 38 | + test commands below, the "sbatch" command is not available as Python |
| 39 | + launches a new process without a proper PATH variable. Running "which |
| 40 | + sbatch" with a shell instance provides the full path to sbatch which can |
| 41 | + later be used directly to invoke the script directly instead of using the |
| 42 | + alias. If sbatch is not installed on the system, the application will exit. |
| 43 | +
|
| 44 | + Returns |
| 45 | + ------- |
| 46 | + str |
| 47 | + Returns a ``string`` of the full local path to the sbatch script. |
| 48 | + """ |
| 49 | + result = subprocess.run('which sbatch', capture_output=True, shell=True) |
| 50 | + if not result.stderr and result.stdout: |
| 51 | + return str(result.stdout.strip().decode('ascii')) |
| 52 | + else: |
| 53 | + print('sbatch command not found. Please ensure SLURM is installed and ' |
| 54 | + 'functional.') |
| 55 | + sys.exit(SBATCH_CALL_ERROR) |
| 56 | + |
| 57 | + |
| 58 | +def run_nccl(args: Namespace, version: str) -> NoReturn: |
| 59 | + """ |
| 60 | + Launch a multi-node NCCL test via SLURM. |
| 61 | +
|
| 62 | + Launch a NCCL test for N-nodes managed by a SLURM cluster. Multiple tests |
| 63 | + are queued-up as sbatch commands which will only launch once the previous |
| 64 | + test has completed. |
| 65 | +
|
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + args : Namespace |
| 69 | + A ``Namespace`` of all settings specified by the user for the test. |
| 70 | + version : str |
| 71 | + A ``string`` of the Bobber version. |
| 72 | + """ |
| 73 | + # Update the version to be used in filenames |
| 74 | + version_underscore = version.replace('.', '_') |
| 75 | + # If not sweeping, set the range of nodes from N-hosts to N-hosts for a |
| 76 | + # single iteration of tests. |
| 77 | + lower_bound = args.hosts |
| 78 | + if args.sweep: |
| 79 | + lower_bound = 1 |
| 80 | + for hosts in range(lower_bound, args.hosts + 1): |
| 81 | + for iteration in range(1, args.iterations + 1): |
| 82 | + nccl_log = os.path.join(args.log_path, |
| 83 | + f'nccl_iteration_{iteration}_' |
| 84 | + f'gpus_{args.gpus}_' |
| 85 | + f'nccl_max_{args.nccl_max}_' |
| 86 | + f'gid_{args.compute_gid}_' |
| 87 | + f'nccl_tc_{args.nccl_tc}_' |
| 88 | + f'systems_{hosts}_' |
| 89 | + f'version_{version_underscore}.log') |
| 90 | + nccl_path = os.path.join(_slurm_scripts_path(), 'nccl.sub') |
| 91 | + sbatch = _sbatch_path() |
| 92 | + env = { |
| 93 | + 'HOSTS': str(hosts), |
| 94 | + 'FS_PATH': args.storage_path, |
| 95 | + 'CONT_VERSION': f'nvcr.io/nvidian/bobber:{version}', |
| 96 | + 'NCCL_MAX': str(args.nccl_max), |
| 97 | + 'LOGDIR': args.log_path, |
| 98 | + 'LOGPATH': nccl_log, |
| 99 | + 'NCCL_IB_HCAS': args.nccl_ib_hcas, |
| 100 | + 'COMPUTE_GID': str(args.compute_gid), |
| 101 | + 'NCCL_TC': args.nccl_tc or '' |
| 102 | + } |
| 103 | + cmd = [f'{sbatch}', |
| 104 | + '-N', |
| 105 | + f'{hosts}', |
| 106 | + f'--gpus-per-node={args.gpus}', |
| 107 | + '--wait', |
| 108 | + '--dependency=singleton', |
| 109 | + f'{nccl_path}'] |
| 110 | + try: |
| 111 | + print('Running:', cmd) |
| 112 | + subprocess.Popen(cmd, env=env) |
| 113 | + except subprocess.CalledProcessError: |
| 114 | + print('Error queueing SLURM job for NCCL tests. ' |
| 115 | + 'See output for errors.') |
| 116 | + sys.exit(SLURM_QUEUE_ERROR) |
| 117 | + |
| 118 | + |
| 119 | +def run_dali(args: Namespace, version: str) -> NoReturn: |
| 120 | + """ |
| 121 | + Launch a multi-node DALI test via SLURM. |
| 122 | +
|
| 123 | + Launch a DALI test for N-nodes managed by a SLURM cluster. Multiple tests |
| 124 | + are queued-up as sbatch commands which will only launch once the previous |
| 125 | + test has completed. |
| 126 | +
|
| 127 | + Parameters |
| 128 | + ---------- |
| 129 | + args : Namespace |
| 130 | + A ``Namespace`` of all settings specified by the user for the test. |
| 131 | + version : str |
| 132 | + A ``string`` of the Bobber version. |
| 133 | + """ |
| 134 | + # Update the version to be used in filenames |
| 135 | + version_underscore = version.replace('.', '_') |
| 136 | + # If not sweeping, set the range of nodes from N-hosts to N-hosts for a |
| 137 | + # single iteration of tests. |
| 138 | + lower_bound = args.hosts |
| 139 | + if args.sweep: |
| 140 | + lower_bound = 1 |
| 141 | + for hosts in range(lower_bound, args.hosts + 1): |
| 142 | + for iteration in range(1, args.iterations + 1): |
| 143 | + dali_log = os.path.join(args.log_path, |
| 144 | + f'dali_iteration_{iteration}_' |
| 145 | + f'gpus_{args.gpus}_' |
| 146 | + f'batch_size_lg_{args.batch_size_lg}_' |
| 147 | + f'batch_size_sm_{args.batch_size_sm}_' |
| 148 | + f'systems_{hosts}_' |
| 149 | + f'version_{version_underscore}.log') |
| 150 | + dali_path = os.path.join(_slurm_scripts_path(), 'dali.sub') |
| 151 | + sbatch = _sbatch_path() |
| 152 | + env = { |
| 153 | + 'HOSTS': str(hosts), |
| 154 | + 'FS_PATH': args.storage_path, |
| 155 | + 'CONT_VERSION': f'nvcr.io/nvidian/bobber:{version}', |
| 156 | + 'GPUS': str(args.gpus), |
| 157 | + 'LOGDIR': args.log_path, |
| 158 | + 'LOGPATH': dali_log, |
| 159 | + 'BATCH_SIZE_SM': str(args.batch_size_sm), |
| 160 | + 'BATCH_SIZE_LG': str(args.batch_size_lg) |
| 161 | + } |
| 162 | + cmd = [f'{sbatch}', |
| 163 | + '-N', |
| 164 | + f'{hosts}', |
| 165 | + f'--gpus-per-node={args.gpus}', |
| 166 | + '--wait', |
| 167 | + '--dependency=singleton', |
| 168 | + f'{dali_path}'] |
| 169 | + try: |
| 170 | + print('Running:', cmd) |
| 171 | + subprocess.Popen(cmd, env=env) |
| 172 | + except subprocess.CalledProcessError: |
| 173 | + print('Error queueing SLURM job for DALI tests. ' |
| 174 | + 'See output for errors.') |
| 175 | + sys.exit(SLURM_QUEUE_ERROR) |
0 commit comments