o
    h                     @   s@   d dl mZ ddlmZ e rd dlmZ d	ddZdd ZdS )
    N   )is_fp8_availableTc           	      C   s  t  std|  D ]\}}t|tjrP|rP|rPtdd |jjD r' dS |j	du}t
j|j|j||jjd}|j|j |rI|j	|j	 t| || qt|tjr}|r}|r}t
j|jd |j|jjd}|j|j |j	|j	 t| || qt|t
jr|s|r|j	du}tj|j|j||jjd}|j|j |r|j	|j	 t| || qt|t
jr|s|rtj|jd |j|jjd}|j|j |j	|j	 t| || qt||||d qd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.c                 s   s    | ]	}|d  dkV  qdS )   r   N ).0pr   r   Y/var/www/html/ai/venv/lib/python3.10/site-packages/accelerate/utils/transformer_engine.py	<genexpr>!   s    z convert_model.<locals>.<genexpr>N)biasparams_dtyper   )epsr   )to_transformer_engine_convert_linear_convert_ln)r   ImportErrornamed_children
isinstancennLinearanyweightshaper
   tein_featuresout_featuresdtypecopy_setattr	LayerNormnormalized_shaper   convert_model)	modelr   r   r   namemodulehas_bias	te_module
new_moduler   r   r   r       sP   

r    c                 C   s8   t  std|  D ]}t|tjtjfr dS q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.TF)r   r   modulesr   r   r   r   )r!   mr   r   r   has_transformer_engine_layersK   s   r)   )TTT)	torch.nnr   importsr   transformer_engine.pytorchpytorchr   r    r)   r   r   r   r   <module>   s   
3