Repository: huggingface/diffusers
File: src/diffusers/quantizers/quantization_config.py (Python)

Code before:
            subfolder="transformer",
            quantization_config=quantization_config,
            torch_dtype=torch.bfloat16,
        )
        ```
    """

    def __init__(
        self,
        quant_type: Union[str, "AOBaseConfig"],  # noqa: F821
        modules_to_not_convert: Optional[List[str]] = None,
        **kwargs,
    ) -> None:
        self.quant_method = QuantizationMethod.TORCHAO
        self.quant_type = quant_type
        self.modules_to_not_convert = modules_to_not_convert

        # When we load from serialized config, "quant_type_kwargs" will be the key
        if "quant_type_kwargs" in kwargs:
            self.quant_type_kwargs = kwargs["quant_type_kwargs"]
        else:
            self.quant_type_kwargs = kwargs

        self.post_init()

    def post_init(self):
        TORCHAO_QUANT_TYPE_METHODS = self._get_torchao_quant_type_to_method()
        AO_VERSION = self._get_ao_version()

        if isinstance(self.quant_type, str):
            if self.quant_type not in TORCHAO_QUANT_TYPE_METHODS.keys():
                is_floating_quant_type = self.quant_type.startswith("float") or self.quant_type.startswith("fp")
                if is_floating_quant_type and not self._is_xpu_or_cuda_capability_atleast_8_9():
                    raise ValueError(
                        f"Requested quantization type: {self.quant_type} is not supported on GPUs with CUDA capability <= 8.9. You "
                        f"can check the CUDA capability of your GPU using `torch.cuda.get_device_capability()`."
                    )

                raise ValueError(
                    f"Requested quantization type: {self.quant_type} is not supported or is an incorrect `quant_type` name. If you think the "
                    f"provided quantization type should be supported, please open an issue at https://github.com/huggingface/diffusers/issues."
                )

            method = TORCHAO_QUANT_TYPE_METHODS[self.quant_type]
            signature = inspect.signature(method)
            all_kwargs = {
                param.name
                for param in signature.parameters.values()
                if param.kind in [inspect.Parameter.KEYWORD_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD]
            }
            unsupported_kwargs = list(self.quant_type_kwargs.keys() - all_kwargs)

Review comment (bug):
Think it would be cleaner to run the checks in the begining and exit/fail early

```python
def post_init(self):
    if not isinstance(self.quant_type, str):
        if not is_torchao_version(">=", "0.9.0"):
            raise ValueError(
                f"torchao <= 0.9.0 only supports string quant_type, got {type(self.quant_type).__name__}. "
                f"Upgrade to torchao > 0.9.0 to use AOBaseConfig."
            )
        
        from torchao.quantization.quant_api import AOBaseConfig
        if not isinstance(self.quant_type, AOBaseConfig):
            raise TypeError(
                f"quant_type must be a string or AOBaseConfig instance, got {type(self.quant_type).__name__}"
            )
        return
    
    TORCHAO_QUANT_TYPE_METHODS = self._get_torchao_quant_type_to_method()
    
    if self.quant_type not in TORCHAO_QUANT_TYPE_METHODS:
        # remaining str type validation
``` 

Code after:
            subfolder="transformer",
            quantization_config=quantization_config,
            torch_dtype=torch.bfloat16,
        )
        ```
    """

    def __init__(
        self,
        quant_type: Union[str, "AOBaseConfig"],  # noqa: F821
        modules_to_not_convert: Optional[List[str]] = None,
        **kwargs,
    ) -> None:
        self.quant_method = QuantizationMethod.TORCHAO
        self.quant_type = quant_type
        self.modules_to_not_convert = modules_to_not_convert

        # When we load from serialized config, "quant_type_kwargs" will be the key
        if "quant_type_kwargs" in kwargs:
            self.quant_type_kwargs = kwargs["quant_type_kwargs"]
        else:
            self.quant_type_kwargs = kwargs

        self.post_init()

    def post_init(self):
        if not isinstance(self.quant_type, str):
            if is_torchao_version("<=", "0.9.0"):
                raise ValueError(
                    f"torchao <= 0.9.0 only supports string quant_type, got {type(self.quant_type).__name__}. "
                    f"Upgrade to torchao > 0.9.0 to use AOBaseConfig."
                )

            from torchao.quantization.quant_api import AOBaseConfig

            if not isinstance(self.quant_type, AOBaseConfig):
                raise TypeError(f"quant_type must be a AOBaseConfig instance, got {type(self.quant_type).__name__}")

        elif isinstance(self.quant_type, str):
            TORCHAO_QUANT_TYPE_METHODS = self._get_torchao_quant_type_to_method()

            if self.quant_type not in TORCHAO_QUANT_TYPE_METHODS.keys():
                is_floating_quant_type = self.quant_type.startswith("float") or self.quant_type.startswith("fp")
                if is_floating_quant_type and not self._is_xpu_or_cuda_capability_atleast_8_9():
                    raise ValueError(
                        f"Requested quantization type: {self.quant_type} is not supported on GPUs with CUDA capability <= 8.9. You "
                        f"can check the CUDA capability of your GPU using `torch.cuda.get_device_capability()`."
                    )

                raise ValueError(
                    f"Requested quantization type: {self.quant_type} is not supported or is an incorrect `quant_type` name. If you think the "