Skip to content

Fix SliceUpdate vmap with unbatched update - #4770

Merged
zcbenz merged 1 commit into
ml-explore:mainfrom
mahimn01:fix-slice-update-vmap-unbatched-update
Oct 29, 2026
Merged

zcbenz merged 1 commit into
ml-explore:mainfrom
mahimn01:fix-slice-update-vmap-unbatched-update

Conversation

@mahimn01

Copy link
Copy Markdown
Contributor

Summary

SliceUpdate::vmap and DynamicSliceUpdate::vmap silently return a wrong result when src is batched and the update is not. The update gets a size-1 axis at the batch position but is never broadcast to the batch size, so only the first batch element is updated and the rest come back unchanged. No error is raised.

Repro (CPU)

import mlx.core as mx

src = mx.zeros((3, 4, 2))
upd = mx.array([[100.0, 200.0]])
start = mx.array([1, 0])
f = lambda s, u, k: mx.slice_update(s, u, k, axes=(0, 1))

out = mx.vmap(f, in_axes=(0, None, None))(src, upd, start)
expected = mx.stack([f(src[i], upd, start) for i in range(3)])
print(out[:, 1].tolist())       # [[100.0, 200.0], [0.0, 0.0], [0.0, 0.0]]
print(expected[:, 1].tolist())  # [[100.0, 200.0], [100.0, 200.0], [100.0, 200.0]]

The static form (s[1:2, 0:2] = u inside a vmapped function) has the same bug because it goes through SliceUpdate.

Fix

Both vmap rules now broadcast upd to the batch size of src after the expand_dims. Cases where the update is already batched are unchanged.

Tests

Added test_vmap_slice_update, which compares against a per-example loop for both the dynamic and static forms. It covers:

  • src batched on axis 0, 1, and 2 with an unbatched update
  • only the update batched
  • both src and the update batched

The test fails before the fix and passes after.

test_vmap, test_autograd, test_ops, test_blas, test_compile, test_export_import, and the C++ tests all pass locally, and pre-commit is clean. My local build is CPU-only, so CI will cover Metal.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Used AI tools to help find the bug and draft the fix and test. I wrote this PR description myself.

@zcbenz
zcbenz force-pushed the fix-slice-update-vmap-unbatched-update branch from 558f0de to 9aafe07 Compare October 27, 2026 09:28
@zcbenz
zcbenz merged commit bdbde25 into ml-explore:main Oct 29, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants