Skip to content

[rewriter] Transpose initializer rule #2255

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

Closed
wants to merge 23 commits into from
Closed

Conversation

justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented Apr 30, 2025

Create a rule to absorb a Transpose node into the initializer (fold constant). This rule is in place to handle initializers of any size. So users do not need to change the size limit of the optimizer when constant folding.

TODO

  • tests

Fix #2158

Copy link

codecov bot commented Apr 30, 2025

❌ 6 Tests Failed:

Tests completed Failed Passed Skipped
6173 6 6167 3059
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0788_test_neg_example
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_neg_example'

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_neg_example' (e=No module named 'tests.onnx_backend_test_code.test_neg_example') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_neg_example.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_neg_example.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_neg_example(x: FLOAT[2]) -> (FLOAT[2]):
E       y = opset13.Neg(x)
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0493_test_gather_elements_1
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_gather_elements_1'

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_gather_elements_1' (e=No module named 'tests.onnx_backend_test_code.test_gather_elements_1') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_gather_elements_1.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_gather_elements_1.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 opset13
E   
E   @script()
E   def bck_test_gather_elements_1(data: FLOAT[3,3], indices: INT64[2,3]) -> (FLOAT[2,3]):
E       y = opset13.GatherElements(data, indices, axis=0)
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0769_test_pow_types_int32_int32
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_pow_types_int32_int32'

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_pow_types_int32_int32' (e=No module named 'tests.onnx_backend_test_code.test_pow_types_int32_int32') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_pow_types_int32_int32.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_pow_types_int32_int32.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
E   from onnxscript.onnx_opset import opset15
E   
E   @script()
E   def bck_test_pow_types_int32_int32(x: INT32[3], y: INT32[3]) -> (INT32[3]):
E       z = opset15.Pow(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 marked this pull request as ready for review April 30, 2025 18:57
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 adds a new rewrite rule to collapse Transpose nodes into initializers for constant folding, which removes the need to modify the optimizer’s size limit when folding constants.

  • Introduces the TransposeInitializer rule to handle initializer folding.
  • Implements the rewriting and checking logic to ensure the initializer meets usage requirements.
  • Registers the new rule in the rewriter’s initializer.

Reviewed Changes

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

File Description
onnxscript/rewriter/transpose_initializer.py Adds a new rewrite rule for folding Transpose nodes into initializers.
onnxscript/rewriter/init.py Registers the new transpose_initializer rule in the rule set.
Comments suppressed due to low confidence (1)

onnxscript/rewriter/transpose_initializer.py:28

  • [nitpick] Consider renaming 'original_transpose' to 'transpose_node' to more clearly indicate that it represents the Transpose node consumer of the initializer.
original_transpose = initializer.consumers()[0]

@justinchuby
Copy link
Collaborator Author

I think this is less efficient than an IR pass. Because it will match all transpose nodes, but we only need to look at users of the initializers. Thoughts? @titaiwangms @shubhambhokare1 @gramalingam

@justinchuby justinchuby marked this pull request as draft April 30, 2025 19:16
@justinchuby justinchuby marked this pull request as ready for review April 30, 2025 19:18
@justinchuby
Copy link
Collaborator Author

But this is op-specific. I don't think it fits in the common passes. So I prefer that it is a rewrite rule.

@titaiwangms titaiwangms requested a review from gramalingam April 30, 2025 20:01
Co-authored-by: Shubham Bhokare <32080845+shubhambhokare1@users.noreply.github.com>
@titaiwangms
Copy link
Contributor

I think this is less efficient than an IR pass. Because it will match all transpose nodes, but we only need to look at users of the initializers. Thoughts? @titaiwangms @shubhambhokare1 @gramalingam

With the check inside the rule, I assume it should not be too bad?

@titaiwangms
Copy link
Contributor

titaiwangms commented May 1, 2025

In the original issue, there are also transpose -> transpose and transpose -> MatMul/Gemm pattern matching. Should we have those in this rule as well? If we are going to go through Transpose nods anyway (I forget if we can check whether multiple rules are applicable within the same traverse.).

@justinchuby
Copy link
Collaborator Author

I think we should have different rules since they will match different nodes

@shubhambhokare1
Copy link
Contributor

Could we add

def test_transpose_transpose_onnxscript(self):
# TODO(rama): Attribute-parameters not yet supported in multi-output matching.
# def transpose_transpose_pattern(op, X, perm0, perm1):
# xt = op.Transpose(X, perm=perm0)
# Y = op.Transpose(xt, perm=perm1)
# return Y
def transpose_transpose_pattern(op, X):
XT = op.Transpose(X, _outputs=["XT"])
Y = op.Transpose(XT, _outputs=["Y"])
return Y
def transpose_transpose_mapping(perm0, perm1):
new_perm = [0 for p in perm0]
for i, p in enumerate(perm1):
new_perm[i] = perm0[p]
# replace by return [perm0[p] for p in perm1] ?
return new_perm
def transpose_transpose_check(op, **_) -> bool:
return True
def transpose_transpose_apply_pattern(op, X, XT: ir.Value, Y, **_):
perm0 = XT.producer().attributes.get("perm")
if perm0 is not None:
perm0 = perm0.value # TODO(rama): handle RefAttr
perm1 = Y.producer().attributes.get("perm")
if perm1 is not None:
perm1 = perm1.value # TODO(rama): handle RefAttr
if perm0 is None and perm1 is None:
return op.Identity(X)
if perm0 is None:
perm0 = range(len(perm1) - 1, -1, -1)
if perm1 is None:
perm1 = range(len(perm0) - 1, -1, -1)
composed_perm = transpose_transpose_mapping(perm0, perm1)
return op.Transpose(X, perm=composed_perm)
rule = pattern.RewriteRule(
transpose_transpose_pattern,
transpose_transpose_apply_pattern,
transpose_transpose_check,
self.matcher_algo,
verbose=0,

as part of this PR to upstream all the fusions done in https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/python/tools/transformers/fusion_constant_fold.py

@justinchuby
Copy link
Collaborator Author

@shubhambhokare1 I see it's already in

transpose_transpose_rule,
, was it not applied?

@shubhambhokare1
Copy link
Contributor

@shubhambhokare1 I see it's already in

transpose_transpose_rule,

, was it not applied?

Ah I see, however the one in https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/python/tools/transformers/fusion_constant_fold.py seems to be doing,
inp -> Transpose -> Transpose -> out
to
inp -> out if transpose perm values are equal

Whereas the current rewrite rule only replace two transposes with an equivalent single transpose

@justinchuby
Copy link
Collaborator Author

@shubhambhokare1 I see it's already in

transpose_transpose_rule,

, was it not applied?

Ah I see, however the one in microsoft/onnxruntime@main/onnxruntime/python/tools/transformers/fusion_constant_fold.py seems to be doing, inp -> Transpose -> Transpose -> out to inp -> out if transpose perm values are equal

Whereas the current rewrite rule only replace two transposes with an equivalent single transpose

Sounds good. I will update

@justinchuby
Copy link
Collaborator Author

@shubhambhokare1 does this one

or this one do it?

@justinchuby justinchuby changed the title [rewriter] Transpose rule [rewriter] Transpose initializer rule May 14, 2025
if new_value.const_value is None:
new_value.const_value = old_value.const_value
if new_value.name is None:
new_value.name = old_value.name
Copy link
Collaborator Author

@justinchuby justinchuby May 28, 2025

Choose a reason for hiding this comment

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

Maybe create an option? Retain new value shapes?

@justinchuby justinchuby mentioned this pull request May 28, 2025
9 tasks
if perm_attr is not None:
perm = perm_attr.as_ints()
else:
perm = None
Copy link
Member

Choose a reason for hiding this comment

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

Can we eliminate that case in def check?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When perm is None the transpose can still be evaluated. So I don't think it can be eliminated in check()?

@justinchuby
Copy link
Collaborator Author

Replaced by #2355

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.

[rewrite] Transpose initializer -> initializer (transposed)
4 participants