Skip to content

Commit 425b1db

Browse files
esantorellafacebook-github-bot
authored andcommitted
Explicitly check timeout against None so that 0.0 isn't ignored (#2348)
Summary: Pull Request resolved: #2348 While testing some timeout-related code, I set a timeout to 0.0 to ensure that the timeout would bind. I was confused when nothing happened; it turned out that the presence of a timeout was tested for with "if timeout" rather than "if timeout is None," so that 0.0 was not considered a timeout. Reviewed By: Balandat, SebastianAment Differential Revision: D57542718 fbshipit-source-id: 9de3bbb20e9c21ada28d6357c7ef64dcbf829f3f
1 parent 4e1cb12 commit 425b1db

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

botorch/optim/utils/timeout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
def minimize_with_timeout(
19-
fun: Callable[[np.ndarray, *Any], float],
19+
fun: Callable[[np.ndarray, ...], float],
2020
x0: np.ndarray,
2121
args: Tuple[Any, ...] = (),
2222
method: Optional[str] = None,
@@ -40,7 +40,7 @@ def minimize_with_timeout(
4040
method that is injected to the scipy.optimize.minimize call and that keeps
4141
track of the runtime and the optimization variables at the current iteration.
4242
"""
43-
if timeout_sec:
43+
if timeout_sec is not None:
4444

4545
start_time = time.monotonic()
4646
callback_data = {"num_iterations": 0} # update from withing callback below

test/optim/utils/test_timeout.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ def f_and_g(x: np.ndarray, sleep_sec: float = 0.0):
4141
self.assertEqual(res.nit, 2) # quadratic approx. is exact
4242

4343
with self.subTest("test w/ binding timeout"):
44-
res = minimize_with_timeout(**base_kwargs, args=(1e-2,), timeout_sec=1e-4)
45-
self.assertFalse(res.success)
46-
self.assertEqual(res.nit, 1) # only one call to the callback is made
44+
for timeout_sec in [0, 1e-4]:
45+
res = minimize_with_timeout(
46+
**base_kwargs, args=(1e-2,), timeout_sec=timeout_sec
47+
)
48+
self.assertFalse(res.success)
49+
self.assertEqual(res.nit, 1) # only one call to the callback is made
50+
self.assertIn("Optimization timed out", res.message)
4751

4852
# set up callback with mutable object to verify callback execution
4953
check_set = set()

0 commit comments

Comments
 (0)