Ë
    ÜÓ^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ÑAÐBUÓ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    € Ø”E—M‘MÒ!Ü�K‰KÐ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Ðñ 	" %Ð)?À×ATÑATÔUØ×#Ñ#×>Ò>Ø�O‰O×*Ñ*°Ð*Õ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:   € ð ×#Ñ#×>Ò>Àt×GZÒGZØ�O‰O×,Ñ,°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_   € à@IØFOØ>GØDMØ@Iñ
ˆð ×!Ñ!Ó#Ð/°F×4JÑ4JÓ4L×4_Ñ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'   € ð ×&Ñ&Ð&Òa¨d×.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   è ø€ Ò;mÐST¸IÈQÔ<NÑ;mùs   ‚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ä˜d¤OÔ4¼Ñ;mÐX\×XlÑXlÔ;mÔ8mØ—‘˜tÔ$ØØ)-×)=Ñ)=ÖW AÀÇÁÈIÕAVšaÐWˆNÐWÙØ9GÖ H°A  Z£Ð H�Ð HØ8FÖ G°1  Y£Ð G�Ð GØ8FÖ G°1  Y£Ð G�Ð GØ$(×$8Ñ$8ÖV˜qÀÇ
Á
È9Õ@UšÐV�ÐVØ+¨mÑ;¸mÑKÈeÑS�Ü,¨TÓ2Ð3´d¸4¿?¹?Ó6KÑK�Ü&Ø$/Ø$(×$BÑ$BØ&ô�ð
 �N‰N˜4Ö ð%	!ð& 	�‰�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   