Skip to content

FqnToConfig raises inside TorchAoConfig #14667

Closed

Description

@Beinsezii

Describe the bug

seems like it's not applied to quantize_ properly

Reproduction

from collections import OrderedDict

import torch
from diffusers.models.transformers.transformer_flux2 import Flux2Transformer2DModel
from diffusers.quantizers.quantization_config import TorchAoConfig
from torchao.quantization import FqnToConfig, Int8WeightOnlyConfig, PerGroup, PerTensor

with torch.no_grad():
    _t = Flux2Transformer2DModel.from_pretrained(
        "black-forest-labs/FLUX.2-klein-4B",
        subfolder="transformer",
        torch_dtype=torch.bfloat16,
        device_map="cuda",
        low_cpu_mem_usage=True,
        quantization_config=TorchAoConfig(
            FqnToConfig(
                OrderedDict(
                    [
                        (
                            r"re:.*transformer_blocks\.\d+\.ff.*",
                            Int8WeightOnlyConfig(version=2, granularity=PerTensor()),
                        ),
                        (
                            r"re:.*transformer_blocks\.\d+\.attn.*",
                            Int8WeightOnlyConfig(version=2, granularity=PerGroup(64)),
                        ),
                        ("_default", None),
                    ]
                )
            )
        ),
    )

Logs

