
    `i                     v    d dl Zd dlZd dlZd dlZd dlm	Z	  G d d          Z
di dfddej        ddd	Zd
 ZdS )    N)runtimec                       e Zd ZdZd ZdefdZedej	        fd            Z
edej	        fd            Zed             Zdd	Zd
 ZdS )_PerfCaseResulta1   An obscure object encompassing timing results recorded by
    :func:`~cupyx.profiler.benchmark`. Simple statistics can be obtained by
    converting an instance of this class to a string.

    .. warning::
        This API is currently experimental and subject to change in future
        releases.

    c                     |j         dk    sJ |j        d         t          |          dz   k    sJ |j        d         dk    sJ || _        || _        || _        d S )N   r      )ndimshapelenname_ts_devices)selfr   tsdevicess       h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/profiler/_time.py__init__z_PerfCaseResult.__init__   s_    w!||||x{c'llQ.....x{Q	    returnc                 .    |                      d          S )z Returns a string representation of the object.

        Returns:
            str: A string representation of the object.
        Tshow_gputo_strr   s    r   __repr__z_PerfCaseResult.__repr__   s     {{D{)))r   c                     | j         d         S )zA :class:`numpy.ndarray` of shape ``(n_repeat,)``, holding times spent
        on CPU in seconds.

        These values are delta of the host-side performance counter
        (:func:`time.perf_counter`) between each repeat step.
        r   r   r   s    r   	cpu_timesz_PerfCaseResult.cpu_times%   s     x{r   c                      | j         dd         S )zA :class:`numpy.ndarray` of shape ``(len(devices), n_repeat)``,
        holding times spent on GPU in seconds.

        These values are measured using ``cudaEventElapsedTime`` with events
        recorded before/after each repeat step.
        r   Nr   r   s    r   	gpu_timesz_PerfCaseResult.gpu_times/   s     x|r   c                 J   |j         dk    sJ |j        dk    sJ |dz  }d                    | |                                          }|j        dk    rP|d                    |                                |                                |                                          z  }|S )Nr   r   g    .Az    {}: {:9.03f} usz2   +/- {:6.03f} (min: {:9.03f} / max: {:9.03f}) us)r	   sizeformatmeanstdminmax)device_namett_usss       r   _to_str_per_itemz _PerfCaseResult._to_str_per_item9   s    v{{{{vzzzz3w!((diikkBB6A::ELL

DHHJJ

4 4 4Ar   Fc           	      n   |                      d| j        d                   g}|rdt          | j                  D ]O\  }}|                    |                      d                    |          | j        d|z                                 Pd                    | j        d                    |                    S )NCPUr   zGPU-{}r   z
{:<20s}:{} )r-   r   	enumerater   appendr$   r   join)r   r   resultsids        r   r   z_PerfCaseResult.to_strE   s    ((<<= 	<!$-00 < <1))(//!*<*<*.(1q5/; ;< < < < ""49chhw.?.?@@@r   c                 .    |                      d          S )NTr   r   r   s    r   __str__z_PerfCaseResult.__str__N   s    {{D{)))r   N)F)__name__
__module____qualname____doc__r   strr   property_numpyndarrayr   r!   staticmethodr-   r   r8    r   r   r   r   
   s              *# * * * * 6>    X 6>    X 	 	 \	A A A A* * * * *r   r   rB   i'  
   )r   n_warmupmax_durationr   c          
         || j         }|t          j                                        f}t	          |           st          d          t          |t                    st          d          t          |t                    st          d          t          |t                    st          d          t          |t                    st          d          t          |t                    st          d          t          j        |          st          d          t          |t                    st          d	          t          | |||||||          S )
aO   Timing utility for measuring time spent by both CPU and GPU.

    This function is a very convenient helper for setting up a timing test. The
    GPU time is properly recorded by synchronizing internal streams. As a
    result, to time a multi-GPU function all participating devices must be
    passed as the ``devices`` argument so that this helper knows which devices
    to record. A simple example is given as follows:

    .. code-block:: py

        import cupy as cp
        from cupyx.profiler import benchmark

        def f(a, b):
            return 3 * cp.sin(-a) * b

        a = 0.5 - cp.random.random((100,))
        b = cp.random.random((100,))
        print(benchmark(f, (a, b), n_repeat=1000))


    Args:
        func (callable): a callable object to be timed.
        args (tuple): positional arguments to be passed to the callable.
        kwargs (dict): keyword arguments to be passed to the callable.
        n_repeat (int): number of times the callable is called. Increasing
            this value would improve the collected statistics at the cost
            of longer test time.
        name (str): the function name to be reported. If not given, the
            callable's ``__name__`` attribute is used.
        n_warmup (int): number of times the callable is called. The warm-up
            runs are not timed.
        max_duration (float): the maximum time (in seconds) that the entire
            test can use. If the taken time is longer than this limit, the test
            is stopped and the statistics collected up to the breakpoint is
            reported.
        devices (tuple): a tuple of device IDs (int) that will be timed during
            the timing test. If not given, the current device is used.

    Returns:
        :class:`~cupyx.profiler._time._PerfCaseResult`:
            an object collecting all test results.

    Nz#`func` should be a callable object.z`args` should be of tuple type.z `kwargs` should be of dict type.z `n_repeat` should be an integer.z`name` should be a string.z `n_warmup` should be an integer.z)`max_duration` should be given in secondsz!`devices` should be of tuple type)r9   _cupycudaget_device_idcallable
ValueError
isinstancetupledictintr=   r?   isreal_repeat)funcargskwargsn_repeatr   rD   rE   r   s           r   	benchmarkrV   R   sX   ` |}:++--/D>> @>???dE"" <:;;;fd## =;<<<h$$ =;<<<dC   75666h$$ =;<<<=&& FDEEEgu%% ><===dFHdHlGM M Mr   c                 :   g }g }	|D ]}
t          j                    }	 t          j        |
           |                    t          j        j                                                   |	                    t          j        j                                                   t          j        |           # t          j        |           w xY wt          |          D ]
}
 | |i | t          ||          D ]\  }}t          j                    }	 t          j        |           |
                                 t          j        |           n# t          j        |           w xY w|                                 g }d |D             }d}t          |          D ]M}
