pymc-devs / pymc-devs/pytensor
MLX AdvancedIncSubtensor fails when Blockwise adds a batch dim
Open
Nobody has claimed this yet.
backend compatibility
bug
mlx
- Dominant language
- Python
- Stars
- 644
- Forks
- 208
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 16
Description
The MLX dispatch for AdvancedIncSubtensor does x.at[indices].add(y) with the unbatched indices, so a scatter-add that vectorize_graph has given a leading batch dim can't broadcast. Works on every other backend.
import numpy as np
import pytensor
import pytensor.tensor as pt
from pytensor.graph.replace import vectorize_graph
B, N, K = 4, 7, 3
idx = pt.constant(np.random.default_rng(0).integers(0, K, size=N))
levels = pt.vector("levels", shape=(K,))
grad = pt.grad(levels[idx].sum(), levels) # AdvancedIncSubtensor: scatter-add into (K,)
batched = pt.matrix("batched", shape=(B, K))
vectorized = vectorize_graph(grad, replace={levels: batched})
x = np.ones((B, K), dtype=pytensor.config.floatX)
print(pytensor.function([batched], vectorized, mode="FAST_RUN")(x).shape) # (4, 3)
pytensor.function([batched], vectorized, mode="MLX")(x)
# ValueError: [broadcast_shapes] Shapes (4,1) and (7,3) cannot be broadcast
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 MLX dispatch for AdvancedIncSubtensor and reproduce the vectorize_graph example from the issue in MLX mode. The fix is complete when the batched scatter-add runs without a broadcast error and returns an array with shape (4, 3), matching the other backend.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, machine-learning
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 70/100