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

PyTorchGradScaler: Cache _found_inf on the CPU #746

Merged
Merged
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
17 changes: 10 additions & 7 deletions thinc/shims/pytorch_grad_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ def __init__(
self._backoff_factor = backoff_factor
self._growth_interval = growth_interval

self._found_inf = torch.full((1,), 0.0)
self._growth_tracker = torch.full((1,), 0, dtype=torch.int)
self._scale = torch.full((1,), init_scale)
self._found_inf = False

def to_(self, device):
self._found_inf = self._found_inf.to(device)
self._growth_tracker = self._growth_tracker.to(device)
self._scale = self._scale.to(device)

Expand Down Expand Up @@ -132,7 +131,7 @@ def _tensors_per_device(self, tensors):

@property
def found_inf(self):
return bool(self._found_inf) != 0
return self._found_inf

def unscale(self, tensors):
"""Unscale the given tensors. Returns True if any of the gradients were infinite."""
Expand All @@ -152,9 +151,10 @@ def unscale(self, tensors):
device_tensors, found_inf_device, inv_scale_device
)

self._found_inf += found_inf_device.to(self._found_inf.device)
if bool(found_inf_device != 0):
self._found_inf = True

return bool(self._found_inf != 0)
return self._found_inf

def update(self):
"""
Expand All @@ -165,14 +165,17 @@ def update(self):
if not self._enabled:
return

found_inf_device = torch.full(
(1,), 1.0 if self._found_inf else 0.0, device=self._scale.device
)
torch._amp_update_scale_(
self._scale,
self._growth_tracker,
self._found_inf,
found_inf_device,
self._growth_factor,
self._backoff_factor,
self._growth_interval,
)

# Clear infinity found status
self._found_inf = torch.zeros_like(self._found_inf)
self._found_inf = False