|
| 1 | +This rule applies to PyTorch data loading, where the use of pinned memory can significantly optimize data transfer between CPU and GPU. |
| 2 | + |
| 3 | +== Non Compliant Code Example |
| 4 | + |
| 5 | +[source,python] |
| 6 | +---- |
| 7 | +train_loader = torch.utils.data.DataLoader( |
| 8 | + dataset, |
| 9 | + batch_size=64, |
| 10 | + shuffle=True, |
| 11 | + pin_memory=False # Not using pinned memory |
| 12 | +) |
| 13 | +---- |
| 14 | + |
| 15 | +In this example, the DataLoader does not use pinned memory, which leads to slower host-to-device data transfers. |
| 16 | + |
| 17 | +== Compliant Solution |
| 18 | + |
| 19 | +[source,python] |
| 20 | +---- |
| 21 | +train_loader = torch.utils.data.DataLoader( |
| 22 | + dataset, |
| 23 | + batch_size=64, |
| 24 | + shuffle=True, |
| 25 | + pin_memory=True # Enables faster transfer to GPU |
| 26 | +) |
| 27 | +---- |
| 28 | + |
| 29 | +When `pin_memory=True`, PyTorch allocates page-locked memory on the host side, allowing for faster data transfer to the GPU via DMA (Direct Memory Access). |
| 30 | + |
| 31 | +== Relevance Analysis |
| 32 | + |
| 33 | +Experiments were conducted to evaluate the performance and environmental impact of using pinned memory in DataLoaders. |
| 34 | + |
| 35 | +=== Configuration |
| 36 | + |
| 37 | +* Processor: Intel(R) Xeon(R) CPU 3.80GHz |
| 38 | +* RAM: 64GB |
| 39 | +* GPU: NVIDIA Quadro RTX 6000 |
| 40 | +* CO₂ Emissions Measurement: https://mlco2.github.io/codecarbon/[CodeCarbon] |
| 41 | +* Framework: PyTorch |
| 42 | + |
| 43 | +=== Context |
| 44 | + |
| 45 | +Two training configurations were compared: |
| 46 | +- One using standard memory allocation (`pin_memory=False`) |
| 47 | +- One using pinned memory (`pin_memory=True`) |
| 48 | + |
| 49 | +Metrics assessed: |
| 50 | +- Average batch processing time |
| 51 | +- Total training time |
| 52 | +- CO₂ emissions |
| 53 | + |
| 54 | +=== Impact Analysis |
| 55 | + |
| 56 | +image:image.png[] |
| 57 | + |
| 58 | +image::results.png[] |
| 59 | + |
| 60 | +- **Batch Processing Time:** Reduced from 0.0472s to 0.0378s (~20% improvement). |
| 61 | +- **Training Time:** Decreased by 9.82% when using pinned memory. |
| 62 | +- **Carbon Emissions:** Lowered by 7.56%, indicating a measurable environmental benefit. |
| 63 | + |
| 64 | +The improvements observed are particularly significant in large-scale or long-running training tasks, where data transfer becomes a bottleneck. |
| 65 | + |
| 66 | +== Conclusion |
| 67 | + |
| 68 | +Enabling pinned memory in PyTorch DataLoaders: |
| 69 | +- Reduces batch processing time significantly |
| 70 | +- Slightly shortens total training duration |
| 71 | +- Contributes to lowering CO₂ emissions |
| 72 | +- Is a recommended best practice for GPU-accelerated training |
| 73 | + |
| 74 | +== References |
| 75 | +Credit : https://github.com/AghilesAzzoug/GreenPyData |
| 76 | + |
| 77 | +- https://pytorch.org/docs/stable/data.html |
| 78 | +- NVIDIA CUDA Documentation on Pinned Memory: https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#page-locked-host-memory |
0 commit comments