Skip to content

[IR] Export all common passes in onnxscript.ir.passes.common #2270

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 9 commits into from
May 5, 2025

Conversation

justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented May 4, 2025

Summary

Expose all common passes in onnxscript.ir.passes.common. Original apis are retained and unchanged (so not bc breaking).

Rational

In actual usage, exposing the passes under onnxscript.ir.passes.common.[module_name] make the passes harder to be discovered. In pytorch, nn modules are exposed under the same namespace, torch.nn, making it easier for user to find and use them. We use the same idea and expose all common passes under onnxscript.ir.passes.common.

Before this change, users will need to do

from onnxscript.ir.passes.common import unused_removal

unused_removal.RemoveUnusedNodesPass()(model)

With this change, uses can now do

import onnxscript.ir.passes.common as common_passes

common_passes.RemoveUnusedNodesPass()(model)

without having to know the module name.

Updated documentation page

image

Copy link

codecov bot commented May 4, 2025

❌ 20 Tests Failed:

Tests completed Failed Passed Skipped
14648 20 14628 1697
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0753_test_or3d
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_or3d'

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_or3d' (e=No module named 'tests.onnx_backend_test_code.test_or3d') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_or3d.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_or3d.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_or3d(x: BOOL[3,4,5], y: BOOL[3,4,5]) -> (BOOL[3,4,5]):
E       r_or = opset7.Or(x, y)
E       return r_or
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_1184_test_squeeze_negative_axes
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_squeeze_negative_axes'

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_squeeze_negative_axes' (e=No module named 'tests.onnx_backend_test_code.test_squeeze_negative_axes') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_squeeze_negative_axes.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_squeeze_negative_axes.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 opset21
E   
E   @script()
E   def bck_test_squeeze_negative_axes(x: FLOAT[1,3,1,5], axes: INT64[1]) -> (FLOAT[1,3,5]):
E       y = opset21.Squeeze(x, axes)
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0926_test_reduce_sum_square_keepdims_random
Stack Traces | 0.004s 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_reduce_sum_square_keepdims_random'

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_reduce_sum_square_keepdims_random' (e=No module named 'tests.onnx_backend_test_code.test_reduce_sum_square_keepdims_random') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_reduce_sum_square_keepdims_random.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_reduce_sum_square_keepdims_random.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 opset18
E   
E   @script()
E   def bck_test_reduce_sum_square_keepdims_random(data: FLOAT[3,2,2], axes: INT64[1]) -> (FLOAT[3,1,2]):
E       reduced = opset18.ReduceSumSquare(data, axes, keepdims=1)
E       return reduced

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 requested a review from Copilot May 4, 2025 21:27
@justinchuby justinchuby added topic: documentation Improvements or additions to documentation topic: api module: IR Intermediate representation topic: passes labels May 4, 2025
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 exposes all common passes under the common namespace (onnxscript.ir.passes.common) so that users no longer need to import passes from nested modules.

  • Updated import statements throughout the codebase to reference the new common namespace.
  • Adjusted usage of pass classes in optimizer, version converter, and tests to follow the unified import convention.
  • Updated documentation to reflect the new API structure.

Reviewed Changes

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

Show a summary per file
File Description
tests/function_libs/torch_lib/ops_test_common.py Changed import to use the new common namespace.
onnxscript/version_converter/init.py Replaced deep imports with common namespace; introduced an error in an error message string.
onnxscript/optimizer/_optimizer.py Updated pass usage with new namespace.
onnxscript/optimizer/init.py Updated pass usage with new namespace.
onnxscript/ir/passes/common/onnx_checker.py Updated property annotations with Literal types.
onnxscript/ir/passes/common/inliner.py Added documentation for the InlinePass class.
onnxscript/ir/passes/common/clear_metadata_and_docstring.py Added docstring for the ClearMetadataAndDocStringPass.
onnxscript/ir/passes/common/init.py Reorganized exports and removed module-setting logic.
docs/ir/ir_api/ir_passes_common.md Updated documentation to use automodule.

@justinchuby justinchuby added the merge at lgtm Reviewers can merge when they approve label May 4, 2025
@github-project-automation github-project-automation bot moved this from Todo to Done in ONNX Script Review Board May 5, 2025
@titaiwangms titaiwangms merged commit 3bd6a79 into main May 5, 2025
26 of 29 checks passed
@titaiwangms titaiwangms deleted the justinchu/export-common branch May 5, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge at lgtm Reviewers can merge when they approve module: IR Intermediate representation topic: api topic: documentation Improvements or additions to documentation topic: passes
Projects
Development

Successfully merging this pull request may close these issues.

3 participants