+
    "Ý³j¾  ã                   óR  € ^ RI HtHtHtHt ^ RIt^ RIHt ^ RI	H
t
Ht ^ RIHt Rt]P                  P!                  4       ;'       d    ]P"                  P$                  RJt]P(                  ]P*                  ]P,                  ]P.                  0t]P2                  ! 4       RR R ll4       t]P2                  ! 4       RR]P6                  R3R	 R
 ll4       t]P2                  ! 4       RR]P6                  RR3R R ll4       tR R ltR R ltR R lt R t!R R lt"R R lt#R R lt$R# )é    )ÚIterableÚOptionalÚTupleÚUnionN)ÚAsyncCollectiveTensorÚ
all_reduce)ÚScalingGranularitygê-�™—q=Fc                ód   € V ^8„  d   QhR\         P                  R\         P                  R\        /# )é   ÚamaxÚfloat8_dtypeÚround_scales_to_power_of_2)ÚtorchÚTensorÚdtypeÚbool)Úformats   "Ú\/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torchao/float8/float8_utils.pyÚ__annotate__r       s.   € ÷ ñ Ü
�,‰,ðä—+‘+ðô !%ñó    c                ó^  € V P                  \        P                  4      p V\        9   db   \        P                  ! V4      P
                  \        P                  ! V \        R7      ,          pVP                  \        P                  4      pM\        RV 24      hV'       d   \        V4      pV# )zæConverts the amax value of a tensor to the fp8 scale.
Args:
    amax: The amax value of the tensor.
    float8_dtype: The float8 dtype.
    round_scales_to_power_of_2: if true, round scaling factor down to the nearest power of 2.
)ÚminúUnsupported float8_dtype: )Útor   Úfloat64Ú	FP8_TYPESÚfinfoÚmaxÚclampÚEPSÚfloat32Ú
ValueErrorÚ_round_scale_down_to_power_of_2)r   r   r   Úress   &&& r   Úamax_to_scaler%      sy   € ð �7‰7”5—=‘=Ó!€DØ”yÔ Ü�kŠk˜,Ó'×+Ñ+¬e¯kªk¸$ÄCÔ.HÕHˆØ�f‰f”U—]‘]Ó#‰äÐ5°l°^ÐDÓEÐEß!Ü-¨cÓ2ˆØ€Jr   c          
      ó’   € V ^8„  d   QhR\         P                  R\        R\        R\        \
        ,          R\         P                  /# )r   ÚxÚreduce_amaxÚscaling_granularityÚaxiswise_dimÚreturn)r   r   r   r	   r   Úint)r   s   "r   r   r   9   sF   € ÷ ñ Ü‡|�|ðäðô ,ð	ô
 œ3•-ðô ‡\�\ñr   c                 ó\  € V\         P                  J d,   \        P                  ! \        P                  ! V 4      4      pMSV\         P
                  J g   Q R 4       hVf   Q R 4       h\        P                  ! \        P                  ! V 4      VRR7      pV'       d’   \        P                  ! 4       '       dw   Ve   VP                  4       MRpVf(   \        \        \        P                  ! 4       4      4      MTp\        VRV4      p\        V\        4      '       d   VP!                  4       pV# )ÚunsupportedNT)ÚdimÚkeepdimÚMAX)r	   Ú
TENSORWISEr   r   ÚabsÚAXISWISEr   ÚdistÚis_initializedÚ	get_groupÚlistÚrangeÚget_world_sizer   Ú
isinstancer   Úwait)r'   r(   Údevice_meshr)   r*   r   ÚpgÚgroups   &&&&&   r   Útensor_to_amaxr@   8   sà   € ð Ô0×;Ñ;Ó;Ü�yŠyœŸš 1›Ó&‰à"Ô&8×&AÑ&AÓAÐPÀ=ÓPÐAØÒ'Ð6¨Ó6Ð'Ü�zŠzœ%Ÿ)š) A›,¨LÀ$ÔGˆ÷
 ”t×*Ò*×,Ò,Ø(3Ò(?ˆ[×"Ñ"Ô$ÀTˆà68²j””Uœ4×.Ò.Ó0Ó1Ô2ÀbˆÜ˜$  uÓ-ˆÜ�dÔ1×2Ò2Ø—9‘9“;ˆDà€Kr   c                ó¾   € V ^8„  d   QhR\         P                  R\         P                  R\        R\        R\
        \        ,          R\        R\         P                  /# )r   Ú	hp_tensorr   r(   r)   r*   r   r+   )r   r   r   r   r	   r   r,   )r   s   "r   r   r   V   s^   € ÷ ñ Ü�|‰|ðä—+‘+ðô ðô
 ,ðô œ3•-ðô !%ðô ‡\�\ñr   c                ó:   € \        V VVVV4      p\        WqVR7      # )a×  
Compute scaling factor for the given high precision tensor.

Args:
    hp_tensor: high precision tensor
    float8_dtype: the float8 dtype to use
    reduce_amax: whether to reduce the max(abs(hp_tensor)) value across distributed ranks
    scaling_granularity: Defines the scaling granularity
    axiswise_dim: if axiswise granularity is used, defines the dim to scale across
    round_scales_to_power_of_2: if true, round scaling factor down to the nearest power of 2.
)r   )r@   r%   )rB   r   r(   r=   r)   r*   r   r   s   &&&&&&& r   Útensor_to_scalerD   U   s2   € ô* ØØØØØó€Dô ØÐ7Qôð r   c                óX   € V ^8„  d   QhR\         P                  R\         P                  /# )r   r'   r   )r   r   r   )r   s   "r   r   r   v   s&   € ÷ Fñ FœŸ™ð F´E·K±Kñ Fr   c                ó¾   € V\         9   dF   \        P                  ! V4      P                  pV P	                  V) VR7      p V P                  V4      # \        RV 24      h)aå  Converts a tensor to a saturated fp8 tensor.

