Skip to content

Commit 53d8f88

Browse files
saitcakmakfacebook-github-bot
authored andcommitted
Deprecate gp_sampling module in favor of pathwise sampling (#2432)
Summary: Pull Request resolved: #2432 GP sampling functionality has been superseded by pathwise sampling, which has been in BoTorch for quite some time now. Marking it for deprecation in v0.13. All existing usage has been replaced with `get_matheron_path_model`. Reviewed By: esantorella Differential Revision: D59815658 fbshipit-source-id: 57a0e13b76ff5b8d848b4f10e23e5d691475666a
1 parent 2771266 commit 53d8f88

File tree

5 files changed

+41
-26
lines changed

5 files changed

+41
-26
lines changed

botorch/acquisition/multi_objective/utils.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from botorch.models.fully_bayesian import MCMC_DIM
2828
from botorch.models.model import Model
2929
from botorch.sampling.get_sampler import get_sampler
30-
from botorch.utils.gp_sampling import get_gp_samples
30+
from botorch.sampling.pathwise.posterior_samplers import get_matheron_path_model
3131
from botorch.utils.multi_objective.box_decompositions.box_decomposition import (
3232
BoxDecomposition,
3333
)
@@ -320,7 +320,6 @@ def sample_optimal_points(
320320
optimizer: Callable[
321321
[GenericDeterministicModel, Tensor, int, bool, Any], Tuple[Tensor, Tensor]
322322
] = random_search_optimizer,
323-
num_rff_features: int = 512,
324323
maximize: bool = True,
325324
optimizer_kwargs: Optional[Dict[str, Any]] = None,
326325
) -> Tuple[Tensor, Tensor]:
@@ -344,7 +343,6 @@ def sample_optimal_points(
344343
num_samples: The number of GP samples.
345344
num_points: The number of optimal points to be outputted.
346345
optimizer: A callable that solves the deterministic optimization problem.
347-
num_rff_features: The number of random Fourier features.
348346
maximize: If true, we consider a maximization problem.
349347
optimizer_kwargs: The additional arguments for the optimizer.
350348
@@ -356,7 +354,7 @@ def sample_optimal_points(
356354
- A `num_samples x num_points x M`-dim Tensor containing the collection of
357355
optimal objectives.
358356
"""
359-
tkwargs = {"dtype": bounds.dtype, "device": bounds.device}
357+
tkwargs: Dict[str, Any] = {"dtype": bounds.dtype, "device": bounds.device}
360358
M = model.num_outputs
361359
d = bounds.shape[-1]
362360
if M == 1:
@@ -369,9 +367,7 @@ def sample_optimal_points(
369367
pareto_sets = torch.zeros((num_samples, num_points, d), **tkwargs)
370368
pareto_fronts = torch.zeros((num_samples, num_points, M), **tkwargs)
371369
for i in range(num_samples):
372-
sample_i = get_gp_samples(
373-
model=model, num_outputs=M, n_samples=1, num_rff_features=num_rff_features
374-
)
370+
sample_i = get_matheron_path_model(model=model)
375371
ps_i, pf_i = optimizer(
376372
model=sample_i,
377373
bounds=bounds,

botorch/models/deterministic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
observation. `GenericDeterministicModel` supports arbitrary deterministic
2020
functions, while `AffineFidelityCostModel` is a particular cost model for
2121
multi-fidelity optimization. Other use cases of deterministic models include
22-
representing approximate GP sample paths, e.g. random Fourier features obtained
23-
with `get_gp_samples`, which allows them to be substituted in acquisition
22+
representing approximate GP sample paths, e.g. Matheron paths obtained
23+
with `get_matheron_path_model`, which allows them to be substituted in acquisition
2424
functions or in other places where a `Model` is expected.
2525
"""
2626

botorch/utils/gp_sampling.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from __future__ import annotations
88

9+
import warnings
910
from copy import deepcopy
1011
from math import pi
1112
from typing import List, Optional
@@ -41,6 +42,14 @@ def __init__(self, model: Model, seed: Optional[int] = None) -> None:
4142
Args:
4243
model: The Model defining the GP prior.
4344
"""
45+
warnings.warn(
46+
"`GPDraw` is deprecated and will be removed in v0.13 release. "
47+
"For drawing GP sample paths, we recommend using pathwise "
48+
"sampling code found in `botorch/sampling/pathwise`. We recommend "
49+
"`get_matheron_path_model` for most use cases.",
50+
DeprecationWarning,
51+
stacklevel=2,
52+
)
4453
super().__init__()
4554
self._model = deepcopy(model)
4655
self._num_outputs = self._model.num_outputs
@@ -429,6 +438,14 @@ def get_gp_samples(
429438
A `GenericDeterministicModel` that evaluates `n_samples` sampled functions.
430439
If `n_samples > 1`, this will be a batched model.
431440
"""
441+
warnings.warn(
442+
"`get_gp_samples` is deprecated and will be removed in v0.13 release. "
443+
"For drawing GP sample paths, we recommend using pathwise "
444+
"sampling code found in `botorch/sampling/pathwise`. We recommend "
445+
"`get_matheron_path_model` for most use cases.",
446+
DeprecationWarning,
447+
stacklevel=2,
448+
)
432449
# Get transforms from the model.
433450
intf = getattr(model, "input_transform", None)
434451
octf = getattr(model, "outcome_transform", None)

test/acquisition/multi_objective/test_utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from botorch.models.gp_regression import SingleTaskGP
2525
from botorch.models.model_list_gp_regression import ModelListGP
2626
from botorch.models.transforms.outcome import Standardize
27-
from botorch.utils.gp_sampling import get_gp_samples
27+
from botorch.sampling.pathwise import get_matheron_path_model
2828
from botorch.utils.multi_objective import is_non_dominated
2929
from botorch.utils.testing import BotorchTestCase, MockModel, MockPosterior
3030
from torch import Tensor
@@ -306,11 +306,7 @@ def test_random_search_optimizer(self):
306306
**tkwargs,
307307
)
308308

309-
model_sample = get_gp_samples(
310-
model=model,
311-
num_outputs=num_objectives,
312-
n_samples=1,
313-
)
309+
model_sample = get_matheron_path_model(model=model)
314310

315311
input_dim = X.shape[-1]
316312
# fake bounds

test/utils/test_gp_sampling.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ def test_gp_draw_single_output(self):
190190
tkwargs = {"device": self.device, "dtype": dtype}
191191
model, _, _ = _get_model(**tkwargs)
192192
mean = model.mean_module.raw_constant.detach().clone()
193-
gp = GPDraw(model)
193+
with self.assertWarnsRegex(
194+
DeprecationWarning, "is deprecated and will be removed"
195+
):
196+
gp = GPDraw(model)
194197
# test initialization
195198
self.assertIsNone(gp.Xs)
196199
self.assertIsNone(gp.Ys)
@@ -547,16 +550,19 @@ def test_get_gp_samples(self):
547550
)
548551
with torch.random.fork_rng():
549552
torch.manual_seed(0)
550-
gp_samples = get_gp_samples(
551-
model=(
552-
batched_to_model_list(model)
553-
if ((not use_batch_model) and (m > 1))
554-
else model
555-
),
556-
num_outputs=m,
557-
n_samples=n_samples,
558-
num_rff_features=512,
559-
)
553+
with self.assertWarnsRegex(
554+
DeprecationWarning, "is deprecated and will be removed"
555+
):
556+
gp_samples = get_gp_samples(
557+
model=(
558+
batched_to_model_list(model)
559+
if ((not use_batch_model) and (m > 1))
560+
else model
561+
),
562+
num_outputs=m,
563+
n_samples=n_samples,
564+
num_rff_features=512,
565+
)
560566
samples = gp_samples.posterior(X).mean
561567
self.assertEqual(samples.shape[0], n_samples)
562568
if batched_inputs:

0 commit comments

Comments
 (0)