
    Pi                         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Zddej        dedej        fdZdS )    N)Union)nn)TransformerCrossAttentionLayerTransformerDecoderTransformerSelfAttentionLayer)CEWithChunkedOutputLossForwardKLWithChunkedOutputLossReverseKLWithChunkedOutputLoss SymmetricKLWithChunkedOutputLoss)DeepFusionModel)
get_loggerINFOTmodelverbosereturnc                    t           j                            dd          }t          | t                    r| j        } |rt                              d           t          t          | 
                                                    D ]B}t          |t                    st          |t                    r|                    |           Cd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   ms       o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchtune/training/_compile.pycompile_modelr$      s    " jnn4jAAG%))  A?@@@d5==??++,, ' 'a677 	':-<
 <
 	' IIgI&&&	' '    lossc                 Z   t           j                            dd          }|rt                              d           t          | t                    r!t          j        | j	        |          | _	        nt          | t                    r!t          j        | j        |          | _        nt          | t                    r!t          j        | j        |          | _        nLt          | t                    r!t          j        | j        |          | _        nt          j        | |          } | 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   s      r#   compile_lossr-   <   s    jnn4jAAG 97888$/00 4%*]&&
 &
 &
"" 
D8	9	9 4dmWEEE	D8	9	9 4dmWEEE	D:	;	; 4 =)97KKK}T7333Kr%   )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#   <module>r6      s_   
			                      
            ; : : : : : & & & & & &j
 ' '#_45'' 
' ' ' '< ry 4 29      r%   