+
    ˜°³jkf  ã                   óâ  € R t ^ RIt^ RIt^ RIHt ^ RIHt ^ RIHtH	t	H
t
HtHt ]! ]P                  P                  P                   4      tR]9   d$   ]! ]	]]
].4      '       d   ]P'                  R4       ]'       d   ] U u. uF  q R-9  g   K  V NK  	  up tR.R ltRR]P*                  3R ltR t^ ^ÿ]P*                  3R	 ltR
]P2                  3R ltR tR t]R 4       t]R 4       tR tR t R t!R t"R t#R t$R t%R t&R t'R R lt(^^ut)t*](! ])^,
          4      t+R R lt,R R lt-R t.R  R! lt/R/R" lt0R# R$ lt1R% t2R& R' lt3^^ut4t5R( t6R0R) R* llt7^]Pp                  R+3R, lt9R# u up i )1z`Importing this file includes common utility methods for checking quantized
tensors and modules.
N)ÚTensor)Úcontextmanager)ÚTEST_WITH_TSANÚIS_PPCÚIS_MACOSÚ
IS_WINDOWSÚIS_ARM64Úqnnpackc                óÈ   € \         P                  ! V ^V,          ,           V,
          V^,
          V^,
          ,          ,
          V,          4      ^V,          ,           ^,           # )z7Computes the output shape given convolution parameters.)ÚnpÚfloor)Ú
input_sizeÚkernel_sizeÚpaddingÚstrideÚdilationÚoutput_paddings   &&&&&&Úi/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/testing/_internal/common_quantized.pyÚ_conv_output_shaper      s\   € ô �8Š8�Z ! g¥+Õ-°Õ;¸{ÈQ½Ø  1�õ?&õ &Ø)/õ0ó 1Ø34°~Õ3EõFØHIõJð Jó    c                óf  € Vf!   \         P                  ! V4      P                  pVf!   \         P                  ! V4      P                  p\         P                  ! W,          V,           4      P                  \         P                  4      p\         P                  ! WcV4      pVP                  V4      pV# )zQuantizes a numpy array.)r   ÚiinfoÚminÚmaxÚroundÚastypeÚint64Úclip)ÚxÚscaleÚ
zero_pointÚqminÚqmaxÚdtypeÚqxs   &&&&&& r   Ú	_quantizer%      sy   € à‚|Ü�xŠx˜‹×"Ñ"ˆØ‚|Ü�xŠx˜‹×"Ñ"ˆÜ	�Š�!•)˜jÕ(Ó	)×	0Ñ	0´·±Ó	:€BÜ	�Š�˜4Ó	 €BØ	�‰�5Ó	€BØ€Ir   c                óL   € V P                  \        4      V,
          V,          pV# )zDequantizes a numpy array.)r   Úfloat)r$   r   r    r   s   &&& r   Ú_dequantizer(   )   s   € à	�‰”5Ó	˜JÕ	&¨%Õ/€AØ€Hr   c                óŒ   € W,          P                  4       V,           p\        P                  ! WcV4      P                  V4      pV# )zdRequantizes a numpy array, i.e., intermediate int32 or int16 values are
converted back to given type)r   r   r   r   )r   Ú
multiplierr    r!   r"   Úqtyper$   s   &&&&&& r   Ú_requantizer,   /   s9   € ð �.×	Ñ	Ó	! JÕ	.€BÜ	�Š�˜4Ó	 ×	'Ñ	'¨Ó	.€BØ€Ir   Fc                ó<  € V\         P                  \         P                  39  d   \        RV 24      hV\         P                  8X  d$   V\         P                  8w  d   \        RV 24      h\        V \         P                  4      '       d   V P                  4       p V\         P                  8X  d   V'       d   R^?rTMR^rTMV'       d   ^ ^rTM^ ^ÿrTV P                  4       pV P                  4       pV\         P                  8H  pWg8X  d   Rp	^ p
MúV'       d`   \        Wv) 4      pV) pWv,
          WT,
          ,          p	\        V	\        P                  ! \        P                  4      P                  4      p	^ p
M“\        VR4      p\        VR4      pWv,
          WT,
          ,          p	\        V	\        P                  ! \        P                  4      P                  4      p	V\        Wi,          4      ,
          p