Note:
    In PyTorch 2.11 and older, eager casts to FP8 did not saturate finite
    overflow values. So we saturate in this function. In compile, it was
    satured and so this extra clamp is a no-op in accuracy and regresses
    performance.

    In PyTorch 2.12 and newer, eager mode now saturates and this clamp
    is no longer needed.

    TODO:
    this explicit clamp can be removed after torchao stops supporting
    PyTorch 2.11.
)r   r   r   )r   r   r   r   r   r   r"   )r'   r   Ú	max_values   && r   Úto_fp8_saturatedrH   v   sV   € ð  ”yÔ Ü—K’K Ó-×1Ñ1ˆ	Ø�G‰G˜˜
¨	ˆGÓ2ˆØ�t‰t�LÓ!Ð!äÐ5°l°^ÐDÓEÐEr   c                óx   € V ^8„  d   QhR\         P                  R\         P                  R\         P                  /# )r   r'   Úyr+   ©r   r   )r   s   "r   r   r   Ž   s-   € ÷ %ñ %”U—\‘\ð %¤e§l¡lð %´u·|±|ñ %r   c                óÐ   € \         P                  P                  V 4      p\         P                  P                  W,
          4      p^\         P                  ! W#,          4      ,          # )zÕComputes the error between two tensors in dB.

For more details see:
    https://en.wikipedia.org/wiki/Signal-to-noise_ratio

Args:
    x: The original tensor.
    y: The tensor to compare to the original tensor.
)r   ÚlinalgÚvector_normÚlog10)r'   rJ   ÚPsÚPns   &&  r   Úcompute_errorrR   Ž   sD   € ô 
�‰×	!Ñ	! !Ó	$€BÜ	�‰×	!Ñ	! !¥%Ó	(€BØ”—’˜B�GÓ$Õ$Ð$r   c                ó~   € V ^8„  d   QhR\         P                  R\         P                  R\        \        R3,          /# )r   Útensorr   r+   .)r   r   r   r   r,   )r   s   "r   r   r   �   s4   € ÷ ñ Ü�L‰LðÜ(-¯©ðä
Œ3�ˆ8…_ñr   c                ót  € V\         9   d"   \        P                  ! V4      P                  pM\	        RV 24      hV P
                  P                  V P                  R7      p\        P                  ! V4      V8H  P                  4       P                  4       pV^ 8H  P                  4       P                  4       pWT3# )zÈCalculate FP8 tensor stats

Args:
    tensor: The tensor to calculate stats for.
    float8_dtype: The float8 dtype.

Returns:
    A tuple containing the number of zeros and the number of max values.
r   )r   )r   r   r   r   r"   Ú_datar   Ú_orig_dtyper3   ÚsumÚitem)rT   r   ÚFP8_MAXÚtensor_orig_typeÚnum_maxÚnum_zeros   &&    r   Úfp8_tensor_statisticsr^   �   s™   € ð ”yÔ Ü—+’+˜lÓ+×/Ñ/‰äÐ5°l°^ÐDÓEÐEØ—|‘|—‘¨V×-?Ñ-?�Ó@ÐÜ�yŠyÐ)Ó*¨gÑ5×:Ñ:Ó<×AÑAÓC€GØ  AÑ%×*Ñ*Ó,×1Ñ1Ó3€HØÐÐr   c                 ó~   € \        V 4      ^8X  g   Q R4       hV ^ ,          V ^,          8„  ;'       d    V ^,          ^8H  # )r   z%is_row_major only supports 2D tensors)Úlen)Ústrides   &r   Úis_row_majorrb   ³   s=   € Üˆv‹;˜!ÔÐDÐDÓDÐØ�!�9�v˜a•yÑ ×3Ð3 V¨A¥Y°!¡^Ð3r   c                ó<   € V ^8„  d   QhR\         R\         R\         /# )r   ÚsizeÚalignment_valuer+   )r,   )r   s   "r   r   r   ¸   s&   € ÷ Cñ CœSð C´3ð C¼3ñ Cr   c                ó>   € ^V ^,
          V,          ,           V,          # )aY  
