Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid non-blocking GPU->CPU copies. #11288

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed data fetcher selection ([#11294](https://github.com/PyTorchLightning/pytorch-lightning/pull/11294))


- Fixed a race condition that could result in incorrect (zero) values being observed in prediction writer callbacks ([#11288](https://github.com/PyTorchLightning/pytorch-lightning/pull/11288))


## [1.5.7] - 2021-12-21

### Fixed
Expand Down
8 changes: 7 additions & 1 deletion pytorch_lightning/utilities/apply_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
Batch = type(None)


_CPU_DEVICES = ("cpu", torch.device("cpu"))


def to_dtype_tensor(
value: Union[int, float, List[Union[int, float]]], dtype: torch.dtype, device: Union[str, torch.device]
) -> torch.Tensor:
Expand Down Expand Up @@ -274,7 +277,10 @@ def batch_to(data: Any) -> Any:
setattr(device_data, field, device_field)
return device_data

kwargs = dict(non_blocking=True) if isinstance(data, torch.Tensor) else {}
kwargs = {}
# Don't issue non-blocking transfers to CPU
if isinstance(data, torch.Tensor) and device not in _CPU_DEVICES:
kwargs["non_blocking"] = True
data_output = data.to(device, **kwargs)
if data_output is not None:
return data_output
Expand Down