Skip to content

Fix PruneEmptyTensorsPass crash on data-dependent shapes - #20809

Merged
psiddh merged 1 commit into
pytorch:mainfrom
psiddh:fix-prune-empty-tensors-unbacked-symint
Jul 9, 2026
Merged

Fix PruneEmptyTensorsPass crash on data-dependent shapes#20809
psiddh merged 1 commit into
pytorch:mainfrom
psiddh:fix-prune-empty-tensors-unbacked-symint

Conversation

@psiddh

@psiddh psiddh commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

PruneEmptyTensorsPass crashes during export when cat inputs have data-dependent shapes (e.g. from conservatively keep the tensor. This is safe because the pass is purely an optimization; keeping an extra tensor in cat is a no-op at runtime.

This removes the need for HuggingFace's _patch_remove_empty_tensors_from_cat monkey-patch in their ExecuTorch exporter:(https://github.com/huggingface/transformers/blob/main/src/transformers/exporters/exporter_executorch.py#L456-L485).

Test plan

Existing tests pass: test_empty_tensor_removed_from_cat, test_cat_removed_all_empty. New test test_unbacked_symint_numel_does_not_crash exports a model using torch.nonzero (produces unbacked SymInt shapes) and verifies the pass completes without crashing

Verified guard_or_true returns True for undecidable expressions and correct concrete values for decidable ones

python -m pytest exir/tests/test_prune_empty_tensors_pass.py -v

@psiddh
psiddh requested a review from manuelcandales as a code owner July 9, 2026 17:34
Copilot AI review requested due to automatic review settings July 9, 2026 17:34
@pytorch-bot

pytorch-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ 1 Pending, 1 Unrelated Failure

As of commit 68e9ca2 with merge base f4b01a8 (image):

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

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

  • pull / android / build-android (gh) (trunk failure)
    ImportError: /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages/executorch/extension/training/pybindings/_training_lib.cpython-311-x86_64-linux-gnu.so: undefined symbol: _ZN3c104impl3cow23materialize_cow_storageERNS_11StorageImplE

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 Jul 9, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 9, 2026

Copy link
Copy Markdown

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

  • ✅ login: psiddh / name: Siddartha Pothapragada (6cb8730)

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@psiddh
psiddh force-pushed the fix-prune-empty-tensors-unbacked-symint branch from e06e55a to 3959dcd Compare July 9, 2026 17:35

Copilot AI left a comment

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.

Pull request overview

This PR aims to prevent PruneEmptyTensorsPass from crashing when it encounters data-dependent (unbacked) symbolic shapes (e.g., shapes arising from nonzero/MoE routing), and adds end-to-end support for emitting/evaluating torch.sym_ite via an ExecuTorch prim op.

Changes:

  • Update PruneEmptyTensorsPass to handle symbolic numel() comparisons without crashing on unbacked symbols.
  • Add executorch_prim::sym_ite.Scalar registration + runtime kernel, and register the corresponding EXIR prim op mapping.
  • Add C++ and Python tests covering sym_ite registration/runtime behavior and emit-to-ExecuTorch correctness.

Reviewed changes

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

Show a summary per file
File Description
kernels/prim_ops/test/prim_ops_test.cpp Adds registration and runtime-value tests for executorch_prim::sym_ite.Scalar.
kernels/prim_ops/register_prim_ops.cpp Registers the executorch_prim::sym_ite.Scalar runtime kernel (selective-build gated).
exir/tests/test_prune_empty_tensors_pass.py Adds a regression test for unbacked/data-dependent shapes in PruneEmptyTensorsPass.
exir/passes/prune_empty_tensors_pass.py Changes empty-tensor pruning condition to avoid crashes on symbolic numel().
exir/passes/executorch_prim_ops_registry.py Introduces EXIR backend op schema/binding and mapping for torch.sym_ite.
exir/emit/test/test_emit.py Adds an end-to-end emit/export test for torch.sym_ite with dynamic shapes.

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

Comment thread exir/passes/prune_empty_tensors_pass.py
Comment thread exir/passes/prune_empty_tensors_pass.py
Comment thread exir/tests/test_prune_empty_tensors_pass.py Outdated
Copilot AI review requested due to automatic review settings July 9, 2026 17:40
@psiddh
psiddh force-pushed the fix-prune-empty-tensors-unbacked-symint branch from 3959dcd to 6cb8730 Compare July 9, 2026 17:42

Copilot AI left a comment

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.

Pull request overview

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

Comment thread exir/tests/test_prune_empty_tensors_pass.py Outdated
Copilot AI review requested due to automatic review settings July 9, 2026 17:44

Copilot AI left a comment

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.

Pull request overview

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

Comment thread exir/tests/test_prune_empty_tensors_pass.py Outdated
Copilot AI review requested due to automatic review settings July 9, 2026 17:48
@psiddh
psiddh force-pushed the fix-prune-empty-tensors-unbacked-symint branch from 6cb8730 to 75753c7 Compare July 9, 2026 17:48

Copilot AI left a comment

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.

Pull request overview

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

Comment thread exir/tests/test_prune_empty_tensors_pass.py
PruneEmptyTensorsPass crashes with GuardOnDataDependentSymNode when
cat inputs have unbacked dynamic shapes (e.g. from torch.nonzero in
MoE expert routing). The numel() check returns a SymBool that can't
be evaluated as a Python bool.

Use guard_or_true to conservatively keep tensors when emptiness can't
be statically determined — this pass is purely an optimization, so
keeping a potentially-empty tensor is always safe.

This removes the need for HuggingFace's _patch_remove_empty_tensors_from_cat
monkey-patch in their ExecuTorch exporter.
Copilot AI review requested due to automatic review settings July 9, 2026 18:04
@psiddh
psiddh force-pushed the fix-prune-empty-tensors-unbacked-symint branch from 75753c7 to 68e9ca2 Compare July 9, 2026 18:04

Copilot AI left a comment

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.

Pull request overview

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

Comment on lines +39 to 40
if guard_or_true(input_arg_tensor.numel() != 0):
pruned_concat_list.append(input_arg)
@psiddh
psiddh merged commit a588b26 into pytorch:main Jul 9, 2026
180 of 182 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants