+
    "Ý³já  ã                   ó  € R t ^ RIHt ^ RIt^ RIHt ^ RIHt ^ RIH	t	H
t
HtHt ^ RIHt R]
P                  R]P                   RR3R R	 lltR
 R lt]P&                  P(                   ! R R]P*                  P,                  4      4       tR# )z9
Utilities for scaling high precision tensors to float8.
)ÚOptionalN)ÚScalingGranularity)Útensor_already_casted_to_fp8)ÚFloat8TrainingTensorÚGemmInputRoleÚLinearMMConfigÚhp_tensor_and_scale_to_float8)Útensor_to_scaleFc                óÂ   € V ^8„  d   QhR\         P                  R\         P                  R\        R\        R\
        R\        R\        \        ,          R\        R	\        /	# )
é   Ú	hp_tensorÚfloat8_dtypeÚlinear_mm_configÚreduce_amaxÚgemm_input_roleÚscaling_granularityÚaxiswise_dimÚround_scales_to_power_of_2Úreturn)
ÚtorchÚTensorÚdtyper   Úboolr   r   r   Úintr   )Úformats   "Úd/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torchao/float8/float8_scaling_utils.pyÚ__annotate__r      sn   € ÷ +ñ +Ü�|‰|ð+ä—+‘+ð+ô %ð+ô ð	+ô
 #ð+ô ,ð+ô œ3•-ð+ô !%ð+ô ñ+ó    c	           	     óD   € \        V VVVVVV4      p	\        V V	VVVV4      # )aþ  
Given a high precision tensor `hp_tensor`,
scales `hp_tensor` dynamically and returns a `Float8TrainingTensor` of the result.

Args:
    hp_tensor: the tensor to convert
    float8_dtype: the float8 dtype to use
    linear_mm_config: Defines the configuration for the scaled_mm for
      the 3 fwd/bwd gemms of linear
    reduce_amax: whether to reduce the max(abs(hp_tensor)) value across distributed ranks
    gemm_input_role: Defines the role of this tensor (input, weight or grad_output) in
      the 3 fwd/bwd gemms of linear
    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   r   r   r   r   Údevice_meshr   r   r   Úscales
   &&&&&&&&& r   Úhp_tensor_to_float8_dynamicr!      sD   € ô6 ØØØØØØØ"ó€Eô )ØØØØØØóð r   c                óR   € V ^8„  d   QhR\         R\        R\        \         ,          /# )r   r   r   r   )r   r   r   )r   s   "r   r   r   K   s*   € ÷ ñ Üðä+ðô Œc…]ñr   c                ó2   € V\         P                  J d   V # R# )a  
Convenience function which takes in an axiswise dim which is only relevant
for axiswise scaing, and a scaling type.  The output is pass-through
if scaling type is axiswise, and None otherwise.  This is done to keep the
logic from choosing the axiswise dim out of the scaling function.
N)r   ÚAXISWISE)r   r   s   &&r   Úget_maybe_axiswise_dimr%   K   s   € ð Ô0×9Ñ9Ó9ØÐÙr   c                   óP   a € ] tR t^Zt o Rt]V 3R lR l4       t]R 4       tRtV t	R# )ÚNoopFwToFloat8BwDynamiczF
Forward: no-op
Backward: convert to float8_e5m2 with dynamic scaling
c                ó:   <€ V ^8„  d   QhRS[ RS[P                  /# )r   r   Útarget_dtype)r   r   r   )r   Ú__classdict__s   "€r   r   Ú$NoopFwToFloat8BwDynamic.__annotate__b   s%   ø€ ÷ ñ ñ )ðñ —k‘kñ	r   c                ó   € W n         W0n        V# ©N)r   r)   )ÚctxÚtensorr   r)   s   &&&&r   ÚforwardÚNoopFwToFloat8BwDynamic.forwarda   s   € ð  0ÔØ'ÔØˆr   c                óÄ   € \        V4      '       d   VR R 3# \        WP                  4      p\        VVV P                  V P                  \
        P                  4      pVR R 3# r-   )r   r	   r)   r   r   r   ÚGRAD_OUTPUT)r.   ÚgradYÚgradY_scaleÚ
fp8_tensors   &&  r   ÚbackwardÚ NoopFwToFloat8BwDynamic.backwardl   sd   € ä'¨×.Ò.Ø˜$ Ð$Ð$Ü% e×-=Ñ-=Ó>ˆÜ2ØØØ×ÑØ× Ñ Ü×%Ñ%ó
ˆ
ð ˜4 Ð%Ð%r   © N)
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ústaticmethodr0   r7   Ú__static_attributes__Ú__classdictcell__)r*   s   @r   r'   r'   Z   s2   ø‡ € ñð
 ÷ó ðð ñ&ó ö&r   r'   )r>   Útypingr   r   Útorchao.float8.configr   Ú torchao.float8.distributed_utilsr   Ú%torchao.float8.float8_training_tensorr   r   r   r   Útorchao.float8.float8_utilsr	   ÚINPUTÚ
TENSORWISEr!   r%   Ú_dynamoÚallow_in_graphÚautogradÚFunctionr'   r9   r   r   Ú<module>rM      s�   ðñõ ã å 4Ý I÷ó õð Ø%2×%8Ñ%8ØØ.@×.KÑ.KØ"&Ø',÷+õ\ð ‡�×Ñô&˜eŸn™n×5Ñ5ó &ó ò&r   