-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[quant][sparsity] Generic convert function #60728
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
Conversation
[ghstack-poisoned]
💊 CI failures summary and remediationsAs of commit 0b6ca79 (more details on the Dr. CI page and at hud.pytorch.org/pr/60728):
🕵️ 3 new failures recognized by patternsThe following CI failures do not appear to be due to upstream breakages:
|
Job | Step | Action |
---|---|---|
Ensure correct trailing newlines | 🔁 rerun | |
Fail if there were any warnings | 🔁 rerun |
Preview docs built from this PR
This comment was automatically generated by Dr. CI (expand for details).
Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions to the (internal) Dr. CI Users group.
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` [ghstack-poisoned]
return model | ||
|
||
|
||
def convert(model: nn.Module, mapping: dict = None, config: dict = None) -> nn.Module: |
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.
For eager mode, the API should only take the config for sparsity, the qconfig is an attribute of the module. The config is identical to the config arg that is passed to the prepare function of the sparsifier. This way, the arguments to convert do not need a joint sparsity+quantization config.
config = {
... 'seq.0.linear1': {
... 'zero_block_shape': (1, 4)
... 'seq.0.linear2: {
... 'zero_block_shape': (1, 4)
... }
... }
raise AttributeError('FP sparse convert is not yet supported') | ||
|
||
quant_config = config.get('quantized', dict()) | ||
for mode in config.keys(): |
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.
Lets do this instead. We can reuse the logic in swap_module for eager mode quantization and keep the configs for
sparsity and quantization separate
def convert(model: nn.Module, mapping: dict = None, config: dict = None, convert_custom_config_dict:dict=None) -> nn.Module:
if mapping is None:
mapping = dict()
if config is None:
raise AttributeError('Currently, you have to specify the convert config')
mapping.setdefault('quantized', torch.quantization.get_static_quant_module_class())
mapping.setdefault(
'sparse_quantized',
{
'static': {nn.Linear, torch.ao.nn.sparse.quantized.Linear},
'dynamic': {nn.Linear, torch.ao.nn.sparse.quantized.dynamic.Linear},
})
if 'sparse' in config:
raise AttributeError('FP sparse convert is not yet supported')
if convert_custom_config_dict is None:
convert_custom_config_dict = {}
custom_module_class_mapping = convert_custom_config_dict.get("observed_to_quantized_custom_module_class", {})
model = _joint_convert(model, mapping, config, custom_module_class_mapping)
def _joint_convert(model, mapping, config, custom_module_class_mapping):
reassign = {}
for name, mod in module.named_children():
# both fused modules and observed custom modules are
# swapped as one unit
if not isinstance(mod, _FusedModule) and \
type(mod) not in custom_module_class_mapping:
_joint_convert(mod, mapping, config)
# if mod has qconfig attribute and is present in sparsifier config:
if hasattr(mod, 'qconfig')
if _module_to_path(mod) in config:
mapping = mapping['sparse_quantized']
else:
mapping = mapping['quantized']
# custom module class mapping will not work with sparsity, for now we could set it to None.
torch.quantization.swap_module(mod, mapping, custom_module_class_mapping)
reassign[name] = swap_module(mod, mapping, custom_module_class_mapping)
for key, value in reassign.items():
module._modules[key] = value
return module
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.
Please see comments
@zafartahirov has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
@zafartahirov has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
@zafartahirov has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
@zafartahirov has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
@zafartahirov has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
@zafartahirov has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
This is a wrapper that implements a common convert function for use with quantization and sparsity. Currently, only the whole model/module conversion is supported. Test Plan: ``` python test/test_ao_sparsity.py ``` Differential Revision: [D29465899](https://our.internmc.facebook.com/intern/diff/D29465899) [ghstack-poisoned]
@zafartahirov has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Stack from ghstack:
This is a wrapper that implements a common convert function for use with quantization and sparsity.
Currently, only the whole model/module conversion is supported.
Test Plan:
Differential Revision: D29465899