+
    "Ý³j¢(  ã                   ó  € R t ^ RIt^ RIHtHtHtHtHt ^ RIt^ RI	H
t
Ht ^ RIHt ^ RIHtHtHt ]P$                  t ! R R]4      tR R	 ltR
 R ltRR R lltR R ltR R ltR R ltR R ltR R ltR R ltR R ltR R ltR# )z;
Defines an nn module designed to be used during inference
N)ÚListÚ
NamedTupleÚOptionalÚTupleÚUnion)Úis_row_majorÚpad_tensor_for_matmul)ÚFP8Granularity)Úis_MI300Úis_MI350Úis_sm_at_least_89c                   ó<   a € ] tR t^t o RtRtRtRtV 3R ltRt	V t
R# )ÚFloat8MMConfiga{  
Configuration for the scaled_mm in the forward and backward pass.

Attributes:
    emulate (bool): Whether to emulate the matmuls in fp32.
    use_fast_accum (bool): Whether to use the fast-accumulation option for scaled_mm.
    pad_inner_dim (bool): Whether to pad the inner dimension of a and b with 0s.
                          This is needed for matmuls not aligned to 16.
Fc                ó>   <€ V ^8„  d   Qh/ S[ ;R&   S[ ;R&   S[ ;R&   # )é   ÚemulateÚuse_fast_accumÚpad_inner_dim)Úbool)ÚformatÚ__classdict__s   "€ÚY/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torchao/float8/inference.pyÚ__annotate__ÚFloat8MMConfig.__annotate__   s,   ø‡ ‚ ñ Ññ ñ Ñ ñ ñ Ñò ó    © N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   r   Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__)r   s   @r   r   r      s"   ø‡ € ñð €GØ €NØ€M÷ ƒ r   r   c          
      ój   € V ^8„  d   QhR\         R\         R\        R\        \         \         3,          /# )r   Úa_dataÚb_dataÚscaled_mm_configÚreturn)ÚTensorr   r   )r   s   "r   r   r   *   s9   € ÷ ñ Üðäðô %ðô Œ6”6ˆ>Õñ	r   c                óô  € VP                   '       dk   V P                  ^4      VP                  ^ 4      8X  g,   Q RV P                  ^4       RVP                  ^ 4       24       h\        V ^R7      p \        V^ R7      p\        V P	                  4       4      '       g   V P                  4       p \        VP	                  4       4      '       d-   VP                  4       P                  4       P                  4       pW3# )zêPreprocess the inner fp8 data tensors for admmm
Args:
    a_data: Input tensor A.
    b_data: Input tensor B.
    scaled_mm_config: Configuration for _scaled_mm.
Returns:
    Preprocessed tensors A and B in the format for _scaled_mm.
z"Inner dims must match for mm, got z and )Údims)r   Úsizer   r   ÚstrideÚ
contiguousÚt)r%   r&   r'   s   &&&r   Úpreprocess_datar0   *   sÇ   € ð ×%×%Ð%Ø�{‰{˜1‹~ §¡¨Q£Ô/ð 	
Ø0°·±¸Q³Ð0@ÀÀfÇkÁkÐRSÃnÐEUÐVó	
Ð/ô ' v°AÔ6ˆÜ& v°AÔ6ˆÜ˜Ÿ™›×(Ò(Ø×"Ñ"Ó$ˆÜ�F—M‘M“O×$Ò$Ø—‘“×&Ñ&Ó(×*Ñ*Ó,ˆØˆ>Ðr   c                ó^   € V ^8„  d   QhR\         P                  R\        \        R3,          /# )r   Úinput_scaleÚinput_shape.)Útorchr)   r   Úint)r   s   "r   r   r   D   s&   € ÷ ñ ¤%§,¡,ð ¼UÄ3ÈÀ8½_ñ r   c                óæ   € V P                  4       ^8X  d   V P                  ^^4      # V P                  R4      p V P                  4       ^8”  d$   V P                  RV P                  R,          4      p V # )z:Ensures input tensor is correctly formatted for _scaled_mméÿÿÿÿ)ÚnumelÚreshapeÚ	unsqueezeÚdimÚshape)r2   r3   s   &&r   Úpreprocess_scaler=   D   sn   € ð ×ÑÓ˜aÔà×"Ñ" 1 aÓ(Ð(ð ×'Ñ'¨Ó+€Kð ‡�Ó˜1ÔØ!×)Ñ)¨"¨k×.?Ñ.?ÀÕ.CÓDˆàÐr   c                óÄ   € V ^8„  d   QhR\         R\         R\         R\         R\        P                  R\        \         ,          R\        \         ,          R\        R	\         /	# )
r   r%   Úa_scaler&   Úb_scaleÚoutput_dtypeÚoutput_scaleÚbiasr   r(   )r)   r4   Údtyper   r   )r   s   "r   r   r   V   sp   € ÷ %ñ %Üð%äð%ô ð%ô ð	%ô
 —+‘+ð%ô œ6Õ"ð%ô ”6Õ
ð%ô ð%ô ñ%r   c                óº   € V\         P                  8X  d*   Ve&   \         P                  ! V VVVVVVR7      pW†,           # \         P                  ! V VVVVVVVR7      # )zÓ
This is the unwrapped version of addmm_float8, which does not take in Float8TrainingTensors
as inputs. This is used to standardize the logic between subclassed and non subclassed
versions of the linear module.
)Úscale_aÚscale_bÚscale_resultÚ	out_dtyper   )rF   rG   rC   rH   rI   r   )r4   Úfloat32Ú
_scaled_mm)	r%   r?   r&   r@   rA   rB   rC   r   Úoutputs	   &&&&&&&& r   Ú addmm_float8_unwrapped_inferencerM   V   so   € ð  ”u—}‘}Ô$¨Ò)9ä×!Ò!ØØØØØ%Ø"Ø)ô
ˆð �}ÐÜ×ÒØØØØØØ!ØØ%ô	ð 	r   c                óª   € V ^8„  d   QhR\         P                  R\        \        ,          R\        R\        R\        R\        R\         P                  /# )r   ÚscaleÚ
data_shaper;   ÚstartÚendÚstepr(   )r4   r)   r   r5   )r   s   "r   r   r   ~   sc   € ÷ 0Hñ 0HÜ�<‰<ð0Hä”S•	ð0Hô 
ð0Hô ð	0Hô
 
ð0Hô ð0Hô ‡\�\ñ0Hr   c                óv  a a€ \         P                  P                  pS P                  S8X  d   VP                  P                  S W#WE4      # \        ;QJ d*    . VV 3R l\        \        S4      4       4       F  NK  	  5M#! VV 3R l\        \        S4      4       4       4      pV\        V4      8¼  d   S # Wr,          pV^8X  d   VP                  P                  S W#WE4      # Ve	   W8,          MRp	Ve   WH,           ^,
          V,          MRp
V^8”  d   \        R4      hVP                  P                  S W)V
^4      # )zá
Slice the scale tensor appropriately based on the data tensor slicing.
This function calculates how the scale should be sliced when the data tensor
is sliced along a given dimension, taking into account the block structure.
c              3   óf   <"  € T F&  pSV,          SP                   V,          ,          x € K(  	  R # 5i)N)r<   )Ú.0ÚirP   rO   s   & €€r   Ú	<genexpr>Ú-_slice_scale_for_dimension.<locals>.<genexpr>’   s'   øé € ÐXÑAW¸A˜
 1�¨¯©°Q­×7Ò7ÓAWùs   ƒ.1Nz;Slicing with step > 1 is not implemented for scale tensors.)
r4   ÚopsÚatenr<   Úslicer)   ÚtupleÚrangeÚlenÚNotImplementedError)rO   rP   r;   rQ   rR   rS   r[   Úblock_sizesÚblock_size_for_dimÚscale_startÚ	scale_ends   ff&&&&     r   Ú_slice_scale_for_dimensionre   ~   s  ù€ ô �9‰9�>‰>€Dð ‡{�{�jÔ Ø�z‰z× Ñ  ¨°CÓ>Ð>÷ ”%ÕXÄÄsÈ:ÃÔAWÓX—%‘%ÕXÄÄsÈ:ÃÔAWÓXÓX€Kà
Œc�+ÓÔàˆà$Õ)Ðà˜QÔð �z‰z× Ñ  ¨°CÓ>Ð>ð 6;Ò5F�eÖ1ÈDˆð Šð Õ%¨Õ)Ð.@Ö@àð 	ð �!Œ8Ü%ØMóð ð �z‰z× Ñ  ¨¸)ÀQÓGÐGr   c                óD   € V ^8„  d   QhR\         P                  R\        /# ©r   Úxr(   ©r4   r)   r   )r   s   "r   r   r   ±   s"   € ÷ Hñ Hœ%Ÿ,™,ð H¬4ñ Hr   c                óÎ   € \        V R4      '       g   Q R4       h\        V P                  4      RV P                  4       ^,
          ,          V P                  R,          3,           8H  # )úrChecks if a quantized tensor is rowwise scaled
Args:
    x: quantized tensor (should have `block_size` attribute)
