o
    ϯi                     @   s   d dl Z d dlZd dlmZmZmZmZ d dlZ	 dde	de	deeg ef geg ee
ef f f fddZdd d	d
eg ef de
dee de	de	ddfddZdS )    N)AnyCallableDictList	num_iterswarmup_itersreturnc                    s4   dt g tf dt g tttf f f fdd}|S )a  
    This is intened to be used as a decorator to time any function.

    Args:
        num_iters (int): number of iterations used to compute the average time
            (sec) required to run the function. If negative, the number of
            iterations is determined dynamically by running the function a few
            times to make sure the estimate is stable.
        warmup_iters (int): number of iterations used to warm up the function.
            This is useful for functions that exhibit poor performance during
            the first few times they run (due to caches, autotuning, etc).
    Returns:
        Dict[str, float]: dictionary of the aggregated timing estimates.
            "iterations": number of iterations used to compute the estimated
                          time.
            "mean": averate time (sec) used to run the function.
            "median": median time (sec) used to run the function.
            "min": minimal time (sec) used to run the function.
            "max": maximal time (sec) used to run the function.
            "stddev": standard deviation of the time (sec) used to run the
                      function.
    funcr	   c                    s*   dt dt dtttf f fdd}|S )Nargskwargsr	   c            
         s   t D ]	} | i | qd}d}g }dkrntj}t |D ]*}t } | i | t | }|| ||7 }|d7 }dk rK|dkrK nq!|t|ksTJ i }	||	d< || |	d< t||	d< t||	d< t	||	d< t
||	d	< |	S )
Nr      g      ?
iterationsmeanmedianminmaxstddev)rangesysmaxsizetimeappendlennpr   r   r   std)
r   r   _
total_timecount	run_timesmax_num_iters
start_timerun_timeret)r
   r   r    K/home/ubuntu/.local/lib/python3.10/site-packages/fvcore/common/benchmark.py	decorated%   s2   
z,timeit.<locals>.decorator.<locals>.decorated)r   r   strfloat)r
   r&   r   r   )r
   r%   	decorator$   s   & ztimeit.<locals>.decorator)r   r   r   r'   r(   )r   r   r*   r$   r)   r%   timeit
   s   0#r+   r)   r
   bm_namekwargs_listc                C   s~  t d g }|D ]A}| di |}t||d|}| }	|}
|r0|
dddd | D  7 }
||
t|	d d t|	d d t|	d	 g qt|}tt	|d
d
df td}tt	|d
d
df td}tt	|d
d
df td}tt	|d
d
df td}d}t d
d|d|d|d| t | |D ]}t d
|d |t|d t|d t|d  qt | d
S )u  
    Benchmark the input function and print out the results.

    Args:
        func (callable): a closure that returns a function for benchmarking,
            where initialization can be done before the function to benchmark.
        bm_name (str): name of the benchmark to print out, e.g. "BM_UPDATE".
        kwargs_list (list): a list of argument dict to pass to the function. The
            intput function will be timed separately for each argument dict.
        num_iters (int): number of iterations to run. Defaults to run until 0.5s.
        warmup_iters (int): number of iterations used to warm up the function.

    Outputs:
        For each argument dict, print out the time (in microseconds) required
        to run the function along with the number of iterations used to get
        the timing estimate. Example output:

        Benchmark               Avg Time(μs)   Peak Time(μs)     Iterations
        -------------------------------------------------------------------
        BM_UPDATE_100                    820             914            610
        BM_UPDATE_1000                  7655            8709             66
        BM_UPDATE_10000                78062           81748              7
        -------------------------------------------------------------------
     r)   r   c                 s   s    | ]	\}}t |V  qd S )N)r'   ).0kvr$   r$   r%   	<genexpr>t   s    zbenchmark.<locals>.<genexpr>r   i@B r   r   Nr   )keyr         zP--------------------------------------------------------------------------------z{:{}s} {:>{}s} {:>{}s} {:>{}s}	Benchmarku   Avg Time(μs)u   Peak Time(μs)
Iterationsz{:{}s} {:15.0f} {:15.0f} {:14d}r$   )printr+   joinitemsr   r'   r   arrayr   r   formatr(   int)r
   r,   r-   r   r   outputsr   func_bm	time_funcr#   namec1c2c3c4dashoutputr$   r$   r%   	benchmarkJ   sF   ! 

&rH   )r   r   )r   r   typingr   r   r   r   numpyr   r=   r'   r(   r+   rH   r$   r$   r$   r%   <module>   s6   $
E
