Skip to content

Quantizer configs are mutated in place, leaking modules_to_not_convert across loads #15124

Open

Description

@OnePunchMonk

Describe the bug

BitsAndBytes (4-bit and 8-bit), TorchAO, Quanto, and ModelOpt quantizers each alias a modules_to_not_convert-style list directly from self.quantization_config and then call .extend() on it, instead of copying it first:

  • BitsAndBytes 4/8-bit: bnb_quantizer.py (_process_model_before_weight_loading)
  • TorchAO: torchao_quantizer.py (_process_model_before_weight_loading)
  • Quanto: quanto_quantizer.py (_process_model_before_weight_loading)
  • ModelOpt: modelopt_quantizer.py (_process_model_before_weight_loading), which also writes module entries into modelopt_config["quant_cfg"]

Reusing the same quantization_config object across two from_pretrained calls leaks state between loads — keep_in_fp32_modules and CPU/disk-offloaded module names accumulate on the shared config object across calls, and in PipelineQuantizationConfig the same quant_kwargs list / quant_mapping object is shared across components (including transformers text encoders), so the leak isn't confined to one component. The accumulated list also gets written into config.json on save.

Reproduction

import torch
from diffusers import BitsAndBytesConfig
from diffusers.quantizers.bitsandbytes.bnb_quantizer import BnB8BitDiffusersQuantizer

skip_modules = ["proj_out"]
config = BitsAndBytesConfig(load_in_8bit=True, llm_int8_skip_modules=skip_modules)
model = torch.nn.Sequential(torch.nn.Linear(4, 4), torch.nn.Linear(4, 4))

for _ in range(2):
    quantizer = BnB8BitDiffusersQuantizer(config)
    quantizer._process_model_before_weight_loading(model, device_map=None, keep_in_fp32_modules=["norm_out"])

print(config.llm_int8_skip_modules)
# ['proj_out', 'norm_out', 'norm_out'] after two loads - should stay ['proj_out']

System Info

  • diffusers @ main, commit aaf57d0
  • Python 3.12, Linux

Who can help?

(leaving blank — no specific quantization reviewer listed in the template's tag guide)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions