o
    }oi9                     @   sR   d Z ddlZddlmZ ddlZddlZddgZG dd deZ	G dd dZ
dS )z,
This module support timing of code blocks.
    N)Optional
NamedTimerSimpleTimerc                   @   s   e Zd ZdZg dZdddZdd	 Zed
d Zedd Z	dddZ
d ddZd ddZd ddZdd Zd ddZdd ZdS )!r   a   
    A timer class that supports multiple named timers.
    A named timer can be used multiple times, in which case the average
    dt will be returned.
    A named timer cannot be started if it is already currently running.
    Use case: measuring execution of multiple code blocks.
    )meansumminmaxnoner   Fc                 C   s>   || j vrtd| d| j  || _|| _|| _|   dS )ac  
        Args:
            reduction (str): reduction over multiple timings of the same timer
                             (none - returns the list instead of a scalar)
            sync_cuda (bool): if True torch.cuda.synchronize() is called for start/stop
            buffer_size (int): if positive, limits the number of stored measures per name
        zUnknown reduction=z please use one of N)_REDUCTION_TYPE
ValueError
_reduction
_sync_cuda_buffer_sizereset)self	reduction	sync_cudabuffer_size r   E/home/ubuntu/.local/lib/python3.10/site-packages/nemo/utils/timers.py__init__&   s   
zNamedTimer.__init__c                 C   s
   |  |S N)get)r   kr   r   r   __getitem__7   s   
zNamedTimer.__getitem__c                 C   s   | j S r   )r   r   r   r   r   r   :   s   zNamedTimer.buffer_sizec                 C   s&   | j dkrdd }|S tt| j }|S )Nr	   c                 S   s   | S r   r   )xr   r   r   <lambda>A   s    z*NamedTimer._reduction_fn.<locals>.<lambda>)r   getattrnp)r   fnr   r   r   _reduction_fn>   s
   
zNamedTimer._reduction_fnNc                 C   s    |du r	i | _ dS i | j |< dS )z
        Resents all / specific timer

        Args:
            name (str): timer name to reset (if None all timers are reset)
        N)timers)r   namer   r   r   r   G   s   
zNamedTimer.reset c                 C   sZ   | j |i }d|v rtd| d| jr tj r tj  t |d< || j |< dS )zl
        Starts measuring a named timer.

        Args:
            name (str): timer name to start
        startzCannot start timer = 'z' since it is already activeN)	r#   r   RuntimeErrorr   torchcudais_initializedsynchronizetimer   r$   
timer_datar   r   r   r&   S   s   
zNamedTimer.startc                 C   s   | j |d}|du sd|vrtd| d| jr$tj r$tj  t |	d }|dg |g |d< | j
dkrI|d | j
 d |d< || j |< dS )zj
        Stops measuring a named timer.

        Args:
            name (str): timer name to stop
        Nr&   zCannot end timer = 'z' since it is not activedtr   )r#   r   r'   r   r(   r)   r*   r+   r,   popr   )r   r$   r.   r/   r   r   r   stopg   s   

zNamedTimer.stopc                 C   s   | j |i }d|v rdS dS )Nr&   TF)r#   r   r-   r   r   r   	is_active   s   zNamedTimer.is_activec                 C   s   dd | j  D S )z8
        Return list of all active named timers
        c                 S   s   g | ]
\}}d |v r|qS )r&   r   .0r   vr   r   r   
<listcomp>   s    z,NamedTimer.active_timers.<locals>.<listcomp>)r#   itemsr   r   r   r   active_timers   s   zNamedTimer.active_timersc                 C   s   | j | dg }| |S )zp
        Returns the value of a named timer

        Args:
            name (str): timer name to return
        r/   )r#   r   r"   )r   r$   dt_listr   r   r   r      s   
zNamedTimer.getc                    s"   | j   fdd| j D }|S )zJ
        Exports a dictionary with average/all dt per named timer
        c                    s&   i | ]\}}d |v r| |d  qS )r/   r   r3   r!   r   r   
<dictcomp>   s   & z%NamedTimer.export.<locals>.<dictcomp>)r"   r#   r7   )r   datar   r:   r   export   s   zNamedTimer.export)r   Fr
   r   )r%   )__name__
__module____qualname____doc__r   r   r   propertyr   r"   r   r&   r1   r2   r8   r   r=   r   r   r   r   r      s     







c                   @   s\   e Zd ZdZdddZdd Zddeej fd	d
Z	ddeej fddZ
defddZdS )r   zU
    Simple Timer with maximum possible resolution, uses `time.perf_counter_ns`.
    Tc                 C   s   d| _ d| _|| _dS )z

        Args:
            sync_cuda: synchronize CUDA device.
                The synchronization is done only if the device for start/stop is None or CUDA device.
        r   N)
total_time_start_timer   )r   r   r   r   r   r      s   
zSimpleTimer.__init__c                 C   s   d| _ d| _dS )zReset timerr   N)rC   rD   r   r   r   r   r      s   
zSimpleTimer.resetNdevicec                 C   sP   | j rtj r|du s|jdkrtjj|d | jdur!tdt	 | _dS )zh
        Start timer.

        Args:
            device: CUDA device to synchronize (optional).
        Nr)   rE   zTimer already started)
r   r(   r)   r*   typer+   rD   r'   r,   perf_counter_nsr   rE   r   r   r   r&      s
   "
zSimpleTimer.startc                 C   sd   | j rtj r|du s|jdkrtjj|d | jdu r!td|  jt	
 | j 7  _d| _dS )zh
        Stop device.

        Args:
            device: CUDA device to synchronize (optional).
        Nr)   rF   zTimer not started)r   r(   r)   r*   rG   r+   rD   r'   rC   r,   rH   rI   r   r   r   r1      s   "

zSimpleTimer.stopreturnc                 C   s
   | j d S )zReturn total time in secondsg    eA)rC   r   r   r   r   	total_sec   s   
zSimpleTimer.total_sec)Tr   )r>   r?   r@   rA   r   r   r   r(   rE   r&   r1   floatrK   r   r   r   r   r      s    
)rA   r,   typingr   numpyr    r(   __all__objectr   r   r   r   r   r   <module>   s     
