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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DN6
Copy link

@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
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.

1 participant