Skip to content

Allow aten.masked_select in Core ATen exception list - #25111

Open
Roxen-AS wants to merge 2 commits into
pytorch:mainfrom
Roxen-AS:fix/masked-select-core-aten-opset
Open

Roxen-AS wants to merge 2 commits into
pytorch:mainfrom
Roxen-AS:fix/masked-select-core-aten-opset

Conversation

@Roxen-AS

@Roxen-AS Roxen-AS commented Nov 16, 2026 •

Copy link
Copy Markdown

Summary

Fixes #24764.

Models that call torch.masked_select export cleanly with torch.export, but to_edge previously rejected them with SpecViolationError ("Operator torch._ops.aten.masked_select.default is not in Core ATen opset") because aten.masked_select.default is not in Core ATen.

ExecuTorch already provides a portable kernel for masked_select.out (kernels/portable/cpu/op_masked_select.cpp). Adding torch.ops.aten.masked_select.default to _get_core_aten_ops_exception_list in exir/verification/verifier.py (analogous to _upsample_bicubic2d_aa, full_like, etc.) allows models using masked_select to lower to Edge IR and serialize to ExecuTorch out-of-the-box without requiring manual _core_aten_ops_exception_list configuration.

Test plan

  1. Added test_edge_verifier_masked_select in exir/verification/test/test_verifier.py.
  2. Ran verification unit tests:
    python -m unittest executorch.exir.verification.test.test_verifier
    All 5 tests pass.
  3. Verified both to_edge and to_edge_transform_and_lower plus serialization:
    import torch
    from executorch.exir import to_edge, to_edge_transform_and_lower
    
    class M(torch.nn.Module):
        def forward(self, x):
            return torch.masked_select(x, x > 0)
    
    ep = torch.export.export(M(), (torch.tensor([1.0, -1.0, 2.0]),))
    edge = to_edge(ep)
    et = edge.to_executorch()
  4. Ran linter and formatting checks:
    python -m flake8 --config=.flake8 exir/verification/verifier.py exir/verification/test/test_verifier.py
    python -m ufmt check exir/verification/verifier.py exir/verification/test/test_verifier.py

cc @JacobSzwejbka @angelayi @nil-is-all

@pytorch-bot

pytorch-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit be149f3 with merge base 799098d (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Nov 16, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

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

  • ✅ login: Roxen-AS / name: Roxen-AS (75db76d)

@Roxen-AS

Copy link
Copy Markdown
Author

@pytorchbot label "release notes: exir"

@pytorch-bot pytorch-bot Bot added the release notes: exir Changes to any dialects and passes on these dialects, such as memory planning label Nov 16, 2026
@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Nov 16, 2026
@nil-is-all nil-is-all added the module: exir Issues related to Export IR and the code under exir/ label Nov 17, 2026
@shoumikhin

Copy link
Copy Markdown
Contributor

@claude review effort=max

@claude

claude Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Claude finished @shoumikhin's task in 17m 53s —— View job


This PR makes aten.masked_select pass the Core ATen check by default, which needs a decision from the exir maintainers (a global default vs. the per-program opt-in), and the new test doesn't run in OSS CI or reach to_executorch().

Detailed review

PR Review: #25111

Summary

The PR adds torch.ops.aten.masked_select.default to the hard-coded list in _get_core_aten_ops_exception_list, so to_edge accepts masked_select without an explicit opt-in. It also adds a test that to_edge no longer raises. There are two problems:

  • Making this exemption global is a policy decision that needs a maintainer call.
  • The test isn't collected by OSS CI, and it stops before the stages where an op with a data-dependent output size can fail.

Python couldn't be run in this job, so nothing was executed. Every finding comes from reading the code.

API Design

  • exir/verification/verifier.py:147: this makes masked_select valid Edge IR for every caller by default. For non-core ops, the verifier's own guidance is to opt in per program.
    • The hard-coded list applies to every to_edge and to_edge_transform_and_lower run with IR validation (exir/program/_program.py:1509, :1449).
    • Through verifier.py:296-300 and :333-335 it also applies to every EXIREdgeDialectVerifier.
    • A caller can't remove an entry from this list except by turning off all checks with _check_ir_validity=False.
    • The rejection this removes is the one that tells users to pass EdgeCompileConfig(_core_aten_ops_exception_list=[...]) after confirming that their backend or runtime handles the op (verifier.py:186-189).
    • The list itself is annotated "These special ops should be removable easily" (verifier.py:158).
    • Adding to the default list is a decision for the exir maintainers, who are already cc'd.

Testing

  • exir/verification/test/test_verifier.py:139-150: the regression test never runs in OSS CI, and it stops before to_executorch(), which is where the PR claims a fix.
    • Not collected. pytest.ini:52 ignores this file. Its only Buck target (exir/verification/targets.bzl:63-64) isn't in .ci/scripts/unittest-buck2.sh. The green CI on this PR therefore doesn't include the new test.
    • Stops at to_edge. The PR says models now "serialize to ExecuTorch out-of-the-box", but the test never calls to_executorch() and never runs the program. Those are the steps where a data-dependent output size matters:
      • The default ConstraintBasedSymShapeEvalPass (exir/capture/_config.py:94-96) plans the output buffer from torch's value range for the unbacked size (exir/sym_util.py:190-193). exir has no bound specific to masked_select.
      • At runtime, the portable kernel resizes out within that capacity (kernels/portable/cpu/op_masked_select.cpp:67-72).
    • Weak assertions.
      • Lines 149-150 run EXIREdgeDialectVerifier again, but to_edge already runs it (exir/program/_program.py:1522-1528).
      • Nothing checks that masked_select is still in the Edge graph. If torch later decomposes the op or tags it core, the test still passes while line 147 becomes dead code.
    • Suggested fix.
      • Move the test to exir/program/test/test_program.py, next to test_edge_dialect_non_core_aten_ops (L991-1019). That file is collected and already uses _count_nodes (L1022) and _load_for_executorch_from_buffer.
      • Assert that the masked_select node survives, call .to_executorch(), and compare the output against eager.
      • Use inputs whose number of selected elements differs from the trace input, such as all-true and all-false masks.
    • Broadcasting case (low confidence). Also test a mask that broadcasts self to a larger shape, such as x of shape [3] with a [2, 3] mask. I believe torch's fake kernel bounds the unbacked size by self.numel() rather than by the broadcast numel. If so, this case plans 3 elements, and resize_tensor fails at runtime once more than 3 are selected. The torch source wasn't readable in this job, so I couldn't confirm this.
    • Fix this →

Recommendation

Needs Discussion

The exir maintainers need to decide whether masked_select belongs in the default exception list or should stay a per-program _core_aten_ops_exception_list opt-in. If it stays in the list, the test changes above should land before merge.

Fixes pytorch#24764.

Models that call torch.masked_select export cleanly with torch.export,
but to_edge rejected them with SpecViolationError because
aten.masked_select.default is not in the Core ATen opset.

ExecuTorch already provides a portable kernel for masked_select.out.
Adding torch.ops.aten.masked_select.default to the verifier's exception
list enables models using masked_select to lower to Edge IR and serialize
to ExecuTorch without requiring manual _core_aten_ops_exception_list configuration.

Test Plan:
Ran test_edge_dialect_masked_select in exir.program.test.test_program
verifying that masked_select is retained in Edge IR and serializes cleanly
to ExecuTorch with to_executorch().

Signed-off-by: Roxen-AS <[email protected]>
@Roxen-AS
Roxen-AS force-pushed the fix/masked-select-core-aten-opset branch from 75db76d to 8ad0377 Compare November 17, 2026 08:27
@Roxen-AS

Copy link
Copy Markdown
Author

Thanks for the thorough review! Updated the PR:

  • Moved the regression test to \exir/program/test/test_program.py\ (\ est_edge_dialect_masked_select) right next to \ est_edge_dialect_non_core_aten_ops, ensuring it is properly collected and executed in OSS CI.
  • Updated the test to verify that \exir_ops.edge.aten.masked_select.default\ is retained in the Edge dialect via _count_nodes, and verified end-to-end serialization with .to_executorch().
  • Reverted the changes in \exir/verification/test/test_verifier.py.

This branch has not been deployed

No deployments
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. community: contribution PRs coming from community (excluding hardware partners) module: exir Issues related to Export IR and the code under exir/ release notes: exir Changes to any dialects and passes on these dialects, such as memory planning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torch.masked_select fails to_edge ("not in Core ATen opset"), although the portable kernel exists

4 participants