
    Z j                      8   S SK r S SKJr  SSKJr  SSKJrJr  \(       a  SSKJ	r	  SSK
Jr  S S	KJr  SS
KJrJrJr  Sr\" 5       (       a  SSKJr  \" 5       (       a  S SKr\" 5       (       a  S SKJr  \R0                  " \5      rS\S\S-  4S jr " S S\5      rg)    N)TYPE_CHECKING   )HfQuantizer)get_module_from_nameshould_convert_module   )PreTrainedModel)TorchAoConfig)	safe_open)is_torch_availableis_torchao_availableloggingz2.5.0)WeightConverter)flatten_tensor_state_dictconfig_namereturnc                     [         R                  " SU R                  5       5      nU(       a  UR                  S5      $ S$ )z
Extract the size digit from torchao config class names like "Int4WeightOnlyConfig", "Int8WeightOnlyConfig".
Returns the digit as a string if found, otherwise None.
z
(\d)weightr   N)researchlowergroup)r   matchs     z/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/quantizers/quantizer_torchao.py_fuzzy_match_sizer   1   s2    
 IIm[%6%6%89E"5;;q>,,    c                     ^  \ rS rSr% SrSrS\S'   U 4S jrS rS r	S	S
S\
SSS\4U 4S jjrS\\
\\
-  4   S\\
\\
-  4   4S jrSSS jjrS	S
S\
S\4S jrS\4S jr\S\4S j5       r\S\4S j5       rS\\
   4S jrS rS rSrU =r$ )TorchAoHfQuantizer:   z7
Quantizer for torchao: https://github.com/pytorch/ao/
Fr
   quantization_configc                    > [         TU ]  " U40 UD6  [        [        U R                  R
                  5      R                  5      nUS:X  a  SU l        g SU l        g )N4g      ?r   )super__init__r   typer   
