Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/pyrecest/_backend/pytorch/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ def _normal_device(*values):
return None


def _validate_normal_parameter(value, name):
if _contains_boolean_value(value):
raise TypeError(f"{name} must be real numeric, not boolean")


def _normal_array_parameters(loc, scale):
device = _normal_device(loc, scale)
loc = _torch.as_tensor(loc, device=device)
Expand Down Expand Up @@ -413,6 +418,8 @@ def multinomial(n, pvals, size=None):

@_allow_complex_dtype
def normal(loc=0.0, scale=1.0, size=None):
_validate_normal_parameter(loc, "loc")
_validate_normal_parameter(scale, "scale")
size = _normal_size(size)
if not (_is_array_parameter(loc) or _is_array_parameter(scale)):
return _torch.normal(mean=loc, std=scale, size=size or ())
Expand Down
23 changes: 23 additions & 0 deletions tests/backend/test_pytorch_random_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,29 @@ def test_uniform_rejects_boolean_bounds(low, high):
random.uniform(low, high)


@pytest.mark.parametrize(
("loc", "scale"),
[
(False, 1.0),
(0.0, True),
(np.bool_(True), 1.0),
(0.0, np.bool_(True)),
(torch.tensor(True), 1.0),
(0.0, torch.tensor(True)),
(torch.tensor([False, False]), torch.tensor([1.0, 2.0])),
([False, 0.0], [1.0, 2.0]),
([0.0, 0.5], [1.0, np.bool_(True)]),
(
np.array([0.0, np.bool_(False)], dtype=object),
np.array([1.0, 2.0], dtype=object),
),
],
)
def test_normal_rejects_boolean_parameters(loc, scale):
with pytest.raises(TypeError, match="real numeric"):
random.normal(loc, scale)


@pytest.mark.parametrize(
("low", "high"),
[
Expand Down
Loading