Skip to content

Commit b993a3c

Browse files
saitcakmakmeta-codesync[bot]
authored andcommitted
Clean up nits across acquisition module (meta-pytorch#3210)
Summary: Pull Request resolved: meta-pytorch#3210 Fix code quality issues across multiple files in `botorch/acquisition/`: **penalized.py:** 1. Rename misleading `pdf` variable to `penalty` in `GaussianPenalty.forward`. 2. Replace `__call__` with `forward` in `L0Approximation`, `L0PenaltyApprox`, and `L0PenaltyApproxObjective`. 3. Fix copy-paste docstring in `PenalizedAcquisitionFunction.__init__`. 4. Remove duplicated line in `PenalizedMCObjective` docstring example. 5. Replace bare `assert` with `ValueError` in `PenalizedMCObjective.forward`. **fixed_feature.py:** 6. Fix return type annotation: `get_device_of_sequence` returns `torch.device`, not `torch.dtype`. 7. Fix `_is_cuda` to work on multi-GPU: use `value.device.type == "cuda"` instead of comparing against `torch.device("cuda")`. **objective.py:** 8. Fix typo "Trasform" → "Transform" in comment. 9. Use `isinstance(eta, Tensor)` instead of `type(eta) is not Tensor`. 10. Replace bare `assert` with `ValueError` in `LearnedObjective.__init__`. **proximal.py:** 11. Fix typos "decrese" → "decrease" and "vaidation" → "validation". **thompson_sampling.py:** 12. Fix typo "inherting" → "inheriting". **active_learning.py:** 13. Remove duplicate phrase "points that have" in `X_pending` docstring. 14. Fix return shape docstring: `batch_size` not `batch_size x q`. **multioutput_acquisition.py:** 15. Fix `MultiOutputPosteriorMean` docstring: lists correct params (`model`, `weights`) instead of `acqfs`. 16. Fix `MultiOutputAcquisitionFunctionWrapper` docstring: says "wrapper" not "base class". Reviewed By: dme65 Differential Revision: D94963222 fbshipit-source-id: 1a09876ab0aebb00e908cd5d5373dcafdc34723b
1 parent 53f9ba2 commit b993a3c

8 files changed

Lines changed: 29 additions & 24 deletions

File tree

botorch/acquisition/active_learning.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(
7878
a PosteriorTransform that transforms the multi-output posterior into a
7979
single-output posterior is required.
8080
X_pending: A ``n' x d``-dim Tensor of ``n'`` design points that have
81-
points that have been submitted for function evaluation but
81+
been submitted for function evaluation but
8282
have not yet been evaluated.
8383
"""
8484
super().__init__(model=model)
@@ -170,7 +170,7 @@ def forward(self, X: Tensor) -> Tensor:
170170
X: A ``batch_size x q x d``-dim Tensor. q should be a multiple of 2.
171171
172172
Returns:
173-
Tensor of shape ``batch_size x q`` representing the posterior variance
173+
Tensor of shape ``batch_size`` representing the posterior variance
174174
of link function at X that active learning hopes to maximize
175175
"""
176176
if X.shape[-2] == 0 or X.shape[-2] % 2 != 0:

botorch/acquisition/fixed_feature.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ def _is_single(value: Tensor | float) -> bool:
3535
return torch.float32 if all_single_precision else torch.float64
3636

3737

38-
def get_device_of_sequence(values: Sequence[Tensor | float]) -> torch.dtype:
38+
def get_device_of_sequence(values: Sequence[Tensor | float]) -> torch.device:
3939
"""
4040
CPU if everything is on the CPU; Cuda otherwise.
4141
4242
Numbers (non-tensors) are considered to be on the CPU.
4343
"""
4444

4545
def _is_cuda(value: Tensor | float) -> bool:
46-
return hasattr(value, "device") and value.device == torch.device("cuda")
46+
return isinstance(value, Tensor) and value.device.type == "cuda"
4747

4848
any_cuda = any(_is_cuda(value) for value in values)
4949
return torch.device("cuda") if any_cuda else torch.device("cpu")

botorch/acquisition/multioutput_acquisition.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ def set_X_pending(self, X_pending: Tensor | None) -> None:
5656

5757
class MultiOutputPosteriorMean(MultiOutputAcquisitionFunction):
5858
def __init__(self, model: Model, weights: Tensor | None = None) -> None:
59-
r"""Constructor for the MultiPosteriorMean.
59+
r"""Constructor for the MultiOutputPosteriorMean.
6060
6161
Maximization of all outputs is assumed by default. Minimizing outputs can
6262
be achieved by setting the corresponding weights to negative.
6363
6464
Args:
65-
acqfs: A list of ``m`` acquisition functions.
65+
model: A fitted model.
6666
weights: A one-dimensional tensor with ``m`` elements representing the
6767
weights on the outputs.
6868
"""
@@ -102,7 +102,7 @@ class MultiOutputAcquisitionFunctionWrapper(MultiOutputAcquisitionFunction):
102102
r"""Multi-output wrapper around single-output acquisition functions."""
103103

104104
def __init__(self, acqfs: list[AcquisitionFunction]) -> None:
105-
r"""Constructor for the AcquisitionFunction base class.
105+
r"""Constructor for the MultiOutputAcquisitionFunctionWrapper.
106106
107107
Args:
108108
acqfs: A list of ``m`` acquisition functions.

botorch/acquisition/objective.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def forward(
219219
# ``n_w`` sized diagonal. The ``m`` outcomes are not interleaved.
220220
for i in range(q * m):
221221
weights[i, self.n_w * i : self.n_w * (i + 1)] = self.weights[:, i // q]
222-
# Trasform the mean.
222+
# Transform the mean.
223223
new_loc = (
224224
(weights @ org_mvn.loc.unsqueeze(-1))
225225
.view(*batch_shape, m, q)
@@ -452,7 +452,7 @@ def __init__(
452452
"""
453453
super().__init__(objective=objective)
454454
self.constraints = constraints
455-
if type(eta) is not Tensor:
455+
if not isinstance(eta, Tensor):
456456
eta = torch.full((len(constraints),), eta)
457457
self.register_buffer("eta", eta)
458458
self.register_buffer("infeasible_cost", torch.as_tensor(infeasible_cost))
@@ -528,7 +528,8 @@ def __init__(
528528
super().__init__()
529529
self.pref_model = pref_model
530530
if isinstance(pref_model, DeterministicModel):
531-
assert sample_shape is None
531+
if sample_shape is not None:
532+
raise ValueError("sample_shape must be None for DeterministicModel.")
532533
self.sampler = None
533534
else:
534535
if sample_shape is None:

botorch/acquisition/penalized.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def forward(self, X: Tensor) -> Tensor:
104104
A tensor of size "batch_shape" representing the acqfn for each q-batch.
105105
"""
106106
sq_diff = torch.linalg.norm((X - self.init_point), ord=2, dim=-1) ** 2
107-
pdf = torch.exp(sq_diff / 2 / self.sigma**2)
108-
regularization_term = pdf.max(dim=-1).values
107+
penalty = torch.exp(sq_diff / 2 / self.sigma**2)
108+
regularization_term = penalty.max(dim=-1).values
109109
return regularization_term
110110

111111

@@ -174,7 +174,7 @@ def __init__(self, target_point: Tensor, a: float = 1.0, **tkwargs: Any) -> None
174174
# hyperparameter to control the differentiable relaxation in L0 norm function.
175175
self.register_buffer("a", torch.tensor(a, **tkwargs))
176176

177-
def __call__(self, X: Tensor) -> Tensor:
177+
def forward(self, X: Tensor) -> Tensor:
178178
return nnz_approx(X=X, target_point=self.target_point, a=self.a)
179179

180180

@@ -191,14 +191,14 @@ def __init__(self, target_point: Tensor, a: float = 1.0, **tkwargs: Any) -> None
191191
"""
192192
super().__init__(target_point=target_point, a=a, **tkwargs)
193193

194-
def __call__(self, X: Tensor) -> Tensor:
194+
def forward(self, X: Tensor) -> Tensor:
195195
r"""
196196
Args:
197197
X: A "batch_shape x q x dim" representing the points to be evaluated.
198198
Returns:
199199
A tensor of size "batch_shape" representing the acqfn for each q-batch.
200200
"""
201-
return super().__call__(X=X).squeeze(dim=-1).min(dim=-1).values
201+
return super().forward(X=X).squeeze(dim=-1).min(dim=-1).values
202202

203203

204204
class PenalizedAcquisitionFunction(AcquisitionFunction):
@@ -216,7 +216,7 @@ def __init__(
216216
penalty_func: torch.nn.Module,
217217
regularization_parameter: float,
218218
) -> None:
219-
r"""Initializing Group-Lasso regularization.
219+
r"""Initializing penalized acquisition function.
220220
221221
Args:
222222
raw_acqf: The raw acquisition function that is going to be regularized.
@@ -317,7 +317,6 @@ class PenalizedMCObjective(GenericMCObjective):
317317
objective, l1_penalty_objective, regularization_parameter
318318
)
319319
>>> samples = sampler(posterior)
320-
objective, l1_penalty_objective, regularization_parameter
321320
"""
322321

323322
def __init__(
@@ -371,7 +370,12 @@ def forward(self, samples: Tensor, X: Tensor | None = None) -> Tensor:
371370
# tensor; obj returned from GenericMCObjective is a ``q``-dim tensor and
372371
# penalty_obj is a ``1 x q``-dim tensor.
373372
if obj.ndim == 1:
374-
assert penalty_obj.shape == torch.Size([1, samples.shape[-2]])
373+
if penalty_obj.shape != torch.Size([1, samples.shape[-2]]):
374+
raise ValueError( # pragma: no cover
375+
f"Expected penalty_obj of shape "
376+
f"{torch.Size([1, samples.shape[-2]])}, but got "
377+
f"{penalty_obj.shape}."
378+
)
375379
penalty_obj = penalty_obj.squeeze(dim=0)
376380
return obj - self.regularization_parameter * penalty_obj
377381

@@ -391,12 +395,12 @@ def __init__(self, target_point: Tensor, a: float = 1.0, **tkwargs: Any) -> None
391395
"""
392396
super().__init__(target_point=target_point, a=a, **tkwargs)
393397

394-
def __call__(self, X: Tensor) -> Tensor:
398+
def forward(self, X: Tensor) -> Tensor:
395399
r"""
396400
Args:
397401
X: A "batch_shape x q x dim" representing the points to be evaluated.
398402
Returns:
399403
A "1 x batch_shape x q" tensor representing the penalty for each point.
400404
The first dimension corresponds to the dimension of MC samples.
401405
"""
402-
return super().__call__(X=X).squeeze(dim=-1).unsqueeze(dim=0)
406+
return super().forward(X=X).squeeze(dim=-1).unsqueeze(dim=0)

botorch/acquisition/proximal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ProximalAcquisitionFunction(AcquisitionFunction):
3434
proximal weighting.
3535
3636
Small values of ``proximal_weights`` corresponds to strong biasing towards recently
37-
observed points, which smoothes optimization with a small potential decrese in
37+
observed points, which smoothes optimization with a small potential decrease in
3838
convergence rate.
3939
4040
@@ -156,7 +156,7 @@ def forward(self, X: Tensor) -> Tensor:
156156
def _validate_model(model: Model, proximal_weights: Tensor) -> None:
157157
r"""Validate model
158158
159-
Perform vaidation checks on model used in base acquisition function to make sure
159+
Perform validation checks on model used in base acquisition function to make sure
160160
it is compatible with proximal weighting.
161161
162162
Args:

botorch/acquisition/thompson_sampling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(
6060
self.ensemble_indices: Tensor | None = None
6161

6262
# NOTE: This conditional block is copied from MCAcquisitionFunction, we should
63-
# consider inherting from it and e.g. getting the X_pending logic as well.
63+
# consider inheriting from it and e.g. getting the X_pending logic as well.
6464
if objective is None and model.num_outputs != 1:
6565
if posterior_transform is None:
6666
raise UnsupportedError(

test/acquisition/test_objective.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ def test_learned_preference_objective(self) -> None:
570570
self.assertAllClose(avg_obj_val, flipped_avg_obj_val)
571571

572572
# cannot use a deterministic model together with a sampler
573-
with self.subTest("deterministic model"), self.assertRaises(AssertionError):
573+
with self.subTest("deterministic model"), self.assertRaises(ValueError):
574574
LearnedObjective(
575575
pref_model=mean_pref_model,
576576
sample_shape=torch.Size([num_samples]),

0 commit comments

Comments
 (0)