o
    }oi                     @   s<   d dl Z d dlZdddZdddZdddZdd	d
ZdS )    NFc                    H   |rt jddd td d dd d fd	d
}|S )a  
    A forward hook to dump all of the module input and output norms. It is called at every time after forward() has computed an output.
    Only float type input/output tensor norms are computed.
    For more details about the forward hook, check https://pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_hook.html

    Args:
        name: tensor name
        trainer: PTL trainer
        rank: worker rank
        logger: PTL log function
        dump_to_file:  wether dump the csv file to the disk
    
debug_infoTexist_okzdebug_info/forward__rank.txtwFc           	           j rg }g }t|D ]<\}}t|tjrG|jtjks'|jtjks'|jtjkrGs.|	d |j
 }|	|  d d d| | qt|trt|D ]<\}}t|tjr|jtjksm|jtjksm|jtjkrst|	d |j
 }|	|  d d d| | qQn|	d |	|j
   |	j  s|	d  d|d	  d
 d|d	     d S )Ninputzdebug_info_forward/r   _inputoutput_outputstep,
Ttraining	enumerate
isinstancetorchTensordtypefloathalfbfloat16appenddatanormtupleglobal_stepwritejoinflush	moduleinputsoutputsvaluesheadersni
input_normoutput_normfpheaderloggernameranktrainer I/home/ubuntu/.local/lib/python3.10/site-packages/nemo/utils/debug_hook.pyforward_hook&   @   $


$




z&get_forward_hook.<locals>.forward_hookosmakedirsopen)r1   r3   r2   r0   dump_to_filer6   r4   r-   r5   get_forward_hook      $r=   c                    r   )aE  
    A backward hook to dump all of the module input and output grad norms. The hook will be called every time the gradients with respect to module inputs are computed.
    Only float type input/output grad tensor norms are computed.
    For more details about the backward hook, check https://pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_full_backward_hook.html

    Args:
        name: tensor name
        trainer: PTL trainer
        rank: worker rank
        logger: PTL log function
        dump_to_file:  wether dump the csv file to the disk
    r   Tr   zdebug_info/backward_r   r   r   Fc           	         r	   )Nr
   zdebug_info_backward/r   r   r   r   r   r   r   Tr   r#   r-   r4   r5   backward_hook_   r7   z(get_backward_hook.<locals>.backward_hookr8   )r1   r3   r2   r0   r<   r?   r4   r-   r5   get_backward_hookM   r>   r@   c                    sL    rt jddd td d ddd fd	d
}|S )a  
    A tensor hook to dump all of the tensor weight norms and grad norms at the end of each of the backward steps. 
    For more details about the tensor hook, check https://pytorch.org/docs/stable/generated/torch.Tensor.register_hook.html 

    Args:
        module: the model module 
        name: tensor name
        trainer: PTL trainer
        rank: worker rank
        logger: PTL log function
        dump_to_file:  wether dump the csv file to the disk
    r   Tr   zdebug_info/tensor_r   z.csvr   Fc                    s   g }g } }|j }| j }d d d| d d d| ||  ||  |j   rks]|d |d |d d|d	  d
d|d	    | S )Nzdebug_info_tensors/r   
_grad_norm_weight_normweightgradr   r   r   T)get_parameterr   r   r   r   r    r!   r"   )rD   r'   r(   rC   weight_norm	grad_normr<   r.   r/   r0   r$   r1   r2   r3   r4   r5   tensor_hook   s(   





z$get_tensor_hook.<locals>.tensor_hookr8   )r$   r1   r3   r2   r0   r<   rI   r4   rH   r5   get_tensor_hook   s   rJ   c              
   C   s   d}t j rt j }|  D ]\}}|dkr$|t| ||||| q|  D ]\}}|dkrG|t	||||| |
t||||| q)dS )z
    Register debug hooks. It can
    1. track the module forward step input/ouput norm
    2. track the module backward step input/output grad norm
    3. track the parameter weight norm and grad norm.
    r    N)r   distributedis_initializedget_ranknamed_parametersregister_hookrJ   named_modulesregister_forward_hookr=   register_full_backward_hookr@   )r$   r3   r0   r<   r2   r1   tensorlayerr4   r4   r5   register_debug_hooks   s   

rV   )F)r9   r   r=   r@   rJ   rV   r4   r4   r4   r5   <module>   s   

9
9.