
    sgL                     \   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      Z/ G d d      Z0y)    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                   6    e Zd ZdZedefd       Zed        Zy)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           	         |j                  dd       }|j                  dd      s|j                  dd      r*|j                  dd      rdnd}t        j                  |z   }n|t        d      |t        j                         vr,t        d| d	t        t        j                                      t        |   }|j                  |      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)clsr0   r2   suffix
target_clss        O/var/www/html/venv/lib/python3.12/site-packages/transformers/quantizers/auto.pyr@   z AutoQuantizationConfig.from_dictT   s    /33NDI#''>BZB^B^_motBu 8 < <^U SWY`F-<<vEL! \  ?DDFF1, @/44678: 
 6lC
##$<==    c                     t        j                  |fi |}t        |dd       t        d| d      |j                  }| j                  |      } |j                  di | |S )Nquantization_configz)Did not found a `quantization_config` in z2. Make sure that the model is correctly quantized. )r   from_pretrainedgetattrr;   rG   r@   update)rA   pretrained_model_name_or_pathkwargsmodel_configr0   rG   s         rD   rI   z&AutoQuantizationConfig.from_pretrainedi   s    !112OZSYZ<!6=E;<Y;Z  [M  N  $0#C#C !mm,DE""",V,""rE   N)__name__
__module____qualname____doc__classmethodr   r@   rI   rH   rE   rD   r/   r/   N   s6    
 > > >( 
# 
#rE   r/   c                   j    e Zd ZdZedeeef   fd       Zed        Z	edee
ef   dee   fd       Zy)AutoHfQuantizerz
     The Auto-HF quantizer class that takes care of automatically instantiating to the correct
    `HfQuantizer` given the `QuantizationConfig`.
    rG   c           	      `   t        |t              rt        j                  |      }|j                  }|t
        j                  k(  r|j                  r|dz  }n|dz  }|t        j                         vr,t        d| dt        t        j                                      t        |   } ||fi |S )Nr6   r5   r7   r8   )
isinstancedictr/   r@   r2   r   r:   r3   r?   r=   r;   r>   )rA   rG   rM   r2   rC   s        rD   from_configzAutoHfQuantizer.from_config}   s     )40"8"B"BCV"W*77 -<<<"//''5::<<1, @/44678: 
 ,L9
-888rE   c                 P    t        j                  |fi |}| j                  |      S )N)r/   rI   rY   )rA   rL   rM   rG   s       rD   rI   zAutoHfQuantizer.from_pretrained   s*    4DDEbmflm233rE   quantization_config_from_argsc                 v   |d}nd}t        |t              rt        j                  |      }t        |t        t
        t        f      rW|U|j                         }|j                         D ]  \  }}t        |||        |dt        |j                                dz  }|dk7  rt        j                  |       |S )z
        handles situations where both quantization_config from args and quantization_config from model config are present.
        zYou 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.)rW   rX   r/   r@   r   r	   r   get_loading_attributesitemssetattrr>   r=   warningswarn)rA   rG   r[   warning_msgloading_attr_dictattrvals          rD   merge_quantization_configsz*AutoHfQuantizer.merge_quantization_configs   s     )4y 
 K)40"8"B"BCV"W *ZO,TU-9 !> T T V.446 8	c+T378?EVE[E[E]@^?_  `}  ~  ~K"MM+&""rE   N)rO   rP   rQ   rR   rS   r   r   r   rY   rI   rX   r   rg   rH   rE   rD   rU   rU   w   sx    
 9e4KT4Q.R 9 90 4 4  #"4)@#@A # (00G'H #  #rE   rU   )1ra   typingr   r   r   models.auto.configuration_autor   utils.quantization_configr   r	   r
   r   r   r   r   r   r   r   r   r   r   quantizer_aqlmr   quantizer_awqr   quantizer_bitnetr   quantizer_bnb_4bitr   quantizer_bnb_8bitr   quantizer_compressed_tensorsr   quantizer_eetqr   quantizer_fbgemm_fp8r   quantizer_gptqr   quantizer_hqqr   quantizer_quantor    quantizer_torchaor!   r?   r<   r/   rU   rH   rE   rD   <module>rw      s     ( ( 7    , ' / 2 2 F + 6 + ) / 1 ++6&!   ++1!$   &# &#RE# E#rE   