Returns the minimum alignment value that is greater than or equal to the given size.

Args:
    size: The size of the data to be aligned.
    alignment_value: The alignment value to be used.

Returns:
    int: The minimum alignment value that is greater than or equal to the given size.

Usage:
```
    >>> _get_min_alignment(10, 8)
    16
```
© )rd   re   s   &&r   Ú_get_min_alignmentrh   ¸   s   € ð" �$˜•(˜Õ.Õ/°?ÕBÐBr   c                óœ   € V ^8„  d   QhR\         P                  R\        \        \        \        ,          3,          R\         P                  /# )r   rT   Údimsr+   )r   r   r   r,   r   )r   s   "r   r   r   Ì   s?   € ÷ %Gñ %GÜ�L‰Lð%GÜ %¤c¬8´C­=Ð&8Õ 9ð%Gä
‡\�\ñ%Gr   c                óN  € V P                  4       ^8X  g   Q hV P                  w  r#\        V\        4      '       d   V3p^ V9   d   \	        V^4      MTp^V9   d   \	        V^4      MTpWB,
          pWS,
          p\
        P                  P                  P                  V ^ V^ V34      # )a  
Pads a 2D tensor with zeros to ensure that its dimensions are multiples of 16, which is required `torch._scaled_mm`

Args:
    tensor: The tensor to pad.
    dims: Dimensions to pad.

Returns:
    torch.Tensor: The padded tensor.

Usage:
```
    >>> pad_tensor_for_matmul(torch.randn((10, 10)), dims=0).shape
    torch.Size([16, 10])
    >>> pad_tensor_for_matmul(torch.randn((10, 10)), dims=1).shape
    torch.Size([10, 16])
    >>> pad_tensor_for_matmul(torch.randn((10, 10)), dims=(0, 1)).shape
    torch.Size([16, 16])
```
)	r/   Úshaper;   r,   rh   r   ÚnnÚ
functionalÚpad)rT   rj   Údim1Údim2Údim1_alignedÚdim2_alignedÚpad_dim1Úpad_dim2s   &&      r   Úpad_tensor_for_matmulrv   Ì   s›   € ð. �:‰:‹<˜1ÔÐÐØ—‘�J€Dä�$œ×ÒØˆwˆð 45¸´9Ô% d¨BÔ/À$€LØ34¸´9Ô% d¨BÔ/À$€Lð Õ"€HØÕ"€Hä�8‰8×Ñ×"Ñ" 6¨A¨x¸¸HÐ+EÓFÐFr   c                ó8   € V ^8„  d   QhR\         P                  /# )r   ÚscalerK   )r   s   "r   r   r   ô   s   € ÷ 6ñ 6¬5¯<©<ñ 6r   c                 óÊ   € V P                   \        P                  8X  g   Q R 4       h\        P                  ! \        P                  ! \        P
                  ! V 4      4      4      # )zscale must be float32 tensor)r   r   r!   Úexp2ÚfloorÚlog2)rx   s   &r   r#   r#   ô   s?   € Ø�;‰;œ%Ÿ-™-Ô'ÐGÐ)GÓGÐ'Ü�:Š:”e—k’k¤%§*¢*¨UÓ"3Ó4Ó5Ð5r   )F)%Útypingr   r   r   r   r   Útorch.distributedÚdistributedr5   Ú)torch.distributed._functional_collectivesr   r   Útorchao.float8.configr	   r    ÚcudaÚis_availableÚversionÚhipÚIS_ROCMÚfloat8_e4m3fnÚfloat8_e5m2Úfloat8_e4m3fnuzÚfloat8_e5m2fnuzr   Úno_gradr%   r2   r@   rD   rH   rR   r^   rb   rh   rv   r#   rg   r   r   Ú<module>rŒ      s  ð÷ 4Ó 3ã Ý  ß Wå 4ð €à
�*‰*×
!Ñ
!Ó
#×
EÐ
E¨¯©×(9Ñ(9ÀÐ(E€à	×ÑØ	×ÑØ	×ÑØ	×Ñð	€	ð ‡‚ƒöó ðð0 ‡‚ƒð ØØ.@×.KÑ.KØ"&öó ðð8 ‡‚ƒð ØØ.@×.KÑ.KØ"&Ø',öó ðõ@Fõ0%õò,4õ
Cõ(%G÷P6r   