Skip to content
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

Importing quantized models after bias correction #827

Closed
i-colbert opened this issue Feb 6, 2024 · 0 comments · Fixed by #868
Closed

Importing quantized models after bias correction #827

i-colbert opened this issue Feb 6, 2024 · 0 comments · Fixed by #868
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@i-colbert
Copy link
Collaborator

When quantizing floating-point models that don't have a bias in their layer (e.g., nn.Linear(in_features=10, out_features=2, bias=False)), bias correction currently will add a bias to the layer. This leads to the new bias being exported with the state dictionary. However, when loading this modified state dictionary in a new instance of original model, there is a missing keys error from pytorch because there is no bias in the floating-point (or even quantized model) without first running bias correction.

The issue can be resolved by first running bias correction before loading the modified state dictionary (see below), but a more flexible solution may be to add support into the state dictionary loading mechanism itself.

def _prepare_bias_corrected_quant_model(model: nn.Module):
    model.eval()
    dtype = next(model.parameters()).dtype
    device = next(model.parameters()).device
    images = torch.randn(10, 3, 32, 32)
    images = images.to(device)
    images = images.to(dtype)
    with torch.no_grad():
        with bias_correction_mode(model):
            model(images)
@Giuseppe5 Giuseppe5 added the enhancement New feature or request label Feb 7, 2024
@costigt-dev costigt-dev self-assigned this Feb 7, 2024
@nickfraser nickfraser added the good first issue Good for newcomers label Feb 7, 2024
@capnramses capnramses linked a pull request Feb 21, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants