Skip to content

Commit 07f12b7

Browse files
esantorellafacebook-github-bot
authored andcommitted
Remove unused ** arguments (#2327)
Summary: Pull Request resolved: #2327 In the process of auditing places where arguments (especially of the ** form) are ignored in BoTorch, I fixed the occurrences that seemed easy. Reviewed By: saitcakmak Differential Revision: D56828338 fbshipit-source-id: cdb603de098a911bd5bdfa28f624a15c53b07138
1 parent f9315b2 commit 07f12b7

14 files changed

+33
-37
lines changed

botorch/acquisition/cost_aware.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import warnings
1515
from abc import ABC, abstractmethod
16-
from typing import Any, Callable, Optional, Union
16+
from typing import Callable, Optional, Union
1717

1818
import torch
1919
from botorch import settings
@@ -38,7 +38,9 @@ class CostAwareUtility(Module, ABC):
3838
"""
3939

4040
@abstractmethod
41-
def forward(self, X: Tensor, deltas: Tensor, **kwargs: Any) -> Tensor:
41+
def forward(
42+
self, X: Tensor, deltas: Tensor, sampler: Optional[MCSampler] = None
43+
) -> Tensor:
4244
r"""Evaluate the cost-aware utility on the candidates and improvements.
4345
4446
Args:
@@ -47,6 +49,8 @@ def forward(self, X: Tensor, deltas: Tensor, **kwargs: Any) -> Tensor:
4749
deltas: A `num_fantasies x batch_shape`-dim Tensor of `num_fantasy`
4850
samples from the marginal improvement in utility over the
4951
current state at `X` for each t-batch.
52+
sampler: A sampler used for sampling from the posterior of the cost
53+
model. Some subclasses ignore this argument.
5054
5155
Returns:
5256
A `num_fantasies x batch_shape`-dim Tensor of cost-transformed utilities.
@@ -67,7 +71,9 @@ def __init__(self, cost: Callable[[Tensor, Tensor], Tensor]) -> None:
6771
super().__init__()
6872
self._cost_callable: Callable[[Tensor, Tensor], Tensor] = cost
6973

70-
def forward(self, X: Tensor, deltas: Tensor, **kwargs: Any) -> Tensor:
74+
def forward(
75+
self, X: Tensor, deltas: Tensor, sampler: Optional[MCSampler] = None
76+
) -> Tensor:
7177
r"""Evaluate the cost function on the candidates and improvements.
7278
7379
Args:
@@ -76,6 +82,7 @@ def forward(self, X: Tensor, deltas: Tensor, **kwargs: Any) -> Tensor:
7682
deltas: A `num_fantasies x batch_shape`-dim Tensor of `num_fantasy`
7783
samples from the marginal improvement in utility over the
7884
current state at `X` for each t-batch.
85+
sampler: Ignored.
7986
8087
Returns:
8188
A `num_fantasies x batch_shape`-dim Tensor of cost-weighted utilities.
@@ -143,7 +150,7 @@ def __init__(
143150
cost_objective = GenericMCObjective(lambda Y, X: Y.sum(dim=-1))
144151

145152
self.cost_model = cost_model
146-
self.cost_objective = cost_objective
153+
self.cost_objective: MCAcquisitionObjective = cost_objective
147154
self._use_mean = use_mean
148155
self._min_cost = min_cost
149156

@@ -153,7 +160,6 @@ def forward(
153160
deltas: Tensor,
154161
sampler: Optional[MCSampler] = None,
155162
X_evaluation_mask: Optional[Tensor] = None,
156-
**kwargs: Any,
157163
) -> Tensor:
158164
r"""Evaluate the cost function on the candidates and improvements. Note
159165
that negative values of `deltas` are instead scaled by the cost, and not

botorch/acquisition/max_value_entropy_search.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from abc import ABC, abstractmethod
3434
from copy import deepcopy
3535
from math import log
36-
from typing import Any, Callable, Optional
36+
from typing import Callable, Optional
3737

3838
import numpy as np
3939
import torch
@@ -155,15 +155,14 @@ def set_X_pending(self, X_pending: Optional[Tensor] = None) -> None:
155155
# ------- Abstract methods that need to be implemented by subclasses ------- #
156156

157157
@abstractmethod
158-
def _compute_information_gain(self, X: Tensor, **kwargs: Any) -> Tensor:
158+
def _compute_information_gain(self, X: Tensor) -> Tensor:
159159
r"""Compute the information gain at the design points `X`.
160160
161161
`num_fantasies = 1` for non-fantasized models.
162162
163163
Args:
164164
X: A `batch_shape x 1 x d`-dim Tensor of `batch_shape` t-batches
165165
with `1` `d`-dim design point each.
166-
kwargs: Other keyword arguments used by subclasses.
167166
168167
Returns:
169168
A `num_fantasies x batch_shape`-dim Tensor of information gains at the
@@ -174,11 +173,12 @@ def _compute_information_gain(self, X: Tensor, **kwargs: Any) -> Tensor:
174173
@abstractmethod
175174
def _sample_max_values(
176175
self, num_samples: int, X_pending: Optional[Tensor] = None
177-
) -> Tensor:
176+
) -> None:
178177
r"""Draw samples from the posterior over maximum values.
179178
180179
These samples are used to compute Monte Carlo approximations of expectations
181-
over the posterior over the function maximum.
180+
over the posterior over the function maximum. This function sets
181+
`self.posterior_max_values`.
182182
183183
Args:
184184
num_samples: The number of samples to draw.
@@ -254,11 +254,12 @@ def __init__(
254254

255255
def _sample_max_values(
256256
self, num_samples: int, X_pending: Optional[Tensor] = None
257-
) -> Tensor:
257+
) -> None:
258258
r"""Draw samples from the posterior over maximum values on a discrete set.
259259
260260
These samples are used to compute Monte Carlo approximations of expectations
261-
over the posterior over the function maximum.
261+
over the posterior over the function maximum. This function sets
262+
`self.posterior_max_values`.
262263
263264
Args:
264265
num_samples: The number of samples to draw.

botorch/acquisition/multi_objective/hypervolume_knowledge_gradient.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def __init__(
8484
current_value: Optional[Tensor] = None,
8585
use_posterior_mean: bool = True,
8686
cost_aware_utility: Optional[CostAwareUtility] = None,
87-
**kwargs: Any,
8887
) -> None:
8988
r"""q-Hypervolume Knowledge Gradient.
9089

botorch/acquisition/multi_objective/joint_entropy_search.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from abc import abstractmethod
2121
from math import pi
22-
from typing import Any, Optional, Tuple, Union
22+
from typing import Optional, Tuple, Union
2323

2424
import torch
2525
from botorch import settings
@@ -50,7 +50,6 @@ def __init__(
5050
X_pending: Optional[Tensor] = None,
5151
estimation_type: str = "LB",
5252
num_samples: int = 64,
53-
**kwargs: Any,
5453
) -> None:
5554
r"""Lower bound multi-objective entropy search acquisition function.
5655
@@ -283,7 +282,6 @@ def __init__(
283282
X_pending: Optional[Tensor] = None,
284283
estimation_type: str = "LB",
285284
num_samples: int = 64,
286-
**kwargs: Any,
287285
) -> None:
288286
r"""Lower bound multi-objective joint entropy search acquisition function.
289287

botorch/acquisition/multi_objective/max_value_entropy_search.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from math import pi
2323

24-
from typing import Any, Callable, Optional, Tuple, Union
24+
from typing import Callable, Optional, Tuple, Union
2525

2626
import torch
2727
from botorch.acquisition.max_value_entropy_search import qMaxValueEntropy
@@ -78,7 +78,6 @@ def __init__(
7878
num_fantasies: int = 16,
7979
X_pending: Optional[Tensor] = None,
8080
sampler: Optional[MCSampler] = None,
81-
**kwargs: Any,
8281
) -> None:
8382
r"""Multi-objective max-value entropy search acquisition function.
8483
@@ -165,7 +164,9 @@ def set_X_pending(self, X_pending: Optional[Tensor] = None) -> None:
165164
self._sample_max_values()
166165

167166
def _sample_max_values(self) -> None:
168-
r"""Sample max values for MC approximation of the expectation in MES"""
167+
"""Sample max values for MC approximation of the expectation in MES.
168+
169+
Sets self.posterior_max_values."""
169170
with torch.no_grad():
170171
# num_samples x (num_fantasies) x n_pareto_points x m
171172
sampled_pfs = self.sample_pareto_frontiers(self.mo_model)
@@ -220,7 +221,6 @@ def __init__(
220221
X_pending: Optional[Tensor] = None,
221222
estimation_type: str = "LB",
222223
num_samples: int = 64,
223-
**kwargs: Any,
224224
) -> None:
225225
r"""Lower bound multi-objective max-value entropy search acquisition function.
226226

botorch/acquisition/multi_objective/multi_fidelity.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from __future__ import annotations
2020

21-
from typing import Any, Callable, List, Optional, Union
21+
from typing import Callable, List, Optional, Union
2222

2323
import torch
2424
from botorch.acquisition.cost_aware import InverseCostWeightedUtility
@@ -48,8 +48,7 @@ def __init__(
4848
constraints: Optional[List[Callable[[Tensor], Tensor]]] = None,
4949
eta: Optional[Union[Tensor, float]] = 1e-3,
5050
X_pending: Optional[Tensor] = None,
51-
cost_call: Callable[Tensor, Tensor] = None,
52-
**kwargs: Any,
51+
cost_call: Optional[Callable[[Tensor], Tensor]] = None,
5352
) -> None:
5453
r"""MOMF acquisition function supporting m>=2 outcomes.
5554
The model needs to have train_obj that has a fidelity

botorch/acquisition/multi_objective/objective.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MCMultiOutputObjective(MCAcquisitionObjective):
2828
_is_mo: bool = True
2929

3030
@abstractmethod
31-
def forward(self, samples: Tensor, X: Optional[Tensor] = None, **kwargs) -> Tensor:
31+
def forward(self, samples: Tensor, X: Optional[Tensor] = None) -> Tensor:
3232
r"""Evaluate the multi-output objective on the samples.
3333
3434
Args:

botorch/acquisition/multi_objective/predictive_entropy_search.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from __future__ import annotations
2525

26-
from typing import Any, Optional, Tuple
26+
from typing import Optional, Tuple
2727

2828
import torch
2929
from botorch.acquisition.acquisition import AcquisitionFunction
@@ -107,7 +107,6 @@ def __init__(
107107
ep_jitter: float = 1e-4,
108108
test_jitter: float = 1e-4,
109109
threshold: float = 1e-2,
110-
**kwargs: Any,
111110
) -> None:
112111
r"""Multi-objective predictive entropy search acquisition function.
113112

botorch/acquisition/multi_step_lookahead.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,6 @@ def warmstart_multistep(
598598
num_restarts: int,
599599
raw_samples: int,
600600
full_optimizer: Tensor,
601-
**kwargs: Any,
602601
) -> Tensor:
603602
r"""Warm-start initialization for multi-step look-ahead acquisition functions.
604603
@@ -614,7 +613,6 @@ def warmstart_multistep(
614613
full_optimizer: The full tree of optimizers of the previous iteration of shape
615614
`batch_shape x q' x d`. Typically obtained by passing
616615
`return_best_only=False` and `return_full_tree=True` into `optimize_acqf`.
617-
kwargs: Optimization kwargs.
618616
619617
Returns:
620618
A `num_restarts x q' x d` tensor for initial points for optimization.

botorch/acquisition/predictive_entropy_search.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from __future__ import annotations
1717

18-
from typing import Any, Optional
18+
from typing import Optional
1919

2020
from botorch.acquisition.multi_objective.predictive_entropy_search import (
2121
qMultiObjectivePredictiveEntropySearch,
@@ -53,7 +53,6 @@ def __init__(
5353
ep_jitter: float = 1e-4,
5454
test_jitter: float = 1e-4,
5555
threshold: float = 1e-2,
56-
**kwargs: Any,
5756
) -> None:
5857
r"""Predictive entropy search acquisition function.
5958

0 commit comments

Comments
 (0)