Skip to content

[MLX]: Add trunc op handler for mlx backend - #22139

Open
bharath-123 wants to merge 2 commits into
pytorch:mainfrom
bharath-123:add-mlx-trunc-handler
Open

[MLX]: Add trunc op handler for mlx backend#22139
bharath-123 wants to merge 2 commits into
pytorch:mainfrom
bharath-123:add-mlx-trunc-handler

Conversation

@bharath-123

@bharath-123 bharath-123 commented Aug 25, 2026

Copy link
Copy Markdown

Summary

Add trunc op handler for the mlx backend. The issue suggested to implement the handler by decomposing it with a floor and ceil op. But I instead chose to implement it via the trunc backend method already available in MLX C++ ops.

Fixes #18923

Test plan

I added a Unary op test and ran the test with:

python3 -m executorch.backends.mlx.test.run_all_tests --rebuild trunc

I have an M3 pro Mac so the MLX backend tests ran with my installed MLX libraries.

@pytorch-bot

pytorch-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 9030882 with merge base 81969a9 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@meta-cla

meta-cla Bot commented Aug 25, 2026

Copy link
Copy Markdown

Hi @bharath-123!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 25, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: bharath-123 / name: Bharath Vedartham (46227ca)

@bharath-123

Copy link
Copy Markdown
Author

@pytorchbot label "release notes: mlx"

@pytorch-bot pytorch-bot Bot added the release notes: mlx Changes to the MLX Backend: Metal-accelerated inference on Apple Silicon label Aug 25, 2026
@meta-cla

meta-cla Bot commented Aug 25, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@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 Aug 25, 2026
@bharath-123
bharath-123 force-pushed the add-mlx-trunc-handler branch from 46227ca to bcf449c Compare August 25, 2026 09:00
@bharath-123
bharath-123 force-pushed the add-mlx-trunc-handler branch from bcf449c to b5bb8fd Compare August 25, 2026 15:06
@metascroy

Copy link
Copy Markdown
Contributor

@claude review this PR

@claude

claude Bot commented Aug 25, 2026

Copy link
Copy Markdown

Claude finished @metascroy's task in 2m 3s —— View job


PR Review: Add trunc op handler for MLX backend

  • Read changed files and gather context
  • Verify schema / node-class wiring
  • Check op registration & interpreter correctness
  • Post review feedback

Overall this is a clean, well-scoped change that follows the existing patterns for rounding ops (floor/ceil) exactly. Using MLX's native trunc instead of decomposing into floor/ceil is the right call — it's simpler and avoids sign-handling edge cases. Nice work. ✅

Correctness — looks good

  • schema.fbs: TruncNode is defined with the same x/out shape as NegNode/CeilNode, and is appended to the end of the OpNode union (respecting the // BC: Add new op nodes here (append only) contract). ✅
  • MLXInterpreter.h: exec_trunc and the OpCode::TRUNC dispatch case mirror exec_ceil correctly. ✅
  • ops.py: TruncNode import + registration in _UNARY_OPS next to floor/ceil is consistent. ✅
  • test_ops.py: unary test added with scale=10 inputs, matching floor/ceil. ✅

Minor issue — likely lint failure

In backends/mlx/ops.py, the diff removes a blank line before three module-level statements, collapsing the PEP8-standard two blank lines down to one after the preceding for loops:

  • backends/mlx/ops.py:840 (before _SCALAR_INT_OPS)
  • backends/mlx/ops.py:940 (before _FULL_OPS)
  • backends/mlx/ops.py:990 (before _FULL_LIKE_OPS)

These appear unrelated to the trunc change and will likely trip the Lint workflow (E305: expected 2 blank lines after ...). Recommend restoring the blank lines so the diff stays minimal and lint passes.

Fix this →

Questions / suggestions (non-blocking)

  1. Generated files not committed — per backends/mlx/serialization/README.md, schema.fbs is the source of truth and generate.py regenerates mlx_graph_schema.py, MLXLoader.h/.cpp, schema_generated.h, etc. Those are gitignored (per backends/mlx/.gitignore), so nothing to commit — but please confirm you ran python backends/mlx/serialization/generate.py locally so the TruncNode dataclass and C++ loader entries exist for the build/tests. The manual MLXInterpreter.h edit depends on the generated TruncNode struct + OpCode::TRUNC.
  2. Test coverage of the sign behavior — since the main correctness concern with trunc vs floor/ceil is negative values (trunc(-1.5) == -1 vs floor == -2), it's worth confirming the _input_fn(scale=10) generates negatives (it should, given it matches floor/ceil). No change needed if the existing test already exercises both signs.

--- · branch add-mlx-trunc-handler

@bharath-123
bharath-123 force-pushed the add-mlx-trunc-handler branch from b5bb8fd to 9030882 Compare August 25, 2026 17:40
@bharath-123 bharath-123 changed the title Add trunc op handler for mlx backend [MLX]: Add trunc op handler for mlx backend Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/mlx CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: mlx Changes to the MLX Backend: Metal-accelerated inference on Apple Silicon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants