ó
    qyüiÙ3  ã                   óÔ   • S SK Jr  SSKJr  \(       a  SSKJr  SSKJr  SSKJ	r	J
r
JrJrJr  SSKJr  \" 5       (       a
  S S	KrSS
KJr  \R&                  " \5      rS	r " S S\5      rg	)é    )ÚTYPE_CHECKINGé   )ÚHfQuantizeré   )ÚPreTrainedModel)ÚMxfp4Config)Úis_accelerate_availableÚis_kernels_availableÚis_torch_availableÚis_triton_availableÚlogging)Úget_module_from_nameN)ÚWeightConverterc                   ó¼   ^ • \ rS rSr% SrSrS\S'   U 4S jrS rS r	S	S
S\
S\4S jrSS jr SS	S
S\4S jjrS rS rS rS r\S\4S j5       rS rS rSrU =r$ )ÚMxfp4HfQuantizeré*   z'
FP4 quantization using fbgemm kernels
Fr   Úquantization_configc                 ó6   >• [         TU ]  " U40 UD6  S U l        g ©N)ÚsuperÚ__init__Útriton_kernels_hub)Úselfr   ÚkwargsÚ	__class__s      €Úd/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/quantizers/quantizer_mxfp4.pyr   ÚMxfp4HfQuantizer.__init__2   s   ø€ Ü‰ÒÐ,Ñ7°Ò7Ø"&ˆÕó    c                 ó¦   • U R                   c    SSKJn  U" S5      U l         U R                   $ U R                   $ ! [         a    [        S5      ef = f)z3Lazy import and initialize kernels only when neededr   )Ú
get_kernelz(kernels-community/gpt-oss-triton-kernelsz2kernels package is required for MXFP4 quantization)r   Úintegrations.hub_kernelsr    ÚImportError)r   r    s     r   Ú_lazy_import_kernelsÚ%Mxfp4HfQuantizer._lazy_import_kernels6   s]   € à×"Ñ"Ñ*ðXÝAá*4Ð5_Ó*`�Ô'ð ×&Ñ&Ð&ˆt×&Ñ&Ð&øô ó XÜ!Ð"VÓWÐWðXús	   �: ºAc                 óø  • [        5       (       d  [        S5      eU R                  R                  (       a  g [	        5       (       d  [        S5      e[
        R                  R                  5       =(       d    [
        R                  " S5      nUR                  S;  aK  U R                  (       a+  [        R                  SU S35        SU R                  l        g [        SU S	35      e[
        R                  R                  5       (       a  Sn[!        S
5      n[#        5       nOŠ[
        R$                  R                  5       (       a9  [
        R$                  R'                  5       nUS:¬  n[!        S5      n[#        5       nO.UR                  S:X  a  Sn[!        S
5      n[#        5       nOSnSnSnU R                  (       a‹  U(       d'  [        R                  S5        SU R                  l        g U(       d'  [        R                  S5        SU R                  l        g U(       d'  [        R                  S5        SU R                  l        g O6U(       d  [)        S5      eU(       d  [)        S5      eU(       d  [)        S5      eU R                  (       d  U R+                  5         UR-                  S5      nUbH  [/        U[0        5      (       a2  U R                  (       d   SUR3                  5       ;   a  [)        S5      eg g g g )NzqUsing mxfp4 quantization requires torchPlease install the latest version of torch ( pip install --upgrade torch )z9Using mxfp4 requires Accelerate: `pip install accelerate`Úcpu)ÚcudaÚxpur&   zGUsing MXFP4 quantized models requires model on cuda/xpu/cpu, but found zj, we will default to dequantizing the model to bf16. To use mxfp4, please disable the current accelerator.TzIQuantizing a model using MXFP4 requires model on cuda/xpu/cpu, but found z7. To use mxfp4, please disable the current accelerator.z3.5.0)é   é   z3.4.0FuÒ   MXFP4 quantization is only supported on GPUs with compute capability >= 7.5 (e.g T4, A100, L4, H100, or B200) or XPUs (e.g IntelÂ® Data Center GPU Max Series). We will default to dequantizing the model to bf16.zÄMXFP4 quantization requires Triton: CUDA requires Triton >= 3.4.0, XPU/CPU requires Triton >= 3.5.0. Please install triton: `pip install triton`. We will default to dequantizing the model to bf16.z„MXFP4 quantization requires the `kernels` package: `pip install kernels>=0.12.0`. We will default to dequantizing the model to bf16.u¥   MXFP4 quantization is only supported on GPUs with compute capability >= 7.5 (e.g T4, A100, L4, H100, or B200) or XPUs (e.g IntelÂ® Data Center GPU Max Series) or CPUz�MXFP4 quantization requires Triton: CUDA requires Triton >= 3.4.0, XPU/CPU requires Triton >= 3.5.0. Please install triton: `pip install triton`zPMXFP4 quantization requires the `kernels` package: `pip install kernels>=0.12.0`Ú
device_mapÚdiskzäYou are attempting to load an FP4 model with a device_map that contains a disk device.This is not supported when the model is quantized on the fly. Please use a quantized checkpoint or remove the disk device from the device_map.)r   r"   r   Ú
dequantizer	   ÚtorchÚacceleratorÚcurrent_acceleratorÚdeviceÚtypeÚpre_quantizedÚloggerÚwarning_onceÚRuntimeErrorr(   Úis_availabler   r
   r'   Úget_device_capabilityÚ
ValueErrorr#   ÚgetÚ
isinstanceÚdictÚvalues)	r   Úargsr   r1   Úis_device_supported_mxfp4Útriton_availableÚkernels_installedÚcompute_capabilityr+   s	            r   Úvalidate_environmentÚ%Mxfp4HfQuantizer.validate_environmentA   sÅ  € Ü!×#Ñ#Üð]óð ð
 ×#Ñ#×.×.Øä&×(Ñ(ÜÐYÓZÐZä×"Ñ"×6Ñ6Ó8×O¼E¿LºLÈÓ<OˆØ�;‰;Ð4Ó4Ø×!×!Ü×#Ñ#Ø]Ð^dÐ]eð  fPð  Qôð 7;�×(Ñ(Ô3Øä"Ø_Ð`fÐ_gð  h_ð  `óð ô �9‰9×!Ñ!×#Ñ#Ø(,Ð%Ü2°7Ó;ÐÜ 4Ó 6ÑÜ�Z‰Z×$Ñ$×&Ñ&Ü!&§¡×!AÑ!AÓ!CÐØ(:¸fÑ(DÐ%Ü2°7Ó;ÐÜ 4Ó 6ÑØ�[‰[˜EÓ!Ø(,Ð%Ü2°7Ó;ÐÜ 4Ó 6Ñà(-Ð%Ø$ÐØ %Ðà××Þ,Ü×#Ñ#ðIôð
 7;�×(Ñ(Ô3Øæ#Ü×#Ñ#ðIôð
 7;�×(Ñ(Ô3Øæ$Ü×#Ñ#ðIôð
 7;�×(Ñ(Ô3Øð %ö +Üðlóð ö "Üð`óð ö #ÜÐoÓpÐpà×!×!Ø×%Ñ%Ô'à—Z‘Z Ó-ˆ
