o
    ̳i                     @   s   d dl Z d dlmZ d dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZmZmZ d dlmZ d dlmZ edZ		dd
eeef deddfddZddejdedejfddZdS )    N)Union)nn)TransformerCrossAttentionLayerTransformerDecoderTransformerSelfAttentionLayer)CEWithChunkedOutputLossForwardKLWithChunkedOutputLossReverseKLWithChunkedOutputLoss SymmetricKLWithChunkedOutputLoss)DeepFusionModel)
get_loggerINFOTmodelverbosereturnc                 C   sf   t jdd}t| tr| j} |rtd tt	| 
 D ]}t|ts*t|tr0|j|d qdS )a  
    Utility to compile a transformer model inplace. On PyTorch nightlies we use per-layer compile
    to reduce compile times. Otherwise we compile the full model, which takes longer.

    Args:
        model (Union[TransformerDecoder, DeepFusionModel]): A model to compile.
            Can be a TransformerDecoder or DeepFusionModel; in the latter case only
            the model's decoder will be compiled.
        verbose (bool): Whether to log compile info. Default: True
    Returns:
        None

    TORCH_COMPILE_BACKENDinductorz,Compiling model layers with torch.compile...backendN)osenvironget
isinstancer   decoderloginforeversedlistmodulesr   r   compile)r   r   r   m r!   O/home/ubuntu/.local/lib/python3.10/site-packages/torchtune/training/_compile.pycompile_model   s   

r#   lossc                 C   s   t jdd}|rtd t| trtj| j	|d| _	| S t| t
r.tj| j|d| _| S t| tr>tj| j|d| _| S t| trNtj| j|d| _| S tj| |d} | S )a0  
    Utility to compile and return loss function. If the loss function is chunked cross-entropy,
    we only compile the upcast + cross-entropy calculation, not the chunking. For other losses
    we compile the entire loss function.

    Args:
        loss (nn.Module): A loss function to compile.
        verbose (bool): Whether to log compile info. Default: True
    Returns:
        loss (nn.Module): loss with either entire module compiled or (in the case of
            CEWithChunkedOutputLoss) only the upcast and cross-entropy calculation compiled.
    r   r   z$Compiling loss with torch.compile...r   )r   r   r   r   r   r   r   torchr   compute_cross_entropyr   fkl_lossr	   rkl_lossr
   sym_kl_loss)r$   r   r   r!   r!   r"   compile_loss<   s&   




r*   )T)r   typingr   r%   r   torchtune.modulesr   r   r   torchtune.modules.lossr   r   r	   r
   torchtune.modules.model_fusionr   torchtune.utilsr   r   boolr#   Moduler*   r!   r!   r!   r"   <module>   s$   

 