#!python

if __name__ == '__main__':
    import os, sys, torch
    arguments = sys.argv[1:]
    run_file_name = arguments[0]

    try:
        import deepspeed
        parallel_type = 'ds'
    except:
        gpu_count = torch.cuda.device_count()
        if gpu_count <= 1:
            parallel_type = 'none'
        else:
            parallel_type = 'ddp'

    os.environ['PARALLEL_TYPE'] = parallel_type

    if parallel_type == 'ds':
        command = f'deepspeed {run_file_name}'
    elif parallel_type == 'ddp':
        command = f'torchrun --standalone --nproc_per_node=gpu {run_file_name}'
    else:
        command = f'python3 {run_file_name}'

    print(f'real command is {command}')
    os.system(command)
