U
    diL                     @   sP  d dl Z d dlmZmZmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, eeee&ee*e"e(e e$e,edZ-e	eeeeeeeeeee
dZ.G dd dZ/G dd dZ0dS )    N)DictOptionalUnion   )
AutoConfig)
AqlmConfig	AwqConfigBitNetConfigBitsAndBytesConfigCompressedTensorsConfig
EetqConfigFbgemmFp8Config
GPTQConfig	HqqConfigQuantizationConfigMixinQuantizationMethodQuantoConfigTorchAoConfig   )AqlmHfQuantizer)AwqQuantizer)BitNetHfQuantizer)Bnb4BitHfQuantizer)Bnb8BitHfQuantizer)CompressedTensorsHfQuantizer)EetqHfQuantizer)FbgemmFp8HfQuantizer)GptqHfQuantizer)HqqHfQuantizer)QuantoHfQuantizer)TorchAoHfQuantizer)awqbitsandbytes_4bitbitsandbytes_8bitgptqaqlmquantoeetqhqqcompressed-tensors
fbgemm_fp8torchaobitnet)r!   r"   r#   r'   r$   r%   r&   r(   r)   r*   r+   r,   c                   @   s.   e Zd ZdZeedddZedd ZdS )AutoQuantizationConfigz
    The Auto-HF quantization config class that takes care of automatically dispatching to the correct
    quantization config given a quantization config stored in a dictionary.
    )quantization_config_dictc                 C   s   | dd }| dds$| ddrD| ddr4dnd}tj| }n|d krTtd|t kr|td| d	tt  t| }||S )
Nquant_methodload_in_8bitFZ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@   @/tmp/pip-unpacked-wheel-bm_b0l5e/transformers/quantizers/auto.pyr<   T   s    z AutoQuantizationConfig.from_dictc                 K   sN   t j|f|}t|dd d kr.td| d|j}| |}|jf | |S )Nquantization_configz)Did not found a `quantization_config` in z2. Make sure that the model is correctly quantized.)r   from_pretrainedgetattrr7   rB   r<   update)r=   pretrained_model_name_or_pathkwargsZmodel_configr.   rB   r@   r@   rA   rC   i   s    

z&AutoQuantizationConfig.from_pretrainedN)__name__
__module____qualname____doc__classmethodr   r<   rC   r@   r@   r@   rA   r-   N   s
   r-   c                   @   sV   e Zd ZdZeeeef dddZedd Z	eee
ef ee ddd	Zd
S )AutoHfQuantizerz
     The Auto-HF quantizer class that takes care of automatically instantiating to the correct
    `HfQuantizer` given the `QuantizationConfig`.
    )rB   c                 K   sx   t |trt|}|j}|tjkr<|jr4|d7 }n|d7 }|t	 krdt
d| dtt	  t| }||f|S )Nr2   r1   r3   r4   )
isinstancedictr-   r<   r/   r   r6   r0   r;   r9   r7   r:   )r=   rB   rG   r/   r?   r@   r@   rA   from_config}   s    



zAutoHfQuantizer.from_configc                 K   s   t j|f|}| |S )N)r-   rC   rP   )r=   rF   rG   rB   r@   r@   rA   rC      s    zAutoHfQuantizer.from_pretrained)rB   quantization_config_from_argsc                 C   s   |dk	rd}nd}t |tr&t|}t |tttfr||dk	r|| }| D ]\}}t	||| qN|dt
|  d7 }|dkrt| |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. z"However, loading attributes (e.g. z]) will be overwritten with the one you passed to `from_pretrained`. The rest will be ignored.)rN   rO   r-   r<   r   r   r   Zget_loading_attributesitemssetattrr:   r9   warningswarn)r=   rB   rQ   Zwarning_msgZloading_attr_dictattrvalr@   r@   rA   merge_quantization_configs   s"    	


z*AutoHfQuantizer.merge_quantization_configsN)rH   rI   rJ   rK   rL   r   r   r   rP   rC   rO   r   rY   r@   r@   r@   rA   rM   w   s   

rM   )1rU   typingr   r   r   Zmodels.auto.configuration_autor   Zutils.quantization_configr   r   r	   r
   r   r   r   r   r   r   r   r   r   Zquantizer_aqlmr   Zquantizer_awqr   Zquantizer_bitnetr   Zquantizer_bnb_4bitr   Zquantizer_bnb_8bitr   Zquantizer_compressed_tensorsr   Zquantizer_eetqr   Zquantizer_fbgemm_fp8r   Zquantizer_gptqr   Zquantizer_hqqr   Zquantizer_quantor   Zquantizer_torchaor    r;   r8   r-   rM   r@   r@   r@   rA   <module>   sV   <)