-
Notifications
You must be signed in to change notification settings - Fork 197
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
Feat: Remove QOP Export #917
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
423cc64
removed qop export
costigt-dev e994f8e
remove qop reference in some tests
costigt-dev 5d37881
removed reference that was missed in notebook to qop
costigt-dev d1ae6a3
removing unused code
costigt-dev 91e6248
removed ExitStack context manager from export code as nothing is bein…
costigt-dev 1dff353
Merge branch 'dev' of github.com:Xilinx/brevitas into feat/remove_qop…
costigt-dev 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 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 |
---|---|---|
|
@@ -170,61 +170,12 @@ class BaseManager(ABC): | |
def export(cls, *args, **kwargs): | ||
return | ||
|
||
@classmethod | ||
def _gen_patches(cls, fn_dispatcher): | ||
patches = [] | ||
for fn in cls._fn_to_cache: | ||
dispatcher = partial(fn_dispatcher, fn) | ||
p = patch(torch.nn.functional, fn.__name__, dispatcher) | ||
patches.append(p) | ||
return patches | ||
|
||
@classmethod | ||
def _trace_patches(cls): | ||
patches = cls._gen_patches(cls._trace_fn_dispatcher) | ||
return patches | ||
|
||
@classmethod | ||
def _cache_patches(cls): | ||
return cls._gen_patches(cls._cache_fn_dispatcher) | ||
|
||
@classmethod | ||
def _restore_fn_patches(cls): | ||
return [patch(torch.nn.functional, fn.__name__, fn) for fn in cls._fn_to_cache] | ||
|
||
@classmethod | ||
def _trace_fn_dispatcher(cls, fn, input, *args, **kwargs): | ||
# baseline impl | ||
cls._fn_to_cache.pop(0) | ||
return fn(input, *args, **kwargs) | ||
|
||
@classmethod | ||
def _cache_fn_dispatcher(cls, fn, input, *args, **kwargs): | ||
with ExitStack() as stack: | ||
# disable recursing into this patch | ||
for mgr in cls._restore_fn_patches(): | ||
stack.enter_context(mgr) | ||
if isinstance(input, QuantTensor): | ||
inp_cache = None | ||
out_cache = None | ||
inp_cache = _CachedIO(input, metadata_only=True) | ||
output = fn(input, *args, **kwargs) | ||
if isinstance(output, QuantTensor): | ||
out_cache = _CachedIO(output, metadata_only=True) | ||
cached_io = (inp_cache, out_cache) | ||
if fn in cls._cached_io_handler_map: | ||
cached_io = cls._cached_io_handler_map[fn](cached_io) | ||
cls._fn_cache.append(cached_io) | ||
else: | ||
# could be a fn invoked within a quant module on a dequant tensor | ||
# or a function invoked on a float tensor. The former won't show | ||
# up during jit tracing as they are replaced by symbolic functions, | ||
# but the latter will, so we have to account for them in the _fn_cache | ||
output = fn(input, *args, **kwargs) | ||
if not config._IS_INSIDE_QUANT_LAYER: | ||
cls._fn_cache.append(None) | ||
return output | ||
|
||
@classmethod | ||
def handler_from_module(cls, module: Module, no_inheritance=False): | ||
for handler in cls.handlers: | ||
|
@@ -254,8 +205,6 @@ def _cache_inp_out(cls, module, *args, **kwargs): | |
module.apply(lambda m: _override_inp_caching_mode(m, enabled=True)) | ||
module.apply(lambda m: _override_out_caching_mode(m, enabled=True)) | ||
with ExitStack() as stack: | ||
for mgr in cls._cache_patches(): | ||
stack.enter_context(mgr) | ||
_ = module.forward(*args, **kwargs) | ||
# Restore previous caching properties | ||
module.apply(lambda m: _restore_quant_metadata_caching_mode(m)) | ||
|
@@ -280,8 +229,6 @@ def jit_inference_trace( | |
# force requires_grad to False to let the wrapped model lambda go through tracing | ||
requires_grad_backup_dict = _force_requires_grad_false(module) | ||
with ExitStack() as stack: | ||
for mgr in cls._trace_patches(): | ||
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. Same as before |
||
stack.enter_context(mgr) | ||
# wrapping with a lambda forces inlining during tracing, | ||
# converts everything to const and removes unused params/buffers | ||
traced_model = torch.jit.trace(_JitTraceExportWrapper(module), args) | ||
|
This file contains 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 |
---|---|---|
|
@@ -123,8 +123,6 @@ def export_onnx( | |
module.apply(lambda m: _override_inp_caching_mode(m, enabled=False)) | ||
# perform export pass | ||
with ExitStack() as stack: | ||
for mgr in cls._trace_patches(): | ||
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. Same as before, everything can be flattened one level down |
||
stack.enter_context(mgr) | ||
if export_path is not None: | ||
export_target = export_path | ||
else: | ||
|
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.
If we don't add anything to the stack, we can remove the context manager and leave the forward pass as part of the function