Skip to content

Turn constant folder and dce into passes #2109

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

Merged
merged 5 commits into from
Mar 20, 2025
Merged

Conversation

justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented Mar 17, 2025

Turn constant folder and dce into passes to allow them to be used as individual passes in the future.

@gramalingam

Copy link
Contributor

@Copilot Copilot AI left a 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 refactors the optimizer by turning the constant folding and dead code elimination logic into individual passes. Key changes include:

  • Renaming and refactoring the unused nodes removal functionality into a new RemoveUnusedNodesPass class.
  • Replacing the old ConstantFolder class with a new FoldConstantsPass class that conforms to the PassBase API.
  • Updating related call sites and ensuring modification tracking is consistently applied.

Reviewed Changes

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

File Description
onnxscript/optimizer/_remove_unused.py Renames an internal function and replaces a standalone function with a class.
onnxscript/optimizer/_constant_folding.py Refactors constant folding into a pass-based API and updates modification logic.
Comments suppressed due to low confidence (2)

onnxscript/optimizer/_remove_unused.py:58

  • [nitpick] The renaming of 'process_function_or_graph' to '_process_function_or_graph' clarifies its internal usage. Consider adding a brief docstring to summarize its behavior for future maintainers.
def _process_function_or_graph(function_or_graph: ir.Function | ir.Graph) -> int:

onnxscript/optimizer/_constant_folding.py:1016

  • Ensuring the modification flag is set after node replacement is crucial; verify that similar modifications in other code paths consistently update this flag.
self.modified = True

Copy link

codecov bot commented Mar 17, 2025

❌ 11 Tests Failed:

Tests completed Failed Passed Skipped
14767 11 14756 2221
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0032_test_and_bcast3v2d
Stack Traces | 0.003s run time
onnxscript\backend\onnx_export_test.py:137: in extract_functions
    mod = importlib.import_module(import_name)
C:\hostedtoolcache\windows\Python\3.11.9\x64\Lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named 'tests.onnx_backend_test_code.test_and_bcast3v2d'

The above exception was the direct cause of the following exception:
.nox\test_onnx_weekly\Lib\site-packages\parameterized\parameterized.py:620: in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
onnxscript\backend\onnx_export_test.py:271: in test_export2python_produces_correct_onnx_script_model
    functions = extract_functions(backend_test.name, code, self.test_folder)
