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

@IshanG97 IshanG97 commented Apr 22, 2026

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.

Flat roll (dims=[]) raises NotImplementedError for now; no known consumer needs it yet.

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.

Fixes #18919.

Test plan

  • python backends/mlx/serialization/generate.py — regenerates cleanly with RollNode in all expected outputs.
  • lintrunner --skip MYPY --paths-cmd 'git diff --name-only upstream/main' — no issues.
  • End-to-end run_all_tests -k roll not run locally (no executorch build on this machine); relying on CI. Happy to push fixes if it finds anything.

@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.

⚠️ 12 Awaiting Approval

As of commit eb9cc01 with merge base 3be4546 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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.

@metascroy
Copy link
Copy Markdown
Contributor

Looks fantastic @IshanG97!

Let's see what CI says, but is there a reason you didn't run the test locally? Ideally, I'd like to see the output of the test in the PR summary :)

@metascroy
Copy link
Copy Markdown
Contributor

Ignore all of the test-mlx-llm result failures in CI, they have to do with an HF token issue on external contributions.

@IshanG97
Copy link
Copy Markdown
Author

IshanG97 commented May 1, 2026

Looks fantastic @IshanG97!

Let's see what CI says, but is there a reason you didn't run the test locally? Ideally, I'd like to see the output of the test in the PR summary :)

Hi @metascroy! Sorry for the late response, I didn't initially because I couldn't get the latest version of Xcode on my machine, I had to use a proxy (to be honest, I was relying on the CI to catch anything haha!). I've now downloaded Xcode and run the tests locally, see my trimmed output:

  $ python -m executorch.backends.mlx.test.run_all_tests roll -v

  ============================================================
  Running test: roll_shift(2)_dim(0)
  ============================================================

  Step 1: Generating test files...
  Exporting model to .../op_tests/roll_shift(2)_dim(0)/model.pte

  ============================================================
  EXPORTED PROGRAM (torch.export)
  ============================================================
  ExportedProgram:
      class GraphModule(torch.nn.Module):
          def forward(self, x: "f32[8]"):
              roll: "f32[8]" = torch.ops.aten.roll.default(x, [2], [0]);  x = None
              return (roll,)

  ============================================================
  EDGE PROGRAM (after decomposition)
  ============================================================
  ExportedProgram:
      class GraphModule(torch.nn.Module):
          def forward(self, x: "f32[8]"):
              lowered_module_0 = self.lowered_module_0
              executorch_call_delegate = torch.ops.higher_order.executorch_call_delegate(lowered_module_0, x);  lowered_module_0 = x = None
              getitem: "f32[8]" = executorch_call_delegate[0];  executorch_call_delegate = None
              return (getitem,)

  ======================================================================
  MLX Graph Summary
  ======================================================================
  Version: 1
  Constant tensors: 0
  Input tensors: 1
  Output tensors: 1
  Instruction chains: 1
  Main chain idx: 0

  Chain 0 (main) (1 instructions):
    [0] RollNode
        x: tid 0
        out: tid 1
        shift: [{'literal': 2}]
        axes: [0]

  Inputs:  tid 0
  Outputs: tid 1
  ======================================================================

  Step 2: Verifying MLX delegation...
    Expected MLX segments: 1
    Actual MLX segments:   1
  ✓ MLX delegation verified

  Step 3: Running C++ binary...
  C++ binary output: Loading model...
  Model loaded successfully
    Input 0: dtype=0, shape=[8]
  Executing forward...
  Execution succeeded, 1 outputs
    Output 0: dtype=0, shape=[8]
  OK

  Step 4: Comparing outputs...
  ✓ PASSED: All outputs match

  ============================================================
  Running test: roll_shift(1)_dim(0)        ... ✓ PASSED
  Running test: roll_shift(-2)_dim(1)       ... ✓ PASSED
  Running test: roll_shift(3)_dim(2)        ... ✓ PASSED
  Running test: roll_shift(1,2)_dim(0,2)    ... ✓ PASSED
  Running test: roll_shift(-1,-2,-3)_dim(0,1,2) ... ✓ PASSED
  Running test: roll_shift(2)_dim(-1)       ... ✓ PASSED
  ============================================================

  ============================================================
  TEST SUMMARY
  ============================================================
  Passed: 7
  Failed: 0
  ============================================================

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
@IshanG97 IshanG97 force-pushed the ishan/mlx-roll-18919 branch from b2f3bf0 to eb9cc01 Compare May 1, 2026 10:55
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

4 participants