pymc-devs / pymc-devs/pytensor

MLX: gradient of batched advanced indexing fails in broadcast_shapes

Open
#2,387 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug gradients indexing mlx
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

The gradient of advanced integer indexing drops the leading batch dimensions on MLX, so the AdvancedIncSubtensor scattering the adjoint back tries to broadcast a (5, 3) against the (3, 3) core and dies. Easy to hit without writing any indexing yourself: specialize rewrites diagonal(cholesky(X)) into this form, so the gradient of a log-determinant over a batch of matrices fails.

import numpy as np
import pytensor
import pytensor.tensor as pt

X = pt.tensor("X", shape=(5, 3, 3), dtype="float32")
idx = pt.arange(3)
g = pt.grad(X[..., idx, idx].sum(), X)
Xv = np.zeros((5, 3, 3), dtype="float32")

print(pytensor.function([X], g, mode="CVM")(Xv).sum())  # 15.0
print(pytensor.function([X], g, mode="MLX")(Xv).sum())
# ValueError: [broadcast_shapes] Shapes (5,3) and (3,3) cannot be broadcast.

pt.diagonal(X, axis1=-2, axis2=-1) and its gradient are both fine; it's the advanced-indexing spelling the rewrite produces that breaks. Writing the diagonal as (X * pt.eye(3)).sum(-1) avoids it.

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 by running the reproducer in the issue with the MLX and CVM modes, then trace the AdvancedIncSubtensor gradient through broadcast_shapes. Compare the batched advanced-indexing result with the working diagonal case; done means the MLX gradient evaluates successfully and matches the expected batched result, including the 15.0 sum.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.