§
    †Pƒiá  ã                   ó<  — d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZ de
j        dej        ddfd	ej        d
ej        dedede
dedee         dede	fd„Zdededee         fd„Zej        j         G d„ dej        j        ¦  «        ¦   «         ZdS )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_scaleFÚ	hp_tensorÚfloat8_dtypeÚlinear_mm_configÚreduce_amaxÚgemm_input_roleÚscaling_granularityÚaxiswise_dimÚround_scales_to_power_of_2Úreturnc	           	      óT   — t          | ||||||¦  «        }	t          | |	||||¦  «        S )a6  
    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
             úw/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchao/float8/float8_scaling_utils.pyÚhp_tensor_to_float8_dynamicr      sN   € õ6 ØØØØØØØ"ñô €Eõ )ØØØØØØñô ð ó    c                 ó&   — |t           j        u r| S dS )a2  
    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ØÐØˆ4r   c                   óR   — e Zd ZdZededej        fd„¦   «         Zed„ ¦   «         Z	dS )ÚNoopFwToFloat8BwDynamiczR
    Forward: no-op
    Backward: convert to float8_e5m2 with dynamic scaling
    r   Útarget_dtypec                 ó"   — || _         || _        |S ©N)r   r   )ÚctxÚtensorr   r   s       r   ÚforwardzNoopFwToFloat8BwDynamic.forwarda   s   € ð  0ˆÔØ'ˆÔØˆr   c                 ó¬   — t          |¦  «        r|d d fS t          || j        ¦  «        }t          ||| j        | j        t
          j        ¦  «        }|d d fS r!   )r   r
   r   r	   r   r   ÚGRAD_OUTPUT)r"   ÚgradYÚgradY_scaleÚ
fp8_tensors       r   Úbackwardz NoopFwToFloat8BwDynamic.backwardl   sg   € å'¨Ñ.Ô.ð 	%Ø˜$ Ð$Ð$Ý% e¨SÔ-=Ñ>Ô>ˆÝ2ØØØÔØÔ ÝÔ%ñ
ô 
ˆ
ð ˜4 Ð%Ð%r   N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   ÚtorchÚdtyper$   r*   © r   r   r   r   Z   so   € € € € € ðð ð
 ðð )ðð ”kð	ð ð ñ „\ðð ð&ð &ñ „\ð&ð &ð &r   r   )r.   Útypingr   r0   Útorchao.float8.configr   Ú torchao.float8.distributed_utilsr   Ú%torchao.float8.float8_training_tensorr   r   r   r	   Útorchao.float8.float8_utilsr
   ÚINPUTÚ
TENSORWISEÚTensorr1   ÚboolÚintr   r   Ú_dynamoÚallow_in_graphÚautogradÚFunctionr   r2   r   r   ú<module>rA      s¹  ððð ð Ð Ð Ð Ð Ð à €€€à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø IÐ IÐ IÐ IÐ IÐ Iðð ð ð ð ð ð ð ð ð ð ð ðð ð ð ð ð ð Ø%2Ô%8ØØ.@Ô.KØ"&Ø',ð+ð +ØŒ|ð+à”+ð+ð %ð+ð ð	+ð
 #ð+ð ,ð+ð ˜3”-ð+ð !%ð+ð ð+ð +ð +ð +ð\Øðà+ðð ˆc„]ðð ð ð ð „Ôð&ð &ð &ð &ð &˜eœnÔ5ñ &ô &ñ Ôð&ð &ð &r   