ó
    qyüi¤+  ã                   ó
  • S SK Jr  SSKJr  SSKJrJrJr  SSKJ	r	  SSK
Jr  \(       a  SSKJr  SS	KJr  \" 5       (       a  S S
Kr\" 5       (       a  S SKJr  \S 5       r\\l        \R*                  " \5      r " S S\	5      rg
)é    )ÚTYPE_CHECKINGé   )Úprepare_for_hqq_linear)Úis_hqq_availableÚis_torch_availableÚloggingé   )ÚHfQuantizer)Úget_module_from_name)ÚPreTrainedModel)Ú	HqqConfigN)Ú	HQQLinearc                 óV   • [         R                  " SU R                  U R                  S9$ )Nr   )ÚdtypeÚdevice)ÚtorchÚemptyÚcompute_dtyper   ©Úselfs    Úb/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/quantizers/quantizer_hqq.pyÚweightr   %   s   € ä�{Š{˜1 D×$6Ñ$6¸t¿{¹{ÑKÐKó    c                   ó”   ^ • \ rS rSr% SrSrS\S'   U 4S jrS rSS	S
\	S\
4S jrS r  SS jrSS jrS r\S\
4S j5       rSrU =r$ )ÚHqqHfQuantizeré.   z~
HQQ quantizer base HF class.
nn.Linear modules are first tagged with quant_config in _process_model_before_weight_loading().
Fr   Úquantization_configc                 ó¾   >• [        5       (       d  [        S5      e[        TU ]  " U40 UD6  S U l        SU l        [        S S 5      R                  5       S1-
  U l        g )Nz�A valid HQQ version (>=0.2.1) is not available. Please follow the instructions to install it: `https://github.com/mobiusml/hqq/`.FÚbias)	r   ÚImportErrorÚsuperÚ__init__r   Úusing_multi_gpur   Ústate_dict_keysÚhqq_keys)r   r   ÚkwargsÚ	__class__s      €r   r"   ÚHqqHfQuantizer.__init__7   sa   ø€ Ü×!Ñ!Üð Tóð ô 	‰ÒÐ,Ñ7°Ò7ØˆŒ
Ø$ˆÔä! $¨Ó-×=Ñ=Ó?À6À(ÑJˆ�r   c                 óœ  • U R                   c;  SU;   a  US   U l         O*[        R                  U l         [        R	                  S5        UR                  S5      n[        U[        5      (       a^  SUR                  5       ;   d  SUR                  5       ;   a  [        S5      e[        [        UR                  5       5      5      S:„  U l        g g )Nr   zOSetting dtype to torch.float32 as the default value since it was not specified.Ú
device_mapÚcpuÚdiskz­You are attempting to use an HQQ model with a device_map that contains a CPU or disk device. This is not supported. Please remove the CPU or disk device from the device_map.r	   )r   r   Úfloat32ÚloggerÚinfoÚgetÚ
isinstanceÚdictÚvaluesÚ
ValueErrorÚlenÚsetr#   )r   Úargsr&   r*   s       r   Úvalidate_environmentÚ#HqqHfQuantizer.validate_environmentB   s­   € Ø�:‰:ÑØ˜&Ó Ø# G™_�•
ä"Ÿ]™]�”
Ü—‘ÐmÔnà—Z‘Z Ó-ˆ
Ü�j¤$×'Ñ'Ø˜
×)Ñ)Ó+Ó+¨v¸×9JÑ9JÓ9LÓ/LÜ ðhóð ô
 (+¬3¨z×/@Ñ/@Ó/BÓ+CÓ'DÀqÑ'H�Õ$ð (r   Úmodelr   Ú
param_nameÚreturnc                 ód   • [        X5      u  pE[        U[        R                  R                  5      $ ©N)r   r1   r   ÚnnÚLinear)r   r:   r;   r&   ÚmoduleÚ_s         r   Úparam_needs_quantizationÚ'HqqHfQuantizer.param_needs_quantization’   s%   € Ü(¨Ó;‰	ˆô ˜&¤%§(¡(§/¡/Ó2Ð2r   c                 ó(   ^^• S mUU4S jTl         T$ )Nc                 óÔ   • [         R                  " UR                  U R                  5      U R	                  5       R                  5       5      nU R                  b  X R                  -  nU$ r>   )r   ÚmatmulÚtor   Ú
dequantizeÚtr   )r   ÚxÚouts      r   Úforward_with_deviceÚEHqqHfQuantizer._patch_layer_for_multigpu.<locals>.forward_with_deviceì   sJ   € Ü—,’,˜qŸt™t D§K¡KÓ0°$·/±/Ó2C×2EÑ2EÓ2GÓHˆCØ�y‰yÑ$Ø—y‘yÑ �ØˆJr   c                 ó   >• T" TU 5      $ r>   © )rK   rM   Ú	hqq_layers    €€r   Ú<lambda>Ú:HqqHfQuantizer._patch_layer_for_multigpu.<locals>.<lambda>ò   s   ø€ Ñ&9¸)ÀQÔ&Gr   )Úforward)r   rQ   rM   s    `@r   Ú_patch_layer_for_multigpuÚ(HqqHfQuantizer._patch_layer_for_multigpuë   s   ù€ ò	õ Hˆ	ÔØÐr   c                 ó*   • [        XR                  S9ng )N)r   )r   r   ©r   r:   r&   s      r   Ú$_process_model_before_weight_loadingÚ3HqqHfQuantizer._process_model_before_weight_loadingõ   s   € ô ' u×BZÑBZÑ[‰r   c                 óV   • [        USS5        [        USU R                  5       5        U$ )NÚis_hqq_quantizedTÚis_hqq_serializable)ÚsetattrÚis_serializablerX   s      r   Ú#_process_model_after_weight_loadingÚ2HqqHfQuantizer._process_model_after_weight_loadingþ   s*   € Ü�Ð)¨4Ô0Ü�Ð,¨d×.BÑ.BÓ.DÔEØˆr   c                 ó   • g©NTrP   r   s    r   r_   ÚHqqHfQuantizer.is_serializable  s   € Ør   c                 ó   • grc   rP   r   s    r   Úis_trainableÚHqqHfQuantizer.is_trainable  s   € àr   )r   r%   r#   )r:   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úrequires_calibrationÚ__annotations__r"   r8   ÚstrÚboolrC   rU   rY   r`   r_   Úpropertyrf   Ú__static_attributes__Ú__classcell__)r'   s   @r   r   r   .   sy   ø‡ ñð
 !ÐØ$Ó$õ	KòIð`3Ð.?ð 3ÈSð 3Ð_cô 3òrð\à ô\ôò
ð ð˜dó ó ör   r   )Útypingr   Úintegrationsr   Úutilsr   r   r   Úbaser
   Úquantizers_utilsr   Úmodeling_utilsr   Úutils.quantization_configr   r   Úhqq.core.quantizer   rq   r   Ú
get_loggerrh   r.   r   rP   r   r   Ú<module>r}      s|   ðõ !å 1ß AÑ AÝ Ý 2ö Ý0Ý5ñ ×ÑÛá×ÑÝ+ð
 ñLó ðLð €IÔà	×	Ò	˜HÓ	%€ôZ�[õ Zr   