Ë
    T^(h1  ã                   óœ   — d dl Z d dlZd dlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ  e«       rd dlZ ej                  e«      Z G d„ d	e«      Zy)
é    N)ÚListé   )Úis_compressed_tensors_availableÚis_torch_availableÚlogging)ÚCompressedTensorsConfigé   )ÚHfQuantizerc                   ó¼   ‡ — e Zd ZdZdZdgZdefˆ fd„Zdee	   de	dee	   fd	„Z
d
ee	   de	dee	   fd„Zd„ Zdd„Zd„ Zd„ Zd„ Zed„ «       Zdefd„Zddefd„Zˆ xZS )ÚCompressedTensorsHfQuantizerz�
    Quantizer for the compressed_tensors package.  Loads and restores models to
    quantized state with compressed_tensors
    TÚcompressed_tensorsÚquantization_configc                 óØ   •— t        ‰| �  |fi |¤Ž t        «       st        d«      ‚|j	                  «        ddlm} |j                  |«      | _        |j                  | _	        || _
        y )NúuUsing `compressed_tensors` quantized models requires the compressed-tensors library: `pip install compressed-tensors`r   )ÚModelCompressor)ÚsuperÚ__init__r   ÚImportErrorÚ	post_initÚcompressed_tensors.compressorsr   Úfrom_compression_configÚ
compressorÚrun_compressedr   )Úselfr   Úkwargsr   Ú	__class__s       €úr/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/quantizers/quantizer_compressed_tensors.pyr   z%CompressedTensorsHfQuantizer.__init__(   sh   ø€ Ü‰ÑÐ,Ñ7°Ò7ä.Ô0Üð3óð ð 	×%Ñ%Ô'ÝBà)×AÑAÐBUÓVˆŒØ1×@Ñ@ˆÔØ#6ˆÕ ó    Úmissing_keysÚprefixÚreturnc                 ó¬   ‡— | j                   r|S | j                  j                  |«      }|D �‡cg c]  Št        ˆfd„|D «       «      rŒ‰‘Œ c}S c c}w )a}  
        Update missing keys after loading the model. This is necessary for compressed tensors
        to load the model correctly. We expect weights to be present in missing keys.
        The weight's are re-constructed by ModelCompressor in _process_model_after_weight_loading

        This function cleans up expected missing keys and returns the remaining missing keys
        c              3   óP   •K  — | ]  }t        j                  d |› �‰«      –— Œ y­w©z.*N©ÚreÚmatch©Ú.0ÚpatternÚkeys     €r   ú	<genexpr>zQCompressedTensorsHfQuantizer.update_missing_keys_after_loading.<locals>.<genexpr>N   s#   øè ø€ Ò2vÐU\´2·8±8¸bÀÀ	¸NÈC×3PÑ2vùó   ƒ#&)r   r   Úget_missing_module_keysÚany)r   Úmodelr   r    Úexpected_missing_keysr+   s        `r   Ú!update_missing_keys_after_loadingz>CompressedTensorsHfQuantizer.update_missing_keys_after_loading;   sV   ø€ ð ×ÒØÐð !%§¡× GÑ GÈÓ NÐà'÷
Ø¬sÓ2vÐ`uÔ2vÕ/vŠCò
ð 	
ùò 
ó   °AÁ
AÚunexpected_keysc                 ó¬   ‡— | j                   r|S | j                  j                  |«      }|D �‡cg c]  Št        ˆfd„|D «       «      rŒ‰‘Œ c}S c c}w )zÿ
        Override this method if you want to adjust the `unexpected_keys`.

        Args:
            unexpected_keys (`List[str]`, *optional*):
                The list of unexpected keys in the checkpoint compared to the state dict of the model
        c              3   óP   •K  — | ]  }t        j                  d |› �‰«      –— Œ y­wr$   r%   r(   s     €r   r,   zFCompressedTensorsHfQuantizer.update_unexpected_keys.<locals>.<genexpr>`   s$   øè ø€ Ò9vÐ\c¼"¿(¹(ÀRÈÀyÀ>ÐSV×:WÑ9vùr-   )r   r   Úget_unexpected_file_keysr/   )r   r0   r4   r    Úkeys_to_ignorer+   s        `r   Úupdate_unexpected_keysz3CompressedTensorsHfQuantizer.update_unexpected_keysQ   sL   ø€ ð ×ÒØ"Ð"ð Ÿ™×AÑAÀ%ÓHˆØ.×w˜´cÓ9vÐguÔ9vÕ6v’ÒwÐwùÒwr3   c                 óX   — t        «       st        d«      ‚t        «       st        d«      ‚y )Nr   z;torch is required for using compressed-tensors quantization)r   r   r   )r   Úargsr   s      r   Úvalidate_environmentz1CompressedTensorsHfQuantizer.validate_environmentb   s3   € Ü.Ô0Üð3óð ô "Ô#äÐ[Ó\Ð\ð $r   c                 ó¨   — |€'t         j                  d«       t        j                  }|S |t        j                  k7  rt         j                  d«       |S )NzELoading model using torch.float16 for compressed-tensors quantizationz`We suggest you to set `torch_dtype=torch.float16` for better efficiency with compressed_tensors.)ÚloggerÚinfoÚtorchÚfloat16)r   Útorch_dtypes     r   Úupdate_torch_dtypez/CompressedTensorsHfQuantizer.update_torch_dtypel   sK   € ØÐÜ�K‰KÐ_Ô`ÜŸ-™-ˆKð
 Ðð	 œEŸM™MÒ)Ü�K‰KØrôð Ðr   c                 ó¬   — ddl m} | j                  j                  }| j                  r |||d¬«       y | j                  j
                  s
 |||«       y y )Nr   )Úapply_quantization_configT)r   )Úcompressed_tensors.quantizationrE   r   r   r   Úis_quantization_compressed)r   r0   r   rE   Úct_quantization_configs        r   Ú$_process_model_before_weight_loadingzACompressedTensorsHfQuantizer._process_model_before_weight_loadingv   sL   € ÝMà!%§¡×!DÑ!DÐà×ÒÙ% eÐ-CÐTXÖYØ×)Ñ)×DÒDÙ% eÐ-CÕDð Er   c                 óŠ  — | j                   j                  r| j                  r| j                   j                  �r