t          ||          D ]n\  }}t          j                    }	 t          j        |           |
                                 t          j        |           V# t          j        |           w xY wt          j                    } | |i | t          j                    }||z
  }|                    |           t          |	|          D ]n\  }}t          j                    }	 t          j        |           |
                                 t          j        |           V# t          j        |           w xY wt          |	|          D ]n\  }}t          j                    }	 t          j        |           |                                 t          j        |           V# t          j        |           w xY wt          t          ||	                    D ]F\  }
\  }}t          j                            ||          dz  }||
                             |           G|t          j                    |z
  z  }||k    r nOt!          j        |g|z   t           j                  }t'          |||          S )Nc                     g | ]}g S rB   rB   ).0r5   s     r   
<listcomp>z_repeat.<locals>.<listcomp>   s    &&&&&&r   r   gMbP?)dtype)r   )r   	getDevice	setDevicer2   rG   rH   streamEventrangeziprecordsynchronize_timeperf_counterr1   get_elapsed_timer?   asarrayfloat64r   )rR   rS   rT   rU   r   rD   rE   r   events_1events_2r5   prev_deviceeventdevicer   r!   durationt1t2cpu_timeev1ev2gpu_timer   s                           r   rQ   rQ      s    HH + +'))	+a   OOEJ-3355666OOEJ-3355666k****Gk****8__  dfXw//  v'))	+f%%%LLNNNk****Gk****I&&X&&&IH8__ % % 733 	/ 	/ME6!+--K/!&)))!+....!+....!!df!!7""" 733 	/ 	/ME6!+--K/!&)))!+....!+.... 733 	/ 	/ME6!+--K/!&)))!!###!+....!+....&s8X'>'>?? 	* 	*MAzSz223<<tCHaL))))E&((2--l""E # 
i/v~	F	F	FB4W5555s<   B B22C(EE!(HH(KK
(MM)math_mathtimerd   numpyr?   cupyrG   cupy_backends.cuda.apir   r   infrV   rQ   rB   r   r   <module>r|      s                    * * * * * *E* E* E* E* E* E* E* E*R b5HMBUYHM HM HM HM HMVF6 F6 F6 F6 F6r   