Traceback (most recent call last):
  File "/tmp/reproduce.py", line 9, in <module>
    _t = Flux2Transformer2DModel.from_pretrained(
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/repro/.venv/lib/python3.12/site-packages/huggingface_hub/utils/_validators.py", line 88, in _inner_fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/tmp/repro/.venv/lib/python3.12/site-packages/diffusers/models/modeling_utils.py", line 1398, in from_pretrained
    ) = cls._load_pretrained_model(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/repro/.venv/lib/python3.12/site-packages/diffusers/models/modeling_utils.py", line 1792, in _load_pretrained_model
    offload_index, state_dict_index, _mismatched_keys, _error_msgs = load_fn(shard_file)
                                                                     ^^^^^^^^^^^^^^^^^^^
  File "/tmp/repro/.venv/lib/python3.12/site-packages/diffusers/models/model_loading_utils.py", line 371, in _load_shard_file
    offload_index, state_dict_index = load_model_dict_into_meta(
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/repro/.venv/lib/python3.12/site-packages/diffusers/models/model_loading_utils.py", line 303, in load_model_dict_into_meta
    hf_quantizer.create_quantized_param(
  File "/tmp/repro/.venv/lib/python3.12/site-packages/diffusers/quantizers/torchao/torchao_quantizer.py", line 366, in create_quantized_param
    quantize_(module, self.quantization_config.get_apply_tensor_subclass())
  File "/tmp/repro/.venv/lib/python3.12/site-packages/torchao/quantization/quant_api.py", line 422, in quantize_
    raise ValueError(
ValueError: Custom filter_fn and FqnToConfig were both specified. Only filter_fn=None is supported when FqnToConfig is specified.

System Info

Copy-and-paste the text below in your GitHub issue and FILL OUT the two last points.

- 🤗 Diffusers version: 0.40.0.dev0
- Platform: Linux-7.2.2-arch1-1-x86_64-with-glibc2.44
- Running on Google Colab?: No
- Python version: 3.12.13
- PyTorch version (GPU?): 2.13.0+rocm7.2 (True)
- Huggingface_hub version: 1.24.0
- Transformers version: 5.5.4
- Accelerate version: 1.12.0
- PEFT version: 0.18.1
- bitsandbytes version: 0.49.2
- optimum-quanto version: 0.2.7
- torchao version: 0.17.0+rocm7.2
- Safetensors version: 0.8.0
- xFormers version: not installed
- Accelerator: NA
- Using GPU in script?: optional
- Using distributed or parallel set-up in script?: no

Who can help?

@sayakpaul @DN6

Activity

  1. added
    bug
    on Sep 1, 2026
  2. DN6 commented on Sep 1, 2026

    @DN6
    Collaborator

    @Beinsezii The traceback seems to point to an issue with TorchAO? Are you able to quantize the model with TorchAO directly?

  3. Beinsezii commented on Sep 1, 2026

    @Beinsezii
    ContributorAuthor

    @Beinsezii The traceback seems to point to an issue with TorchAO? Are you able to quantize the model with TorchAO directly?

    @DN6, Yes, the issue is diffusers callsite of quantize_. Transformers already handles this, so passing FqnToConfig to ModularPipeline.load_components only works on text encoders.

    direct_quantization.py
    from collections import OrderedDict
    
    import torch
    from diffusers.models.transformers.transformer_flux2 import Flux2Transformer2DModel
    from diffusers.quantizers.quantization_config import TorchAoConfig
    from torchao.quantization import (
        FqnToConfig,
        Int8WeightOnlyConfig,
        PerGroup,
        PerTensor,
        quantize_,
    )
    
    with torch.no_grad():
        t = Flux2Transformer2DModel.from_pretrained(
            "black-forest-labs/FLUX.2-klein-4B",
            subfolder="transformer",
            torch_dtype=torch.bfloat16,
            device_map="cuda",
            low_cpu_mem_usage=True,
            # # raises because filter_fn is set
            # quantization_config=TorchAoConfig(
            #     FqnToConfig(
            #         OrderedDict(
            #             [
            #                 (
            #                     r"re:.*transformer_blocks\.\d+\.ff.*.weight",
            #                     Int8WeightOnlyConfig(version=2, granularity=PerTensor()),
            #                 ),
            #                 (
            #                     r"re:.*transformer_blocks\.\d+\.attn.*.weight",
            #                     Int8WeightOnlyConfig(version=2, granularity=PerGroup(64)),
            #                 ),
            #                 ("_default", None),
            #             ]
            #         )
            #     )
            # ),
        )
        # direct quantization works fine
        quantize_(
            t,
            FqnToConfig(
                OrderedDict(
                    [
                        (
                            r"re:.*transformer_blocks\.\d+\.ff.*.weight",
                            Int8WeightOnlyConfig(version=2, granularity=PerTensor()),
                        ),
                        (
                            r"re:.*transformer_blocks\.\d+\.attn.*.weight",
                            Int8WeightOnlyConfig(version=2, granularity=PerGroup(64)),
                        ),
                        ("_default", None),
                    ]
                )
            ),
            filter_fn=None,
        )

    i made a hacky patch on a fork that just adds a special case for FqnToConfig but the Transformers source code seems to have a more sophisticated solution so I thought it would be better for a HuggingFace maintainer to implement this.

  4. sayakpaul commented on Sep 2, 2026

    @sayakpaul
    Member

    Checking.

  5. sayakpaul commented on Sep 2, 2026

    @sayakpaul
    Member

    @Beinsezii can you check #14686 and see if that fixes the problem?

  6. modelpath-dev commented on Sep 4, 2026

    @modelpath-dev

    I will take this issue. Please assign it to me.

    The problem seems to be with how FqnToConfig is applied within TorchAoConfig. It looks like the quantization configuration is not being applied correctly to the quantize_ function. I will start by checking the FqnToConfig implementation to ensure it correctly matches and applies configurations based on the provided patterns. I will also verify if the TorchAoConfig is correctly integrating these configurations. A small fix might involve adjusting the regex patterns or the application logic in FqnToConfig.

  7. modelpath-dev commented on Sep 6, 2026

    @modelpath-dev

    @sayakpaul, I will review the changes in PR #14686 to see if they resolve the issue. If it fixes the problem, I will confirm here. Thank you for the update.

  8. added this to the Release 0.41.0 milestone on Sep 7, 2026
  9. modelpath-dev commented on Sep 8, 2026

    @modelpath-dev

    @sayakpaul, I checked the changes in PR #14686. It looks promising. I will test it with the provided reproduction script to confirm if it resolves the issue. I will update here once I have the results. Thank you for addressing this.

  10. moved this from In Progress to Done in Diffusers Roadmapon Sep 15, 2026
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

    bug

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions