Add MLX op handler for aten.isinf#18988
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/18988
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below:
|
This PR needs a
|
Implement isinf op handler for the MLX delegate backend. isinf(x) is decomposed as abs(x) == inf, using existing AbsNode and EqualNode which are already supported in the MLX graph schema. The handler also includes a corresponding test case with _inf_input_fn that generates inputs containing both positive and negative infinity. Fixes: pytorch#18922
441eca1 to
810bc80
Compare
|
@claude review |
|
Claude finished @nil-is-all's task in 1m 31s —— View job Reviewing PR #18988: Add MLX op handler for aten.isinf
Review SummaryThe decomposition approach ( Issues1. Formatting: The # Before (line 4129-4131 in original):
{"op_name": "isnan", ...},
# activations
{"op_name": "relu", ...},
# After (current):
{"op_name": "isnan", ...},
{"op_name": "isinf", ...}, # activations
{"op_name": "relu", ...},The {"op_name": "isnan", ...},
{"op_name": "isinf", ...},
# activations
{"op_name": "relu", ...},2. Hardcoded inf_slot = emit_lifted_constant(P, float('inf'), torch.float32)The inf constant is always created as 3. Minor: mask_pos = torch.rand(shape) > 0.8 # ~20% set to +inf
mask_neg = torch.rand(shape) > 0.9 # ~10% set to -inf (may overwrite +inf)
x[mask_pos] = float('inf')
x[mask_neg] = float('-inf')Since Looks Good
Issue (1) is the only one I'd consider a must-fix before merge. The |
| def fn(shape, dtype): | ||
| x = torch.randn(shape, dtype=dtype) | ||
| # Insert some inf values | ||
| mask_pos = torch.rand(shape) > 0.8 |
There was a problem hiding this comment.
Can we add nan cases as well?
|
Looks great @Jah-yee! Left a small nit and running CI now. |
|
Can you run the lintrunner? |
Address @nil-is-all review comments: 1. Move '# activations' section comment to its own line (was merged onto isinf entry) 2. Add clarifying comment explaining float32 inf constant is intentionally typed for EqualNode type promotion 3. Use non-overlapping masks in _inf_input_fn to avoid +inf being overwritten by -inf
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows. |
|
Thanks for the thorough review, @nil-is-all! 思考路径收到 3 条反馈,逐一分析:
修改方案Fix 1: 格式化Fix 2: dtype 注释# Create inf constant (float32; EqualNode handles type promotion to match input dtype)
inf_slot = emit_lifted_constant(P, float('inf'), torch.float32)Fix 3: 非重叠 mask# Before: mask_neg 覆盖部分 mask_pos
mask_pos = torch.rand(shape) > 0.8
mask_neg = torch.rand(shape) > 0.9 # may overwrite +inf
# After: 完全独立的 mask
mask_pos = torch.rand(shape) > 0.8
mask_neg = (~mask_pos) & (torch.rand(shape) > 0.9)Commit 已推送: Please re-review! |
|
@Jah-yee it looks like lintrunner is still failing in CI, e.g., Make sure you run the linter locally and then resubmit the PR with the linted code. |
|
@Jah-yee 请改一下这2个 linter |
Fixes UFMT format warnings:
- backends/mlx/ops.py: float('inf') -> float("inf")
- backends/mlx/test/test_ops.py: float('inf')/float('-inf') -> double quotes
|
|
Thanks for the suggestion on adding nan cases! Supporting |
Good day
Summary
This PR adds a decomposed MLX op handler for
aten.isinfto the pytorch/executorch project.Motivation
The MLX delegate converts PyTorch aten ops into MLX graph nodes during export. When an aten op has no handler, it falls back to CPU execution, breaking the GPU acceleration pipeline. Adding a handler for
aten.isinfenables it to run on Metal GPU via MLX.Implementation
The handler uses a decomposed approach:
This uses existing
AbsNodeandEqualNodewhich are already supported, avoiding the need for a dedicated MLX isinf op.Changes
_isinf_handlerfunction registered fortorch.ops.aten.isinf.defaultisinfto_UNARY_OP_TESTSwith standard test configurationTesting
The handler can be tested with:
Thank you for your attention. If there are any issues or suggestions, please leave a comment and I will address them promptly.
Warmly,
RoomWithOutRoof