Skip to content

Commit de25ba7

Browse files
committed
modified DDP tutorial from Princeton
1 parent 216740a commit de25ba7

1 file changed

Lines changed: 22 additions & 27 deletions

File tree

docs/hpc/08_ml_ai_hpc/03_pytorch_on_hpc_mulit_gpu.md

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ The Python interpreter will be launched 8 times (2 x 4) and each of the 8 tasks
137137

138138
Below is a full Slurm script for using DDP for Della (GPU) where there are 2 GPUs per node:
139139

140+
**run-full.SBATCH:**
140141
```bash
141142
#!/bin/bash
142143

@@ -220,9 +221,6 @@ Below is an example Slurm script for DDP:
220221
#SBATCH --mem=32G # total memory per node (4 GB per cpu-core is default)
221222
#SBATCH --gres=gpu:2 # number of gpus per node
222223
#SBATCH --time=00:05:00 # total run time limit (HH:MM:SS)
223-
#SBATCH --mail-type=begin # send email when job begins
224-
#SBATCH --mail-type=end # send email when job ends
225-
#SBATCH --mail-user=<YourNetID>@princeton.edu
226224

227225
export MASTER_PORT=$(expr 10000 + $(echo -n $SLURM_JOBID | tail -c 4))
228226
export WORLD_SIZE=$(($SLURM_NNODES * $SLURM_NTASKS_PER_NODE))
@@ -232,11 +230,11 @@ master_addr=$(scontrol show hostnames "$SLURM_JOB_NODELIST" | head -n 1)
232230
export MASTER_ADDR=$master_addr
233231
echo "MASTER_ADDR="$MASTER_ADDR
234232

235-
module purge
236-
module load anaconda3/2021.11
237-
conda activate torch-env
238-
239-
srun python mnist_classify_ddp.py --epochs=2
233+
srun singularity exec --nv \
234+
--overlay /scratch/NetID/pytorch-example/my_pytorch.ext3:ro \
235+
/scratch/work/public/singularity/cuda12.1.1-cudnn8.9.0-devel-ubuntu22.04.2.sif\
236+
/bin/bash -c "source /ext3/env.sh; python download_data.py; python mnist_classify_ddp.py --epoch
237+
s=2"
240238
```
241239

242240
The script above uses 2 nodes with 2 tasks per node and therefore 2 GPUs per node. This yields a total of 4 processes and each process can use 8 CPU-cores for data loading. An allocation of 4 GPUs is substantial so the queue time may be long. In all cases make sure that the GPUs are being used efficiently by monitoring the [GPU utilization](https://researchcomputing.princeton.edu/support/knowledge-base/gpu-computing).
@@ -411,29 +409,26 @@ In the script above the number of workers is taken directly from the value of `-
411409
cuda_kwargs = {'num_workers': int(os.environ["SLURM_CPUS_PER_TASK"]), 'pin_memory': True, 'shuffle': True}
412410
```
413411

414-
Execute the commands below to run the example above:
412+
It also relies on the script [download_data.py](https://github.com/PrincetonUniversity/multi_gpu_training/blob/main/01_single_gpu/download_data.py):
413+
```python
414+
import torchvision
415+
import warnings
416+
warnings.simplefilter("ignore")
417+
418+
# compute nodes do not have internet so download the data in advance
419+
420+
_ = torchvision.datasets.MNIST(root='data',
421+
train=True,
422+
transform=None,
423+
target_transform=None,
424+
download=True)
425+
```
415426

427+
Execute the commands below to run the example above:
416428
```bash
417-
$ git clone https://github.com/PrincetonUniversity/multi_gpu_training.git
418-
$ cd multi_gpu_training/02_pytorch_ddp
419-
$ module load anaconda3/2021.11
420-
$ conda activate torch-env # see 01_single_gpu in this repo for installation directions
421-
(torch-env) $ python download_data.py
422-
(torch-env) $ sbatch job.slurm # edit your email address in job.slurm before submitting
429+
[NetID@log-1 full-ddp-test]$ sbatch run-full.SBATCH
423430
```
424431

425432
## Memory issues
426433

427434
Use `gradient_as_bucket_view=True` when making the DDP model to decrease the required memory by 1/3.
428-
429-
## NGC Container
430-
431-
If you are using the [PyTorch container](https://researchcomputing.princeton.edu/support/knowledge-base/pytorch#containers) then the last line of your Slurm script will look like:
432-
433-
```
434-
srun singularity exec --nv $HOME/software/pytorch_22.01-py3.sif python mnist_classify_ddp.py --epochs=3
435-
```
436-
437-
## Notes on Traverse
438-
439-
Be sure to use the example above for DDP. Do not use the file-based method for initializing the process group. Be sure to follow the [installation directions](https://researchcomputing.princeton.edu/support/knowledge-base/tensorflow#install) using the MIT Conda channel.

0 commit comments

Comments
 (0)