Skip to content

fix(numba): non-contiguous shared variable #217

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

Merged
merged 1 commit into from
May 28, 2025
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
4 changes: 2 additions & 2 deletions python/nutpie/compile_pymc.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def with_data(self, **updates):
if name not in shared_data:
raise KeyError(f"Unknown shared variable: {name}")
old_val = shared_data[name]
new_val = np.asarray(new_val, dtype=old_val.dtype).copy()
new_val = np.array(new_val, dtype=old_val.dtype, order="C", copy=True)
new_val.flags.writeable = False
if old_val.ndim != new_val.ndim:
raise ValueError(
Expand Down Expand Up @@ -256,7 +256,7 @@ def _compile_pymc_model_numba(
for val in [*logp_fn_pt.get_shared(), *expand_fn_pt.get_shared()]:
if val.name in shared_data and val not in seen:
raise ValueError(f"Shared variables must have unique names: {val.name}")
shared_data[val.name] = val.get_value()
shared_data[val.name] = np.array(val.get_value(), order="C", copy=True)
Copy link
Member

Choose a reason for hiding this comment

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

if you're copying anyway you can do get_value(borrow=True) to avoid doing 2 copies

shared_vars[val.name] = val
seen.add(val)

Expand Down
32 changes: 32 additions & 0 deletions tests/test_pymc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ def test_pymc_model(backend, gradient_backend):
trace.posterior.a # noqa: B018


@pytest.mark.pymc
def test_order_shared():
a_val = np.array([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]])
with pm.Model() as model:
a = pm.Data("a", np.copy(a_val, order="C"))
b = pm.Normal("b", shape=(2, 5))
pm.Deterministic("c", (a[:, None, :] * b[:, :, None]).sum(-1))

compiled = nutpie.compile_pymc_model(model, backend="numba")
trace = nutpie.sample(compiled)
np.testing.assert_allclose(
(
trace.posterior.b.values[:, :, :, :, None] * a_val[None, None, :, None, :]
).sum(-1),
trace.posterior.c.values,
)

with pm.Model() as model:
a = pm.Data("a", np.copy(a_val, order="F"))
b = pm.Normal("b", shape=(2, 5))
pm.Deterministic("c", (a[:, None, :] * b[:, :, None]).sum(-1))

compiled = nutpie.compile_pymc_model(model, backend="numba")
trace = nutpie.sample(compiled)
np.testing.assert_allclose(
(
trace.posterior.b.values[:, :, :, :, None] * a_val[None, None, :, None, :]
).sum(-1),
trace.posterior.c.values,
)


@pytest.mark.pymc
@parameterize_backends
def test_low_rank(backend, gradient_backend):
Expand Down