Skip to content

Commit a0a2f0b

Browse files
Balandatfacebook-github-bot
authored andcommitted
Suppress warnings during testing (#191)
Summary: Suppresses a number of OptimizationWarnings emitted during testing caused by having very small limits on the number of optimization iterations (for unit testing performance reasons). Pull Request resolved: #191 Reviewed By: danielrjiang Differential Revision: D16025104 Pulled By: Balandat fbshipit-source-id: f1cf19d5d4c0d691126cbe1d60e888f4913f34a2
1 parent 72a476b commit a0a2f0b

File tree

9 files changed

+83
-42
lines changed

9 files changed

+83
-42
lines changed

botorch/models/converter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ def _check_compatibility(models: ModelListGP) -> None:
6161
"Conversion of HeteroskedasticSingleTaskGP is currently unsupported."
6262
)
6363

64-
# if the list has only one model, we can just return a copy of that
65-
if len(models) == 1:
66-
return deepcopy(models[0])
67-
6864
# check that each model is single-output
6965
if not all(m._num_outputs == 1 for m in models):
7066
raise UnsupportedError("All models must be single-output.")
@@ -96,6 +92,10 @@ def model_list_to_batched(model_list: ModelListGP) -> BatchedMultiOutputGPyTorch
9692
models = model_list.models
9793
_check_compatibility(models)
9894

95+
# if the list has only one model, we can just return a copy of that
96+
if len(models) == 1:
97+
return deepcopy(models[0])
98+
9999
# construct inputs
100100
train_X = deepcopy(models[0].train_inputs[0])
101101
train_Y = torch.stack([m.train_targets.clone() for m in models], dim=-1)

test/models/test_gp_regression.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
import math
66
import unittest
7+
import warnings
78

89
import torch
910
from botorch import fit_gpytorch_model
11+
from botorch.exceptions.warnings import OptimizationWarning
1012
from botorch.models.gp_regression import (
1113
FixedNoiseGP,
1214
HeteroskedasticSingleTaskGP,
@@ -63,7 +65,9 @@ def test_gp(self, cuda=False):
6365
mll = ExactMarginalLogLikelihood(model.likelihood, model).to(
6466
**tkwargs
6567
)
66-
fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
68+
with warnings.catch_warnings():
69+
warnings.filterwarnings("ignore", category=OptimizationWarning)
70+
fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
6771

6872
# test init
6973
self.assertIsInstance(model.mean_module, ConstantMean)

test/models/test_model_list_gp_regression.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
import math
66
import unittest
7+
import warnings
78

89
import torch
9-
from botorch import fit_gpytorch_model
10+
from botorch.exceptions.warnings import OptimizationWarning
11+
from botorch.fit import fit_gpytorch_model
1012
from botorch.models import ModelListGP
1113
from botorch.models.gp_regression import FixedNoiseGP, SingleTaskGP
1214
from botorch.posteriors import GPyTorchPosterior
@@ -70,11 +72,15 @@ def test_ModelListGP(self, cuda=False):
7072
self.assertIsInstance(mll_, ExactMarginalLogLikelihood)
7173

7274
# test model fitting (sequential)
73-
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
74-
# test model fitting (joint)
75-
mll = fit_gpytorch_model(
76-
mll, options={"maxiter": 1}, max_retries=1, sequential=False
77-
)
75+
with warnings.catch_warnings():
76+
warnings.filterwarnings("ignore", category=OptimizationWarning)
77+
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
78+
with warnings.catch_warnings():
79+
warnings.filterwarnings("ignore", category=OptimizationWarning)
80+
# test model fitting (joint)
81+
mll = fit_gpytorch_model(
82+
mll, options={"maxiter": 1}, max_retries=1, sequential=False
83+
)
7884

7985
# test posterior
8086
test_x = torch.tensor([[0.25], [0.75]], **tkwargs)
@@ -140,7 +146,9 @@ def test_ModelListGP_fixed_noise(self, cuda=False):
140146
mll = SumMarginalLogLikelihood(model.likelihood, model)
141147
for mll_ in mll.mlls:
142148
self.assertIsInstance(mll_, ExactMarginalLogLikelihood)
143-
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
149+
with warnings.catch_warnings():
150+
warnings.filterwarnings("ignore", category=OptimizationWarning)
151+
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
144152

145153
# test posterior
146154
test_x = torch.tensor([[0.25], [0.75]], **tkwargs)

test/models/test_multitask.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
import math
66
import unittest
7+
import warnings
78

89
import torch
9-
from botorch import fit_gpytorch_model
10+
from botorch.exceptions.warnings import OptimizationWarning
11+
from botorch.fit import fit_gpytorch_model
1012
from botorch.models.multitask import FixedNoiseMultiTaskGP, MultiTaskGP
1113
from botorch.posteriors import GPyTorchPosterior
1214
from gpytorch.distributions import MultitaskMultivariateNormal, MultivariateNormal
@@ -82,7 +84,9 @@ def test_MultiTaskGP(self, cuda=False):
8284

8385
# test model fitting
8486
mll = ExactMarginalLogLikelihood(model.likelihood, model)
85-
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
87+
with warnings.catch_warnings():
88+
warnings.filterwarnings("ignore", category=OptimizationWarning)
89+
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
8690

8791
# test posterior
8892
test_x = torch.rand(2, 1, **tkwargs)
@@ -155,7 +159,9 @@ def test_MultiTaskGP_single_output(self, cuda=False):
155159

156160
# test model fitting
157161
mll = ExactMarginalLogLikelihood(model.likelihood, model)
158-
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
162+
with warnings.catch_warnings():
163+
warnings.filterwarnings("ignore", category=OptimizationWarning)
164+
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
159165

160166
# test posterior
161167
test_x = torch.rand(2, 1, **tkwargs)
@@ -197,7 +203,9 @@ def test_FixedNoiseMultiTaskGP(self, cuda=False):
197203

198204
# test model fitting
199205
mll = ExactMarginalLogLikelihood(model.likelihood, model)
200-
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
206+
with warnings.catch_warnings():
207+
warnings.filterwarnings("ignore", category=OptimizationWarning)
208+
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
201209

202210
# test posterior
203211
test_x = torch.rand(2, 1, **tkwargs)
@@ -268,7 +276,9 @@ def test_FixedNoiseMultiTaskGP_single_output(self, cuda=False):
268276

269277
# test model fitting
270278
mll = ExactMarginalLogLikelihood(model.likelihood, model)
271-
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
279+
with warnings.catch_warnings():
280+
warnings.filterwarnings("ignore", category=OptimizationWarning)
281+
mll = fit_gpytorch_model(mll, options={"maxiter": 1}, max_retries=1)
272282

273283
# test posterior
274284
test_x = torch.rand(2, 1, **tkwargs)

test/optim/test_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
44

55
import unittest
6+
import warnings
67
from copy import deepcopy
78

89
import torch
@@ -229,7 +230,10 @@ def test_sample_all_priors(self, cuda=False):
229230
outputscale_prior=GammaPrior(2.0, 0.15),
230231
)
231232
original_state_dict = dict(deepcopy(mll.model.state_dict()))
232-
sample_all_priors(model)
233+
with warnings.catch_warnings(record=True) as ws:
234+
sample_all_priors(model)
235+
self.assertEqual(len(ws), 1)
236+
self.assertTrue("rsample" in str(ws[0].message))
233237

