forked from quic/efficient-transformers
-
Notifications
You must be signed in to change notification settings - Fork 0
Split ONNX opsets for legacy and dynamo export #2
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
quic-amitraj
wants to merge
2
commits into
smedhe:smedhe_enable_dynamo_for_causallm
from
quic:opset-split-legacy-dynamo
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,72 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # | ||
| # Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import inspect | ||
| from collections.abc import Callable | ||
|
|
||
| import onnxscript | ||
|
|
||
| from QEfficient.utils import constants | ||
|
|
||
| _DYNAMO_FUNC_ATTR = "_qeff_dynamo_onnxscript_func" | ||
| _MISSING = object() | ||
|
|
||
|
|
||
| def _onnx_opset(opset_version: int) -> onnxscript.values.Opset: | ||
| return getattr(onnxscript, f"opset{opset_version}") | ||
|
|
||
|
|
||
| def _compile_with_default_opset( | ||
| fn: Callable, | ||
| custom_opset: onnxscript.values.Opset, | ||
| default_opset_version: int, | ||
| ): | ||
| """Compile an ONNXScript function with the requested default-domain opset. | ||
|
|
||
| Existing custom-op definitions refer to a module-level ``ops`` symbol. The | ||
| ONNXScript compiler captures module globals while the decorator runs, so we | ||
| temporarily point that symbol at the requested default opset and immediately | ||
| restore it after compilation. | ||
| """ | ||
| module = inspect.getmodule(fn) | ||
| if module is None: | ||
| raise RuntimeError(f"Unable to resolve module for ONNXScript function {fn.__name__}") | ||
|
|
||
| previous_ops = module.__dict__.get("ops", _MISSING) | ||
| module.__dict__["ops"] = _onnx_opset(default_opset_version) | ||
| try: | ||
| return onnxscript.script(custom_opset)(fn) | ||
| finally: | ||
| if previous_ops is _MISSING: | ||
| module.__dict__.pop("ops", None) | ||
| else: | ||
| module.__dict__["ops"] = previous_ops | ||
|
|
||
|
|
||
| def qeff_custom_op(domain: str, version: int): | ||
| """Compile one custom op body into legacy and dynamo ONNXScript variants.""" | ||
| custom_opset = onnxscript.values.Opset(domain, version) | ||
|
|
||
| def decorator(fn: Callable): | ||
| legacy_func = _compile_with_default_opset(fn, custom_opset, constants.ONNX_LEGACY_EXPORT_OPSET) | ||
| dynamo_func = _compile_with_default_opset(fn, custom_opset, constants.ONNX_DYNAMO_EXPORT_OPSET) | ||
| setattr(legacy_func, _DYNAMO_FUNC_ATTR, dynamo_func) | ||
| return legacy_func | ||
|
|
||
| return decorator | ||
|
|
||
|
|
||
| def get_dynamo_onnxscript_func(onnxscript_func): | ||
| """Return the dynamo/opset18 variant attached by ``qeff_custom_op``.""" | ||
| return getattr(onnxscript_func, _DYNAMO_FUNC_ATTR) | ||
|
|
||
|
|
||
| def get_onnxscript_func(onnxscript_func, onnx_export_opset: int): | ||
| """Return the ONNXScript variant matching the requested export opset.""" | ||
| if onnx_export_opset == constants.ONNX_DYNAMO_EXPORT_OPSET: | ||
| return get_dynamo_onnxscript_func(onnxscript_func) | ||
| return onnxscript_func | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment saying get rid of the decorator once, legacy export is deprecated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think is this the right way to do this?