Skip to content

Commit e8d12b5

Browse files
docusaurus-botfacebook-github-bot
authored andcommitted
Replace deprecated np.bool with equivalent bool (#1524)
Summary: ## Motivation `np.bool` has been removed in numpy 1.24.0, currently only available as a pre-release. This caused failures like [this one](https://github.com/pytorch/botorch/actions/runs/3542694073) `np.bool` has always been equivalent to `bool`, so I replaced it with `bool`. Surprisingly (to me), we are only running that version of numpy in the tutorials and are using an older version of unit tests, so tests all passed. Pull Request resolved: #1524 Test Plan: Added a test that the dtype mapping in `torch_to_numpy_dtype_dict` matches the behavior of torch `.numpy()` Reviewed By: Balandat Differential Revision: D41527521 Pulled By: esantorella fbshipit-source-id: 0718aa3af5eb5a2423c152cb911babf7754e6b89
1 parent 9f55da2 commit e8d12b5

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

botorch/optim/utils/numpy_utils.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,19 @@
1717
from numpy import ndarray
1818
from torch import Tensor
1919

20-
# Dictionaries mapping numpy to torch dtypes and vice-versa
21-
numpy_to_torch_dtype_dict = {
22-
np.bool: torch.bool,
23-
np.uint8: torch.uint8,
24-
np.int8: torch.int8,
25-
np.int16: torch.int16,
26-
np.int32: torch.int32,
27-
np.int64: torch.int64,
28-
np.float16: torch.float16,
29-
np.float32: torch.float32,
30-
np.float64: torch.float64,
31-
np.complex64: torch.complex64,
32-
np.complex128: torch.complex128,
33-
}
3420

3521
torch_to_numpy_dtype_dict = {
36-
value: key for (key, value) in numpy_to_torch_dtype_dict.items()
22+
torch.bool: bool,
23+
torch.uint8: np.uint8,
24+
torch.int8: np.int8,
25+
torch.int16: np.int16,
26+
torch.int32: np.int32,
27+
torch.int64: np.int64,
28+
torch.float16: np.float16,
29+
torch.float32: np.float32,
30+
torch.float64: np.float64,
31+
torch.complex64: np.complex64,
32+
torch.complex128: np.complex128,
3733
}
3834

3935

test/optim/utils/test_numpy_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
set_tensors_from_ndarray_1d,
1717
)
1818
from botorch.optim.utils import get_bounds_as_ndarray
19+
from botorch.optim.utils.numpy_utils import torch_to_numpy_dtype_dict
1920
from botorch.utils.testing import BotorchTestCase
2021
from torch.nn import Parameter
2122

@@ -54,6 +55,13 @@ def test_as_ndarray(self):
5455
mock_tensor.clone.assert_not_called()
5556
mock_tensor.numpy.assert_called_once()
5657

58+
def test_as_ndarray_dtypes(self) -> None:
59+
for torch_dtype, np_dtype in torch_to_numpy_dtype_dict.items():
60+
tens = torch.tensor(0, dtype=torch_dtype, device="cpu")
61+
self.assertEqual(torch_dtype, tens.dtype)
62+
self.assertEqual(tens.numpy().dtype, np_dtype)
63+
self.assertEqual(as_ndarray(tens, np_dtype).dtype, np_dtype)
64+
5765
def test_get_tensors_as_ndarray_1d(self):
5866
with self.assertRaisesRegex(RuntimeError, "Argument `tensors` .* is empty"):
5967
get_tensors_as_ndarray_1d(())

0 commit comments

Comments
 (0)