Bird
Raised Fist0
PyTorchml~8 mins

Multi-GPU training in PyTorch - Model Metrics & Evaluation

Choose your learning style10 modes available
Metrics & Evaluation - Multi-GPU training
Which metric matters for Multi-GPU training and WHY

When training on multiple GPUs, the main goal is to speed up training without losing model quality. So, the key metrics are training time per epoch and model accuracy or loss. We want to see if the model learns just as well (accuracy/loss) but faster (time). If accuracy drops, the speed gain is not useful. If training time does not improve, multi-GPU is not effective.

Confusion matrix or equivalent visualization

Multi-GPU training itself does not change the confusion matrix of the model. The confusion matrix shows how well the model predicts classes:

      | Predicted Positive | Predicted Negative |
      |--------------------|--------------------|
      | True Positive (TP)  | False Negative (FN) |
      | False Positive (FP) | True Negative (TN)  |
    

We compare confusion matrices from single-GPU and multi-GPU training to check if predictions stay consistent.

Precision vs Recall tradeoff with concrete examples

Multi-GPU training aims to keep model quality the same while speeding up training. If the model trained on multiple GPUs has lower precision or recall, it means the training process might have issues like poor synchronization or batch size effects.

Example: If a cancer detector model trained on one GPU has 90% recall, but on multiple GPUs recall drops to 80%, it means the multi-GPU setup hurt the model's ability to find cancer cases. This is bad even if training is faster.

What "good" vs "bad" metric values look like for Multi-GPU training

Good:

  • Training time per epoch decreases significantly (e.g., from 60 minutes to 20 minutes).
  • Model accuracy and loss remain similar or improve slightly.
  • Precision and recall stay stable compared to single-GPU training.

Bad:

  • Training time does not improve or improves very little.
  • Model accuracy drops noticeably (e.g., from 85% to 75%).
  • Precision or recall decrease, indicating worse predictions.
  • Training becomes unstable or loss fluctuates wildly.
Metrics pitfalls in Multi-GPU training
  • Data leakage: If data is not properly split or shuffled across GPUs, the model may see the same data multiple times, inflating accuracy.
  • Batch size effects: Multi-GPU training often increases batch size, which can affect learning dynamics and model quality.
  • Overfitting: Faster training might cause the model to overfit if early stopping or validation checks are not used.
  • Synchronization issues: Improper gradient synchronization can cause model divergence and poor metrics.
  • Accuracy paradox: High accuracy with imbalanced data can be misleading; always check precision and recall.
Self-check question

Your multi-GPU model trains 3 times faster than single-GPU but has 98% accuracy and only 12% recall on fraud detection. Is it good for production? Why or why not?

Answer: No, it is not good. Even though accuracy is high, recall is very low. This means the model misses most fraud cases, which is dangerous. For fraud detection, high recall is critical to catch as many frauds as possible. Speed alone does not make the model useful.

Key Result
Multi-GPU training should reduce training time significantly while keeping accuracy, precision, and recall stable to ensure model quality.

Practice

(1/5)
1. What is the main purpose of using torch.nn.DataParallel in PyTorch?
easy
A. To reduce the model size by pruning layers
B. To convert the model to a CPU-only version
C. To split the model and data across multiple GPUs for faster training
D. To visualize the model architecture

Solution

  1. Step 1: Understand the role of DataParallel

    torch.nn.DataParallel is designed to distribute the model and input data across multiple GPUs to speed up training.
  2. Step 2: Compare options

    Reducing the model size by pruning layers, converting the model to a CPU-only version, and visualizing the model architecture do not relate to multi-GPU training. Only splitting the model and data across multiple GPUs for faster training correctly describes the purpose.
  3. Final Answer:

    To split the model and data across multiple GPUs for faster training -> Option C
  4. Quick Check:

    Multi-GPU training = DataParallel usage [OK]
Hint: DataParallel splits model and data across GPUs for speed [OK]
Common Mistakes:
  • Thinking DataParallel reduces model size
  • Confusing DataParallel with CPU conversion
  • Assuming DataParallel is for visualization
