torchtitan icon indicating copy to clipboard operation
torchtitan copied to clipboard

Hpc setup

Open githubsgi opened this issue 8 months ago • 9 comments

HPC type setup ( e.g. mpirun , mpiexec) currently requires code change in train.py ( or elsewhere) . Adding couple of optional additional args ( e.g. rank and local rank) to toml file along with a simple function in train.py removes the need to make those changes.

githubsgi avatar Mar 22 '25 01:03 githubsgi

Aren’t those environment variables set automatically when using torchrun command?

JungHoyoun avatar Mar 22 '25 02:03 JungHoyoun

As I mentioned above, this is for non-torchrun launchers like mpirun/mpiexec etc. Torchrun is very specific to torch, HPC environments run many other applications which can not use torchrun. HPC environments are optimized for whatever launcher they were designed for. In those environments, torchrun usually does not work or has sub-optimal performance.

githubsgi avatar Mar 22 '25 02:03 githubsgi

@tianyu-l and @fegin , thanks for your replies. Let me try to address the questions one by one below.

  1. Setting variable in run_train.sh : It does not work because of a subtle reason. The job launchers like torchrun/mpirun/mpiexec set the local and global rank variable uniquely in the shell/process of each rank. Hence, setting rank variables outside the launcher will make them all same or non-unique.
  2. LOCAL_RANK=some_rank to launch command : same as 1 above .
  3. Extending-jobconfig: it is certainly possible to make it work, but it would be far more complex than what is needed. Also, it would require maintenance of a separate piece of code elsewhere. Different launcher's already uniquely set the ranks variables per-rank process/shell . All that is left to be done is copy them to the variables that TorchTitan is expecting.

githubsgi avatar Mar 24 '25 17:03 githubsgi

Hi @githubsgi, you might be interested on running something like this

TJ-Solergibert avatar Mar 24 '25 17:03 TJ-Solergibert

@TJ-Solergibert , it has the same issue as 1 above, if I understand the script.

githubsgi avatar Mar 24 '25 18:03 githubsgi

In torch distributed programs you have to set 5 environment variables:

  • MASTER_ADDR, MASTER_PORT & WORLD_SIZE which need to be COMMON for all the processes. This is done here
  • RANK & LOCAL_RANK which are PARTICULAR for every process. Recall that we have one process por GPU. This is done here.

In this example you can consider srun as mpirun and you set the number of processes through this 2 settings.

TJ-Solergibert avatar Mar 24 '25 18:03 TJ-Solergibert

@TJ-Solergibert , thanks for pointing out the following line for SLURM launcher srun

srun $SRUN_ARGS bash -c "RANK=\$SLURM_PROCID LOCAL_RANK=\$SLURM_LOCALID $CMD" 2>&1 | tee -a $LOG_PATH .

The above also would benefit from simplification I am proposing - see below.

srun $SRUN_ARGS $CMD 2>&1 | tee -a $LOG_PATH

githubsgi avatar Mar 24 '25 19:03 githubsgi

@githubsgi You can check https://github.com/pytorch/torchtitan/blob/main/docs/extension.md#extending-jobconfig. This should meet your goal instead of adding new ones to the main JobConfig.

fegin avatar Mar 25 '25 17:03 fegin

@fegin , I have already addressed the complexity of that approach above. In fact, the approach suggested by @TJ-Solergibert above is simpler and more maintainable. The 2 environment variables I am proposing is applicable to any launcher and thus makes TorchTitan code very portable,

githubsgi avatar Mar 25 '25 18:03 githubsgi