Ú
block_sizeú.Expecting input to have `block_size` attribute)é   r7   )Úhasattrr]   rl   r;   r<   ©rh   s   &r   Ú_is_rowwise_scaledrq   ±   sO   € ô
 �1�l×#Ò#ÐUÐ%UÓUÐ#Ü�—‘Ó $¨!¯%©%«'°A­+Õ"6¸!¿'¹'À"½+¸Õ"GÑGÐGr   c                óD   € V ^8„  d   QhR\         P                  R\        /# rg   ri   )r   s   "r   r   r   º   s   € ÷ ñ œUŸ\™\ð ¬dñ r   c                óö   a € \        S R4      '       g   Q R4       h\        ;QJ d2    V 3R l\        S P                  4       4       F  '       d   K   R# 	  R# ! V 3R l\        S P                  4       4       4      # )rk   rl   rm   c              3   ó®   <"  € T FJ  pSP                   V,          R8H  ;'       g(    SP                   V,          SP                  V,          8H  x € KL  	  R# 5i)rn   Nr7   )rl   r<   )rV   rW   rh   s   & €r   rX   Ú(_is_tensorwise_scaled.<locals>.<genexpr>À   sA   øé € ð ÙHUÀ1ˆ�‰�Q�˜2Ñ×>Ð> §¡¨a¥°A·G±G¸AµJÑ!>Ô>Ëùs
   ƒ!A¥0AFT)ro   Úallr^   Úndimrp   s   fr   Ú_is_tensorwise_scaledrx   º   si   ø€ ô
 �1�l×#Ò#ÐUÐ%UÓUÐ#ß‹3ô ÜHMÈaÏfÉfÌó�3Œ3ð Š3ð ˆ3ô ÜHMÈaÏfÉfÌóó ð r   c                óD   € V ^8„  d   QhR\         P                  R\        /# rg   ri   )r   s   "r   r   r   Å   s"   € ÷ Cñ CœŸ™ð C¬ñ Cr   c                óØ   € \        V R4      '       g   Q R4       hV P                  p\        V4      ^8¬  ;'       d1    \        P                  ! VRR 4      ^8H  ;'       d    VR,          ^€8H  # )z…Checks if a quantized tensor is scaled with a block size of 1x128
Args:
    x: quantized tensor (should have `block_size` attribute)
rl   rm   Nr7   )ro   rl   r_   ÚmathÚprod©rh   Úbs   & r   Ú_is_1_128_scaledr   Å   s`   € ô
 �1�l×#Ò#ÐUÐ%UÓUÐ#Ø	�‰€AÜˆq‹6�Q‰;×BÐBœ4Ÿ9š9 Q s¨ VÓ,°Ñ1×BÐB°a¸µe¸s±lÐBr   c                óD   € V ^8„  d   QhR\         P                  R\        /# rg   ri   )r   s   "r   r   r   Ï   s   € ÷ 7ñ 7œ%Ÿ,™,ð 7¬4ñ 7r   c                ó¸   € \        V R4      '       g   Q R4       hV P                  p\        V4      ^8H  ;'       d!    V^ ,          ^€8H  ;'       d    V^,          ^€8H  # )z‡Checks if a quantized tensor is scaled with a block size of 128x128
Args:
    x: quantized tensor (should have `block_size` attribute)
rl   rm   )ro   rl   r_   r}   s   & r   Ú_is_128_128_scaledr‚   Ï   sT   € ô
 �1�l×#Ò#ÐUÐ%UÓUÐ#Ø	�‰€AÜˆq‹6�Q‰;×6Ð6˜1˜Q�4 3™;×6Ð6¨1¨Q­4°3©;Ð6r   c                ó”   € V ^8„  d   QhR\         \        \        \        \        3,          \        \        ,          3,          R\        /# )r   Úgr(   )r   r	   r   Úlistr   )r   s   "r   r   r   Ù   sD   € ÷ Wñ WÜÜÜŒnœnÐ,Õ-ÜŒ^Õð	õðWô 
ñWr   c                 óš   € ^ RI Hp \        V 4      ^8H  ;'       d1    V ^ ,          V! ^^€.4      8H  ;'       d    V ^,          V! ^€^€.4      8H  # )é    )ÚPerBlock)Ú torchao.quantization.granularityrˆ   r_   )r„   rˆ   s   & r   Ú!_granularity_is_a_1_128_w_128_128rŠ   Ù   sL   € õô ˆq‹6�Q‰;×VÐV˜1˜Q�4¡8¨Q°¨HÓ#5Ñ5×VÐV¸!¸A½$Á(ÈCÐQTÈ:ÓBVÑ:VÐVr   c                óÌ   € V ^8„  d   QhR\         \        \        \        \        \        3,          \        \        ,          3,          ,          R\        \        \        3,          /# )r   Úgranularityr(   )r   r   r	   r   r…   )r   s   "r   r   r   ç   sR   € ÷ %!ñ %!ÜÜÜÜ”.¤.Ð0Õ1Ü”Õ ð"õ	
õð%!ô Œ>œ>Ð)Õ*ñ%!r   c                 óŠ  € ^ RI HpHp RpV f   V! 4       V! 4       3pV# \        WV34      '       d   W 3pV# \        V \        \
        34      '       dä   \        V 4      ^8X  dÔ   \        V ^ ,          V4      ;'       d    \        V ^,          V4      p\        V ^ ,          V4      ;'       d    \        V ^,          V4      p\        V 4      pV'       g    V'       g   V'       g   \        RV  R24      h\        V ^ ,          \        V ^,          4      4      '       g   \        RV  R24      h\	        V 4      pV# \        RV  R24      h)r‡   ©ÚPerRowÚ	PerTensorNzUnsupported granularity types: Ú.zEDifferent granularities for activation and weight are not supported: z#Invalid granularity specification: )
r‰   r�   r�   Ú
isinstancer]   r…   r_   rŠ   Ú
ValueErrorÚtype)rŒ   r�   r�   Úprocessed_granularityÚis_per_tensorÚ
is_per_rowÚis_a_1_128_w_128_128s   &      r   Ú_normalize_granularityr™   ç   sR  € ÷ð
 !ÐØÒÙ!*£©i«kÐ :Ðð* !Ð ô) 
�K¨VÐ!4×	5Ò	5Ø!,Ð :Ðð& !Ð ô% 
�K¤%¬ ×	/Ò	/´C¸Ó4DÈÔ4IÜ" ;¨q¥>°9Ó=÷ 
ð 
Ä*Ø˜�N˜IóC
ˆô   ¨A¥°Ó7÷ 
ð 
¼JØ˜�N˜Fó=
ˆ
ô  AÀÓMÐç§×/CÜÐ>¸{¸mÈ1ÐMÓNÐNÜ˜+ a�.¬$¨{¸1­~Ó*>×?Ò?ÜØWÐXcÐWdÐdeÐfóð ô !& kÓ 2Ðð !Ð ô Ð>¸{¸mÈ1ÐMÓNÐNr   c                óJ   € V ^8„  d   QhR\         \        \        3,          RR/# )r   Úgranularitiesr(   N)r   r	   )r   s   "r   r   r     s*   € ÷ )Dñ )DÜœ¬Ð7Õ8ð)Dà	ñ)Dr   c                óº  € ^ RI HpHp \        V ^ ,          V4      ;'       d    \        V ^,          V4      p\        V ^ ,          V4      ;'       d    \        V ^,          V4      p\	        V 4      pV'       g	   V'       dˆ   \
        P                  P                  4       '       gb   \
        P                  P                  4       '       d   \        4       '       g,   \        4       '       g   \        4       '       g   Q R4       hR# R# R# R# V'       d@   \
        P                  P                  4       '       g   \        4       '       g   Q R4       hR# R# \        RV  R24      h)a  
Validate that the hardware supports the requested granularities.

Args:
    granularities: Tuple of (activation_granularity, weight_granularity)

Raises:
    AssertionError: If hardware doesn't support the requested granularity
    ValueError: If invalid granularity type is provided
rŽ   uU   Float8 dynamic quantization requires CUDA compute capability â‰¥8.9 or MI300+ or XPU.ub   Float8 1x128 activation and 128x128 weight scaling requires CUDA compute capability â‰¥8.9 or XPU.zInvalid granularities r‘   N)r‰   r�   r�   r’   rŠ   r4   ÚxpuÚis_availableÚcudar   r
   r   r“   )r›   r�   r�   r–   r—   r˜   s   &     r   Ú_check_hardware_supportr      s*  € ÷ô
 ˜}¨QÕ/°Ó;÷ ð Ä
Ø�aÕ˜)óA€Mô ˜M¨!Õ,¨fÓ5÷ ð ¼*Ø�aÕ˜&ó;€Jô =¸]ÓKÐçŸ
Ü�y‰y×%Ñ%×'Ò'Ü�J‰J×#Ñ#×%Ò%Ü!×#Ò#Ü�zŠzÜ�zŠzð		
ð dó	
ð 
ñ ñ ñ $ñ (÷ 
ä�y‰y×%Ñ%×'Ò'Ô+<×+>Ò+>ð 	
Øpó	
Ð>Ñ+>Ñ'ô Ð1°-°ÀÐBÓCÐCr   )NNF)r    r{   Útypingr   r   r   r   r   r4   Útorchao.float8.float8_utilsr   r   Útorchao.float8.typesr	   Útorchao.utilsr
   r   r   r)   r   r0   r=   rM   re   rq   rx   r   r‚   rŠ   r™   r    r   r   r   Ú<module>r¥      s~   ðñó ß ;Õ ;ã ç KÝ /÷ñ ð 
�‰€ô �Zô  õ õ4÷$%õP0HõfHõõCõ7õWõ%!÷P)Dr   