2. Which of the following is the correct way to wrap a PyTorch model for multi-GPU training?
easy
A. model = torch.nn.DataParallel(model)
B. model = torch.nn.Sequential(model)
C. model = torch.nn.ModuleList(model)
D. model = torch.nn.Parameter(model)

Solution

  1. Step 1: Identify the correct wrapper for multi-GPU

    To enable multi-GPU training, PyTorch provides torch.nn.DataParallel which wraps the model.
  2. Step 2: Check other options

    Sequential chains layers, ModuleList stores layers, and Parameter is for tensors, none enable multi-GPU.
  3. Final Answer:

    model = torch.nn.DataParallel(model) -> Option A
  4. Quick Check:

    Wrap model with DataParallel for multi-GPU [OK]
Hint: Use DataParallel(model) to enable multi-GPU training [OK]
Common Mistakes:
  • Using Sequential or ModuleList instead of DataParallel
  • Trying to wrap model with Parameter
  • Forgetting to wrap the model at all
3. Consider this code snippet for multi-GPU training:
import torch
model = MyModel()
model = torch.nn.DataParallel(model)
model.cuda()
inputs = inputs.cuda()
outputs = model(inputs)
print(outputs.device)
What will be printed if multiple GPUs are available?
medium
A. cuda:0
B. cpu
C. cuda:1
D. cuda:all

Solution

  1. Step 1: Understand DataParallel device behavior

    When using DataParallel, the model replicates on all GPUs but the output is gathered on the default device, usually cuda:0.
  2. Step 2: Analyze the print statement

    The outputs.device will show the device of the gathered output tensor, which is cuda:0, not all GPUs or CPU.
  3. Final Answer:

    cuda:0 -> Option A
  4. Quick Check:

    DataParallel output device = cuda:0 [OK]
Hint: DataParallel gathers output on cuda:0 device [OK]
Common Mistakes:
  • Expecting output on all GPUs simultaneously
  • Thinking output is on CPU
  • Assuming output device is cuda:1 or others
4. You wrote this code for multi-GPU training but get an error:
model = MyModel()
model = torch.nn.DataParallel(model)
inputs = inputs.cuda()
outputs = model(inputs)
The error says: 'Expected all tensors to be on the same device'. What is the likely cause?
medium
A. Model should be wrapped with Sequential instead
B. Model is not moved to GPU before wrapping with DataParallel
C. DataParallel requires inputs on CPU
D. Inputs are not on CPU

Solution

  1. Step 1: Check model and input device placement

    The model must be moved to GPU (e.g., model.cuda()) before wrapping with DataParallel to ensure all parts are on the same device.
  2. Step 2: Analyze error cause

    If the model is on CPU but inputs are on GPU, the error about device mismatch occurs.
  3. Final Answer:

    Model is not moved to GPU before wrapping with DataParallel -> Option B
  4. Quick Check:

    Model and inputs must be on same device [OK]
Hint: Move model to GPU before DataParallel wrap [OK]
Common Mistakes:
  • Wrapping model before moving to GPU
  • Assuming inputs must be on CPU
  • Using wrong wrapper like Sequential
5. You want to train a model on 4 GPUs using PyTorch. Which of these steps is NOT necessary for correct multi-GPU training?
hard
A. Move input data to GPU before passing to the model
B. Move the model to GPU before wrapping
C. Wrap the model with torch.nn.DataParallel
D. Manually split input batches and send each to a different GPU

Solution

  1. Step 1: Review multi-GPU training steps

    Using DataParallel automatically splits input batches and sends them to multiple GPUs, so manual splitting is not needed.
  2. Step 2: Confirm necessary steps

    Wrapping the model, moving it to GPU, and moving inputs to GPU are all required for proper multi-GPU training.
  3. Final Answer:

    Manually split input batches and send each to a different GPU -> Option D
  4. Quick Check:

    DataParallel handles input splitting automatically [OK]
Hint: DataParallel auto-splits inputs; manual split not needed [OK]
Common Mistakes:
  • Trying to manually split inputs across GPUs
  • Forgetting to move model to GPU
  • Not wrapping model with DataParallel
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard