
    ^j                         d dl mZmZmZ d dlmZ ddlmZ  e       rddlZd dl	m
Z
 d dlmZ  ej                  e      Z G d	 d
e      Zy)   )is_compressed_tensors_availableis_torch_availablelogging)CompressedTensorsConfig   )HfQuantizer    N)WeightConverter)DecompressExpertsc                        e Zd ZU dZdZeed<   def fdZd ZddZ	d Z
d	 Zd
 Zed        ZdefdZdefdZd Zd Z xZS )CompressedTensorsHfQuantizerz
    Quantizer for the compressed_tensors package.  Loads and restores models to
    quantized state with compressed_tensors
    Tquantization_configc                     t        |   |fi | |j                          ddlm} |j                  |      | _        |j                  | _        || _        y )Nr	   )ModelCompressor)	super__init__	post_initcompressed_tensors.compressorsr   from_compression_config
compressorrun_compressedr   )selfr   kwargsr   	__class__s       /var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/quantizers/quantizer_compressed_tensors.pyr   z%CompressedTensorsHfQuantizer.__init__'   sP    ,77
 	%%'B)AABUV1@@#6     c                 .    t               st        d      y )NzyUsing `compressed_tensors` quantized models requires compressed-tensors>=0.15.0: `pip install compressed-tensors>=0.15.0`)r   ImportError)r   argsr   s      r   validate_environmentz1CompressedTensorsHfQuantizer.validate_environment4   s    .0;  1r   returnc                 V    |t         j                  k7  rt        j                  d       |S )NzZWe suggest you to set `dtype=torch.float16` for better efficiency with compressed_tensors.)torchfloat16loggerinfo)r   dtypes     r   update_dtypez)CompressedTensorsHfQuantizer.update_dtype;   s     EMM!KKtur   c                     ddl m} | j                  j                  } |||| j                         | j                  j
                  r| j                  j                  |       y y )Nr	   )apply_quantization_configmodel)compressed_tensors.quantizationr*   r   r   r   is_quantization_compressedcompress_model)r   r,   r   r*   ct_quantization_configs        r   $_process_model_before_weight_loadingzACompressedTensorsHfQuantizer._process_model_before_weight_loading@   sS    M!%!D!D 	"%)?ATATU##>>OO***7 ?r   c                     | j                   j                  r*| j                  s| j                  j	                  |       yyy)z3Decompress loaded model if necessary - need for qatr+   N)r   r.   r   r   decompress_model)r   r,   r   s      r   #_process_model_after_weight_loadingz@CompressedTensorsHfQuantizer._process_model_after_weight_loadingJ   s:     ##>>tGZGZOO,,5,9 H[>r   c                     dddddd}|j                         C|j                         j                  )|j                         j                  j                  |       |S )Ncolwiserowwise)z0layers.*.feed_forward.experts.*.gate_proj.weightz6layers.*.feed_forward.experts.*.gate_proj.weight_scalez.layers.*.feed_forward.experts.*.up_proj.weightz4layers.*.feed_forward.experts.*.up_proj.weight_scalez0layers.*.feed_forward.experts.*.down_proj.weight)get_text_configbase_model_tp_planupdate)r   configadditional_plans      r   update_tp_planz+CompressedTensorsHfQuantizer.update_tp_planR   s_    @IFO>GDM@I
 !!#/F4J4J4L4_4_4k""$77>>Or   c                      y)NT r   s    r   is_trainablez)CompressedTensorsHfQuantizer.is_trainable_       r   c                 N    | j                    xs | j                  j                   S )z7Loaded Models can carry out quantization aware training)r   r   r.   r@   s    r   is_qat_trainablez-CompressedTensorsHfQuantizer.is_qat_trainablec   s'     &&&ad.F.F.a.a*aar   c                      y)z>Models quantized using compressed tensors can be saved to diskTr?   r@   s    r   is_serializablez,CompressedTensorsHfQuantizer.is_serializableh   rB   r   c                 <    t        g ddt        |       g      g}|S )N)z.weight_packed$z.weight_scale$z.weight_shape$weightsource_patternstarget_patterns
operations)r
   r   )r   dequant_conversionss     r   get_weight_conversionsz3CompressedTensorsHfQuantizer.get_weight_conversionsl   s1     !
 !*-d34

 #"r   c                    g }|D ]%  }t        |t              rt        d |j                  D              r|j	                  |       B|j                  D cg c]  }|j                  d      s| }}|r|D cg c]  }|dz   	 }}|D cg c]  }|dz   	 }}|D cg c]  }|dz   	 }}|j                  D cg c]  }|j                  d      r| }	}||z   |z   |	z   }
t        |       gt        |j                        z   }t        |
|j                  |      }|j	                  |       ( |j                  | j                                |S c c}w c c}w c c}w c c}w c c}w )Nc              3   $   K   | ]  }d |v 
 yw)expertsNr?   ).0ps     r   	<genexpr>zICompressedTensorsHfQuantizer.update_weight_conversions.<locals>.<genexpr>   s     ;mSTIQ<N;ms   z.weightz_packed$z_scale$z_shape$rI   )
isinstancer
   anyrJ   appendendswithr   listrL   _original_target_patternsextendrN   )r   weight_conversionsupdatedconvrS   weight_sourcespacked_weightscale_sourcesshape_sourcesothernew_sourcesnew_opss               r   update_weight_conversionsz6CompressedTensorsHfQuantizer.update_weight_conversions~   sW   & 	!DdO4;mX\XlXl;m8mt$)-)=)=WAIAVaWNW9G HAZ H H8F G1Y G G8F G1Y G G$($8$8Vq

9@UVV+m;mKeS,T23d4??6KK&$/$($B$B&
 NN4 %	!& 	t2245 X H G GVs*   E,E8E
EE8E#E#)r'   torch.dtyper!   rg   )__name__
__module____qualname____doc__requires_calibrationr   __annotations__r   r    r(   r1   r4   r=   propertyrA   boolrD   rF   rN   rf   __classcell__)r   s   @r   r   r      st    
  007,C 7
8:  b$ b
 #$r   r   )utilsr   r   r   utils.quantization_configr   baser   r#   core_model_loadingr
   integrations.compressed_tensorsr   
get_loggerrh   r%   r   r?   r   r   <module>rw      sG    Q P ?  4C 
		H	%v; vr   