pymc-devs / pymc-devs/pytensor

MLX AdvancedIncSubtensor fails when Blockwise adds a batch dim

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

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

Open the contributing guide

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.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.