
    Z j                         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	)    )TYPE_CHECKING   )HfQuantizer)get_module_from_name   )PreTrainedModel)is_accelerate_availableis_optimum_quanto_availableis_torch_availablelogging)QuantoConfigNc                      ^  \ rS rSr% SrSrS\S'   S\4U 4S jjrS r	SS	S
\
S\4S jrS\\
\\
-  4   S\\
\\
-  4   4S jrSS	S
\
SSS\4U 4S jjrSS jr\S\4S j5       rS rS rSrU =r$ )QuantoHfQuantizer&   z"
Quantizer for the quanto library
Fr   quantization_configc                    > [         TU ]  " U40 UD6  SSSSS.nUR                  U R                  R                  S 5      U l        g )Nr   g      ?g      ?)int8float8int4int2)super__init__getr   weightsquantized_param_size)selfr   kwargsmap_to_param_size	__class__s       y/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/quantizers/quantizer_quanto.pyr   QuantoHfQuantizer.__init__.   sN    ,77	
 %6$9$9$:R:R:Z:Z\`$a!    c                    [        5       (       d  [        S5      e[        5       (       d  [        S5      eUR                  S5      n[	        U[
        5      (       aB  [        U5      S:  a  SUR                  5       ;   d  SUR                  5       ;   a  [        S5      eU R                  R                  b  [        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`)
device_mapr   cpudiskzYou are attempting to load an model with a device_map that contains a CPU or disk device.This is not supported with quanto when the model is quantized on the fly. Please remove the CPU or disk device from the device_map.zWe 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
   ImportErrorr	   r   
isinstancedictlenvalues
ValueErrorr   activations)r   argsr   r$   s       r    validate_environment&QuantoHfQuantizer.validate_environment8   s    *,,z  '((r  ZZ-
j$'':"u
0A0A0C'CvQ[QbQbQdGd P 
 ##//;O  <r"   modelr   
param_namereturnc                 x    SSK Jn  [        X5      u  pV[        XT5      (       a  SU;   a  UR                  (       + $ g)Nr   )QModuleMixinweightF)optimum.quantor5   r   r(   frozen)r   r1   r2   r   r5   moduletensor_names          r    param_needs_quantization*QuantoHfQuantizer.param_needs_quantizationO   s4    /25Ef++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   r=   keyvals       r    adjust_max_memory#QuantoHfQuantizer.adjust_max_memoryZ   s5    6@6F6F6HI6H(#c:o6H
I Js   *paramztorch.Tensorc                    > U R                  X5      (       a  U R                  b  U R                  $ [        TU ]  XU5      $ )z4Return the element size (in bytes) for `param_name`.)r;   r   r   param_element_size)r   r1   r2   rD   r   s       r    rF   $QuantoHfQuantizer.param_element_size^   s=    ((;;@Y@Y@e,,,w)%UCCr"   c                     SSK Jn  U R                  XR                  R                  UR
                  5      U l        U" XR                  U R                  S9ng )Nr   )replace_with_quanto_layers)modules_to_not_convertr   )integrationsrI   get_modules_to_not_convertr   rJ   _keep_in_fp32_modules)r   r1   r   rI   s       r    $_process_model_before_weight_loading6QuantoHfQuantizer._process_model_before_weight_loadinge   sM    =&*&E&E++BBED_D_'
# +*E*E[_[s[s
r"   c                     g)NT r   s    r    is_trainableQuantoHfQuantizer.is_trainablep   s    r"   c                     g)NFrQ   rR   s    r    is_serializable!QuantoHfQuantizer.is_serializablet   s    r"   c                     SSK Jn  U" U 5      $ )Nr   )QuantoQuantize)integrations.quantorY   )r   rY   s     r    get_quantize_ops"QuantoHfQuantizer.get_quantize_opsw   s    8d##r"   )rJ   r   )r1   r   )__name__
__module____qualname____firstlineno____doc__requires_calibration__annotations__r   r   r/   strboolr;   r)   intrB   floatrF   rN   propertyrS   rV   r[   __static_attributes____classcell__)r   s   @r    r   r   &   s     !''bL b.	.? 	S 	_c 	DcCi,@ T#sUXy.EY D(9 Ds DSa Dfk D	
 d  $ $r"   r   )typingr   baser   quantizers_utilsr   modeling_utilsr   utilsr	   r
   r   r   utils.quantization_configr   torch
get_loggerr]   loggerr   rQ   r"   r    <module>rt      sS    !  2 0  5 			H	%T$ T$r"   