+
    QV-j‚  ã                   óÆ   € ^ RI HtHt ^RIHtHtHt ^RIHt ^RI	H
t
 ]! 4       '       d   ^ RIt]'       d   ^RIHt ^RIHt ]P                   ! ]4      t ! R R	]4      tR# )
é    )ÚTYPE_CHECKINGÚAny)Úis_kernels_availableÚis_torch_availableÚlogging)ÚHfQuantizer)Úget_module_from_nameN)ÚPreTrainedModel)ÚMetalConfigc                   ó°   a a€ ] tR t^t oRtRtV 3R ltR tV3R lR ltV3R lR lt	V3R	 lR
 lt
R t]V3R lR l4       tR tR tV3R ltRtVtV ;t# )ÚMetalHfQuantizera  
Quantizer for Metal affine quantization on Apple Silicon (MPS) devices.

Uses the ``quantization-mlx`` Metal kernels from the Hub to pack weights into
low-bit (2/4/8) uint32 tensors with per-group scales and biases, and performs
fused dequant + matmul in the forward pass.
Fc                ó*   <€ \         SV `  ! V3/ VB  R # )N)ÚsuperÚ__init__)ÚselfÚquantization_configÚkwargsÚ	__class__s   &&,€Úx/Volumes/fast/ai/experiments/ui-tars-smoke/.venv/lib/python3.14/site-packages/transformers/quantizers/quantizer_metal.pyr   ÚMetalHfQuantizer.__init__+   s   ø€ Ü‰ÒÐ,Ñ7°Ô7ó    c                óx  € V P                   P                  '       d   R # \        P                  P                  P                  4       '       gF   V P                  '       d)   \        P                  R4       RV P                   n        R # \        R4      h\        4       '       g   \        R4      hVP                  R4      pVf   \        P                  R4       R # \        V\        4      '       dL   V P                  '       g8   RVP                  4       9   g   RVP                  4       9   d   \!        R	4      hR # R # R # )
Nz�Metal quantization requires an Apple Silicon GPU (MPS), but none is available. We will default to dequantizing the model to the original dtype.TzLMetal quantization requires an Apple Silicon GPU (MPS). No MPS device found.z:Metal quantization requires kernels: `pip install kernels`Ú
device_mapzYou have loaded a Metal quantized model on CPU and have an MPS device available. Set device_map='mps' to use the Metal kernels.ÚcpuÚdiskz—Metal quantization on the fly does not support CPU or disk in the device_map. Please use a pre-quantized checkpoint or remove CPU/disk from device_map.)r   Ú
dequantizeÚtorchÚbackendsÚmpsÚis_availableÚpre_quantizedÚloggerÚwarning_onceÚRuntimeErrorr   ÚImportErrorÚgetÚ
isinstanceÚdictÚvaluesÚ
ValueError)r   Úargsr   r   s   &*, r   Úvalidate_environmentÚ%MetalHfQuantizer.validate_environment.   s
  € Ø×#Ñ#×.×.Ð.Ùä�~‰~×!Ñ!×.Ñ.×0Ò0Ø×!×!Ð!Ü×#Ñ#ðWôð 7;�×(Ñ(Ô3Ùä"Ð#qÓrÐrä#×%Ò%ÜÐZÓ[Ð[à—Z‘Z Ó-ˆ
ØÒÜ×ÑðAöô ˜
¤D×)Ò)Ø×%×%Ð%¨5°J×4EÑ4EÓ4GÔ+GÈ6ÐU_×UfÑUfÓUhÔKhÜ ð`óð ñ LiÑ%ñ *r   c                ón   <€ V ^8„  d   QhRS[ S[S[3,          R,          RS[ S[S[3,          R,          /# )é   r   NÚreturn)r(   Ústrr   )ÚformatÚ__classdict__s   "€r   Ú__annotate__ÚMetalHfQuantizer.__annotate__M   s7   ø€ ÷ ñ ©D±±c°­N¸TÕ,Að ÁdÉ3ÑPSÈ8ÅnÐW[ÕF[ñ r   c                ó   € Vf   RR/pV# )NÚ r   © )r   r   s   &&r   Úupdate_device_mapÚ"MetalHfQuantizer.update_device_mapM   s   € ØÒØ˜e˜ˆJØÐr   c                ó*   <€ V ^8„  d   QhRRRS[ RS[/# )r/   Úmodelr
   Ú
param_namer0   )r1   Úbool)r2   r3   s   "€r   r4   r5   R   s$   ø€ ÷ ñ Ð.?ð ÉSð Ñ_cñ r   c                óˆ   € ^RI Hp \        W4      w  rV\        WT4      '       d   V P                  '       g   VR8w  d   R# R# R# )r/   )ÚMetalLinearÚweightFT)Úintegrations.metal_quantizationr@   r	   r'   r!   )r   r<   r=   r   r@   ÚmoduleÚtensor_names   &&&,   r   Úparam_needs_quantizationÚ)MetalHfQuantizer.param_needs_quantizationR   s9   € ÝAä2°5ÓEÑˆÜ�f×*Ò*Ø×!×!Ð! [°HÔ%<ÙÙÙr   c                ó   <€ V ^8„  d   QhRR/# )r/   r<   r
   r8   )r2   r3   s   "€r   r4   r5   \   s   ø€ ÷ 
ñ 
Ð:Kñ 
r   c                óÐ   € ^RI Hp V P                  WP                  P                  VP
                  4      V n        V! VV P                  V P                  V P                  R7      pR# )r/   )Úreplace_with_metal_linear)Úmodules_to_not_convertr   r!   N)rB   rI   Úget_modules_to_not_convertr   rJ   Ú_keep_in_fp32_modulesr!   )r   r<   r   rI   s   &&, r   Ú$_process_model_before_weight_loadingÚ5MetalHfQuantizer._process_model_before_weight_loading\   sY   € ÝOà&*×&EÑ&EØ×+Ñ+×BÑBÀE×D_ÑD_ó'
ˆÔ#ñ *ØØ#'×#>Ñ#>Ø $× 8Ñ 8Ø×,Ñ,ô	
Šr   c                ó   € R # )Tr8   ©r   s   &r   Úis_serializableÚ MetalHfQuantizer.is_serializablej   s   € Ùr   c                ó    <€ V ^8„  d   QhRS[ /# )r/   r0   )r>   )r2   r3   s   "€r   r4   r5   n   s   ø€ ÷ ñ ™dñ r   c                ó   € R # )Fr8   rP   s   &r   Úis_trainableÚMetalHfQuantizer.is_trainablem   s   € ár   c                ó   € ^RI Hp V! V 4      # )r/   )ÚMetalQuantize)rB   rX   )r   rX   s   & r   Úget_quantize_opsÚ!MetalHfQuantizer.get_quantize_opsq   s   € ÝCá˜TÓ"Ð"r   c                ó¤   € ^RI Hp ^RIHp V P                  '       d2   V P
                  P                  '       d   V! . RORV! V 4      .R7      .# . # )r/   )ÚWeightConverter)ÚMetalDequantizerA   )Úsource_patternsÚtarget_patternsÚ
operations)zweight$ÚscalesÚqbiases)Úcore_model_loadingr\   rB   r]   r!   r   r   )r   r\   r]   s   &  r   Úget_weight_conversionsÚ'MetalHfQuantizer.get_weight_conversionsv   sO   € Ý8ÝEà××Ð $×":Ñ":×"E×"EÐ"EáÚ$DØ$,Ù /°Ó 5Ð6ôðð ð ˆ	r   c                ó$   <€ V ^8„  d   Qh/ R;R&   # )r/   r   r   r8   )r2   r3   s   "€r   r4   r5      s   ø‡ ‚ ð 'Ñ&ò r   )rJ   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úrequires_calibrationr   r,   r9   rE   rM   rQ   ÚpropertyrU   rY   rd   Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__Ú__classcell__)r   r3   s   @@r   r   r      se   ù‡ € ñð !Ðõ8ò÷>ð ÷
ð ÷
ð 
òð ÷ó ðò#ò
÷o … r   r   )Útypingr   r   Úutilsr   r   r   Úbaser   Úquantizers_utilsr	   r   Úmodeling_utilsr
   Úutils.quantization_configr   Ú
get_loggerrg   r"   r   r8   r   r   Ú<module>ry      sK   ð÷ &ç EÑ EÝ Ý 2ñ ×ÒÛçÝ0Ý7à	×	Ò	˜HÓ	%€ôc�{ö cr   