Skip to content

feat(mlx): add handler for aten.roll#19038

Open
IshanG97 wants to merge 1 commit intopytorch:mainfrom
IshanG97:ishan/mlx-roll-18919
Open

feat(mlx): add handler for aten.roll#19038
IshanG97 wants to merge 1 commit intopytorch:mainfrom
IshanG97:ishan/mlx-roll-18919

Conversation

@IshanG97
Copy link
Copy Markdown

Summary

Adds an MLX delegate handler for aten.roll, mapping torch.roll onto mlx::core::roll via a new RollNode in the schema. Replaces the default decomposition (index_select + arange + cat) with a single native kernel — needed by Swin Transformer's shift-window attention.

Fixes #18919.

Changes

  • backends/mlx/serialization/schema.fbs: new RollNode { x, out, shift: [IntOrVid], axes: [int32] }, appended to the OpNode union (APPEND ONLY rule preserved).
  • backends/mlx/ops.py: custom _roll_handler for torch.ops.aten.roll.default. Normalises scalar-vs-list shifts/dims, validates length parity, and raises NotImplementedError for flat roll (dims=[]) since no known consumer needs it yet.
  • backends/mlx/runtime/MLXInterpreter.h: exec_roll using to_shape(n.shift, st) for the Shape conversion, plus the dispatch case.
  • backends/mlx/test/test_ops.py: RollTest covering 1D, 2D, 3D, multi-axis, negative shifts, and a negative dim.

Generated files (MLXLoader.*, schema_generated.h, mlx_graph_schema.py, _generated_serializers.py, _generated_inspector.py, _generated/) are regenerated from schema.fbs by backends/mlx/CMakeLists.txt at build time and are deliberately not committed.

Test plan

  • python backends/mlx/serialization/generate.py regenerates cleanly with RollNode appearing in all expected outputs.
  • lintrunner --skip MYPY --paths-cmd 'git diff --name-only upstream/main' — no issues.
  • Local run of python -m executorch.backends.mlx.test.run_all_tests -k roll is pending a full install_executorch.sh build on this machine; relying on CI for the end-to-end validation. Happy to push fixes if CI finds anything.

PR authored with Claude Code.

Maps torch.roll to mlx::core::roll via a new RollNode. Adds the schema
table, the custom handler for the (shifts, dims) args, the exec_roll
runtime, and test cases covering 1D, 2D, multi-axis, negative shifts,
and negative dims.

Flat roll (dims=[]) is explicitly NotImplementedError for now; all
known use cases (Swin Transformer shift-window attention) pass dims.

Fixes pytorch#18919

Authored-with: Claude <noreply@anthropic.com>
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Apr 22, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19038

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 22, 2026
@github-actions
Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Good First Issue: Add MLX Op Handler for aten.roll

1 participant