234238
# the lengthscale should not have changed because sampling is
235239
# not implemented for SmoothedBoxPrior

test/test_cross_validation.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
import math
66
import unittest
7+
import warnings
78

89
import torch
910
from botorch.cross_validation import batch_cross_validation, gen_loo_cv_folds
11+
from botorch.exceptions.warnings import OptimizationWarning
1012
from botorch.models.gp_regression import FixedNoiseGP, SingleTaskGP
1113
from gpytorch.mlls.exact_marginal_log_likelihood import ExactMarginalLogLikelihood
1214

@@ -42,12 +44,14 @@ def test_single_task_batch_cv(self, cuda=False):
4244
train_X=train_X, train_Y=train_Y
4345
)
4446
# Test SingleTaskGP
45-
cv_results = batch_cross_validation(
46-
model_cls=SingleTaskGP,
47-
mll_cls=ExactMarginalLogLikelihood,
48-
cv_folds=noiseless_cv_folds,
49-
fit_args={"options": {"maxiter": 1}},
50-
)
47+
with warnings.catch_warnings():
48+
warnings.filterwarnings("ignore", category=OptimizationWarning)
49+
cv_results = batch_cross_validation(
50+
model_cls=SingleTaskGP,
51+
mll_cls=ExactMarginalLogLikelihood,
52+
cv_folds=noiseless_cv_folds,
53+
fit_args={"options": {"maxiter": 1}},
54+
)
5155
expected_shape = batch_shape + torch.Size([n, 1, num_outputs])
5256
self.assertEqual(cv_results.posterior.mean.shape, expected_shape)
5357
self.assertEqual(cv_results.observed_Y.shape, expected_shape)
@@ -56,12 +60,14 @@ def test_single_task_batch_cv(self, cuda=False):
5660
noisy_cv_folds = gen_loo_cv_folds(
5761
train_X=train_X, train_Y=train_Y, train_Yvar=train_Yvar
5862
)
59-
cv_results = batch_cross_validation(
60-
model_cls=FixedNoiseGP,
61-
mll_cls=ExactMarginalLogLikelihood,
62-
cv_folds=noisy_cv_folds,
63-
fit_args={"options": {"maxiter": 1}},
64-
)
63+
with warnings.catch_warnings():
64+
warnings.filterwarnings("ignore", category=OptimizationWarning)
65+
cv_results = batch_cross_validation(
66+
model_cls=FixedNoiseGP,
67+
mll_cls=ExactMarginalLogLikelihood,
68+
cv_folds=noisy_cv_folds,
69+
fit_args={"options": {"maxiter": 1}},
70+
)
6571
self.assertEqual(cv_results.posterior.mean.shape, expected_shape)
6672
self.assertEqual(cv_results.observed_Y.shape, expected_shape)
6773
self.assertEqual(cv_results.observed_Y.shape, expected_shape)

