Blockwise(AdvancedSubtensor) with broadcast-batch indices is not rewritten to AdvancedSubtensor
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by running the issue's snippet with pytensor.dprint to confirm the Blockwise(AdvancedSubtensor) node. Then compare it with the plain AdvancedSubtensor that vectorize_graph produces for unbatched indices. The task is a graph rewrite that drops broadcastable batch dims from the index inputs and adds leading full slices. Done when the dprint output shows a plain AdvancedSubtensor and the numerical results match the original graph.
Written by the indexing model from the issue text.
Description
vectorize_graph of advanced indexing with unbatched indices already gives a plain AdvancedSubtensor with a leading slice. But if the indices were batched when the graph was vectorized and only lose their batch dims later (broadcastable batch dims after rewrites), the Blockwise(AdvancedSubtensor) stays:
import pytensor
import pytensor.tensor as pt
from pytensor.compile.mode import Mode
from pytensor.tensor.blockwise import Blockwise
xc = pt.matrix("xc")
r = pt.vector("r", dtype="int64")
c = pt.vector("c", dtype="int64")
core_op = xc[r[:, None], c[None, :]].owner.op
x = pt.tensor3("x")
rb = pt.tensor("rb", shape=(1, None, 1), dtype="int64") # batch dim only broadcast
cb = pt.tensor("cb", shape=(1, 1, None), dtype="int64")
out = Blockwise(core_op, signature="(i00,i01),(i10,i11),(i20,i21)->(o00,o01)")(x, rb, cb)
fn = pytensor.function([x, rb, cb], out, mode=Mode(linker="py", optimizer="fast_run"))
pytensor.dprint(fn)
# Blockwise{AdvancedSubtensor{idx_list=(0, 1)}, (i00,i01),(i10,i11),(i20,i21)->(o00,o01)} [id A] 0
# ├─ x [id B]
# ├─ rb [id C]
# └─ cb [id D]
This is equivalent to x[:, rb[0], cb[0]].
It is what's left in the batched block_diag gradient from #2702 once #2703 removes the batched ARange. There the indices are ix_-style aranges with a broadcast batch dim:
Blockwise{AdvancedSubtensor{idx_list=(0, 1)}, (i00,i01),(i10,i11),(i20,i21)->(o00,o01)} [id A] <Tensor3(float64, shape=(?, ?, ?))>
├─ Mul [id B] <Tensor3(float64, shape=(?, ?, ?))>
│ ├─ [[[2.]]] [id C] <Tensor3(float64, shape=(1, 1, 1))>
│ └─ Blockwise{BlockDiagonal{n_inputs=2}, (m0,n0),(m1,n1)->(m,n)} [id D] <Tensor3(float64, shape=(?, ?, ?))>
├─ ExpandDims{axes=(0, 2)} [id H] <Tensor3(int64, shape=(1, ?, 1))>
│ └─ ARange{dtype='int64'} [id I] <Vector(int64, shape=(?,))>
└─ ExpandDims{axes=(0, 1)} [id BA] <Tensor3(int64, shape=(1, 1, ?))>
└─ ARange{dtype='int64'} [id BB] <Vector(int64, shape=(?,))>
A rewrite that drops the broadcast batch dims of the index inputs and indexes with leading full slices would turn it back into a plain AdvancedSubtensor, as vectorize_graph produces directly.
- Dominant language
- Python
- Stars
- 655
- Forks
- 266
- Avg merge
- 3d 23h
- Merged PRs (30d)
- 43
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing 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.
More from pymc-devs/pytensor
-
BUG: cumsum and cumprod keep bool and small integer dtypes, so cumsum(x > 0) is wrongPossibly taken @rishabhsai claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Docs of mode still mentions C/CVM as the default with FAST_RUNPossibly taken @ADITYASINGH1206 claimed this 13 days ago. Opendocs
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
pymc-devs/pytensor#2641 · 1 comment ·
Maintainers usually reply within 1 day
-
Update conditions page in docsPossibly taken @caxaxa claimed this 3 days ago. Opendocs
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
BUG: geomspace returns wrong sign for the endpoints when start and stop are negativePossibly taken @mayuriphad claimed this 32 days ago. Open
Difficulty 2/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Reciprocal.impl computes np.float32(1.0) / x: float32 precision for float64 scalars under NEP 50 (Python linker)May be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
pymc-devs/pytensor#2430 · 1 comment ·
Maintainers usually reply within 1 day
All issues in pymc-devs/pytensor
Similar issues
-
triage
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
kornia/kornia-rs#1416 · 1 comment ·
Maintainers usually reply within 2 days
-
ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
PolicyEngine/ai-inequality#64 ·
Maintainers usually reply within 1 day
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over timePossibly taken @PabloRoque claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pymc-devs/pymc-extras#839 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day