Skip to content

Conversation

@justinchuby
Copy link
Collaborator

Adds ONNX export support for torch.sym_sum and torch.sym_ite symbolic operations.

Implementation

torch/onnx/_internal/exporter/_torchlib/ops/symops.py

  • sym_sum(args): N-ary addition using iterative ONNX Add operations. Handles empty sequences (returns 0) and single elements.
  • sym_ite(b, t, f): Conditional selection using ONNX Where operation.

test/onnx/exporter/test_small_models_e2e.py

  • Test cases for both operations using dynamic shapes to trigger symbolic tracing.

Both implementations follow reference patterns from torch/utils/_sympy/reference.py and use the standard @onnx_impl(trace_only=True) decorator pattern.

Example

class Model(torch.nn.Module):
    def forward(self, x):
        # Sum of all dimensions
        total = torch.sym_sum(x.shape)
        # Conditional: max of first two dimensions
        result = torch.sym_ite(x.shape[0] > x.shape[1], x.shape[0], x.shape[1])
        return result

# Exports to ONNX with Add and Where ops
onnx_program = torch.onnx.export(Model(), torch.zeros(2, 3), dynamic_shapes=...)

cc @justinchuby @titaiwangms

@pytorch-bot
Copy link

pytorch-bot bot commented Dec 12, 2025

🔗 Helpful Links

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

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

⏳ No Failures, 6 Pending

As of commit 4dd293b with merge base ca98449 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot bot added the release notes: onnx torch.onnx related changes that should show up in the release notes label Dec 12, 2025
@justinchuby justinchuby added module: onnx Related to torch.onnx ciflow/trunk Trigger trunk jobs on your pull request topic: improvements topic category labels Dec 12, 2025
@justinchuby
Copy link
Collaborator Author

@pytorchbot merge -i

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged while ignoring the following 3 checks: pull / linux-jammy-cuda12.8-cudnn9-py3.10-clang12 / build, trunk / libtorch-linux-jammy-cuda12.8-py3.10-gcc11-debug / build, trunk / linux-jammy-cuda12.8-py3.10-gcc11-no-ops / build

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

@justinchuby
Copy link
Collaborator Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@justinchuby
Copy link
Collaborator Author

@pytorchbot merge -f "All related tests passed"

@pytorchmergebot
Copy link
Collaborator

The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command
For more information see pytorch-bot wiki.

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@justinchuby justinchuby deleted the copilot/implement-sym-sum-sym-ite branch December 12, 2025 07:04
vishalgoyal316 pushed a commit to vishalgoyal316/pytorch that referenced this pull request Dec 17, 2025
Adds ONNX export support for `torch.sym_sum` and `torch.sym_ite` symbolic operations.

## Implementation

**torch/onnx/_internal/exporter/_torchlib/ops/symops.py**
- `sym_sum(args)`: N-ary addition using iterative ONNX Add operations. Handles empty sequences (returns 0) and single elements.
- `sym_ite(b, t, f)`: Conditional selection using ONNX Where operation.

**test/onnx/exporter/test_small_models_e2e.py**
- Test cases for both operations using dynamic shapes to trigger symbolic tracing.

Both implementations follow reference patterns from `torch/utils/_sympy/reference.py` and use the standard `@onnx_impl(trace_only=True)` decorator pattern.

## Example

```python
class Model(torch.nn.Module):
    def forward(self, x):
        # Sum of all dimensions
        total = torch.sym_sum(x.shape)
        # Conditional: max of first two dimensions
        result = torch.sym_ite(x.shape[0] > x.shape[1], x.shape[0], x.shape[1])
        return result

# Exports to ONNX with Add and Where ops
onnx_program = torch.onnx.export(Model(), torch.zeros(2, 3), dynamic_shapes=...)
```

Pull Request resolved: pytorch#170263
Approved by: https://github.com/titaiwangms

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged module: onnx Related to torch.onnx open source release notes: onnx torch.onnx related changes that should show up in the release notes topic: improvements topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants