pymc-devs / pymc-devs/pytensor
Handle non-square intermediate Blockwise operations
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 644
- Forks
- 208
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 16
Description
Description
When output shapes depend on input values, Blockwise are not necessarily valid at runtime. For example the following graph is not supported by PyTensor at runtime, because it would require support for ragged arrays in the intermediate Blockwise(Arange):
import pytensor.tensor as pt
from pytensor.graph import vectorize
i = pt.scalar("i", dtype=int)
y = pt.sum(pt.arange(0, i))
new_i = pt.vector("new_i", dtype=int)
new_y = vectorize(y, {i: new_i})
new_y.eval({new_i: [1, 2, 3, 4]}) # ValueError
However if we were to wrap the Arange + Sum in an OpFromGraph, that subgraph would be a valid Blockwise, and PyTensor would be happy to evaluate it:
import pytensor.tensor as pt
from pytensor.graph import vectorize
from pytensor.compile.builders import OpFromGraph
i = pt.scalar("i", dtype=int)
y_ = pt.sum(pt.arange(0, i))
y = OpFromGraph([i], [y_])(i)
new_i = pt.vector("new_i", dtype=int)
new_y = vectorize(y, {i: new_i})
new_y.eval({new_i: [1, 2, 3, 4]}) # [0, 1, 3, 6]
Would be nice to use this trick to support end-to-end vectorization in these cases. Some of the logic needed to infer whether an Op has a square shape or not is being developed in #1015.
Some of the logic developed in https://github.com/pymc-devs/pymc-experimental/pull/300 to understand how dimensions propagate over nodes could be repurposed to figure out in which cases a subgraph collapses ragged dimensions.
We can start very simple and just allow immediate reductions of ragged dimensions.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the vectorize entry point and the Blockwise/Arange example in the issue, then review the shape-inference work in #1015. The related pymc-experimental PR #300 may help explain dimension propagation. Done means ragged intermediate dimensions can be collapsed for the initial immediate-reduction case, with the example evaluating to [0, 1, 3, 6].
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100