Skip to content

Commit c05077f

Browse files
authored
Enable non-blocking for gpu device transfer (#1843)
* Update distrib_parts.py * Update CHANGELOG.md
1 parent bee0392 commit c05077f

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2828

2929
### Changed
3030

31+
- Enable `non-blocking` for device transfers to GPU ([#1843](https://github.com/PyTorchLightning/pytorch-lightning/pull/1843))
32+
3133
- Replace mata_tags.csv with hparams.yaml ([#1271](https://github.com/PyTorchLightning/pytorch-lightning/pull/1271))
3234

3335
- Reduction when `batch_size < num_gpus` ([#1609](https://github.com/PyTorchLightning/pytorch-lightning/pull/1609))

pytorch_lightning/trainer/distrib_parts.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,14 @@ def __transfer_data_to_device(self, batch, device, gpu_id=None):
449449
if device == 'gpu':
450450
# base case: object can be directly moved using `cuda` or `to`
451451
if callable(getattr(batch, 'cuda', None)):
452-
return batch.cuda(gpu_id)
452+
# non_blocking will be ignored if tensor is not pinned.
453+
# so we can always set it to True
454+
return batch.cuda(gpu_id, non_blocking=True)
453455

454456
if callable(getattr(batch, 'to', None)):
455-
return batch.to(torch.device('cuda', gpu_id))
457+
# non_blocking will be ignored if tensor is not pinned.
458+
# so we can always set it to True
459+
return batch.to(torch.device('cuda', gpu_id), non_blocking=True)
456460

457461
# when list
458462
if isinstance(batch, list):

0 commit comments

Comments
 (0)