Skip to content

MLX delegate: add integer support for aten.bitwise_not#19053

Merged
metascroy merged 5 commits intopytorch:mainfrom
AlessandroVacca:mlx-bitwise-not-int
Apr 25, 2026
Merged

MLX delegate: add integer support for aten.bitwise_not#19053
metascroy merged 5 commits intopytorch:mainfrom
AlessandroVacca:mlx-bitwise-not-int

Conversation

@AlessandroVacca
Copy link
Copy Markdown
Contributor

@AlessandroVacca AlessandroVacca commented Apr 22, 2026

Summary

Fixes #18924
Extends aten.bitwise_not support in the MLX delegate to handle integer tensors, not just boolean tensors.
Previously the handler only dispatched to LogicalNotNode for bool and raised NotImplementedError for all other dtypes. This adds a dedicated BitwiseInvertNode backed by mlx::core::bitwise_invert, and updates the handler to dispatch based on dtype:

  • boolLogicalNotNode (unchanged)
  • int32, int64BitwiseInvertNode

Changes:

  • serialization/schema.fbs: add BitwiseInvertNode table and append to OpNode union
  • runtime/MLXInterpreter.h: add exec_bitwise_invert() and dispatch case
  • ops.py: update _bitwise_not_handler to dispatch to BitwiseInvertNode for integers
  • test/test_ops.py: add bitwise_not_int test for int32 and int64

Test plan

All tests were ran on a machine with an Apple M1 Pro CPU, macOS 26.4.1.

  • python3 -m py_compile backends/mlx/ops.py backends/mlx/test/test_ops.py
  • python3 backends/mlx/serialization/generate.py
  • python3 -m executorch.backends.mlx.test.run_all_tests bitwise_not_int

Test output

============================================================
TEST SUMMARY
============================================================
Passed: 6
Failed: 0
============================================================

cc @metascroy

Copilot AI review requested due to automatic review settings April 22, 2026 20:59
@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/19053

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 14 New Failures, 15 Pending, 5 Unrelated Failures

As of commit bbe7c56 with merge base 56da964 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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
@AlessandroVacca
Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: apple"

@pytorch-bot pytorch-bot Bot added the release notes: apple Changes to the Apple backend delegate label Apr 22, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Extends the MLX delegate’s lowering for aten.bitwise_not to support integer tensors by introducing a new BitwiseInvertNode backed by MLX bitwise_invert, while keeping the existing boolean lowering via LogicalNotNode.

Changes:

  • Add BitwiseInvertNode to the FlatBuffers schema and OpNode union.
  • Add MLX runtime execution support (exec_bitwise_invert) and interpreter dispatch for the new opcode.
  • Update the Python op handler to emit BitwiseInvertNode for non-bool inputs and add a basic integer test case.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
backends/mlx/serialization/schema.fbs Adds BitwiseInvertNode table and appends it to the OpNode union for serialization.
backends/mlx/runtime/MLXInterpreter.h Implements exec_bitwise_invert() and adds interpreter dispatch for OpCode::BITWISE_INVERT.
backends/mlx/ops.py Updates aten.bitwise_not handler to emit BitwiseInvertNode for non-bool inputs.
backends/mlx/test/test_ops.py Adds a unary op test for integer torch.bitwise_not.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backends/mlx/ops.py
{"op_name": "abs", "op_fn": torch.abs},
{"op_name": "neg", "op_fn": torch.neg},
{"op_name": "logical_not","op_fn": torch.logical_not, "shapes": [(2, 3, 4), (10,), (4, 8)], "dtypes": [torch.bool], "input_fn": _bool_input_fn()},
{"op_name": "bitwise_not_int", "op_fn": torch.bitwise_not, "shapes": _SHAPES_3, "dtypes": [torch.int32, torch.int64], "input_fn": _int_input_fn()},
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consciously decided to test only what was asked to be added, lmk if you need also the bool test case

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test whatever dtypes you assert as supported in the handler.

Also, can you paste the outcome of successful test in the PR description?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the test summary in the description of the PR any good?

@mergennachin mergennachin requested a review from metascroy April 23, 2026 14:15
@nil-is-all
Copy link
Copy Markdown
Contributor

@claude review this PR

@nil-is-all nil-is-all added the module: mlx Issues related to MLX Backend: Metal-accelerated inference on Apple Silicon label Apr 23, 2026
@claude
Copy link
Copy Markdown

claude Bot commented Apr 23, 2026

Claude encountered an error —— View job

Command failed: git fetch origin --depth=20 pull/19053/head:mlx-bitwise-not-int-c777d55e

I'll analyze this and get back to you.

@metascroy
Copy link
Copy Markdown
Contributor

Overall looks great @AlessandroVacca! Just a few feedbacks, and then we can merge :)

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 23, 2026 16:26
Removed support for int8, int16, and uint8 data types.
@AlessandroVacca
Copy link
Copy Markdown
Contributor Author

AlessandroVacca commented Apr 23, 2026

Should be good now! @metascroy

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Extends the MLX delegate’s lowering for aten.bitwise_not beyond bool tensors by introducing a dedicated BitwiseInvertNode (backed by MLX bitwise_invert) and dispatching to it for supported integer dtypes.

Changes:

  • Added BitwiseInvertNode to the MLX FlatBuffers schema and appended it to the OpNode union.
  • Added MLX runtime execution support (exec_bitwise_invert) and interpreter dispatch for the new opcode.
  • Updated the Python lowering handler to dispatch: bool -> LogicalNotNode, supported integers -> BitwiseInvertNode, otherwise fallback.
  • Added a unary op test entry for integer torch.bitwise_not (currently int32/int64).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
backends/mlx/serialization/schema.fbs Defines BitwiseInvertNode and appends it to the OpNode union for serialization/runtime compatibility.
backends/mlx/runtime/MLXInterpreter.h Implements and dispatches execution for the new bitwise invert op in the MLX interpreter.
backends/mlx/ops.py Updates aten.bitwise_not lowering to route bool to logical-not and integers to bitwise-invert.
backends/mlx/test/test_ops.py Adds a new unary op test case for integer bitwise_not.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

{"op_name": "abs", "op_fn": torch.abs},
{"op_name": "neg", "op_fn": torch.neg},
{"op_name": "logical_not","op_fn": torch.logical_not, "shapes": [(2, 3, 4), (10,), (4, 8)], "dtypes": [torch.bool], "input_fn": _bool_input_fn()},
{"op_name": "bitwise_not_int", "op_fn": torch.bitwise_not, "shapes": _SHAPES_3, "dtypes": [torch.int32, torch.int64], "input_fn": _int_input_fn()},
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not advertised anymore whatsoever

Copilot AI review requested due to automatic review settings April 24, 2026 22:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review is ineligible. To be eligible to request a review, you need a paid Copilot license, or your organization must enable Copilot code review.

@metascroy metascroy merged commit 0a43e2f into pytorch:main Apr 25, 2026
170 of 192 checks passed
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. module: mlx Issues related to MLX Backend: Metal-accelerated inference on Apple Silicon release notes: apple Changes to the Apple backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Good First Issue: Add Full Integer Support for aten.bitwise_not

4 participants