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

Fix error when trying to access state_dict after activation quantization #371

Merged
merged 2 commits into from
Mar 5, 2025

Conversation

DN6
Copy link
Contributor

@DN6 DN6 commented Feb 7, 2025

What does this PR do?

Trying to access the model state dict after using activation quantization throws an unexpected error.

Traceback (most recent call last):
  File "/home/dhruv/diffusers/../scripts/create_dummy_flux.py", line 42, in <module>
    model.state_dict()
  File "/home/dhruv/miniconda3/envs/diffusers/lib/python3.10/site-packages/torch/nn/modules/module.py", line 2219, in state_dict
    module.state_dict(
  File "/home/dhruv/miniconda3/envs/diffusers/lib/python3.10/site-packages/torch/nn/modules/module.py", line 2219, in state_dict
    module.state_dict(
  File "/home/dhruv/miniconda3/envs/diffusers/lib/python3.10/site-packages/torch/nn/modules/module.py", line 2219, in state_dict
    module.state_dict(
  [Previous line repeated 1 more time]
  File "/home/dhruv/miniconda3/envs/diffusers/lib/python3.10/site-packages/torch/nn/modules/module.py", line 2216, in state_dict
    self._save_to_state_dict(destination, prefix, keep_vars)
  File "/home/dhruv/miniconda3/envs/diffusers/lib/python3.10/site-packages/optimum/quanto/nn/qmodule.py", line 150, in _save_to_state_dict
    destination[prefix + "weight"] = self.weight if keep_vars else self.weight.detach()
AttributeError: 'NoneType' object has no attribute 'detach'

This is because when replacing the LayerNorms in the model with QLayerNorm the weights are set to None, so this line will throw an error

destination[prefix + "weight"] = self.weight if keep_vars else self.weight.detach()

Snippet to test

import torch
from diffusers import FluxTransformer2DModel
from optimum.quanto import freeze, quantize, qint8, Calibration

torch_device = torch.device("cuda")


def get_dummy_inputs():
    return {
        "hidden_states": torch.randn(
            (1, 4096, 64), generator=torch.Generator("cpu").manual_seed(0)
        ).to(torch_device, torch.bfloat16),
        "encoder_hidden_states": torch.randn(
            (1, 512, 4096),
            generator=torch.Generator("cpu").manual_seed(0),
        ).to(torch_device, torch.bfloat16),
        "pooled_projections": torch.randn(
            (1, 768),
            generator=torch.Generator("cpu").manual_seed(0),
        ).to(torch_device, torch.bfloat16),
        "timestep": torch.tensor([1]).to(torch_device, torch.bfloat16),
        "img_ids": torch.randn(
            (4096, 3), generator=torch.Generator("cpu").manual_seed(0)
        ).to(torch_device, torch.bfloat16),
        "txt_ids": torch.randn(
            (512, 3), generator=torch.Generator("cpu").manual_seed(0)
        ).to(torch_device, torch.bfloat16),
        "guidance": torch.tensor([3.5]).to(torch_device, torch.bfloat16),
    }


model = FluxTransformer2DModel.from_pretrained(
    "hf-internal-testing/tiny-flux-transformer"
)
quantize(model, weights=qint8, activations=qint8)
model.to(torch_device)

with Calibration(momentum=0.9):
    model(**get_dummy_inputs())

freeze(model)
model.state_dict()

Fixes # (issue)

Before submitting

  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you run all tests locally and make sure they pass.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@DN6 DN6 requested a review from dacorvo as a code owner February 7, 2025 11:08
Copy link
Collaborator

@dacorvo dacorvo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull-request: can you squash your commits and put a meaningful commit message to allow the CI to run ?

git reset --soft HEAD~2
git commit
git push -f

Copy link

github-actions bot commented Mar 2, 2025

This PR is stale because it has been open 15 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Mar 2, 2025
@DN6 DN6 requested a review from dacorvo March 3, 2025 07:09
@dacorvo
Copy link
Collaborator

dacorvo commented Mar 3, 2025

The CI errors are fixed in #373 .

@github-actions github-actions bot removed the Stale label Mar 4, 2025
Copy link
Collaborator

@dacorvo dacorvo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks !

@dacorvo dacorvo merged commit 9af9869 into huggingface:main Mar 5, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants