Skip to content

Multi-GPU Training

📚 This guide explains how to properly use multiple GPUs to train a dataset with YOLOv5 🚀 on single or multiple machine(s).

Before You Start

Clone repo and install requirements.txt in a Python>=3.8.0 environment, including PyTorch>=1.8. Models and datasets download automatically from the latest YOLOv5 release.

git clone  # clone
cd yolov5
pip install -r requirements.txt  # install

💡 ProTip! Docker Image is recommended for all Multi-GPU trainings. See Docker Quickstart Guide Docker Pulls

💡 ProTip! replaces torch.distributed.launch in PyTorch>=1.9. See docs for details.


Select a pretrained model to start training from. Here we select YOLOv5s, the smallest and fastest model available. See our README table for a full comparison of all models. We will train this model with Multi-GPU on the COCO dataset.

YOLOv5 Models

Single GPU

python  --batch 64 --data coco.yaml --weights --device 0

You can increase the device to use Multiple GPUs in DataParallel mode.

python  --batch 64 --data coco.yaml --weights --device 0,1

This method is slow and barely speeds up training compared to using just 1 GPU.

You will have to pass python -m --nproc_per_node, followed by the usual arguments.

python -m --nproc_per_node 2 --batch 64 --data coco.yaml --weights --device 0,1

--nproc_per_node specifies how many GPUs you would like to use. In the example above, it is 2. --batch is the total batch-size. It will be divided evenly to each GPU. In the example above, it is 64/2=32 per GPU.

The code above will use GPUs 0... (N-1).

Use specific GPUs (click to expand) You can do so by simply passing `--device` followed by your specific GPUs. For example, in the code below, we will use GPUs `2,3`.
python -m --nproc_per_node 2 --batch 64 --data coco.yaml --cfg yolov5s.yaml --weights '' --device 2,3
Use SyncBatchNorm (click to expand) [SyncBatchNorm]( could increase accuracy for multiple gpu training, however, it will slow down training by a significant factor. It is **only** available for Multiple GPU DistributedDataParallel training. It is best used when the batch-size on **each** GPU is small (<= 8). To use SyncBatchNorm, simple pass `--sync-bn` to the command like below,
python -m --nproc_per_node 2 --batch 64 --data coco.yaml --cfg yolov5s.yaml --weights '' --sync-bn
Use Multiple machines (click to expand) This is **only** available for Multiple GPU DistributedDataParallel training. Before we continue, make sure the files on all machines are the same, dataset, codebase, etc. Afterward, make sure the machines can communicate to each other. You will have to choose a master machine(the machine that the others will talk to). Note down its address(`master_addr`) and choose a port(`master_port`). I will use `master_addr =` and `master_port = 1234` for the example below. To use it, you can do as the following,
# On master machine 0
python -m --nproc_per_node G --nnodes N --node_rank 0 --master_addr "" --master_port 1234 --batch 64 --data coco.yaml --cfg yolov5s.yaml --weights ''
# On machine R
python -m --nproc_per_node G --nnodes N --node_rank R --master_addr "" --master_port 1234 --batch 64 --data coco.yaml --cfg yolov5s.yaml --weights ''
where `G` is number of GPU per machine, `N` is the number of machines, and `R` is the machine number from `0...(N-1)`. Let's say I have two machines with two GPUs each, it would be `G = 2` , `N = 2`, and `R = 1` for the above. Training will not start until all `N` machines are connected. Output will only be shown on master machine!


  • Windows support is untested, Linux is recommended.
  • --batch must be a multiple of the number of GPUs.
  • GPU 0 will take slightly more memory than the other GPUs as it maintains EMA and is responsible for checkpointing etc.
  • If you get RuntimeError: Address already in use, it could be because you are running multiple trainings at a time. To fix this, simply use a different port number by adding --master_port like below,
python -m --master_port 1234 --nproc_per_node 2 ...


DDP profiling results on an AWS EC2 P4d instance with 8x A100 SXM4-40GB for YOLOv5l for 1 COCO epoch.

Profiling code
# prepare
t=ultralytics/yolov5:latest && sudo docker pull $t && sudo docker run -it --ipc=host --gpus all -v "$(pwd)"/coco:/usr/src/coco $t
pip3 install torch==1.9.0+cu111 torchvision==0.10.0+cu111 -f
cd .. && rm -rf app && git clone -b master app && cd app
cp data/coco.yaml data/coco_profile.yaml

# profile
python --batch-size 16 --data coco_profile.yaml --weights --epochs 1 --device 0
python -m --nproc_per_node 2 --batch-size 32 --data coco_profile.yaml --weights --epochs 1 --device 0,1
python -m --nproc_per_node 4 --batch-size 64 --data coco_profile.yaml --weights --epochs 1 --device 0,1,2,3
python -m --nproc_per_node 8 --batch-size 128 --data coco_profile.yaml --weights --epochs 1 --device 0,1,2,3,4,5,6,7
batch-size CUDA_mem
device0 (G)
1x 16 26GB 20:39 0:55
2x 32 26GB 11:43 0:57
4x 64 26GB 5:57 0:55
8x 128 26GB 3:09 0:57


If an error occurs, please read the checklist below first! (It could save your time)

Checklist (click to expand)
  • Have you properly read this post?
  • Have you tried to re-clone the codebase? The code changes daily.
  • Have you tried to search for your error? Someone may have already encountered it in this repo or in another and have the solution.
  • Have you installed all the requirements listed on top (including the correct Python and Pytorch versions)?
  • Have you tried in other environments listed in the "Environments" section below?
  • Have you tried with another dataset like coco128 or coco2017? It will make it easier to find the root cause.
If you went through all the above, feel free to raise an Issue by giving as much detail as possible following the template.

Supported Environments

Ultralytics provides a range of ready-to-use environments, each pre-installed with essential dependencies such as CUDA, CUDNN, Python, and PyTorch, to kickstart your projects.

Project Status


This badge indicates that all YOLOv5 GitHub Actions Continuous Integration (CI) tests are successfully passing. These CI tests rigorously check the functionality and performance of YOLOv5 across various key aspects: training, validation, inference, export, and benchmarks. They ensure consistent and reliable operation on macOS, Windows, and Ubuntu, with tests conducted every 24 hours and upon each new commit.


We would like to thank @MagicFrogSJTU, who did all the heavy lifting, and @glenn-jocher for guiding us along the way.

Created 2023-11-12, Updated 2024-06-10
Authors: glenn-jocher (4), Burhan-Q (1)