-
Notifications
You must be signed in to change notification settings - Fork 72
[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
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0d8bece
[rewriter] Transpose rule
justinchuby 43b9f26
WIP
justinchuby c9e93e1
Implement the rule
justinchuby 37232d3
init
justinchuby 0c47226
attr
justinchuby f2d2ccf
new_name
justinchuby 278e977
Copyright
justinchuby d028482
typing
justinchuby 3c2369c
todo
justinchuby f271a7c
Update onnxscript/rewriter/transpose_initializer.py
justinchuby b39d763
Merge branch 'main' into justinchu/transpose-rule
justinchuby 19aaedb
update
justinchuby 4edb346
Merge branch 'main' into justinchu/transpose-rule
justinchuby f71773e
unregister_initializer
justinchuby 99fcd4e
name
justinchuby 6cc4f0e
Merge branch 'main' into justinchu/transpose-rule
justinchuby 41cb119
Fix value naming
justinchuby 951261d
Merge branch 'main' into justinchu/transpose-rule
justinchuby a15d495
Merge branch 'main' into justinchu/transpose-rule
justinchuby d78d98c
Merge branch 'main' into justinchu/transpose-rule
justinchuby 35a36b5
Merge branch 'main' into justinchu/transpose-rule
justinchuby 20d959c
Update value replacement
justinchuby bb69f28
address comments
justinchuby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
"""Rules to collapse Transpose nodes into initializers.""" | ||
|
||
|
||
from __future__ import annotations | ||
|
||
|
||
import logging | ||
|
||
import numpy as np | ||
|
||
from onnxscript import ir | ||
from onnxscript.rewriter import _ir_utils as ir_utils | ||
from onnxscript.rewriter import pattern as orp | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TransposeInitializer(orp.RewriteRuleClassBase): | ||
"""Folds Transpose nodes into initializers.""" | ||
|
||
def __init__(self): | ||
super().__init__("TransposeInitializer", remove_nodes=True) | ||
|
||
def pattern(self, op, initializer): | ||
return op.Transpose(initializer, _allow_other_attributes=True) | ||
|
||
def rewrite(self, op, initializer: ir.Value) -> ir.Value: | ||
original_transpose = initializer.consumers()[0] | ||
perm_attr = original_transpose.attributes.get("perm") | ||
|
||
if perm_attr is not None: | ||
perm = perm_attr.as_ints() | ||
else: | ||
perm = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we eliminate that case in def check? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()? |
||
|
||
array = ir_utils.get_numpy_value(initializer) | ||
if array is None: | ||
# Do nothing | ||
logger.debug("Failed to obtain the initializer value. Do nothing") | ||
# perm=None is filtered out when the attribute is constructed so we are ok | ||
return op.Transpose(initializer, perm=perm_attr) | ||
|
||
# np.transpose does not create a copy. So we don't need to use LazyTensors. | ||
transposed = np.transpose(array, axes=perm) | ||
new_name = f"{initializer.name}_transposed" | ||
return op.initializer(ir.tensor(transposed, name=new_name)) | ||
|
||
def check(self, context, initializer: ir.Value) -> orp.MatchResult: | ||
del context # Unused | ||
check_result = orp.MatchResult() | ||
if not initializer.is_initializer(): | ||
return check_result.fail("Value is not an initializer") | ||
if initializer.is_graph_input(): | ||
return check_result.fail("Value is a graph input") | ||
if initializer.const_value is None: | ||
return check_result.fail("Value.const_value is None") | ||
if len(initializer.uses()) != 1: | ||
return check_result.fail("Initializer is used by more than one node") | ||
# TODO(justinchuby): Avoid matching when it is a graph input | ||
return check_result | ||
|
||
|
||
rule = TransposeInitializer.rule() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.