Skip to content

Advanced sub tensor with none and integers fix #7814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions pymc/logprob/mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ def find_measurable_index_mixture(fgraph, node):
indices.dtype.startswith("int") and sum(1 - b for b in indices.type.broadcastable) > 0
for indices in mixing_indices
if not isinstance(indices, SliceConstant)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not isinstance(indices, SliceConstant)
if not isinstance(indices.type, SliceType | NoneTypeT)

if not isinstance(indices, type(NoneConst))
):
return None

Expand Down
23 changes: 23 additions & 0 deletions tests/logprob/test_mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,3 +1156,26 @@ def test_nested_ifelse():
np.testing.assert_almost_equal(mix_logp_fn(0, test_value), sp.norm.logpdf(test_value, -5, 1))
np.testing.assert_almost_equal(mix_logp_fn(1, test_value), sp.norm.logpdf(test_value, 0, 1))
np.testing.assert_almost_equal(mix_logp_fn(2, test_value), sp.norm.logpdf(test_value, 5, 1))


def test_advanced_subtensor_none_and_integer():
a = pt.random.normal(0, 1, size=(10,), name="a")
inds = np.array([0, 1, 2, 3], dtype="int32")
b = a[None, inds]

b_val = b.type()
b_val.name = "b_val"
a_val = a.type()
a_val.name = "a_val"

try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pytest.raises

# b is tested by being rewritten with logp, it should have a runtime error
logp_dict = conditional_logp({b: b_val, a: a_val})

# A runtime error means that a value of None was assigned to b, instead of the internal attribution error
except RuntimeError as e:
if "AttributeError" in str(e):
assert False, f"Rewrite failed with original bug: {e}"

except Exception as e:
assert False, f"Rewrite raised an unexpected error: {e}"