When using DistributedDataParallel (DDP), the main goal is to train a model faster by splitting data across multiple devices. The key metrics to watch are training loss and validation accuracy. These show if the model learns well and generalizes correctly. Also, speedup and scalability metrics matter to check if adding more devices really helps training time without hurting model quality.
DistributedDataParallel in PyTorch - Model Metrics & Evaluation
DistributedDataParallel itself does not change the confusion matrix of the model predictions. However, here is an example confusion matrix from a classification model trained with DDP:
| Predicted Positive | Predicted Negative |
|--------------------|--------------------|
| True Positive (TP) = 90 | False Negative (FN) = 10 |
| False Positive (FP) = 5 | True Negative (TN) = 95 |
Totals: TP + FP + TN + FN = 90 + 5 + 95 + 10 = 200 samples.
Precision = 90 / (90 + 5) = 0.947
Recall = 90 / (90 + 10) = 0.9
F1 Score = 2 * (0.947 * 0.9) / (0.947 + 0.9) ≈ 0.923
In DDP training, the model's precision and recall depend on the data and model, not DDP itself. But DDP helps train faster and on more data, which can improve both.
Example 1: Spam filter - High precision is important to avoid marking good emails as spam. DDP can help train a better model faster.
Example 2: Medical diagnosis - High recall is critical to catch all disease cases. DDP allows training on large datasets to improve recall.
DDP helps by enabling faster training on bigger data, which can improve the balance between precision and recall.
Good metrics after training with DDP:
- Training loss steadily decreases and matches single-device training loss.
- Validation accuracy is similar or better than single-device training.
- Speedup close to the number of devices used (e.g., 4 GPUs -> ~4x faster).
- Consistent precision and recall values without degradation.
Bad metrics:
- Training loss does not decrease or is unstable compared to single-device.
- Validation accuracy drops significantly, indicating poor model quality.
- Speedup is very low, showing overhead or bottlenecks.
- Precision or recall drops, possibly due to synchronization issues.
- Data leakage: If data is not properly split across devices, the model may see the same samples multiple times, inflating accuracy.
- Overfitting: Faster training can cause overfitting if early stopping or validation checks are ignored.
- Synchronization issues: Incorrect gradient averaging can cause training instability and poor metrics.
- Accuracy paradox: High accuracy may hide poor recall or precision on important classes.
- Unequal batch sizes: If batches differ across devices, metrics may be skewed.
Your model trained with DistributedDataParallel has 98% accuracy but only 12% recall on the fraud class. Is it good for production? Why or why not?
Answer: No, it is not good. Although accuracy is high, the very low recall means the model misses most fraud cases. For fraud detection, recall is critical because missing fraud is costly. The model needs improvement to catch more fraud cases before production use.
Practice
DistributedDataParallel in PyTorch?Solution
Step 1: Understand the role of DistributedDataParallel
DistributedDataParallel (DDP) is designed to run training across multiple GPUs or machines to speed up the process.Step 2: Compare options with DDP's purpose
Options B, C, and D do not relate to parallel training or speed-up. Only To speed up training by using multiple GPUs or machines simultaneously correctly describes DDP's main purpose.Final Answer:
To speed up training by using multiple GPUs or machines simultaneously -> Option DQuick Check:
DDP speeds training = A [OK]
- Thinking DDP reduces model size
- Confusing DDP with CPU-only training
- Assuming DDP tunes hyperparameters automatically
model is your neural network and device is the GPU device?Solution
Step 1: Recall the correct syntax for DistributedDataParallel
The correct class istorch.nn.parallel.DistributedDataParalleland it takesdevice_idsas a list of devices.Step 2: Check each option for syntax correctness
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[device]) uses the correct class and parameterdevice_ids=[device]. model = torch.nn.DataParallel(model, device_ids=[device]) uses DataParallel, which is different. Options C and D use incorrect parameter names or class names.Final Answer:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[device]) -> Option BQuick Check:
Correct class and device_ids list = A [OK]
- Using DataParallel instead of DistributedDataParallel
- Passing device instead of device_ids list
- Wrong class or parameter names
import torch
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
def setup(rank, world_size):
dist.init_process_group('gloo', rank=rank, world_size=world_size)
model = torch.nn.Linear(10, 1).to(rank)
model = DDP(model, device_ids=[rank])
print(model.module.weight.device)
What will be the output device of model.module.weight.device if rank=1 and GPUs are available?Solution
Step 1: Understand device assignment in the code
The model is moved torankdevice with.to(rank), so for rank=1, it is oncuda:1.Step 2: Check DistributedDataParallel wrapping
DDP wraps the model and usesdevice_ids=[rank], so the model stays oncuda:1.Final Answer:
cuda:1 -> Option AQuick Check:
Model device matches rank GPU = B [OK]
- Assuming model stays on CPU
- Confusing device index with rank variable
- Thinking device is always cuda:0
RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. What is the most likely cause?Solution
Step 1: Understand the error about unfinished reduction
This error usually means gradients were not synchronized properly before the next iteration.Step 2: Identify missing optimizer step
Not callingoptimizer.step()afterloss.backward()can cause gradients to not be updated and reduction to remain unfinished.Final Answer:
You forgot to call optimizer.step() after loss.backward() -> Option AQuick Check:
Missing optimizer.step() causes reduction error = C [OK]
- Skipping optimizer.step() call
- Ignoring proper model wrapping
- Confusing backend errors with sync errors
Solution
Step 1: Understand multi-machine multi-GPU DDP setup
Each GPU should have one process. For 4 GPUs across 2 machines, launch 4 processes total.Step 2: Confirm process group and model wrapping
Initialize the process group with correct rank and world_size=4. Wrap the model with DDP on each process using the assigned GPU.Final Answer:
Launch 4 processes total, each process uses one GPU, initialize process group with correct rank and world_size, wrap model with DDP on each process -> Option CQuick Check:
One process per GPU with correct init = D [OK]
- Using fewer processes than GPUs
- Using DataParallel instead of DDP for multi-machine
- Ignoring second machine in setup
Ready to prove it?
Pass the test and this pattern moves unknown
