-
Notifications
You must be signed in to change notification settings - Fork 74
[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 2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Rules to collapse Transpose nodes into initializers.""" | ||
|
||
from __future__ import annotations | ||
|
||
from onnxscript import ir | ||
from onnxscript.rewriter import _ir_utils as ir_utils | ||
from onnxscript.rewriter import pattern as orp | ||
|
||
import logging | ||
|
||
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: | ||
array = ir_utils.get_const_value(initializer) | ||
if array is None: | ||
# Do nothing | ||
logger.debug("Failed to obtain the initializer value. Do nothing") | ||
# TODO: Handle both when perms is None and when perms is not None | ||
return op.Transpose(initializer, perms) | ||
|
||
# TODO Obtain perms from the matched node | ||
return op.initializer(ir.tensor()) | ||
|
||
def check(self, context, initializer: ir.Value) -> orp.MatchResult: | ||
del context # Unused | ||
check_result = orp.MatchResult() | ||
if initializer.const_value is None: | ||
return check_result.fail("Value is not an initializer, const_value is None") | ||
if initializer.producer() is not None: | ||
return check_result.fail("Value is not an initializer, producer is not None") | ||
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.