
    Vh                     6   d dl Z d dlmZ d dlZd dlmZ  ej                  ej                        j                  Z
 ej                  ej                        j                  Z G d de      Zdej                  dej                  fdZd Zd	 Zdd
ZddZddZy)    N)Enumc                   $    e Zd ZdZdZdZdefdZy)
DQuantTypez
    Different quantization methods for auto_quantize API are identified here.

    auto_quantize API currently supports fp16 and bfp16 methods.
    )fp16bfp16returnc                     | j                   S N)value)selfs    g/home/dcms/DCMS/lib/python3.12/site-packages/torch/distributed/algorithms/_quantization/quantization.py__str__zDQuantType.__str__   s    zz    N)__name__
__module____qualname____doc__FP16BFP16strr    r   r   r   r      s     DE r   r   tensorr   c                 \    t        j                  | t        t              j	                         S r
   )torchclampTORCH_HALF_MINTORCH_HALF_MAXhalf)r   s    r   _fp32_to_fp16_with_clampr      s    ;;v~~>CCEEr   c                 6   t        | t        j                        st        dt	        |              |t
        j                  k(  rt        |       S |t
        j                  k(  r)t        j                  j                  j                  |       S t        d| d      )Nz;_quantize_tensor expecting torch.Tensor as input but found Quantization type  is not supported)
isinstancer   TensorRuntimeErrortyper   r   r   r   opsquantization_FloatToBfloat16Quantized)r   qtypes     r   _quantize_tensorr+      s    fell+I$v,X
 	
 
'//	*""	"yy%%??GG/w6GHIIr   c                     t        | t              rt        d | D              st        dt	        |              | D cg c]  }t        ||       }}|S c c}w )Nc              3   P   K   | ]  }t        |t        j                           y wr
   r#   r   r$   .0ps     r   	<genexpr>z(_quantize_tensor_list.<locals>.<genexpr>-          4()
1ell#4   $&zH_quantize_tensor_list expecting list of torch.Tensor as input but found )r#   listallr%   r&   r+   )tensor_listr*   tquantized_tensor_lists       r   _quantize_tensor_listr:   ,   sm    k4( 4-84 1 VW[\gWhVij
 	
 BMMA-a7MM   N   Ac                 |   t        | t        j                        st        dt	        |              |t
        j                  k(  rx| j                  t        j                  k7  rt        d| j                   d      | j                  t        j                  k(  r|| j                         S | j                         |z  S |t
        j                  k(  r_| j                  t        j                  k7  rt        d| j                   d      t        j                  j                  j                  |       S t        d| d      )Nz=_dequantize_tensor expecting torch.Tensor as input but found ztensor dtype is z while expected to be FP16.r!   r"   )r#   r   r$   r%   r&   r   r   dtypefloat16floatr   r'   r(   _Bfloat16QuantizedToFloat)r   r*   
quant_losss      r   _dequantize_tensorrB   7   s   fell+KDQWL>Z
 	
 
<<5==("6<<.0KL  \\U]]*z/A<<>!<<>J..	*""	"<<5==("6<<.0KL  99))CCFKK/w6GHIIr   c                     t        | t              rt        d | D              st        dt	        |              | D cg c]  }t        ||       }}|S c c}w )Nc              3   P   K   | ]  }t        |t        j                           y wr
   r.   r/   s     r   r2   z*_dequantize_tensor_list.<locals>.<genexpr>Q   r3   r4   zJ_dequantize_tensor_list expecting list of torch.Tensor as input but found )r#   r5   r6   r%   r&   rB   )r7   r*   rA   r8   dequantized_tensor_lists        r   _dequantize_tensor_listrF   P   sm    k4( 4-84 1 XY]^iYjXkl
 	
 FQQ1!U;QQ"" Rr;   c                 J     t        j                          fd       }|S )a  
    Quantize the input tensors, choose the precision types, and pass other necessary arguments and then dequantizes the output.

    Currently it only supports:
        . FP16 and BFP16 quantization method supported for gloo and nccl backends
        . all_gather, all_to_all collective ops
    Note: BFP16 only supports 2D tensors.
    Args:
        func (Callable): A function representing collective operations.
        qtype (QuantType): Quantization method
        quant_loss (float, optional): This can be used to improve accuracy in the dequantization.
    Returns:
        (Callable): the same collective as func but enables automatic quantization/dequantization.
    c                  x   |j                  dd       }|j                  dd      }|du rt        d      t        j                  k(  r^| d   }t	        | d         }t        |      }t        j                  ||||       t        t        |	            D ]
  \  }}|||<    y t        j                  k(  r^| d   }t        | d         }t        |      }t        j                  ||||       t        t        |	            D ]
  \  }}|||<    y t        j                  k(  r| d   }|j                  d
d       }	|j                  dd       }
t	        | d         }t	        |      }t        j                  |||	|
|       t        t        |	            D ]
  \  }}|||<    y t        d d      )Ngroupasync_opFTz,The async_op=True mode is not supported yet.r      )rI   rJ   )rA   
out_splits	in_splits)rI   zThe collective op z is not supported yet)getr%   dist
all_gatherr+   r:   	enumeraterF   
all_to_allall_to_all_singlerB   )argskwargsrI   rJ   tensorsinput_tensorsout_tensorsir8   rL   rM   funcr*   rA   s              r   wrapperzauto_quantize.<locals>.wrapperk   s   

7D)::j%0tMNN4??"1gG,T!We<M/?KOOKehW!'UzR 1 

 T__$1gG1$q'5AM/?KOOKehW!'UzR 1 

 T+++1gGL$7J

;5I,T!We<M*7E:K""]J	 "";*M 1 

 !3D69NOPPr   )	functoolswraps)rZ   r*   rA   r[   s   ``` r   auto_quantizer^   [   s*      __T(Q (QT Nr   r
   )r\   enumr   r   torch.distributeddistributedrO   finfor>   minr   maxr   r   r$   r   r+   r:   rB   rF   r^   r   r   r   <module>re      s         U]]+//U]]+// FU\\ Fell F
J!J2#;r   