
    @Ki                         S SK r S SKJrJrJr  S SKJr  SSKJr  SSK	J
r
  \(       a  SSKJr  SS	KJrJrJrJr  SS
KJr  \" 5       (       a  S SKr\R*                  " \5      r " S S\5      rg)    N)TYPE_CHECKINGOptionalUnion)version   )HfQuantizer)get_module_from_name   )PreTrainedModel)is_accelerate_availableis_optimum_quanto_availableis_torch_availablelogging)QuantoConfigc                   B  ^  \ rS rSrSrSS/rSrSrS\4U 4S jjr	S	 r
S
 rS rS#S jrS\\   S\S\\   4S jrSSS\S\4S jrS\\\\\4   4   S\\\\\4   4   4S jrSSSSS\SS4S jrS$S jr S%SSS\\\      4S jjrS r\S\4S  j5       rS%S! jrS"rU =r $ )&QuantoHfQuantizer)   z"
Quantizer for the quanto library
quanto
accelerateTFquantization_configc                 H   > [         TU ]  " U40 UD6  U R                  5         g N)super__init__	post_init)selfr   kwargs	__class__s      j/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/transformers/quantizers/quantizer_quanto.pyr   QuantoHfQuantizer.__init__2   s     ,77    c                 l    U R                   R                  b  U R                  (       d  [        S5      egg)z
Safety checker
NzWe don't support quantizing the activations with transformers library.Use quanto library for more complex use cases such as activations quantization, calibration and quantization aware training.)r   activationspre_quantized
ValueErrorr   s    r   r   QuantoHfQuantizer.post_init6   s;     ##//;DDVDVO  EW;r!   c                 l    [        5       (       d  [        S5      e[        5       (       d  [        S5      eg )NzhLoading an optimum-quanto quantized model requires optimum-quanto library (`pip install optimum-quanto`)z`Loading an optimum-quanto quantized model requires accelerate library (`pip install accelerate`))r   ImportErrorr   )r   argsr   s      r   validate_environment&QuantoHfQuantizer.validate_environment@   s;    *,,z  '((r  )r!   c                 >    Uc  SS0n[         R                  S5        U$ )N cpuzThe device_map was not initialized. Setting device_map to {'':'cpu'}. If you want to use the model for inference, please set device_map ='auto')loggerinfo)r   
device_maps     r   update_device_map#QuantoHfQuantizer.update_device_mapJ   s+    eJKK\
 r!   returnc                 V    Uc%  [         R                  S5        [        R                  nU$ )NzPYou did not specify `dtype` in `from_pretrained`. Setting it to `torch.float32`.)r0   r1   torchfloat32)r   dtypes     r   update_dtypeQuantoHfQuantizer.update_dtypeT   s"    =KKjkMMEr!   missing_keysprefixc                 |   [        5       (       a  SSKJn  / nUR                  5        Hs  u  pg[	        UW5      (       d  M  U HU  nXh;   d  Xc SU 3;   d  M  UR                  S5      (       a  M,  UR                  S5      (       a  MD  UR                  U5        MW     Mu     U V	s/ s H  oU;  d  M
  U	PM     sn	$ s  sn	f )Nr   QModuleMixin.z.weightz.bias)r   optimum.quantor@   named_modules
isinstanceendswithappend)
r   modelr<   r=   r@   not_missing_keysnamemodulemissingks
             r   update_missing_keys%QuantoHfQuantizer.update_missing_keysZ   s    &((3!//1LD&,//+GDhay4I,I ' 0 0 ; ; ' 0 0 9 9(//8  , 2 (E<a4D+D<EEEs   #	B90B9rG   r   
param_namec                     [        5       (       a  SSKJn  [        X5      u  pV[	        UW5      (       a  SU;   a  UR
                  (       + $ g)Nr   r?   weightF)r   rB   r@   r	   rD   frozen)r   rG   rO   r   r@   rJ   tensor_names          r   param_needs_quantization*QuantoHfQuantizer.param_needs_quantizationj   s?    &((325Efl++K0G}}$$r!   
max_memoryc                 `    UR                  5        VVs0 s H
  u  p#X#S-  _M     nnnU$ s  snnf )Ng?)items)r   rV   keyvals       r   adjust_max_memory#QuantoHfQuantizer.adjust_max_memoryv   s5    6@6F6F6HI6H(#c:o6H
I Js   *param_valueztorch.Tensortarget_deviceztorch.devicec                     SSK Jn  U" XUR                  U5      5        [        X5      u  pxUR	                  5         SUR
                  l        g )Nr
   )_load_parameter_into_modelF)modeling_utilsr`   tor	   freezerQ   requires_grad)	r   rG   r]   rO   r^   r   r`   rJ   _s	            r   create_quantized_param(QuantoHfQuantizer.create_quantized_paramz   s<     	@"5knn]6ST(;	&+#r!   c                 T   [         R                  " [        R                  R                  S5      5      [         R                  " S5      :  aS  SSKJn  [        R                  UR                  UR                  UR                  S.nX0R                  R                     nU$ [        S5      e)Nr   z0.27.0r   )CustomDtype)int8float8int4int2zYou are using `device_map='auto'` on an optimum-quanto quantized model. To automatically compute the appropriate device map, you should upgrade your `accelerate` library,`pip install --upgrade accelerate` or install it from source.)r   parse	importlibmetadataaccelerate.utilsri   r7   rj   FP8INT4INT2r   weightsr%   )r   target_dtyperi   mappings       r   adjust_target_dtype%QuantoHfQuantizer.adjust_target_dtype   s    ==++33LABW]]S[E\\4 

%//#((#((	G ##;#;#C#CDLP r!   keep_in_fp32_modulesc                     SSK Jn  U R                  XR                  R                  U5      U l        U" XR                  U R                  S9u  pU R                  UR
                  l        g )Nr
   )replace_with_quanto_layers)modules_to_not_convertr   )integrationsr|   get_modules_to_not_convertr   r}   config)r   rG   rz   r   r|   re   s         r   $_process_model_before_weight_loading6QuantoHfQuantizer._process_model_before_weight_loading   s`     	>&*&E&E++BBDX'
# .*E*E[_[s[s
 ,0+C+C(r!   c                     U$ r    )r   rG   r   s      r   #_process_model_after_weight_loading5QuantoHfQuantizer._process_model_after_weight_loading   s    r!   c                     g)NTr   r&   s    r   is_trainableQuantoHfQuantizer.is_trainable   s    r!   c                     g)NFr   )r   safe_serializations     r   is_serializable!QuantoHfQuantizer.is_serializable   s    r!   )r}   )r9   torch.dtyper5   r   )rv   r   r5   r   r   )!__name__
__module____qualname____firstlineno____doc__required_packages requires_parameters_quantizationrequires_calibrationr   r   r   r+   r3   r:   liststrrM   boolrT   dictr   intr[   rf   rx   r   r   r   propertyr   r   __static_attributes____classcell__)r   s   @r   r   r   )   sE    "<0'+$ L FtCy F# FRVWZR[ F 
.? 
S 
_c 
DeCHo1E,F 4PSUZ[^`c[cUdPdKe , , $, 	,
 &,( UYD&D>FtCy>QD d   r!   r   )ro   typingr   r   r   	packagingr   baser   quantizers_utilsr	   ra   r   utilsr   r   r   r   utils.quantization_configr   r7   
get_loggerr   r0   r   r   r!   r   <module>r      s\     1 1   2 0  5 			H	%I Ir!   