onnxscript\backend\onnx_export_test.py:139: in extract_functions
    raise AssertionError(
E   AssertionError: Unable to import 'tests.onnx_backend_test_code.test_and_bcast3v2d' (e=No module named 'tests.onnx_backend_test_code.test_and_bcast3v2d') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_and_bcast3v2d.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_and_bcast3v2d.py', current folder: D:\a\onnxscript\onnxscript
E   ---- CONTENT --
E   import numpy
E   from onnx import TensorProto
E   from onnx.helper import make_tensor
E   from onnxscript import script, external_tensor
E   from onnxscript.values import Opset
E   from onnxscript.onnx_types import BOOL
E   from onnxscript.onnx_opset import opset7
E   
E   @script()
E   def bck_test_and_bcast3v2d(x: BOOL[3,4,5], y: BOOL[4,5]) -> (BOOL[3,4,5]):
E       r_and = opset7.And(x, y)
E       return r_and
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_1032_test_scatter_without_axis
Stack Traces | 0.003s run time
onnxscript\backend\onnx_export_test.py:137: in extract_functions
    mod = importlib.import_module(import_name)
C:\hostedtoolcache\windows\Python\3.11.9\x64\Lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named 'tests.onnx_backend_test_code.test_scatter_without_axis'

The above exception was the direct cause of the following exception:
.nox\test_onnx_weekly\Lib\site-packages\parameterized\parameterized.py:620: in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
onnxscript\backend\onnx_export_test.py:271: in test_export2python_produces_correct_onnx_script_model
    functions = extract_functions(backend_test.name, code, self.test_folder)
onnxscript\backend\onnx_export_test.py:139: in extract_functions
    raise AssertionError(
E   AssertionError: Unable to import 'tests.onnx_backend_test_code.test_scatter_without_axis' (e=No module named 'tests.onnx_backend_test_code.test_scatter_without_axis') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_scatter_without_axis.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_scatter_without_axis.py', current folder: D:\a\onnxscript\onnxscript
E   ---- CONTENT --
E   import numpy
E   from onnx import TensorProto
E   from onnx.helper import make_tensor
E   from onnxscript import script, external_tensor
E   from onnxscript.values import Opset
E   from onnxscript.onnx_types import FLOAT, INT64
E   from onnxscript.onnx_opset import opset10
E   
E   @script()
E   def bck_test_scatter_without_axis(data: FLOAT[3,3], indices: INT64[2,3], updates: FLOAT[2,3]) -> (FLOAT[3,3]):
E       y = opset10.Scatter(data, indices, updates)
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_1171_test_softmax_large_number
Stack Traces | 0.003s run time
onnxscript\backend\onnx_export_test.py:137: in extract_functions
    mod = importlib.import_module(import_name)
C:\hostedtoolcache\windows\Python\3.11.9\x64\Lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named 'tests.onnx_backend_test_code.test_softmax_large_number'

The above exception was the direct cause of the following exception:
.nox\test_onnx_weekly\Lib\site-packages\parameterized\parameterized.py:620: in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
onnxscript\backend\onnx_export_test.py:271: in test_export2python_produces_correct_onnx_script_model
    functions = extract_functions(backend_test.name, code, self.test_folder)
onnxscript\backend\onnx_export_test.py:139: in extract_functions
    raise AssertionError(
E   AssertionError: Unable to import 'tests.onnx_backend_test_code.test_softmax_large_number' (e=No module named 'tests.onnx_backend_test_code.test_softmax_large_number') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_softmax_large_number.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_softmax_large_number.py', current folder: D:\a\onnxscript\onnxscript
E   ---- CONTENT --
E   import numpy
E   from onnx import TensorProto
E   from onnx.helper import make_tensor
E   from onnxscript import script, external_tensor
E   from onnxscript.values import Opset
E   from onnxscript.onnx_types import FLOAT
E   from onnxscript.onnx_opset import opset13
E   
E   @script()
E   def bck_test_softmax_large_number(x: FLOAT[2,4]) -> (FLOAT[2,4]):
E       y = opset13.Softmax(x)
E       return y

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Copy link
Collaborator

@gramalingam gramalingam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine, thanks.

What would be interesting (but is not clear yet to me) is how we use passes effectively and whether that requires other abstractions. For example, I see that the PassManager allows execution of a sequence of passes (iteratively). Would we need the ability to turn a sequence of passes (may be even an iteration N times) into a pass (which can itself then be used as one pass among another sequence of passes)? Essentially a "Sequence of Passes" as a "Pass"?

Of course, that leads to other ways of composing passes (eg. "pass1 OR pass2": in ORT fusion context, there is the possibility that we try GQA fusion only if MHA fusion fails). Of course, this may end up encoding a full programming-language within passes ... but that may be a bit too much right now, and it seems easier to use Python to express compositions for right now.

@justinchuby
Copy link
Collaborator Author

Essentially a "Sequence of Passes" as a "Pass"?

Sure! PassManager itself can be used as a pass. Something that is specialized for that will work too.

@justinchuby justinchuby merged commit 3d8f64a into main Mar 20, 2025
25 of 29 checks passed
@justinchuby justinchuby deleted the justinchu/more-passes branch March 20, 2025 16:38
bmehta001 pushed a commit to bmehta001/onnxscript that referenced this pull request Apr 11, 2025
Turn constant folder and dce into passes to allow them to be used as
individual passes in the future.
bmehta001 pushed a commit to bmehta001/onnxscript that referenced this pull request Apr 11, 2025
Turn constant folder and dce into passes to allow them to be used as
individual passes in the future.
bmehta001 pushed a commit to bmehta001/onnxscript that referenced this pull request Apr 11, 2025
Turn constant folder and dce into passes to allow them to be used as
individual passes in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

2 participants