-
Couldn't load subscription status.
- Fork 87
Add support for traced if statements in onnxscript script #2644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2644 +/- ##
==========================================
+ Coverage 70.46% 70.47% +0.01%
==========================================
Files 224 224
Lines 26572 26684 +112
Branches 2637 2663 +26
==========================================
+ Hits 18723 18806 +83
- Misses 6928 6953 +25
- Partials 921 925 +4 ☔ View full report in Codecov by Sentry. |
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
There was a problem hiding this comment.
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 extends the onnxscript converter to support traced if-statements, where the condition is evaluated at script-definition time rather than runtime. This enables parametric script generation for creating pattern variations. The implementation introduces an AstAnalyzer class to encapsulate analysis information and avoid mutating AST nodes directly.
Key Changes:
- Introduces
AstAnalyzerclass to centralize AST analysis and store analysis results in dictionaries instead of AST node attributes - Adds support for constant if-condition detection when the condition is an outer-scope variable
- Updates if-statement translation to emit only the relevant branch when the condition is constant
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
onnxscript/converter.py |
Integrates AstAnalyzer into the converter and implements traced if-statement handling |
onnxscript/_internal/analysis.py |
Implements AstAnalyzer class with constant if-condition detection and refactored analysis methods |
onnxscript/_internal/analysis_test.py |
Updates tests to use AstAnalyzer and adds test coverage for constant if-conditions |
onnxscript/converter_test.py |
Adds integration test for traced if-statement functionality |
| last = node.body[-1] | ||
| self.results.append(last.live_out) # type: ignore | ||
| live_out = self.analyzer.live_out(last) | ||
| self.results.append(live_out) # type: ignore |
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type: ignore comment should include a specific error code (e.g., type: ignore[arg-type]) to make it clear what type checking issue is being suppressed.
| self.results.append(live_out) # type: ignore | |
| self.results.append(live_out) # type: ignore[assignment] |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Extend the onnxscript converter to handle conditionals that are evaluated at script-time (that is, in the style of trace-mode). This makes it easier to define parametric scripts that can be used to generate variations of a pattern: for example, like the many variations of the SDPA pattern test cases.
This supports just a very basic version, where the if-condition is an outer-scope variable, like below:
For such cases, the script will just include the then or else branch as appropriate, without generating an if-node.
Also: introduce an analyzer class to encapsulate analysis information, and avoid updates to AST node.
TODO: some simple extension may be useful (perhaps allow any expression in the if-condition that does not contain local variables).