\        WJ4      p
\        WZ4      p
\        V	4      \!        V
4      .# )útCalculate the dynamic quantization parameters (scale, zero_point)
according to the min and max element of the tensorzFExpected qscheme to be per_tensor_affine or per_tensor_symmetric, got z<Expected dtype to be torch.qint8 for symmetric qscheme, got ç      ð?ç        iÀÿÿÿi€ÿÿÿ)ÚtorchÚper_tensor_affineÚper_tensor_symmetricÚAssertionErrorÚqint8Ú
isinstancer   Únumpyr   r   r   ÚfinfoÚfloat32Úepsr   r'   Úint)ÚXr#   Úreduce_rangeÚqschemer!   r"   Úmin_valÚmax_valÚis_symmetricr   r    s   &&&&       r   Ú_calculate_dynamic_qparamsrB   6   s²  € ð ”u×.Ñ.´×0JÑ0JÐKÔKÜØTÐU\ÐT]Ð^ó
ð 	
ð ”%×,Ñ,Ô,Ø”E—K‘KÔÜ ØNÈuÈgÐVóð ô �!”U—\‘\×"Ò"Ø�G‰G‹IˆØ”—‘ÔßØ˜b‘$à˜s‘$çØ˜C‘$à˜C�$Ø�e‰e‹g€GØ�e‰e‹g€GØœu×9Ñ9Ñ9€LØÔØˆØ‰
çÜ˜' 8Ó,ˆGØ�hˆGØÕ&¨4­;Õ7ˆEÜ˜œrŸxšx¬¯
©
Ó3×7Ñ7Ó8ˆEØ‰Jä˜' 3Ó'ˆGÜ˜' 3Ó'ˆGØÕ&¨4­;Õ7ˆEÜ˜œrŸxšx¬¯
©
Ó3×7Ñ7Ó8ˆEØ¤ g¥oÓ 6Õ6ˆJÜ˜TÓ.ˆJÜ˜TÓ.ˆJÜ�%‹Lœ#˜j›/Ð*Ð*r   c                óÜ  € \        V \        P                  4      '       d   V P                  4       p \        P                  ! V4      P
                  \        P                  ! V4      P                  r2W2,
          p\        P                  ! V P                  ^ ,          \        P                  R7      p\        P                  ! V P                  ^ ,          \        P                  R7      p\        VP                  ^ ,          4       Få  pV P                  4       pV P                  4       p	W‰8X  d   RWW&   ^ Wg&   K3  \        V	R4      p	\        VR4      pW˜,
          V,          WW&   \        WW,          \        P                  ! \        P                  4      P                  4      WW&   V\!        W…V,          ,          4      ,
          Wg&   \        W&V,          4      Wg&   \        W6V,          4      Wg&   Kç  	  WV3# )r.   ©r#   r/   r0   )r6   r1   r   r7   r   r   r   r   ÚzerosÚshapeÚfloat64r   Úranger8   r9   r:   r   )
r<   r#   r!   r"   Ún_levelsr   r    Úir?   r@   s
   &&        r   Ú&_calculate_dynamic_per_channel_qparamsrK   e   sU  € ô �!”U—\‘\×"Ò"Ø�G‰G‹IˆÜ—’˜UÓ#×'Ñ'¬¯ª°UÓ);×)?Ñ)?ˆ$Ø�{€HÜ�HŠH�Q—W‘W˜Q•Z¤r§z¡zÔ2€EÜ—’˜!Ÿ'™' !�*¬B¯H©HÔ5€JÜ�:×#Ñ# AÕ&Ö'ˆØ—%‘%“'ˆØ—%‘%“'ˆØÔØˆE‰HØˆJ‹Mä˜' 3Ó'ˆGÜ˜' 3Ó'ˆGØÕ)¨XÕ5ˆE‰HÜ˜5�8¤R§X¢X¬b¯j©jÓ%9×%=Ñ%=Ó>ˆE‰HØ ¤5¨¸µ8Õ);Ó#<Õ<ˆJ‰MÜ °¥mÓ4ˆJ‰MÜ °¥mÓ4ˆJ‹Mñ (ð ÐÐr   c                óˆ  € \        V \        \        34      '       dx   \        V 4      \        V4      8w  d$   \	        R\        V 4       R\        V4       24      h\        \        V 4      4       Uu. uF  p\        W,          W,          4      NK  	  ppV# VP                  '       d   VP                  4       pV P                  '       d   V P                  4       p W,
          P                  4       pV^ 8X  d   R\        R4      \        R4      3# V P                  4       pWT,          p^VP                  4       ,          pWTV3# u upi )a�  Calculates the signal to noise ratio and returns the signal and noise
power, as well as the SNR in dB.
If the input is a list/tuple this function is called recursively on each
element. The result will have the same nested structure as the inputs.

Args:
    x, x_hat: Either a tensor or a nested list/tuple of tensors.
Returns:
    signal, noise, SNR(in dB): Either floats or a nested list of floats
z#Expected len(x) == len(x_hat), got z != r0   Úinf)r6   ÚlistÚtupleÚlenr4   rH   Ú_snrÚis_quantizedÚ
dequantizeÚnormr'   Úlog10)r   Úx_hatÚidxÚresÚnoiseÚsignalÚsnrÚsnr_dbs   &&      r   rQ   rQ      s  € ô �!”dœE�]×#Ò#Üˆq‹6”S˜“ZÔÜ Ð#FÄsÈ1ÃvÀhÈdÔSVÐW\ÓS]ÐR^Ð!_Ó`Ð`Ü38¼¸Q»´=ÓA±=¨CŒt�A•F˜E�JÖ'±=ˆÐAØˆ
Ø××ÐØ× Ñ Ó"ˆØ‡~‡~€~Ø�L‰L‹NˆØ�Y×ÑÓ€EØ�„zØ”E˜%“L¤%¨£,Ð.Ð.Ø�V‰V‹X€FØ
�.€CØ�#—)‘)“+Õ€FØ˜&Ð Ð ùò Bs   Á/!D?c              #   ó$  "  € \         P                  P                  P                  pV \         P                  P                  n         R x € V\         P                  P                  n        R #   T\         P                  P                  n        i ; i5i©N©r1   ÚbackendsÚ	quantizedÚengine)ÚqengineÚpreviouss   & r   Úoverride_quantized_enginere   ›   sX   é € ä�~‰~×'Ñ'×.Ñ.€HØ&-„E‡N�N×ÑÔ#ð3Ûà*2Œ�‰× Ñ Ö'ø¨(Œ�‰× Ñ Õ'üs   ‚ABÁA, Á!BÁ,!BÂBc              #   ó  "  €  V '       d   \         P                  P                  4        R x € V '       d!   \         P                  P                  4        R # R #   T '       d    \         P                  P                  4        i i ; i5ir^   )r1   Ú_CÚ!_set_default_mobile_cpu_allocatorÚ#_unset_default_mobile_cpu_allocator)Úqengine_is_qnnpacks   &r   Ú"override_cpu_allocator_for_qnnpackrk   ¤   sQ   é € ð;ßÜ�H‰H×6Ñ6Ô8ÛçÜ�H‰H×8Ñ8Ö:ñ ø×Ü�H‰H×8Ñ8Õ:ð üs   ‚B„*A ®*BÁ)BÂBc                 ó   a € V 3R  lpV# )c                  ó’   <€ \          F'  p\        V4      ;_uu_ 4        S! V / VB  R R R 4       K)  	  R #   + '       g   i     K>  ; ir^   )Úsupported_qenginesre   )ÚargsÚkwargsrc   Ú	qfunctions   *, €r   Útest_fnÚ"override_qengines.<locals>.test_fn²   s7   ø€ ß)ˆGÜ*¨7×3Õ3á˜4Ð* 6Ò*÷ 4Ñ3ó *ß3×3Ð3ús	   ž	5µA© )rq   rr   s   f r   Úoverride_qenginesru   ±   s   ø€ õ+ð
 €Nr   c                  óP   € \         P                  P                  P                  R 8H  # )Úfbgemmr_   rt   r   r   Úqengine_is_fbgemmrx   ¹   ó   € Ü�>‰>×#Ñ#×*Ñ*¨hÑ6Ð6r   c                  óP   € \         P                  P                  P                  R 8H  # )r	   r_   rt   r   r   rj   rj   »   s   € Ü�>‰>×#Ñ#×*Ñ*¨iÑ7Ð7r   c                  óP   € \         P                  P                  P                  R 8H  # )Úonednnr_   rt   r   r   Úqengine_is_onednnr}   ½   ry   r   c                  óP   € \         P                  P                  P                  R 8H  # )Úx86r_   rt   r   r   Úqengine_is_x86r€   ¿   s   € Ü�>‰>×#Ñ#×*Ñ*¨eÑ3Ð3r   c                 ó�   € \        \        V P                  4       4      4      p^ W!&   W^ &   V P                  \	        V4      4      pW23# ©é    )rN   rH   ÚdimÚpermuterO   )r<   ÚaxisÚnew_axis_listÚys   &&  r   Ú_permute_to_axis_zeror‰   Ã   sB   € Üœ˜qŸu™u›w›Ó(€MØ€MÑØ�!ÑØ	�	‰	”%˜Ó&Ó'€AØÐÐr   c           	      ó  € V P                   p\        V P                  \        P                  4      V4      w  r\        P
                  ! V 4      p\        V P                  4       ^ ,          4       Fq  p	\        P                  ! \        P                  ! W	,          RW,          ,          ,          W),          ,           4      WE4      W),          ,
          W,          ,          W‰&   Ks  	  VP                  \        V4      4      p
V
P                  V4      # ©rƒ   r/   )r#   r‰   Útor1   r9   Ú
zeros_likerH   ÚsizeÚclampr   r…   rO   )r<   Úper_channel_scaleÚper_channel_zero_pointr†   Ú	quant_minÚ	quant_maxr#   Úpermute_axis_listrX   rJ   Úouts   &&&&&&     r   Ú+_fake_quantize_per_channel_affine_referencer–   Ì   sÈ   € Ø�G‰G€EÜ0°·±´e·m±mÓ1DÀdÓKÑ€AÜ
×
Ò
˜1Ó
€Cä�1—6‘6“8˜A•;ÖˆÜ—+’+œeŸkšk¨!­$°#Ð8IÕ8LÕ2LÕ*MØ(Õ+õ+,ó -Ø.7óDØF\ÕF_õ`ØctÕcwõxˆ‹ñ  ð �+‰+”eÐ-Ó.Ó
/€CØ�6‰6�%‹=Ðr   c                 ó  € VP                   p\        VP                  \        P                  4      V4      w  r\        P
                  ! V4      p	\        VP                  4       ^ ,          4       FB  p
