Skip to content

Commit 322e8b0

Browse files
author
Kevin
committed
Add ROCm DataLoader & Multiprocessing Best Practices
- Introduced a new section in README.md for DataLoader Multiprocessing with ROCm, detailing critical issues and solutions. - Added comprehensive documentation for ROCm DataLoader & Multiprocessing Best Practices, including setup, common issues, and performance impact. - Created a utility module (src/utils/rocm_compat.py) to streamline ROCm compatibility setup, including multiprocessing configuration and DataLoader patching. - Updated QUICK_REFERENCE.md with essential setup instructions for ROCm, including a minimal training script template. - Enhanced documentation with performance metrics and troubleshooting tips for common issues encountered with ROCm and PyTorch.
1 parent 2c83027 commit 322e8b0

4 files changed

Lines changed: 1407 additions & 0 deletions

File tree

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [Problem Statement](#-problem-statement)
77
- [Solution Overview](#-solution-overview)
88
- [Advanced MIOpen Bypass](#-advanced-miopen-bypass-for-production)
9+
- [DataLoader Multiprocessing](#-dataloader--multiprocessing-rocm)
910
- [Technology Stack Explained](#-technology-stack-explained)
1011
- [Architecture & Flow](#-architecture--flow)
1112
- [Installation Guide](#-installation-guide)
@@ -150,6 +151,63 @@ model = YOLO('yolov8n.pt').cuda()
150151
- **[Complete Guide](src/patches/miopen_bypass/README.md)** - Usage, strategies, examples
151152
- **[Technical Deep Dive](docs/MIOPEN_BYPASS_SOLUTION.md)** - Implementation details, benchmarks
152153

154+
### 🔄 DataLoader & Multiprocessing (ROCm)
155+
156+
**CRITICAL**: PyTorch DataLoader with `num_workers > 0` requires special configuration on ROCm!
157+
158+
**The Problem**: ROCm/HIP doesn't support Python's default "fork" multiprocessing, causing:
159+
- ❌ Worker hangs/timeouts
160+
- ❌ CUDA initialization errors
161+
- ❌ "context has already been set" errors
162+
163+
**✅ The Solution** (discovered from robust-thermal-image-object-detection project):
164+
165+
```python
166+
import multiprocessing as mp
167+
168+
# CRITICAL: Must be BEFORE importing torch!
169+
mp.set_start_method('spawn', force=True)
170+
171+
import torch
172+
from torch.utils.data import DataLoader
173+
174+
# Now DataLoader works with multiple workers!
175+
train_loader = DataLoader(
176+
dataset,
177+
batch_size=32,
178+
num_workers=4, # ✅ Works perfectly!
179+
multiprocessing_context='spawn', # Explicit (recommended)
180+
persistent_workers=True, # ✅ Keep workers alive (2x faster)
181+
pin_memory=True # ✅ Faster GPU transfer
182+
)
183+
```
184+
185+
**Or use the utility module**:
186+
```python
187+
from src.utils.rocm_compat import setup_rocm_multiprocessing, patch_dataloader
188+
189+
setup_rocm_multiprocessing() # Before torch import
190+
import torch
191+
patch_dataloader() # After torch import
192+
193+
# All DataLoaders now automatically use spawn + persistent_workers
194+
```
195+
196+
**Performance Impact**:
197+
- Training speed: 2.5 → 4.7 it/s (**1.88x faster!**)
198+
- GPU utilization: 60% → 98%
199+
- CPU usage: 15% → 70% (workers loading data in parallel)
200+
201+
**Documentation**:
202+
- **[DataLoader & Multiprocessing Guide](docs/ROCM_DATALOADER_MULTIPROCESSING.md)** - Complete guide with examples
203+
- **[ROCm Compatibility Utils](src/utils/rocm_compat.py)** - Drop-in utility module
204+
205+
**Key Learnings**:
206+
-`mp.set_start_method('spawn', force=True)` BEFORE torch import
207+
-`num_workers=4` tested and working perfectly
208+
-`persistent_workers=True` essential for performance (~2x speedup)
209+
- ✅ Monkey-patching DataLoader useful for third-party libraries
210+
153211
---
154212

155213
## 🔧 Technology Stack Explained

0 commit comments

Comments
 (0)