xtensor vectorize_graph raises max() iterable argument is empty when new_tensor_dims is given but no replacement is a plain tensor

Open
#2,699 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@koushik-hs is already working on this.

Since Nov 23, 2026.

  • #2708 by @koushik-hs — open

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
compilers

Research direction

Start in pytensor/xtensor/vectorization.py near line 602, where vectorize_graph calls broadcast_shape with new_tensor_dim_lengths, which is empty when no plain tensor replacement exists. Run the two calls in the issue's snippet to reproduce the ValueError. The issue leaves open whether to skip the broadcast or raise a clearer error, so confirm the choice with maintainers before editing. Done means the chosen behaviour is implemented and a test covers new_tensor_dims with only xtensor replacements.

Written by the indexing model from the issue text.

Description

pytensor.xtensor.vectorization.vectorize_graph raises an unhelpful error when new_tensor_dims is not empty and every replacement is an XTensorVariable:

import numpy as np
from pytensor.xtensor import xtensor_constant
from pytensor.xtensor.type import xtensor
from pytensor.xtensor.vectorization import vectorize_graph

x = xtensor("x", dims=("date",), shape=(3,))
new = xtensor_constant(np.ones((4, 3)), dims=("sample", "date"))

vectorize_graph(x * 2.0, replace={x: new})  # dims ("sample", "date"), fine
vectorize_graph(x * 2.0, replace={x: new}, new_tensor_dims=("sample",))
# ValueError: max() iterable argument is empty

With new_tensor_dims set, vectorize_graph calls broadcast_shape(*new_tensor_dim_lengths, arrays_are_shapes=True) (pytensor/xtensor/vectorization.py:602), and new_tensor_dim_lengths only collects the batch shapes of plain tensor replacements. With none, it is empty, and broadcast_shape_iter fails on max().

Found in pymc-labs/pymc-marketing#3402, where extract_response_distribution replaces posterior variables declared with pymc.dims by xtensors and those declared with pymc by plain tensors. A model can have either kind or both, so the call passes new_tensor_dims=("sample",) only when at least one replacement is a plain tensor.

Should vectorize_graph skip the broadcast when there are no tensor replacements (the names then match nothing), or raise a ValueError that says new_tensor_dims was given without a tensor replacement?

PyTensor 3.3.8; vectorization.py is the same on main (daafd793).

Dominant language
Python
Stars
655
Forks
266
Avg merge
3d 23h
Merged PRs (30d)
43

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pymc-devs/pytensor

All issues in pymc-devs/pytensor

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.