Skip to content

Add MLX hardtanh op handler#19776

Open
atharvjairath wants to merge 8 commits into
pytorch:mainfrom
atharvjairath:mlx-hardtanh-op-handler
Open

Add MLX hardtanh op handler#19776
atharvjairath wants to merge 8 commits into
pytorch:mainfrom
atharvjairath:mlx-hardtanh-op-handler

Conversation

@atharvjairath
Copy link
Copy Markdown

@atharvjairath atharvjairath commented May 26, 2026

Fixes #18921

Adds MLX delegate support for aten.hardtanh.default by lowering it to the existing ClipNode path with the operator's min_val and max_val bounds. This enables bounded activation models, including ReLU6-style hardtanh usage, to stay delegated to MLX instead of failing as an unsupported op.

This also adds focused MLX op tests for:

  • default hardtanh bounds [-1.0, 1.0]
  • ReLU6 bounds [0.0, 6.0]
  • symmetric custom bounds [-2.0, 2.0]
  • asymmetric custom bounds [-0.25, 0.75]

Test plan:

lintrunner backends/mlx/ops.py backends/mlx/test/test_ops.py
ok No lint issues.
CPLUS_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 python -m executorch.backends.mlx.test.run_all_tests --rebuild hardtanh
Rebuilding op_test_runner in /Users/atharvjairath/Desktop/timepass/executorch/cmake-out...
Build succeeded.

Running test: hardtanh_min-1.0_max1.0_2x3x4
✓ MLX delegation verified
C++ binary output: OK
✓ PASSED: All outputs match

Running test: hardtanh_min0.0_max6.0_4x8
✓ MLX delegation verified
C++ binary output: OK
✓ PASSED: All outputs match

Running test: hardtanh_min-2.0_max2.0_10
✓ MLX delegation verified
C++ binary output: OK
✓ PASSED: All outputs match

Running test: hardtanh_min-0.25_max0.75_2x8x16
✓ MLX delegation verified
C++ binary output: OK
✓ PASSED: All outputs match

TEST SUMMARY
Passed: 4
Failed: 0

This follows up on #18986 by adding custom min/max bound coverage and including the requested local test output.

cc @metascroy

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 26, 2026

🔗 Helpful Links

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

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

❌ 1 New Failure, 6 Pending

As of commit 542e9dc with merge base eeb0646 (image):

NEW FAILURE - The following job has failed:

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

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented May 26, 2026

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

  • ✅ login: atharvjairath / name: atharvjairath (19fa148)

@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 May 26, 2026
@atharvjairath
Copy link
Copy Markdown
Author

@pytorchbot label "release notes: apple"

@pytorch-bot pytorch-bot Bot added the release notes: apple Changes to the Apple backend delegate label May 26, 2026
@atharvjairath
Copy link
Copy Markdown
Author

cc: @nil-is-all

@digantdesai digantdesai requested a review from metascroy May 27, 2026 20:28
@atharvjairath
Copy link
Copy Markdown
Author

Friendly bump, PR is ready for review. Added MLX support for aten.hardtanh.default via the existing ClipNode path, with tests for default/custom bounds and ReLU6 usage.

Would appreciate a review when possible. cc @metascroy

@mergennachin
Copy link
Copy Markdown
Contributor

@claude Review this PR

@claude
Copy link
Copy Markdown

claude Bot commented Jun 1, 2026

Claude finished @mergennachin's task in 1m 37s —— View job


Review of PR #19776 — Add MLX hardtanh op handler

  • Read changed files and gather context
  • Review backends/mlx/ops.py changes
  • Review backends/mlx/test/test_ops.py changes
  • Post review feedback

Summary

Clean, well-structured PR that adds aten.hardtanh.default support to the MLX backend by lowering to the existing ClipNode. The implementation correctly mirrors the adjacent _clamp_handler pattern and the tests provide good coverage across bound configurations.

backends/mlx/ops.py

Looks good. The handler correctly:

  • Uses require_args / require_kwargs matching the established pattern
  • Defaults to [-1.0, 1.0] matching the ATen schema (Scalar min_val=-1, Scalar max_val=1)
  • Retrieves dtype from input tensor metadata for emit_lifted_constant
  • Emits a ClipNode with both bounds always present (valid since hardtanh always has finite bounds)

One minor observation: since the ATen schema for hardtanh.default always includes the min/max scalars as positional args (they have defaults but torch.export materializes them explicitly), the len(args) > 1 / len(args) > 2 fallback guards on lines 2937-2938 are defensive-only and will likely never be hit in practice. This is fine for robustness — just noting it's technically dead code under normal export paths.

backends/mlx/test/test_ops.py

Looks good. The HardtanhTest class:

  • Follows the same OpTestCase / @register_test pattern as neighboring tests (e.g., ClampTest)
  • Covers default bounds, ReLU6, symmetric, and asymmetric configurations
  • Uses * 4 scaling on random inputs to ensure values actually exceed bounds (good practice)
  • Tight tolerances (1e-5) are appropriate for a float32 clamp op

Verdict

Approve — this is a straightforward, correct addition that follows existing patterns. No issues found.


@nil-is-all nil-is-all added the module: mlx Issues related to MLX Backend: Metal-accelerated inference on Apple Silicon label Jun 1, 2026
@metascroy
Copy link
Copy Markdown
Contributor

Looks great! Let's see what CI says.

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 MLX Op Handler for aten.hardtanh

4 participants