-
Notifications
You must be signed in to change notification settings - Fork 386
Make FqnToConfig handle module swap configs #3492
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
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/3492
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit 264b3c2 with merge base f3342a0 ( BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
50b55f5 to
8bfc239
Compare
torchao/quantization/quant_api.py
Outdated
| module.to(device=device) | ||
| return | ||
| if isinstance(config, AOBaseConfig): | ||
| _fqn_to_config_replace(config, model, device=device) |
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.
this is hard to follow, would it be cleaner to use iteration instead of recursion here
| or ("_default" in config.fqn_to_config and _is_linear(module)) | ||
| ): | ||
| # this replaces inplace, so no need to reassign | ||
| _fqn_to_config_handler(module, module_fqn, config) |
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.
Maybe we can implement something like
def set_module_by_fqn(model, fqn, new_module):
# set model[fqn] to new_moduleso then we can keep it iterative? Only thing is that we have to get the modules in top-down order (this does appear to be how named_modules is implemented)
for module_fqn, module in model.named_modules():
if (
fqn_matches_fqn_config(module_fqn, config)
or _module_param_matches_fqn_config(module, module_fqn, config)
or ("_default" in config.fqn_to_config and _is_linear(module))
):
new_module = _fqn_to_config_handler(module, module_fqn, config)
# reassign module
set_module_by_fqn(model, fqn, new_module) 8bfc239 to
f8af9cd
Compare
**Summary:** `FqnToConfig` does not handle module swap configs like `QATConfig` today because it does not reassign the replaced modules, but instead assumes there is no need to do so because the underlying tensors are swapped. This breaks when the user tries to use `FqnToConfig` with `QATConfig`, which does not rely on tensor subclasses. This commit fixes this by making the replacement logic in `FqnToConfig` more general. Fixes #3490. **Test Plan:** ``` python test/quantization/test_quant_api.py -k test_fqn_config_quantized_nested_module_module_swap ```
f8af9cd to
264b3c2
Compare
| child_name = module_fqn.split(".")[-1] | ||
| parent_fqn = module_fqn.removesuffix(child_name).removesuffix(".") | ||
| parent_module = named_modules[parent_fqn] | ||
| setattr(parent_module, child_name, replacement) |
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.
I think we're missing a return here at the end
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.
I changed the next case to elif, is that OK?
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.
oh missed that, yeah lgtm
|
Test failure doesn't seem related, merging this. Thanks |
Summary:
FqnToConfigdoes not handle module swap configs likeQATConfigtoday because it does not reassign the replaced modules, but instead assumes there is no need to do so because the underlying tensors are swapped. This breaks when the user tries to useFqnToConfigwithQATConfig, which does not rely on tensor subclasses. This commit fixes this by making the replacement logic inFqnToConfigmore general.Fixes #3490.
Test Plan: