diff --git a/pymc/model/core.py b/pymc/model/core.py index d04011407..469001e80 100644 --- a/pymc/model/core.py +++ b/pymc/model/core.py @@ -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." diff --git a/tests/model/test_core.py b/tests/model/test_core.py index 4375a17ad..b26a9d96b 100644 --- a/tests/model/test_core.py +++ b/tests/model/test_core.py @@ -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 @@ -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: