Skip to content

Only add dim_length shared variable when adding a new coordinate #7809

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
Jun 3, 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
1 change: 1 addition & 0 deletions pymc/model/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ def add_coord(
if name in self.coords:
if not np.array_equal(values, self.coords[name]):
raise ValueError(f"Duplicate and incompatible coordinate: {name}.")
return
if length is not None and not isinstance(length, int | Variable):
raise ValueError(
f"The `length` passed for the '{name}' coord must be an int, PyTensor Variable or None."
Expand Down
14 changes: 14 additions & 0 deletions tests/model/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import scipy.stats as st

from pytensor.graph import graph_inputs
from pytensor.graph.basic import get_var_by_name
from pytensor.raise_op import Assert
from pytensor.tensor.random.op import RandomVariable
from pytensor.tensor.variable import TensorConstant
Expand Down Expand Up @@ -858,6 +859,19 @@ def test_nested_model_coords():
assert set(m2.named_vars_to_dims) < set(m1.named_vars_to_dims)


def test_multiple_add_coords_with_same_name():
coord = {"dim1": ["a", "b", "c"]}
with pm.Model(coords=coord) as m:
a = pm.Normal("a", dims="dim1")
with pm.Model(coords=coord) as nested_m:
b = pm.Normal("b", dims="dim1")
m.add_coords(coord)
c = pm.Normal("c", dims="dim1")
d = pm.Deterministic("d", a + b + c)
variables = get_var_by_name([d], "dim1")
assert len(variables) == 1 and variables[0] is m.dim_lengths["dim1"]


class TestSetUpdateCoords:
def test_shapeerror_from_set_data_dimensionality(self):
with pm.Model() as pmodel:
Expand Down