\        P                  ! W,          RW*,          ,          ,          W:,          ,           4      Wš&   KD  	  V	P                  \        V4      4      p	W•8¬  W–8*  ,          p\        P
                  ! V 4      pW,          WË&   VP                  V4      # r‹   )r#   r‰   rŒ   r1   r9   r�   rH   rŽ   r   r…   rO   )ÚdYr<   r�   r‘   r†   r’   r“   r#   r”   ÚXqrJ   ÚmaskrX   s   &&&&&&&      r   Ú0_fake_quantize_per_channel_affine_grad_referencer›   Ú   sÂ   € Ø�G‰G€EÜ0°·±´e·m±mÓ1DÀdÓKÑ€AÜ	×	Ò	˜!Ó	€BÜ�1—6‘6“8˜A•;ÖˆÜ—’˜A�D CÐ*;Õ*>Õ$>Õ?ÐBXÕB[Õ[Ó\ˆ‹ñ  à	�‰”EÐ+Ó,Ó	-€BØ‰O ¡Õ0€DÜ
×
Ò
˜2Ó
€CØ•€C�IØ�6‰6�%‹=Ðr   c                 ó  € \        V \        P                  4      '       g   \        P                  ! V 4      p MV P	                  4       P                  4       p V P                  \        P                  ! V4      \        P                  R 7      # )©Údevicer#   )	r6   r1   r   ÚtensorÚdetachÚclonerŒ   rž   r9   )r<   rž   s   &&r   Ú	to_tensorr¢   æ   sT   € Ü�aœŸ™×&Ò&Ü�LŠL˜‹O‰à�H‰H‹J×ÑÓˆØ�4‰4”u—|’| FÓ+´5·=±=ˆ4ÓAÐAr   c                ó0   € V ^8„  d   QhR\         R\         /# )é   ÚnÚreturn)r;   )Úformats   "r   Ú__annotate__r¨   ï   s   € ÷ ñ Œsð ”sñ r   c                 ó"   € ^V ,          ^,
          # ©é   rt   )r¥   s   &r   Ú_n_onesr¬   ï   s   € Ø��F�a�<Ðr   c                óH   € V ^8„  d   QhR\         R\        R\        R\         /# ©r¤   r   ÚebitsÚmbitsr¦   ©r   r;   )r§   s   "r   r¨   r¨   ÷   s.   € ÷ uñ uœvð u¬cð u¼#ð uÄ&ñ ur   c                ó¾  € V P                   \        P                  8w  d   \        RV P                    24      h^V,           V,           ^8”  d   \        R^V,           V,            24      h\	        V^,
          4      p\	        W,           4      p^W,           ,          p\	        \
        V,
          ^,
          4      p^\	        V4      V,
          ,          \	        V^,           4      ^V,          ,          ,          p^^V,
          ,          p\        V,
          \
        V,
          ,           ^,           p	V	\
        ,          p
\        P                  ! V
\        P                  R7      P                  \        P                  4      pV P                  \        P                  4      p V R,          pW,          p V P                  \        P                  4      p W8¬  p\        P                  ! \        P                  ! V4      W8  4      p\        P                  ! \        P                  ! WÞ4      4      pW,           pVP                  \        P                  4      pVV
,          pVP                  \        P                  4      pV P                  \        P                  4      pV\
        V,
          ,	          ^,          pV\        ,
          \
        ,          V,           pVV,          pVV,          pV\
        V,
          ,	          pVP                  \        P                  4      p\        P                   ! W\        P                  R7      p \        P"                  ! VVV 4      p \        P"                  ! VVV 4      p V\
        \$        ,           V,
          V,
          ,	          pVP                  \        P                  4      pVV,          pV V,          p V P                  \        P                  4      # )a  Convert FP32 numbers to sub-byte floating point numbers with the given
number of exponent and mantissa bits.

Input: torch.Tensor of dtype torch.float
Output: torch.Tensor of dtype torch.uint8, where the bit encoding is stored
in the least significant bits. e.g.
  fp4: bits 0-3 empty and bits 4-7 in fp4_e2m1 encoding
  fp6: bits 0-1 empty and bits 2-7 in fp6_e2m3 or fp6_e3m2 encoding

Note: there are no special values (NaN, inf) support in this code. Values
outside the representable range of Floatx after rounding are clamped to the
maximum Floatx magnitude (sign is preserved).

Code below is an adaptation of https://fburl.com/code/ciwofcg4

