o
    X۷i                     @  s>   d dl mZ d dlZd dlmZ d dlmZ G dd dZdS )    )annotationsN)cuda)runtimec                   @  s<   e Zd ZdZ	dddZdd Zdd	 Zd
d Zdd ZdS )
time_rangea  Mark function calls with ranges using NVTX/rocTX. This object can be
    used either as a decorator or a context manager.

    When used as a decorator, the decorated function calls are marked as
    ranges:

    >>> from cupyx.profiler import time_range
    >>> @time_range()
    ... def function_to_profile():
    ...     pass

    When used as a context manager, it describes the enclosed block as a nested
    range:

    >>> from cupyx.profiler import time_range
    >>> with time_range('some range in green', color_id=0):
    ...    # do something you want to measure
    ...    pass

    The marked ranges are visible in the profiler (such as nvvp, nsys-ui, etc)
    timeline.

    Args:
        message (str): Name of a range. When used as a decorator, the default
            is ``func.__name__``.
        color_id: range color ID
        argb_color: range color in ARGB (e.g. 0xFF00FF00 for green)
        sync (bool): If ``True``, waits for completion of all outstanding
            processing on GPU before calling :func:`cupy.cuda.nvtx.RangePush()`
            or :func:`cupy.cuda.nvtx.RangePop()`

    .. seealso:: :func:`cupy.cuda.nvtx.RangePush`,
        :func:`cupy.cuda.nvtx.RangePop`
    NFc                 C  sP   t jjstd|d ur|d urtd|| _|d ur|nd| _|| _|| _d S )Nznvtx is not installedz3Only either color_id or argb_color can be specified)	r   nvtx	availableRuntimeError
ValueErrormessagecolor_id
argb_colorsync)selfr   r   r   r    r   P/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/profiler/_time_range.py__init__1   s   
ztime_range.__init__c                 C  sV   | j d u r	td| jrt  | jd ur tj| j | j | S tj	| j | j
 | S )NzCwhen used as a context manager, the message argument cannot be None)r   r
   r   r   deviceSynchronizer   r   r   
RangePushC	RangePushr   )r   r   r   r   	__enter__?   s   

ztime_range.__enter__c                 C  s   | j rt  tj  d S N)r   r   r   r   r   RangePop)r   exc_type	exc_value	tracebackr   r   r   __exit__L   s   ztime_range.__exit__c                 C  s   | j d u r|| _ | S r   )r   )r   r   r   r   r   _recreate_cmQ   s   
ztime_range._recreate_cmc                   s   t   fdd}|S )Nc                    s>     j  | i |W  d    S 1 sw   Y  d S r   )r   __name__)argskwargsfuncr   r   r   innerW   s   $z"time_range.__call__.<locals>.inner)	functoolswraps)r   r"   r#   r   r!   r   __call__V   s   ztime_range.__call__)NNNF)	r   
__module____qualname____doc__r   r   r   r   r&   r   r   r   r   r      s    $
r   )
__future__r   r$   cupyr   cupy_backends.cuda.apir   r   r   r   r   r   <module>   s
    