ØÑ!¤j°¼T×&BÑ&BØ×%×%¨&°J×4EÑ4EÓ4GÓ*GÜ ðgóð ð +HÐ%ð 'CÐ!r   Úmodelr   Ú
param_nameÚreturnc                 óZ   • SSK Jn  [        X5      u  pV[        XT5      (       a  US;   a  ggg)Nr   ©ÚMxfp4GptOssExperts)Údown_proj_biasÚgate_up_proj_biasFT)ÚintegrationsrJ   r   r;   )r   rE   rF   r   rJ   ÚmoduleÚtensor_names          r   Úparam_needs_quantizationÚ)Mxfp4HfQuantizer.param_needs_quantization¡   s/   € Ý5ä2°5ÓEÑˆÜ�f×1Ñ1ØÐEÓEØØØr   c                 ó  • [         R                  R                  5       (       a  [         R                  R                  5         g [         R                  R                  5       (       a  [         R                  R                  5         g g r   )r.   r'   r7   Úempty_cacher(   )r   rE   r   s      r   Ú#_process_model_after_weight_loadingÚ4Mxfp4HfQuantizer._process_model_after_weight_loading«   sM   € ä�:‰:×"Ñ"×$Ñ$Ü�J‰J×"Ñ"Õ$Ü�Y‰Y×#Ñ#×%Ñ%Ü�I‰I×!Ñ!Õ#ð &r   Úuse_kernelsc                 ó  • SSK Jn  [        R                  R	                  5       =(       d    [        R
                  " S5      nU(       a6  UR                  S;  a&  [        R                  S5        SU R                  l
        U(       d6  UR                  S;   a&  [        R                  S5        SU R                  l
        U R                  XR                  R                  UR                  5      U l        U" XR                  U R                  S9ng )	Nr   )Úreplace_with_mxfp4_linearr&   )r&   zžYou are using full precision kernels, we will dequantize the model to bf16. To use the quantized model with quantization kernels, please set use_kernels=FalseTz¯MXFP4 inference on CPU requires use_kernels=True, but use_kernels is disabled. We will dequantize the model to bf16. To run MXFP4 natively on CPU, please set use_kernels=True.)Úmodules_to_not_convertr   )rM   rX   r.   r/   r0   r1   r2   r4   r5   r   r-   Úget_modules_to_not_convertrY   Ú_keep_in_fp32_modules)r   rE   rV   r   rX   r1   s         r   Ú$_process_model_before_weight_loadingÚ5Mxfp4HfQuantizer._process_model_before_weight_loading²   s×   € õ 	=ô ×"Ñ"×6Ñ6Ó8×O¼E¿LºLÈÓ<OˆÞ˜6Ÿ;™;¨gÓ5Ü×Ñðeôð 37ˆD×$Ñ$Ô/æ˜vŸ{™{¨gÓ5Ü×Ñðsôð 37ˆD×$Ñ$Ô/à&*×&EÑ&EØ×+Ñ+×BÑBÀE×D_ÑD_ó'
ˆÔ#ñ *Ø×*EÑ*EÐ[_×[sÑ[sñ
‰r   c                 ó–   • SUR                   R                  ;   a.  [        USS 5      b   UR                  R	                  SSSSS.5        U$ )NÚGptOssConfigÚbase_model_tp_planÚgrouped_gemm©z(layers.*.mlp.experts.gate_up_proj_blocksz(layers.*.mlp.experts.gate_up_proj_scalesz%layers.*.mlp.experts.down_proj_blocksz%layers.*.mlp.experts.down_proj_scales)r   Ú__name__Úgetattrr`   Úupdate©r   Úconfigs     r   Úupdate_tp_planÚMxfp4HfQuantizer.update_tp_planÓ   óR   € Ø˜V×-Ñ-×6Ñ6Ó6Ü�vÐ3°TÓ:ÑFØ×)Ñ)×0Ñ0àDRØDRØAOØAOñ	ôð ˆr   c                 ó–   • SUR                   R                  ;   a.  [        USS 5      b   UR                  R	                  SSSSS.5        U$ )Nr_   Úbase_model_ep_planra   rb   )r   rc   rd   rl   re   rf   s     r   Úupdate_ep_planÚMxfp4HfQuantizer.update_ep_planà   rj   r   c                 óJ  • SSK Jn  UR                  5       n[        UR                  SS5      n[        UR                  SS5      nUR                  5        GHF  u  pg[        Xr5      (       a"  [        US5      (       a  [        US5      (       d  M:  S	 GH  n[        Xx5      n	[        Xx S
35      n
U	R                  R                  R                  U	R                  R                  5      R                  SS5      nUS:X  a  UR                  USSS5      nOUR                  XESS5      nU
R                  R                  R                  R                  U
R                  R                  R                  5      R                  SS5      nX³U SU S3'   XÃU SU S3'   GM     GMI     0 nX=4$ )Nr   rI   Únum_local_expertsé    Úhidden_sizei@  Úgate_up_projÚ	down_proj)rs   rt   Ú_precision_configéÿÿÿÿéþÿÿÿéZ   é   Ú.Ú_blocksÚ_scales)rM   rJ   Ú
state_dictrd   rg   Únamed_modulesr;   ÚhasattrÚstorageÚlayoutÚunswizzle_dataÚdataÚ	transposeÚreshapeÚweight_scale)r   rE   rJ   r}   rp   rr   ÚnamerN   ÚprojÚtriton_tensorÚprecision_configÚblocksÚscalesÚmetadatas                 r   Úget_state_dict_and_metadataÚ,Mxfp4HfQuantizer.get_state_dict_and_metadataí   s‡  € Ý5à×%Ñ%Ó'ˆ
Ü# E§L¡LÐ2EÀrÓJÐÜ˜eŸl™l¨M¸4Ó@ˆà!×/Ñ/×1‰LˆDä˜6×6Ñ6Ü˜F N×3Ñ3Ü˜F K×0Ñ0áä5�Ü '¨Ó 5�Ü#*¨6°VÐ;LÐ3MÓ#NÐ à&×.Ñ.×5Ñ5×DÑDÀ]×EZÑEZ×E_ÑE_Ó`×jÑjÐkmÐoqÓr�Ø˜>Ó)Ø#Ÿ^™^Ð,=¸rÀ2ÀrÓJ‘Fà#Ÿ^™^Ð,=ÈBÐPRÓS�Fà)×6Ñ6×>Ñ>×EÑE×TÑTØ$×1Ñ1×9Ñ9×>Ñ>óç‘)˜B Ó#ð ð 7=˜d˜V 1 T F¨'Ð2Ñ3Ø6<˜d˜V 1 T F¨'Ð2Ô3ô 6ñ 2ð2 ˆØÐ#Ð#r   c                 ó   • g)NT© ©r   s    r   Úis_serializableÚ Mxfp4HfQuantizer.is_serializable  s   € Ør   c                 ó.   • [         R                  S5        g)Nz©MXFP4 quantization don't support training, please consider dequantizing the model first by passing quantization_config=Mxfp4Config(dequantize=True) to .from_pretrained()F)r4   r5   r’   s    r   Úis_trainableÚMxfp4HfQuantizer.is_trainable  s   € ä×Ñð xô	
ð r   c                 ó   • SSK Jn  U" U 5      $ )Nr   )ÚMxfp4Quantize)Úintegrations.mxfp4r™   )r   r™   s     r   Úget_quantize_opsÚ!Mxfp4HfQuantizer.get_quantize_ops  s   € Ý6á˜TÓ"Ð"r   c                 ó  • SSK JnJn  U R                  (       aD  U R                  R
                  (       a)  [        SS/SU" U 5      /S9[        SS/S	/U" U 5      /S9/$ [        SS/S	U" U 5      /S9[        SS/SU" U 5      /S9/$ )
Nr   )ÚMxfp4DequantizeÚMxfp4DeserializeÚdown_proj_blocksÚdown_proj_scalesz
down_proj$)Úsource_patternsÚtarget_patternsÚ
operationsÚgate_up_proj_blocksÚgate_up_proj_scaleszgate_up_proj$)rš   rž   rŸ   r3   r   r-   r   )r   rž   rŸ   s      r   Úget_weight_conversionsÚ'Mxfp4HfQuantizer.get_weight_conversions  sµ   € ßJà×× $×":Ñ":×"E×"EäØ%7Ð9KÐ$LØ$1Ù /°Ó 5Ð6ñô
  Ø%:Ð<QÐ$RØ%4Ð$5Ù /°Ó 5Ð6ñðð ô Ø!6Ð8MÐ NØ 0Ù,¨TÓ2Ð3ñô
 Ø!3Ð5GÐ HØ -Ù,¨TÓ2Ð3ñð
ð 	
r   )rY   r   )rE   r   )F)rc   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úrequires_calibrationÚ__annotations__r   r#   rC   ÚstrÚboolrP   rT   r\   rh   rm   rŽ   r“   Úpropertyr–   r›   r§   Ú__static_attributes__Ú__classcell__)r   s   @r   r   r   *   s¤   ø‡ ñð !ÐØ&Ó&õ'ò	'ò^ð@Ð.?ð ÈSð Ð_cô ô$ð "ñ
à ð
ð õ
òBòò!$òFð ð˜dó ó ðò#÷

ð 
r   r   )Útypingr   Úbaser   Úmodeling_utilsr   Úutils.quantization_configr   Úutilsr	   r
   r   r   r   Úquantizers_utilsr   r.   Úcore_model_loadingr   Ú
get_loggerrc   r4   r   r   r‘   r   r   Ú<module>r¼      s\   ðõ !å ö Ý0Ý7÷õ õ 3ñ ×ÑÛå4à	×	Ò	˜HÓ	%€ØÐ ôQ
�{õ Q
r   