Background 1: last answer in https://stackoverflow.com/q/8981913
Background 2: Computer Organization and Design, RISC-V edition, Chapter 3.5
z(Expected x.dtype to be torch.float, got ú%Expected 1 + ebits + mbits <= 8, got rD   l        )r#   r1   r'   r4   r¬   Ú	MBITS_F32ÚF32_EXP_BIASrŸ   Úint32Úviewr9   Úlogical_andÚlogical_notÚ
logical_orrŒ   Úuint8Ú	full_likeÚwhereÚ	EBITS_F32)r   r¯   r°   Úexp_biasÚmax_intÚ	sign_maskÚmagic_adderÚ
max_normalÚ
min_normalÚ
denorm_expÚdenorm_mask_intÚdenorm_mask_floatÚsignÚsaturate_maskÚdenormal_maskÚnormal_maskÚ
denormal_xÚnormal_xÚmant_oddÚ
val_to_addÚsign_lps   &&&                  r   Ú_f32_to_floatx_unpackedrÑ   ÷   sä  € ð& 	‡w�w”%—+‘+ÔÜÐGÈÏÉÀyÐQÓRÐRØˆ5…y�5Õ˜1ÔÜÐDÀQÈÅYÐQVÕEVÐDWÐXÓYÐYô �u˜q•yÓ!€HÜ�e•mÓ$€GØ�e•mÕ$€Iô œ) eÕ+¨aÕ/Ó0€Kð ”w˜u“~¨Õ0Õ1´W¸UÀQ½YÓ5GÈ1ÈeÍ8Õ5TÕU€Jð �q˜8•|Õ$€Jô 
˜Õ	 ä�uÕõ	ð õ		ð ð !¤IÕ-€Oô Ÿš _¼E¿K¹KÔH×MÑMÜ�‰óÐð 	
�‰Œu�{‰{Ó€AØˆz�>€Dð 	
�€Að 	
�‰Œu�{‰{Ó€Að ‘O€MÜ×%Ò%¤e×&7Ò&7¸Ó&FÈÉÓW€MÜ×#Ò#¤E×$4Ò$4°]Ó$RÓS€Kð Õ&€JØ—‘¤§¡Ó-€JØ�/Õ!€JØ—‘œuŸ{™{Ó+€Jð
 �v‰v”e—k‘kÓ"€HàœY¨Õ.Õ/°1Õ4€HàœlÕ*¬yÕ8¸KÕG€JØ�
Õ€Hà�Õ€HàœI¨Õ-Õ.€HØ�{‰{œ5Ÿ;™;Ó'€Hô
 	�Š˜¬%¯+©+Ô6€AÜ�Š�M :¨qÓ1€AÜ�Š�K ¨1Ó-€Að ”y¤9Õ,¨uÕ4°uÕ<Õ=€GØ�j‰jœŸ™Ó%€Gð
 ˜	Õ!€GØ	ˆG�€Aà�4‰4”—‘ÓÐr   c                óH   € V ^8„  d   QhR\         R\        R\        R\         /# r®   r±   )r§   s   "r   r¨   r¨   q  s.   € ÷ Z$ñ Z$œvð Z$¬cð Z$¼#ð Z$Ä&ñ Z$r   c                ó.  € V P                   \        P                  8w  d   \        RV P                    24      h^V,           V,           ^8”  d   \        R^V,           V,            24      h^W,           ,          p\	        V^,
          4      p\	        V4      pW,          pW,          pV^ 8H  p\        P
                  ! V^ 8„  Wr,	          ^ 8H  4      p	Wr,	          p
W¤,
          \        ,           pVP                  \        P                  4      \        ,          pWu,          P                  \        P                  4      pV\        V,
          ,          pW½,          p^ Wè&   ^V,
          \        ,           pV^8X  d   Wò,
          \        ,          Wé&   M¤\        V4       F~  p\        ^V,          ^V^,           ,          4       FV  pVV,
          pV^V,          ,
          V\        ,           V,
          ,          pVV,
          \        ,          pW½,           WÌV8H  &   KX  	  K€  	  \        P                  ! WœV4      pVP                  \        P                  4      \        V,
          \        ,           V,
          ,          pVV,          pVP                  \        P                  4      # )a‰  Convert sub-byte floating point numbers with the given number of exponent
and mantissa bits to FP32.

Input: torch.Tensor of dtype uint8, where the bit encoding is stored
in the least significant bits. e.g.
  fp4: bits 0-3 empty and bits 4-7 in fp4_e2m1 encoding
  fp6: bits 0-1 empty and bits 2-7 in fp6_e2m3 or fp6_e3m2 encoding
Output: torch.Tensor of dtype fp32 with the dequantized value
z(Expected x.dtype to be torch.uint8, got r³   )r#   r1   r»   r4   r¬   r¸   rµ   rŒ   r¶   r´   rH   r½   r¾   r·   r'   )r   r¯   r°   rÁ   r¿   Úmantissa_maskrÐ   Úx_posÚ	zero_maskrÊ   Úexp_biased_lpÚexp_biased_f32Úmantissa_lp_int32Úmantissa_f32ÚresultÚdenormal_exp_biasedrJ   Úmantissa_cmpÚ
left_shiftÚsign_f32s   &&&                 r   Ú_floatx_unpacked_to_f32rà   q  s   € ð 	‡w�w”%—+‘+ÔÜÐGÈÏÉÀyÐQÓRÐRØˆ5…y�5Õ˜1ÔÜÐDÀQÈÅYÐQVÕEVÐDWÐXÓYÐYà�e•mÕ$€IÜ�u˜q•yÓ!€HÜ˜E“N€Mð �m€Gð �K€Eð
 ˜‘
€Iô
 ×%Ò% u¨q¡y°Uµ^ÈÑ4IÓK€Mð •N€MØ"Õ-´Õ<€NØ#×&Ñ&¤u§{¡{Ó3´yÕ@€Nð Õ.×2Ñ2´5·;±;Ó?ÐØ$¬°UÕ):Õ;€LØÕ*€Fð
 €FÑà˜h�,¬Õ5Ðð �„zØ!4Õ!<ÄÕ JˆÒô �u–ˆAÜ % a¨1¥f¨a°A¸µE­lÖ ;�ð # Q�Y�
Ø ,°°QµÕ 7Ø¤Õ*¨UÕ2õ �ð #6¸
Õ"BÄyÕ!P�ð #Õ1ð "°|Ñ"CÓDó !<ñ ô" —’˜]¸vÓFˆð �z‰zœ%Ÿ+™+Ó&¬9°uÕ+<¼yÕ+HÈ5Õ+PÕQ€HØ�hÕ€Fà�;‰;”u—{‘{Ó#Ð#r   c                 ó.   € W,           ^,
          V,          # rª   rt   )ÚaÚbs   &&r   Úceil_divrä   Î  s   € Ø�E�A�I˜!ÕÐr   c                ó8   € V ^8„  d   QhR\         P                  /# ©r¤   r¦   ©r1   r   )r§   s   "r   r¨   r¨   Õ  s   € ÷ $ñ $´E·L±Lñ $r   c                 óz  € \         P                  ! V P                  ^ 4      V P                  ^4      V,          3V P                  VP                  R7      p\        \        V P                  ^4      V4      ^4      p\        V P                  ^ 4      4       F£  p\        V P                  ^4      V,          4       F{  pV^€,          pV^,          pRWt,          V,           ,          p	V^€,          p
V^,          pWš^ ,          ^,          ,           V
^ ,          ^,          ,           V,           pW,          W5V3&   K}  	  K¥  	  V# )rƒ   r�   i   )r1   rE   rŽ   rž   r#   rä   rH   )ÚinputÚinput_scalesÚ	blocksizeÚoutput_scalesÚnum_col_tilesrJ   ÚjÚscale_tile_hÚscale_tile_wÚtile_offsetÚouterÚinnerÚoffsets   &&&          r   Úfrom_blockedrõ   Õ  s  € ô —K’KØ	�‰�A‹˜Ÿ
™
 1›¨Õ2Ð3Ø�|‰|Ø× Ñ ô€Mô œX e§j¡j°£m°YÓ?ÀÓC€Mô �5—:‘:˜a“=Ö!ˆÜ�u—z‘z !“}¨	Õ1Ö2ˆAà �8ˆLØ �6ˆLð  Õ!=ÀÕ!LÕMˆKð ˜•GˆEØ˜•EˆEð
 !¨B¥J°"Õ#4Õ4¸À½ÀqÕ7HÕHÈ5ÕPˆFà".Õ"6ˆM˜Q˜$Óó% 3ñ "ð* Ðr   c                 óö   € \         P                  ! W^R7      pV P                  \         P                  4      VP                  \         P                  4      ,          pVP                  \         P                  4      # )r«   )r„   )r1   Úrepeat_interleaverŒ   r'   Úbfloat16)Úx_mxfp8Úscales_unswizzledrë   ÚscalesÚx_f32s   &&&  r   Úfrom_blocked_formatrý   û  sN   € ä×$Ò$Ð%6ÀqÔI€Fð �J‰J”u—{‘{Ó# f§i¡i´·±Ó&<Õ<€EØ�8‰8”E—N‘NÓ#Ð#r   c                ó8   € V ^8„  d   QhR\         P                  /# ræ   rç   )r§   s   "r   r¨   r¨     s   € ÷  ñ  ¤§¡ñ  r   c                óÎ  € V P                   w  r\        V^€4      p\        V^4      pV^€,          pV^,          pT pW3WV38w  d9   \        P                  ! WV3V P                  V P
                  R7      pWRV1RV13&   VP                  V^€V^4      P                  ^ ^^^4      pVP                  R^^ ^4      P                  ^^4      P                  R^ ^4      p	V	P                  4       # )a;  
Rearrange a large matrix by breaking it into blocks and applying the rearrangement pattern.

See:
    https://docs.nvidia.com/cuda/cublas/index.html#d-block-scaling-factors-layout

Args:
    input_matrix: Input tensor of shape (H, W)

Returns:
    Rearranged tensor of shape (32*ceil_div(H,128), 16*ceil_div(W,4))
r�   Néÿÿÿÿ)rF   rä   r1   rE   rž   r#   r·   r…   ÚreshapeÚ	transposeÚflatten)
Úinput_matrixÚrowsÚcolsÚn_row_blocksÚn_col_blocksÚpadded_rowsÚpadded_colsÚpaddedÚblocksÚ
rearrangeds
   &         r   Ú
to_blockedr    sí   € ð ×#Ñ#�J€DÜ˜D #Ó&€LÜ˜D !Ó$€Lð  Õ$€KØ Õ"€Kà€Fà€|˜Ð1Ô1Ü—’˜kÐ7À×@SÑ@SÐ[g×[mÑ[mÔnˆØ+ˆu�ˆu�e�t�eˆ|Ñð �[‰[˜ s¨L¸!Ó<×DÑDÀQÈÈ1ÈaÓP€FØ—‘  A r¨1Ó-×7Ñ7¸¸1Ó=×EÑEÀbÈ"ÈbÓQ€Jà×ÑÓÐr   c                 óx   € V R,          ^,          ^ 8w  d   \        V  R24      h. V RR OV R,          ^,          N5# )r«   z last dim not divisible by twoNr   )r4   )rŽ   s   &r   Ú	down_sizer  %  sC   € ØˆB…x�!…|�qÔÜ ˜vÐ%CÐDÓEÐEØ&ˆT�#�2ˆYÐ&˜˜R� A�Ñ&Ð&r   c                ó8   € V ^8„  d   QhR\         P                  /# ræ   rç   )r§   s   "r   r¨   r¨   +  s   € ÷ Lñ LœeŸl™lñ Lr   c                 óü   € V P                   pVR,          ^,          ^ 8w  d   \        RVR,           24      hV P                  R4      p V R,          ^,          V R,          ,          P                  \	        V4      4      # )r«   z-Expected shape[-1] to be divisible by 2, got :r«   Nr¤   :NNr¤   r   )rF   r4   r  r·   r  )Ú
uint8_datarF   s   & r   Ú
pack_uint4r  +  sn   € à×Ñ€EØˆR…y�1…}˜ÔÜÐLÈUÐSUÍYÈKÐXÓYÐYØ×#Ñ# BÓ'€JØ�tÕ Õ! J¨s¥OÕ3×9Ñ9¼)ÀEÓ:JÓKÐKr   c                 ó  € V P                   \        P                  8w  d   \        R V P                    24      h\	        V P                  4       \        \        4      p \        V 4      p V P                  \        P                  4      p V # )z+Expected x.dtype to be torch.bfloat16, got )r#   r1   rø   r4   rÑ   r'   Ú	FP4_EBITSÚ	FP4_MBITSr  r·   Úfloat4_e2m1fn_x2)r   s   &r   Ú_bfloat16_to_float4_e2m1fn_x2r  8  s_   € Ø‡w�w”%—.‘.Ô ÜÐJÈ1Ï7É7È)ÐTÓUÐUÜ §¡£	¬9´iÓ@€AÜ�1‹€AØ	�‰Œu×%Ñ%Ó&€AØ€Hr   c                óP   € V ^8„  d   QhR\         P                  R\        R\        /# )r¤   Údata_hpÚ
block_sizer§   )r1   r   r;   Ústr)r§   s   "r   r¨   r¨   B  s/   € ÷ N&ñ N&Ü�\‰\ðN&äðN&ô ñN&r   c                 óÜ  € V P                   \        P                  \        P                  39  d   \	        V P                    R 24      hV P
                  R
,          V,          ^ 8w  d   \	        RV P
                   RV 24      hV P                  4       '       g   \	        R4      hV P
                  pV P                  ! . VRR
 OVR
,          V,          NVN5!  p \        P                  ! \        P                  ! V 4      R
4      P                  R
4      pV P                  \        P                  4      p VP                  \        P                  4      pVR8X  d2   \        P                  ! \        P                  4      P                  pTpMVR8X  d   RpTpR R	 lpV! WX4      w  ršVR8X  d2   V
P                  \        P                  4      p
V
P                  V4      p
M`VR8X  dZ   \!        V
P                  \        P                  4      4      p
\#        V4      pVR
;;,          ^,          uu&   V
P                  V4      p
V	P%                  \        P&                  4      p	V	P)                  R
4      p	Wš3# )z is not supported yetzthe last dimension of shape z! must be divisible by block_size z'unsupported: data_hp must be contiguousNÚmxfp8Úmxfp4g      @c          
      óº   € V ^8„  d   QhR\         P                  R\         P                  R\        R\        \         P                  \         P                  3,          /# )r¤   r  Úmax_absÚmax_posr¦   )r1   r   r'   rO   )r§   s   "r   r¨   Úto_mxfp.<locals>.__annotate__b  sJ   € ÷ !ñ !Ü—‘ð!ä—‘ð!ô ð!ô 
Œu�|‰|œUŸ\™\Ð)Õ	*ñ	!r   c                 ó,  € ^pW,          p\         P                  ! \         P                  ! V4      ^ÿ\         P                  ! \         P                  ! \         P
                  ! V4      4      V) VR7      V,           P                  \         P                  4      4      p\         P                  ! V^ 8H  R\         P                  ! W5P                  \         P                  4      ,
          4      4      p\         P                  ! W,          RV,          VR7      pWW3# )é   )r   r   r/   r   )
r1   r½   Úisnanr�   ÚceilÚlog2rŒ   r»   Úexp2r9   )r  r"  r#  ÚE8M0_EXPONENT_BIASÚdescaleÚexponentÚ
descale_fpÚdata_lps   &&&     r   Ú_to_mx_rceilÚto_mxfp.<locals>._to_mx_rceilb  sÏ   € ð
 !ÐØÕ#ˆÜ—;’;Ü�KŠK˜Ó Øô —’Ü—J’JœuŸzšz¨'Ó2Ó3Ø+Ð+Ø*ôð
 %õ%÷ ‰b”—‘‹oó
ˆô —[’[Ø˜‰MØÜ�JŠJÐ)¯K©K¼¿¹Ó,FÕFÓGó
ˆ
ô —+’+˜gÕ2¸¸W½È'ÔRˆØÐ Ð r   r   )r#   r1   rø   r'   r4   rF   Úis_contiguousr  ÚamaxÚabsÚ	unsqueezerŒ   r9   r8   Úfloat8_e4m3fnr   r  rN   r·   Úfloat8_e8m0fnuÚsqueeze)r  r  r§   Ú
orig_shaper"  Ú
F8E4M3_MAXr#  Ú
F4E2M1_MAXr0  Úscale_e8m0_biasedr/  Úfinal_shapes   &&&         r   Úto_mxfpr>  B  s  € ð
 ‡}�}œUŸ^™^¬U¯[©[Ð9Ô9Ü §¡˜Ð.CÐDÓEÐEØ‡}�}�RÕ˜:Õ%¨Ô*ÜØ*¨7¯=©=¨/Ð9ZÐ[eÐZfÐgó
ð 	
ð × Ñ ×"Ò"ÜÐFÓGÐGà—‘€JØ�oŠoð Ø	�C�RˆðØ$ R�.¨JÕ6ðØ8Bó€Gô �jŠjœŸš 7Ó+¨RÓ0×:Ñ:¸2Ó>€Gà�j‰jœŸ™Ó'€GØ�j‰jœŸ™Ó'€Gà�ÔÜ—[’[¤×!4Ñ!4Ó5×9Ñ9ˆ
Ø‰Ø	�7Ô	Øˆ
Øˆõ!ñ> ".¨gÀÓ!HÑÐð �ÔØ—*‘*œU×0Ñ0Ó1ˆà—/‘/ *Ó-‰Ø	�7Ô	Ü/°·
±
¼5¿>¹>Ó0JÓKˆÜ˜:Ó&ˆØ�B�˜AÕ‹Ø—/‘/ +Ó.ˆà)×.Ñ.¬u×/CÑ/CÓDÐØ)×1Ñ1°"Ó5ÐØÐ%Ð%r   Úcudac                óØ  € ^ RI pW,          ^ 8w  d   \        RV 24      h\        \        W!^,           V4      4      pV \	        V4      8”  d   \        R4      h\
        P                  ! VP                  VRR V ^,
          4      4      p\
        P                  ! V\
        P                  ! V.4      34      p\
        P                  ! V4      w  rxVP                  V4      P                  V4      # )a  
Utility function for tests and benchmarks.

Generates a tensor of length E, containing random values divisible by `multiple_of`,
from 0 to M, in sorted order, and where the final value in the tensor is always M.
Args:
    E (int): The length of the tensor.
    M (int): The maximum value in the tensor.
Returns:
    torch.Tensor: A tensor of length E with the specified properties.
NzM must be divisible by z5E cannot be larger than the number of possible valuesr   )ÚrandomÚ
ValueErrorrN   rH   rP   r1   rŸ   ÚsampleÚcatÚsortrŒ   )	ÚEÚMÚmultiple_ofr#   rž   rA  Úpossible_valuesÚselected_valuesÚ_s	   &&&&&    r   Úgenerate_jagged_offsrL  “  sÍ   € ó ð 	…˜!ÔÜÐ2°;°-Ð@ÓAÐAô œ5 °!­e°[ÓAÓB€Oð 	Œ3ˆÓÔÜÐPÓQÐQô —l’l 6§=¡=°ÀÀ"Ð1EÀqÈ1ÅuÓ#MÓN€Oô —i’i ´%·,²,À¸sÓ2CÐ DÓE€Oô Ÿš OÓ4Ñ€Oà×Ñ˜eÓ$×'Ñ'¨Ó/Ð/r   )rw   r   r‚   )é    )rM  r  ):Ú__doc__r7   r   r1   r   Ú
contextlibr   Ú$torch.testing._internal.common_utilsr   r   r   r   r   rN   r`   ra   Úsupported_enginesrn   ÚanyÚremover   r»   r%   r(   r,   r2   rB   rK   rQ   re   rk   ru   rx   rj   r}   r€   r‰   r–   r›   r¢   r¬   r¾   r´   rµ   rÑ   rà   rä   rõ   rý   r  r  r  r  r  r  r>  r¶   rL  )Úqes   0r   Ú<module>rU     s®  ðñó Û Ý Ý %ß gÕ gá˜%Ÿ.™.×2Ñ2×DÑDÓEÐ ð Ð"Ô"¡s¨F°NÀHÈjÐ+Y×'ZÒ'ZØ×Ñ˜iÔ(÷ Ù'9ÓYÑ'9 ÐGXÑ=XŸ"˜"Ñ'9ÑYÐôJð *.°DÀÇÁô 	òð 12¸À2Ç8Á8ô ð 7<ÀU×E\ÑE\ô -+ò^ò4!ð8 ñ3ó ð3ð ñ;ó ð;òò7ò8ò7ò4òò
ò
òBõð ˜"Ð €	ˆ9Ù�y 1•}Ó%€õuõtZ$òzõ$ôL$õ òD'õLð ˜!Ð €	ˆ9ò÷N&ðb ,.°U·[±[Èö "0ùò Zs   ÂE,ÂE,