|j	                  dd«      }|j
                  }t        j                  j                  |«      sbddl	m
}  ||d«      }t        j                  j                  j                  |j                  t        j                  j                  «      dd «      }| j                   j                  r7| j                  s+ddlm} |j                   | j"                  j                   _        | j"                  j'                  ||¬«       yy)	z3Decompress loaded model if necessary - need for qatÚconfigNr   )Úcached_filezconfig.jsonéÿÿÿÿ)ÚQuantizationStatus)Ú
model_pathr0   )r   rG   r   Úis_sparsification_compressedÚgetÚ_name_or_pathÚosÚpathÚexistsÚtransformers.utilsrL   ÚsepÚjoinÚsplitrF   rN   ÚFROZENr   Úquantization_statusÚ
decompress)r   r0   r   rK   Ú
cache_pathrL   Úconfig_file_pathrN   s           r   Ú#_process_model_after_weight_loadingz@CompressedTensorsHfQuantizer._process_model_after_weight_loading€   sé   € ð ×$Ñ$×?Ò?È×H[ÒH[Ø×%Ñ%×BÓBØ—Z‘Z ¨$Ó/ˆFØ×-Ñ-ˆJä—7‘7—>‘> *Ô-Ý:á#.¨z¸=Ó#IÐ ÜŸW™WŸ[™[×-Ñ-Ð.>×.DÑ.DÄRÇWÁWÇ[Á[Ó.QÐRUÐSUÐ.VÓW�
à×'Ñ'×BÒBÈ4×K^ÒK^ÝNàJ\×JcÑJc�—‘×3Ñ3ÔGØ�O‰O×&Ñ&°*ÀEÐ&ÕJð Cr   c                 ó¼   — ddddddœ}|j                  «       �C|j                  «       j                  �)|j                  «       j                  j                  |«       |S )NÚlocal_colwiseÚlocal_rowwise)z0layers.*.feed_forward.experts.*.gate_proj.weightz6layers.*.feed_forward.experts.*.gate_proj.weight_scalez.layers.*.feed_forward.experts.*.up_proj.weightz4layers.*.feed_forward.experts.*.up_proj.weight_scalez0layers.*.feed_forward.experts.*.down_proj.weight)Úget_text_configÚbase_model_tp_planÚupdate)r   rK   Úadditional_plans      r   Úupdate_tp_planz+CompressedTensorsHfQuantizer.update_tp_plan•   s_   € à@OØFUØ>MØDSØ@Oñ
ˆð ×!Ñ!Ó#Ð/°F×4JÑ4JÓ4L×4_Ñ4_Ð4kØ×"Ñ"Ó$×7Ñ7×>Ñ>¸ÔOàˆr   c                  ó   — y)NT© ©r   s    r   Úis_trainablez)CompressedTensorsHfQuantizer.is_trainable¢   ó   € àr   c                 óN   — | j                    xs | j                  j                   S )z7Loaded Models can carry out quantization aware training)r   r   rG   rj   s    r   Úis_qat_trainablez-CompressedTensorsHfQuantizer.is_qat_trainable¦   s'   € ð ×&Ñ&Ð&Òa¨d×.FÑ.F×.aÑ.aÐ*aÐar   c                  ó   — y)z>Models quantized using compressed tensors can be saved to diskTri   )r   Úsafe_serializations     r   Úis_serializablez,CompressedTensorsHfQuantizer.is_serializable«   rl   r   )rB   útorch.dtyper!   rr   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrequires_calibrationÚrequired_packagesr   r   r   Ústrr2   r9   r<   rC   rI   r_   rg   Úpropertyrk   Úboolrn   rq   Ú__classcell__)r   s   @r   r   r      sÀ   ø„ ñð
  ÐØ-Ð.Ðð7Ð,Cõ 7ð&
ÀTÈ#ÁYð 
ÐX[ð 
Ð`dÐehÑ`ió 
ð,x¸TÀ#¹Yð xÐPSð xÐX\Ð]`ÑXaó xò"]óòEòKò*ð ñó ððb $ó bñ
¸$÷ r   r   )rS   r&   Útypingr   Úutilsr   r   r   Úutils.quantization_configr   Úbaser
   r@   Ú
get_loggerrs   r>   r   ri   r   r   ú<module>r‚      sG   ðó  
Û 	Ý ç PÑ PÝ ?Ý ñ ÔÛà	ˆ×	Ñ	˜HÓ	%€ôN ;õ Nr   