
    Z j+                     
   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    v/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/quantizers/quantizer_hqq.pyweightr   %   s    {{1D$6$6t{{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 )NzA 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diskzYou 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ZZ-
j$''
))++v9J9J9L/L h 
 (+3z/@/@/B+C'Dq'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_deviceEHqqHfQuantizer._patch_layer_for_multigpu.<locals>.forward_with_device   sJ    ,,qttDKK0$//2C2E2E2GHCyy$yy 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_loading3HqqHfQuantizer._process_model_before_weight_loading   s     'uBZBZ[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_loading2HqqHfQuantizer._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.? 3S 3_c 3r\ \
 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   