
    g                     R    d dl mZ d dlmZ ddlmZ ddlmZ ddZd Z	dd
Z
d ZdS )    )
MethodTypeN   )is_fp8_available)GatheredParametersTc                     t                      st          d          ddlm} |                                 D ]T\  }}t          |t          j                  r|r|r|j        du}|j	        g}|r|
                    |j                   t          |d          5  t          d |j	        j        D                       r	 ddd            dS |                    |j        |j        ||j	        j                  }	|	j	                            |j	                   |r|	j                            |j                   t%          | ||	           ddd           n# 1 swxY w Y   /t          |t          j                  r|r|rt          |j	        |j        gd          5  |                    |j        d         |j        |j	        j                  }	|	j	                            |j	                   |	j                            |j                   ddd           n# 1 swxY w Y   t%          | ||	           t          ||j                  r|s|r|j        du}t          j        |j        |j        ||j	        j                  }
|
j	                            |j	                   |r|
j                            |j                   t%          | ||
           t          ||j                  r|s|rt          j        |j        d         |j        |j	        j                  }
|
j	                            |j	                   |
j                            |j                   t%          | ||
           At-          ||||           VdS )	zu
    Recursively converts the linear and layernorm layers of a model to their `transformers_engine` counterpart.
    zBUsing `convert_model` requires transformer_engine to be installed.r   N)modifier_rankc              3   (   K   | ]}|d z  dk    V  dS )   r   N ).0ps     _/var/www/html/ai-engine/env/lib/python3.11/site-packages/accelerate/utils/transformer_engine.py	<genexpr>z convert_model.<locals>.<genexpr>*   s*      @@qq2v{@@@@@@    )biasparams_dtype)epsr   )to_transformer_engine_convert_linear_convert_ln)r   ImportErrortransformer_engine.pytorchpytorchnamed_children
isinstancennLinearr   weightappendr   anyshapein_featuresout_featuresdtypecopy_setattr	LayerNormnormalized_shaper   convert_model)modelr   r   r   tenamemodulehas_biasparams_to_gather	te_module
new_modules              r   r)   r)      s[     `^___++++++,,.. 0 0ffbi(( /	-B /	 /	{$.H & 5 ''444#$4AFFF 
0 
0@@FM,?@@@@@ 
0 
0 
0 
0 
0 
0 
0 
0 
0 II&(;(Y_YfYl &  	  &&v}555 6N((555tY///
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 -- 	2G 	K 	#V]FK$@PQRRR 2 2LL)@)CbhbobuLvv	 &&v}555$$V[1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 E4++++	** 	3H 	_ 	{$.H"F$7hU[UbUh  J ##FM222 3%%fk222E4,,,,-- 	6K 	P[ 	f&=a&@fj_e_l_rsssJ##FM222O!!&+...E4,,,,&; /'	    W0 0s,   %EA>EE	!E	"A1HH#	&H#	c                     t                      st          d          ddlm} |                                 D ]'}t          ||j        |j        |j        f          r dS (dS )zS
    Returns whether a given model has some `transformer_engine` layer or not.
    zRUsing `has_transformer_engine_layers` requires transformer_engine to be installed.r   NTF)	r   r   r   r   modulesr   r'   r   TransformerLayer)r*   r+   ms      r   has_transformer_engine_layersr6   U   s|      pnooo++++++]]__  a",	23FGHH 	44	5r   Fc                 r     t                      st          d          ddlm  fd} |_        |S )z
    Wrapper for a model's forward method to apply FP8 autocast. Is context aware, meaning that by default it will
    disable FP8 autocast during eval mode, which is generally better for more accurate metrics.
    zLUsing `contextual_fp8_autocast` requires transformer_engine to be installed.r   )fp8_autocastc                 t    p| j         } |          5   |i |cd d d            S # 1 swxY w Y   d S )N)enabled
fp8_recipe)training)selfargskwargsr:   r8   r;   model_forwarduse_during_evals       r   forwardz(contextual_fp8_autocast.<locals>.forwardl   s    !2T]\'jAAA 	2 	2 =$1&11	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s   -11)r   r   r   r8   __wrapped__)r@   r;   rA   rB   r8   s   ``` @r   contextual_fp8_autocastrD   c   so    
  jhiii7777772 2 2 2 2 2 2 2 (GNr   c                    t                      st          d          ddlmc m} ||                                ni }d|v rt          |j        |d                   |d<   |                    dd          } |j	        di |}t          | j        ||          }t          | j        d          rt          ||           | _        n|| _        | S )	zC
    Applies FP8 context manager to the model's forward method
    zGUsing `apply_fp8_autowrap` requires transformer_engine to be installed.r   N
fp8_formatuse_autocast_during_evalF__func__r   )r   r    transformer_engine.common.recipecommonrecipe	to_kwargsgetattrFormatpopDelayedScalingrD   rB   hasattrr   )r*   fp8_recipe_handler	te_reciper?   rA   r;   new_forwards          r   apply_fp8_autowraprU   w   s      ecddd888888888/A/M))+++SUFv&y'79MNN|jj!;UCCO))33F33J)%-_UUKu}j)) $";66#Lr   )TTT)F)typesr   torch.nnr   importsr   
operationsr   r)   r6   rD   rU   r   r   r   <module>rZ      s                % % % % % % * * * * * *8 8 8 8v     (    r   