Compose the command to execute on bolt. Args: script_filename: the .py filename. Note that it is assumed to be executed from the base folder. num_gpus: number of gpus params: the command-line arguments. They will be composed as -
(
script_filename: str,
num_gpus: int,
params: T.Dict[str, T.Any],
switch_names: T.List[str] = tuple(),
prefix: str = '/venv',
use_xvfb: bool = False,
)
| 42 | |
| 43 | |
| 44 | def compile_command( |
| 45 | script_filename: str, |
| 46 | num_gpus: int, |
| 47 | params: T.Dict[str, T.Any], |
| 48 | switch_names: T.List[str] = tuple(), |
| 49 | prefix: str = '/venv', |
| 50 | use_xvfb: bool = False, |
| 51 | ) -> str: |
| 52 | """ |
| 53 | Compose the command to execute on bolt. |
| 54 | |
| 55 | Args: |
| 56 | script_filename: |
| 57 | the .py filename. Note that it is assumed to be executed from the base folder. |
| 58 | num_gpus: |
| 59 | number of gpus |
| 60 | params: |
| 61 | the command-line arguments. They will be composed as --{key} {val}, except for |
| 62 | those contained in `switch_names` (which will become --{key} if val = true). |
| 63 | switch_names: |
| 64 | a list of names of params to be treated as switches. |
| 65 | use_xvfb: |
| 66 | if true, use xvfb to simulate an x window |
| 67 | |
| 68 | Returns: |
| 69 | |
| 70 | """ |
| 71 | |
| 72 | xvfb_prefix = 'xvfb-run -a -s "-screen 0 800x600x24" ' if use_xvfb else '' |
| 73 | if num_gpus > 1: |
| 74 | command = f'source /miniconda/etc/profile.d/conda.sh; ' \ |
| 75 | f'conda activate {prefix}; ' \ |
| 76 | f'CUDA_HOME="{prefix}/pkgs/cuda-toolkit" ' \ |
| 77 | f'PYTHONPATH="." ' \ |
| 78 | f'{xvfb_prefix}' \ |
| 79 | f'torchrun --standalone --nnodes=1 --nproc_per_node={num_gpus} ' \ |
| 80 | f'{script_filename} ' |
| 81 | else: |
| 82 | command = f'source /miniconda/etc/profile.d/conda.sh; ' \ |
| 83 | f'conda activate {prefix}; ' \ |
| 84 | f'CUDA_HOME="{prefix}/pkgs/cuda-toolkit" ' \ |
| 85 | f'PYTHONPATH="." ' \ |
| 86 | f'{xvfb_prefix}' \ |
| 87 | f'python ' \ |
| 88 | f'{script_filename} ' |
| 89 | |
| 90 | for key in params.keys(): |
| 91 | val = params[key] |
| 92 | if key in switch_names: |
| 93 | if val: |
| 94 | command += '--{} '.format(key) |
| 95 | else: |
| 96 | if isinstance(val, (list, tuple)): |
| 97 | command += '--{} '.format(key) |
| 98 | for v in val: |
| 99 | command += '{} '.format(v) |
| 100 | else: |
| 101 | command += '--{} {} '.format(key, val) |
nothing calls this directly
no outgoing calls
no test coverage detected