DistributedDataParallel
One process per rank, with a replicated model and coordinated gradient updates through PyTorch DDP.
An inspectable implementation of PyTorch DistributedDataParallel: learn how torchrun launches workers, how DistributedSampler partitions training examples, and how gradient synchronization supports causal Transformer training and Qwen2.5 LoRA experiments.
Code and notebook are public. Two-process CPU execution is reported as tested; GPU/Qwen configurations are example pathways, not published performance benchmarks.
01 / THE IMPLEMENTATION
Each capability below corresponds to the published Python implementation or README. Start with a two-rank CPU experiment, then study the CUDA-oriented configurations.
One process per rank, with a replicated model and coordinated gradient updates through PyTorch DDP.
Launch one or more workers using the torch distributed runner and environment-defined ranks.
DistributedSampler assigns partitions of the training dataset to workers, with epoch-aware shuffling.
DDP coordinates gradient communication during backward passes; the script also reduces local losses for logging.
A compact byte-level model uses positional embeddings, causal masking, self-attention, and next-token loss.
A separate path loads Qwen2.5-0.5B-Instruct and adds PEFT LoRA adapters to query and value projections.
Gloo is selected for CPU or Windows, while CUDA-enabled Linux can use NCCL when multiple ranks are launched.
Supply UTF-8 text samples, one per line, using --corpus-file to replace the built-in teaching corpus.
Rank zero saves a tiny-model checkpoint or the Qwen LoRA adapters, keeping per-rank file writes coordinated.
02 / SYSTEM ARCHITECTURE
The pipeline below follows the implementation's control flow. DDP is data parallelism: workers use different data, while each keeps the complete model.
Starts worker processes and provides rank metadata.
Initializes Gloo or NCCL as appropriate.
Assigns training examples to each rank.
Computes next-token loss on local batches.
Coordinates gradient reduction during backward.
Writes model checkpoint or adapters to disk.
Two 4 GB GPUs do not create an 8 GB memory pool for one model. For models that do not fit on one device, look into FSDP, tensor parallelism, or other forms of model sharding. The code here is an example of DDP, not those other strategies.
03 / TWO TRAINING PATHS
The same distributed launch concepts are exercised with a tiny model and a pretrained Qwen checkpoint.
Train a small byte-level autoregressive model with learned token and positional embeddings, one Transformer encoder block with causal masking, and a next-token training objective.
tiny_transformer.pt on rank zeroLoad Qwen/Qwen2.5-0.5B-Instruct and configure PEFT LoRA adapters for parameter-efficient next-token training on the provided text corpus.
q_proj and v_proj04 / HARDWARE & VALIDATION
These are repository-documented capabilities and test statements, not invented benchmark scores.
| Scenario | Backend | Hardware | Evidence |
|---|---|---|---|
| 1-rank tiny model | Local (no process group) | CPU | README reports completed 2-step test and checkpoint |
| 2-rank tiny model | Gloo | CPU | README reports completed 2-rank test and checkpoint |
| Qwen LoRA (1 rank) | Local CUDA process | One suitable GPU | Runnable code path; no published GPU execution result |
| Qwen LoRA (multiple ranks) | NCCL on Linux CUDA | At least two supported GPUs | Example launch path; physical multi-GPU results not reported |
05 / GETTING STARTED
Start on CPU to learn rank synchronization. Move to pretrained-model LoRA mode only after checking available CUDA devices and memory.
git clone https://github.com/su394611-del/Distributed-Data-Parallel-Processing.git
cd Distributed-Data-Parallel-Processing
py -3.12 -m venv .venv
.\.venv\Scripts\python.exe -m pip install --upgrade pip
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
.\.venv\Scripts\python.exe -m pip check
# Check whether this environment can access CUDA
.\.venv\Scripts\python.exe -c "import torch; print(torch.cuda.is_available()); print(torch.cuda.device_count())".\.venv\Scripts\python.exe -m torch.distributed.run --standalone --nproc_per_node=2 llm_ddp_parallel_training.py --model tiny --cpu --batch-size 2 --max-steps 2.\.venv\Scripts\python.exe llm_ddp_parallel_training.py --model tiny --cpu --max-steps 2python -m torch.distributed.run --standalone --nproc_per_node=1 llm_ddp_parallel_training.py --model qwen --batch-size 1 --seq-len 64 --max-steps 2python -m torch.distributed.run --standalone --nproc_per_node=2 llm_ddp_parallel_training.py --model qwen --batch-size 1 --seq-len 64 --max-steps 206 / DOCUMENTATION & SOURCE
Follow the implementation directly from this page. Each link points to the current repository name and file paths.
07 / FAQ
No. DDP gives each rank a complete model replica and divides the dataset. Tensor parallelism partitions parts of the model across devices. DDP does not pool VRAM.
Yes. The script and README include a two-process CPU command using the Gloo communication backend. It is suitable for learning the process-group and gradient synchronization mechanics.
No. The Qwen path downloads a pretrained Qwen2.5-0.5B-Instruct checkpoint and applies parameter-efficient LoRA updates. The tiny causal Transformer is the model constructed from scratch.
No. The repository explicitly identifies the Qwen training path as LoRA, without 4-bit QLoRA quantization.
For this example, global batch size equals the per-rank batch size multiplied by the number of ranks. The implementation does not include gradient accumulation.