Skip to content

Turn inliner into a pass and use it in rewriter & optimizer #2149

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
Apr 2, 2025

Conversation

justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented Mar 29, 2025

Use passes in optimizer and rewriter.

  1. By opting into using the pass infra early, we get the benefit of getting the additional features in pass infra w/o having to pay higher refactoring cost in the future. We will be able to add more sophisticated debug utilities/snapshot capabilities etc. to the passes.
  2. Since we are offering the pass infra to users, we can start validating it internally by using it here. If order altering becomes a valid use case we can expect users may need that and we can create appropriate facilities to support the usage.

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 inlining and rewriting functionality by converting them into passes that integrate into the pass manager, improving modularity and consistency in the optimizer pipeline.

  • Introduced RewritePass in rewriter to encapsulate pattern-based rewriting.
  • Updated the optimizer to use a sequential pass manager that includes InlinePass, FoldConstantsPass, RewritePass, and unused removal passes.
  • Refactored the inlining functionality by replacing _Inliner with InlinePass, which now returns a boolean flag indicating modifications.

Reviewed Changes

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

File Description
onnxscript/rewriter/init.py Added RewritePass and updated rewrite function to use pass manager.
onnxscript/optimizer/_optimizer.py Reorganized optimizer passes to integrate new InlinePass and RewritePass.
onnxscript/optimizer/_inliner.py Converted _Inliner to InlinePass with a revised state reset and return value.

Copy link

codecov bot commented Mar 29, 2025

❌ 35 Tests Failed:

Tests completed Failed Passed Skipped
16306 35 16271 3251
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0289_test_convinteger_without_padding
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_convinteger_without_padding'

The above exception was the direct cause of the following exception:
.nox\test_torch_nightly\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_convinteger_without_padding' (e=No module named 'tests.onnx_backend_test_code.test_convinteger_without_padding') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_convinteger_without_padding.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_convinteger_without_padding.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 INT32, UINT8
E   from onnxscript.onnx_opset import opset10
E   
E   @script()
E   def bck_test_convinteger_without_padding(x: UINT8[1,1,3,3], w: UINT8[1,1,2,2], x_zero_point: UINT8) -> (INT32[1,1,2,2]):
E       y = opset10.ConvInteger(x, w, x_zero_point)
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0630_test_max_float16
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_max_float16'

The above exception was the direct cause of the following exception:
.nox\test_ort_nightly\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_max_float16' (e=No module named 'tests.onnx_backend_test_code.test_max_float16') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_max_float16.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_max_float16.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 FLOAT16
E   from onnxscript.onnx_opset import opset13
E   
E   @script()
E   def bck_test_max_float16(data_0: FLOAT16[3], data_1: FLOAT16[3]) -> (FLOAT16[3]):
E       result = opset13.Max(data_0, data_1)
E       return result
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0681_test_mod_uint32
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_mod_uint32'

The above exception was the direct cause of the following exception:
.nox\test_torch_nightly\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_mod_uint32' (e=No module named 'tests.onnx_backend_test_code.test_mod_uint32') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_mod_uint32.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_mod_uint32.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 UINT32
E   from onnxscript.onnx_opset import opset13
E   
E   @script()
E   def bck_test_mod_uint32(x: UINT32[3], y: UINT32[3]) -> (UINT32[3]):
E       z = opset13.Mod(x, y)
E       return z

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

@justinchuby justinchuby changed the title Turn inliner into a pass and use it in rewriter Turn inliner into a pass and use it in rewriter&optimizer Mar 31, 2025
@justinchuby justinchuby changed the title Turn inliner into a pass and use it in rewriter&optimizer Turn inliner into a pass and use it in rewriter & optimizer Mar 31, 2025
@github-project-automation github-project-automation bot moved this from Todo to Done in ONNX Script Review Board Apr 2, 2025
@justinchuby justinchuby merged commit 2962a09 into main Apr 2, 2025
21 of 27 checks passed
@justinchuby justinchuby deleted the justinchu/inliner-pass branch April 2, 2025 17:16
print(f"Applied {count} of general pattern rewrite rules.")
unused_remover = ir.passes.PassManager(

rewrite_pass = ir.passes.PassManager(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is causing a regression when no rewrite-rules are specified. I think we should retain an if pattern_rewrite_rules condition and avoid the RewritePass if no rules are specified.

justinchuby added a commit that referenced this pull request Apr 9, 2025
In #2149 the logic for
skipping rewrite when no rules are provided was removed. This PR adds
the logic back and hardens input checks. Now if no rules are provided to
`rewrite()`, it will only run cleanup passes.
bmehta001 pushed a commit to bmehta001/onnxscript that referenced this pull request Apr 11, 2025
…t#2149)

Use passes in optimizer and rewriter.

1. By opting into using the pass infra early, we get the benefit of
getting the additional features in pass infra w/o having to pay higher
refactoring cost in the future. We will be able to add more
sophisticated debug utilities/snapshot capabilities etc. to the passes.
2. Since we are offering the pass infra to users, we can start
validating it internally by using it here. If order altering becomes a
valid use case we can expect users may need that and we can create
appropriate facilities to support the usage.
bmehta001 pushed a commit to bmehta001/onnxscript that referenced this pull request Apr 11, 2025
…t#2149)

Use passes in optimizer and rewriter.

1. By opting into using the pass infra early, we get the benefit of
getting the additional features in pass infra w/o having to pay higher
refactoring cost in the future. We will be able to add more
sophisticated debug utilities/snapshot capabilities etc. to the passes.
2. Since we are offering the pass infra to users, we can start
validating it internally by using it here. If order altering becomes a
valid use case we can expect users may need that and we can create
appropriate facilities to support the usage.
bmehta001 pushed a commit to bmehta001/onnxscript that referenced this pull request Apr 11, 2025
…t#2149)

Use passes in optimizer and rewriter.

1. By opting into using the pass infra early, we get the benefit of
getting the additional features in pass infra w/o having to pay higher
refactoring cost in the future. We will be able to add more
sophisticated debug utilities/snapshot capabilities etc. to the passes.
2. Since we are offering the pass infra to users, we can start
validating it internally by using it here. If order altering becomes a
valid use case we can expect users may need that and we can create
appropriate facilities to support the usage.
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