quant_type__name__quantized_param_size)selfr   kwargs
size_digit	__class__s       r   r#   TorchAoHfQuantizer.__init__B   sM    ,77&tD,D,D,O,O'P'Y'YZ
+5+<C!!!r   c                    [        5       (       d  [        S5      eUR                  S5      nSU l        [	        U[
        5      (       a  SUR                  5       ;   d  SUR                  5       ;   aZ  [        U5      S:  aJ  SUR                  5       ;   U l        U R                  (       a   SUR                  5       ;   a  [        S5      eg g g g g )NzSLoading an torchao quantized model requires torchao library (`pip install torchao`)
device_mapFdiskcpur   zYou are attempting to perform disk offload with a pre-quantized torchao model This is not supported yet . Please remove the disk device from the device_map.)
r   ImportErrorgetoffload_to_cpu
isinstancedictvalueslenpre_quantized
ValueError)r(   argsr)   r.   s       r   validate_environment'TorchAoHfQuantizer.validate_environmentH   s    #%%sttZZ-
#j$''*++--*:K:K:M1MSVWaSbefSf&+z/@/@/B&B#%%&J4E4E4G*G$i  +H% Tg1M (r   c                 4    [        UR                  5       5      $ )zf
We flatten the state dict of tensor subclasses so that it is compatible with the safetensors format.
)r   
state_dict)r(   models     r   get_state_dict_and_metadata.TorchAoHfQuantizer.get_state_dict_and_metadataW   s     ))9)9);<<r   r?   r	   
param_nameparamztorch.Tensorr   c                    > U R                  X5      (       a  U R                  b  U R                  $ [        TU ]  XU5      $ )z4Return the element size (in bytes) for `param_name`.)param_needs_quantizationr'   r"   param_element_size)r(   r?   rB   rC   r+   s       r   rF   %TorchAoHfQuantizer.param_element_size]   s=    ((;;@Y@Y@e,,,w)%UCCr   
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(   rH   keyvals       r   adjust_max_memory$TorchAoHfQuantizer.adjust_max_memoryd   s5    5?5E5E5GH5Gc9n5G
H Is   *c                 ~   U R                  XR                  R                  UR                  5      U l        U R                  R                  (       a  UR                  5       nUR                  5        VVs/ s H!  u  pV[        U5      [        U5      :X  d  M  UPM#     nnnUR                  5       nUR                  5        VVs/ s H!  u  pV[        U5      [        U5      :X  d  M  UPM#     n	nnU R                   V
s/ s H  oXy-   ;  d  M  U
PM     sn
U l        Ub  U R                  U5        g g s  snnf s  snnf s  sn
f N)
get_modules_to_not_convertr   modules_to_not_convert_keep_in_fp32_modulesinclude_input_output_embeddingsget_input_embeddingsnamed_modulesidget_output_embeddingsset_metadata)r(   r?   checkpoint_filesr)   	input_embnamemoduleinput_emb_names
output_emboutput_emb_namesxs              r   $_process_model_before_weight_loading7TorchAoHfQuantizer._process_model_before_weight_loadingi   s   &*&E&E++BBED_D_'
# ##CC224I8=8K8K8Mm8MQSTZQ[_abk_lQlt8MOm446J9>9L9L9No9NRTU[R\`bcm`nRn9No66+6a?Ce:e6+D' './ ( no+s$   4D.D.D4$D4;D:
D:c                    [        X R                  5      (       d  g[        X5      u  pE[        R                  R
                  /nU R                  R                  (       a)  UR                  [        R                  R                  5        SSK
JnJn  [        U R                  R                  U5      (       a  UR                  SS5      u  pU" XR                  R                  5      (       d_  U" X R                  R                  5      (       d>  SU R                  R                  R                   ;   a  [        U[#        U5      5      (       a  g[        U[#        U5      5      =(       a    US:H  $ )	NFr   )FqnToConfigfqn_matches_fqn_config.r   _defaultTweight)r   rR   r   torchnnLinearr   rT   append	Embeddingtorchao.quantizationre   rf   r4   r%   rsplitfqn_to_configtuple)r(   r?   rB   r)   r]   tensor_name_QUANTIZABLEre   rf   
module_fqn_s              r   rE   +TorchAoHfQuantizer.param_needs_quantizationy   s   $Z1L1LMM 35E(##CC 2 23Ld..99;GG&--c15MJ&z3K3K3V3VWW)*6N6N6Y6YZZ$":":"E"E"S"SS"65+>?? &%"56R;(;RRr   c                     gNT r(   s    r   is_serializable"TorchAoHfQuantizer.is_serializable   s    r   c                 l    [        [        U R                  R                  5      R                  5      S:H  $ )N8)r   r$   r   r%   r&   r{   s    r   is_trainableTorchAoHfQuantizer.is_trainable   s,     !d&>&>&I&I!J!S!STX[[[r   c                     gry   rz   r{   s    r   is_compileable!TorchAoHfQuantizer.is_compileable   s    r   rZ   c                     US   R                  S5      (       aO  0 nU H@  n[        USS9 nUR                  5       =(       d    0 nUR                  U5        S S S 5        MB     X l        g g ! , (       d  f       M[  = f)Nr   z.safetensorspt)	framework)endswithr   metadataupdate)r(   rZ   r   
checkpointf	metadata_s         r   rY   TorchAoHfQuantizer.set_metadata   sl    A''77H.
zT:a !

 2IOOI. ;: /
 %M 8 ;:s   +A**
A9	c                     SSK Jn  U" U 5      $ )Nr   )TorchAoQuantize)integrations.torchaor   )r(   r   s     r   get_quantize_ops#TorchAoHfQuantizer.get_quantize_ops   s    :t$$r   c                 ^    SSK Jn  U R                  (       a  [        / SQSU" U 5      /S9/$ / $ )Nr   )TorchAoDeserialize)_weight_qdata_weight_scale_and_zero_weight_per_tensor_scale_weight_scale_weight_zero_point_weight_act_pre_scaleri   )source_patternstarget_patterns
operations)r   r   r8   r   )r(   r   s     r   get_weight_conversions)TorchAoHfQuantizer.get_weight_conversions   s<    =% %- 24 89 " 	r   )r   rR   r3   r'   rP   )r?   r	   )r&   
__module____qualname____firstlineno____doc__requires_calibration__annotations__r#   r;   r@   strfloatrF   r5   intrM   rb   boolrE   r|   propertyr   r   listrY   r   r   __static_attributes____classcell__)r+   s   @r   r   r   :   s    !((D=D(9 Ds DSa Dfk DDcCi,@ T#sUXy.EY 
0 S.? SS S_c S6  \d \ \   %T#Y %%
 r   r   )r   typingr   baser   quantizers_utilsr   r   modeling_utilsr	   utils.quantization_configr
   safetensorsr   utilsr   r   r   MIN_TORCH_VERSIONcore_model_loadingr   rj   1torchao.prototype.safetensors.safetensors_supportr   
get_loggerr&   loggerr   r   r   rz   r   r   <module>r      s    
    I 09 ! E E   4 
 
		H	%-3 -3: -J Jr   