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

Early stopper inconsistent devices fix #949

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 12 additions & 6 deletions torchtnt/utils/early_stop_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing_extensions import final, Literal

_log: logging.Logger = logging.getLogger(__name__)
_log.setLevel(logging.DEBUG)


@final
Expand Down Expand Up @@ -179,11 +180,13 @@ def check(self, val: Union[torch.Tensor, float, int]) -> bool:
divergence_threshold = divergence_threshold.to(val.device)
improvement_threshold = self.min_delta
if self._threshold_mode == "rel":
base_val = self._best_value if torch.isfinite(self._best_value) else 0.0
base_val = (
self._best_value.to(val.device)
if torch.isfinite(self._best_value)
else 0.0
)
improvement_threshold = self.min_delta.to(val.device) * base_val

improvement_threshold = improvement_threshold.to(val.device)

# Check finite
if self.check_finite and not torch.isfinite(val):
_log.debug(
Expand Down Expand Up @@ -212,7 +215,7 @@ def check(self, val: Union[torch.Tensor, float, int]) -> bool:

# Check if improvement is happening
if self._mode_func(
val - improvement_threshold, self._best_value.to(val.device)
val - improvement_threshold.to(val.device), self._best_value.to(val.device)
):
# Still improving
should_stop = False
Expand Down Expand Up @@ -259,9 +262,12 @@ def _improvement_message(self, val: torch.Tensor) -> str:
"""Formats a log message that informs the user about an improvement in the monitored score."""
if torch.isfinite(self._best_value):
improvement = (
torch.abs(self._best_value - val)
torch.abs(self._best_value.to(val.device) - val)
if self.threshold_mode == "abs"
else torch.abs((self._best_value - val) / (1.0 * self._best_value))
else torch.abs(
(self._best_value.to(val.device) - val)
/ (1.0 * self._best_value.to(val.device))
)
)
msg = (
f"Metric improved by {self.threshold_mode} {improvement} >="
Expand Down