o
    ۷iv                     @   s   d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZmZmZ ddlmZ dd	lmZ eeeeeed
Ze
e
eeeed
ZG dd dZdS )z
Adapted from
https://github.com/huggingface/transformers/blob/c409cd81777fb27aadc043ed3d8339dbc020fb3b/src/transformers/quantizers/auto.py
    N   )BnB4BitDiffusersQuantizerBnB8BitDiffusersQuantizer)GGUFQuantizer)NVIDIAModelOptQuantizer)BitsAndBytesConfigGGUFQuantizationConfigNVIDIAModelOptConfigQuantizationConfigMixinQuantizationMethodQuantoConfigTorchAoConfig)QuantoQuantizer)TorchAoHfQuantizer)bitsandbytes_4bitbitsandbytes_8bitggufquantotorchaomodeloptc                   @   sb   e Zd ZdZedefddZedeeB fddZedd	 Z	edeeB d
edB fddZ
dS )DiffusersAutoQuantizerz
     The auto diffusers quantizer class that takes care of automatically instantiating to the correct
    `DiffusersQuantizer` given the `QuantizationConfig`.
    quantization_config_dictc                 C   s   | dd }| dds| ddr"| ddrdnd}tj| }n|d u r*td|t vr>td| d	tt  t| }||S )
Nquant_methodload_in_8bitFload_in_4bit_4bit_8bitzThe model's quantization config from the arguments has no `quant_method` attribute. Make sure that the model has been correctly quantizedUnknown quantization type, got  - supported types are: )	getr   BITS_AND_BYTES
ValueError AUTO_QUANTIZATION_CONFIG_MAPPINGkeyslistAUTO_QUANTIZER_MAPPING	from_dict)clsr   r   suffix
target_cls r*   O/home/ubuntu/vllm_env/lib/python3.10/site-packages/diffusers/quantizers/auto.pyr&   >   s    

z DiffusersAutoQuantizer.from_dictquantization_configc                 K   s|   t |tr
| |}|j}|tjkr|jr|d7 }n|d7 }|t vr2t	d| dt
t  t| }||fi |S )Nr   r   r   r   )
isinstancedictr&   r   r   r    r   r%   r#   r!   r$   )r'   r,   kwargsr   r)   r*   r*   r+   from_configS   s   




z"DiffusersAutoQuantizer.from_configc                 K   sV   | j |fi |}t|dd d u rtd| d|j}| |}|| | |S )Nr,   z)Did not found a `quantization_config` in z2. Make sure that the model is correctly quantized.)load_configgetattrr!   r,   r&   updater0   )r'   pretrained_model_name_or_pathr/   model_configr   r,   r*   r*   r+   from_pretrainedl   s   



z&DiffusersAutoQuantizer.from_pretrainedquantization_config_from_argsNc                 C   sN   |durd}nd}t |tr| |}t |tr|  |dkr%t| |S )z
        handles situations where both quantization_config from args and quantization_config from model config are
        present.
        NzYou passed `quantization_config` or equivalent parameters to `from_pretrained` but the model you're loading already has a `quantization_config` attribute. The `quantization_config` from the model will be used. )r-   r.   r&   r	   check_model_patchingwarningswarn)r'   r,   r7   warning_msgr*   r*   r+   merge_quantization_configsz   s   




z1DiffusersAutoQuantizer.merge_quantization_configs)__name__
__module____qualname____doc__classmethodr.   r&   r
   r0   r6   r=   r*   r*   r*   r+   r   8   s    
r   )rA   r:   bitsandbytesr   r   r   r   r   r   r,   r   r   r	   r
   r   r   r   r   r   r   r   r%   r"   r   r*   r*   r*   r+   <module>   s.   $	