test/test_end_to_end.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
import math
66
import unittest
7+
import warnings
78

89
import torch
910
from botorch.acquisition import ExpectedImprovement, qExpectedImprovement
11+
from botorch.exceptions.warnings import OptimizationWarning
1012
from botorch.fit import fit_gpytorch_model
1113
from botorch.models import FixedNoiseGP, SingleTaskGP
1214
from botorch.optim import joint_optimize
@@ -35,19 +37,23 @@ def _setUp(self, double=False, cuda=False):
3537
self.mll_st = ExactMarginalLogLikelihood(
3638
self.model_st.likelihood, self.model_st
3739
)
38-
self.mll_st = fit_gpytorch_model(
39-
self.mll_st, options={"maxiter": 5}, max_retries=1
40-
)
40+
with warnings.catch_warnings():
41+
warnings.filterwarnings("ignore", category=OptimizationWarning)
42+
self.mll_st = fit_gpytorch_model(
43+
self.mll_st, options={"maxiter": 5}, max_retries=1
44+
)
4145
model_fn = FixedNoiseGP(
4246
self.train_x, self.train_y, self.train_yvar.expand_as(self.train_y)
4347
)
4448
self.model_fn = model_fn.to(device=device, dtype=dtype)
4549
self.mll_fn = ExactMarginalLogLikelihood(
4650
self.model_fn.likelihood, self.model_fn
4751
)
48-
self.mll_fn = fit_gpytorch_model(
49-
self.mll_fn, options={"maxiter": 5}, max_retries=1
50-
)
52+
with warnings.catch_warnings():
53+
warnings.filterwarnings("ignore", category=OptimizationWarning)
54+
self.mll_fn = fit_gpytorch_model(
55+
self.mll_fn, options={"maxiter": 5}, max_retries=1
56+
)
5157

5258
def test_qEI(self, cuda=False):
5359
for double in (True, False):

test/test_fit.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,8 @@ def test_fit_gpytorch_model_singular(self, cuda=False):
150150
gp = SingleTaskGP(X_train, Y_train, likelihood=test_likelihood)
151151
mll = ExactMarginalLogLikelihood(gp.likelihood, gp)
152152
mll.to(device=device, dtype=dtype)
153-
with warnings.catch_warnings(record=True) as ws:
154-
# this will do multiple retries
155-
fit_gpytorch_model(mll, options=options, max_retries=1)
156-
self.assertEqual(len(ws), 1)
157-
self.assertTrue(MAX_RETRY_MSG in str(ws[0].message))
153+
# this will do multiple retries (and emit warnings, which is desired)
154+
fit_gpytorch_model(mll, options=options, max_retries=2)
158155

159156
def test_fit_gpytorch_model_singular_cuda(self):
160157
if torch.cuda.is_available():

test/test_gen.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
import math
66
import unittest
7+
import warnings
78

89
import torch
910
from botorch.acquisition import qExpectedImprovement
11+
from botorch.exceptions.warnings import OptimizationWarning
1012
from botorch.fit import fit_gpytorch_model
1113
from botorch.gen import gen_candidates_scipy, gen_candidates_torch
1214
from botorch.models import SingleTaskGP
@@ -37,7 +39,11 @@ def _setUp(self, double=False, cuda=False, expand=False):
3739
model = SingleTaskGP(self.train_x, self.train_y)
3840
self.model = model.to(device=device, dtype=dtype)
3941
self.mll = ExactMarginalLogLikelihood(self.model.likelihood, self.model)
40-
self.mll = fit_gpytorch_model(self.mll, options={"maxiter": 1}, max_retries=1)
42+
with warnings.catch_warnings():
43+
warnings.filterwarnings("ignore", category=OptimizationWarning)
44+
self.mll = fit_gpytorch_model(
45+
self.mll, options={"maxiter": 1}, max_retries=1
46+
)
4147

4248

4349
class TestGenCandidates(TestBaseCandidateGeneration):

0 commit comments

Comments
 (0)