
    `i                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
  G d d	          Z	 dd
lZ G d d          ZdddZn# e$ r dddZY nw xY wddZddZd
S )zStream context.    )annotations)c_void_p)Any   )coredevicec                  *    e Zd ZdZddZdd	ZddZdS )StreamContexta1  Represent a stream context in the FFI system.

    StreamContext helps setup ffi environment stream by python `with` statement.
    When entering `with` scope, it caches the current environment stream and
    setup the given new stream.
    When exiting `with` scope, it recovers the stream to the cached environment stream.

    Parameters
    ----------
    device
        The device to which the stream belongs.

    stream
        The stream handle.

    See Also
    --------
    :py:func:`tvm_ffi.use_raw_stream`, :py:func:`tvm_ffi.use_torch_stream`

    r	   core.Devicestreamint | c_void_preturnNonec                T     |j                     | _        |j        | _        || _        dS )z<Initialize a stream context with a device and stream handle.N)dlpack_device_typedevice_typeindex	device_idr   )selfr	   r   s      b/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/tvm_ffi/stream.py__init__zStreamContext.__init__3   s)    46466    c                Z    t          j        | j        | j        | j                  | _        | S )z-Enter the context and set the current stream.)r   _env_set_current_streamr   r   r   prev_stream)r   s    r   	__enter__zStreamContext.__enter__9   s,    7dndk
 
 r   argsr   c                Z    t          j        | j        | j        | j                  | _        dS )z1Exit the context and restore the previous stream.N)r   r   r   r   r   r   r   s     r   __exit__zStreamContext.__exit__@   s,    7dnd.>
 
r   N)r	   r   r   r   r   r   )r   r   r   r   r   r   __name__
__module____qualname____doc__r   r   r!    r   r   r   r      sZ         *      
 
 
 
 
 
r   r   Nc                  *    e Zd ZdZddZddZdd	Zd
S )TorchStreamContextz9Context manager that syncs Torch and FFI stream contexts.contextr   r   r   c                    || _         dS )z?Initialize with an optional Torch stream/graph context wrapper.N)torch_context)r   r+   s     r   r   zTorchStreamContext.__init__M   s    !(Dr   c                &   | j         r| j                                          t          j                                        }t          t          t          |j                            |j                  | _	        | j	                                         | S )z)Enter both Torch and FFI stream contexts.)
r-   r   torchcudacurrent_streamr   r	   strcuda_streamffi_context)r   r1   s     r   r   zTorchStreamContext.__enter__Q   s~    ! /",,..."Z6688N,s>01122N4N   D &&(((Kr   r   c                P    | j         r | j         j        |   | j        j        |  dS )z(Exit both Torch and FFI stream contexts.N)r-   r!   r4   r    s     r   r!   zTorchStreamContext.__exit__\   s9    ! 3+"+T22%D%t,,,,r   N)r+   r   r   r   )r   r*   r"   r#   r(   r   r   r*   r*   J   sV        GG	) 	) 	) 	)		 		 		 			- 	- 	- 	- 	- 	-r   r*   r+   r   r   c                     t          |           S )a  Create an FFI stream context with a Torch stream or graph.

        cuda graph or current stream if `None` provided.

        Parameters
        ----------
        context
            The wrapped torch stream or cuda graph.

        Returns
        -------
        context
            The ffi stream context wrapping torch stream context.

        Examples
        --------
        .. code-block:: python

            s = torch.cuda.Stream()
            with tvm_ffi.use_torch_stream(torch.cuda.stream(s)):
                ...

            g = torch.cuda.CUDAGraph()
            with tvm_ffi.use_torch_stream(torch.cuda.graph(g)):
                ...

        Note
        ----
        When working with a raw ``cudaStream_t`` handle, use
        :py:func:`tvm_ffi.use_raw_stream` instead.

        )r*   r+   s    r   use_torch_streamr8   b   s    B "'***r   c                     t          d          )z5Raise an informative error when Torch is unavailable.zCannot import torch)ImportErrorr7   s    r   r8   r8      s    /000r   r	   r   r   r   c                x    t          |t          t          f          st          d          t	          | |          S )a  Create an FFI stream context with the given device and stream handle.

    Parameters
    ----------
    device
        The device to which the stream belongs.

    stream
        The stream handle (for example, a CUDA ``cudaStream_t`` as an integer, or ``0``).

    Returns
    -------
    context
        The FFI stream context.

    Examples
    --------
    The example below uses a CPU device and a dummy stream handle. On CUDA, pass a
    real ``cudaStream_t`` integer.

    .. code-block:: python

        import tvm_ffi

        dev = tvm_ffi.device("cpu:0")
        with tvm_ffi.use_raw_stream(dev, 0):
            # Within the context, the current stream for this device is set
            assert tvm_ffi.get_raw_stream(dev) == 0

    See Also
    --------
    :py:func:`tvm_ffi.use_torch_stream`
        Use a Torch stream or CUDA graph as the source of truth.
    :py:func:`tvm_ffi.get_raw_stream`
        Query the current FFI stream for a device.

    zuse_raw_stream only accepts int or c_void_p as stream input, try use_torch_stream when using torch.cuda.Stream or torch.cuda.graph)
isinstanceintr   
ValueErrorr   )r	   r   s     r   use_raw_streamr?      sE    L fsHo.. 
T
 
 	
 (((r   r=   c                P    t          j         | j                    | j                  S )ap  Get the current FFI stream of a given device.

    Parameters
    ----------
    device
        The device to which the stream belongs.

    Returns
    -------
    stream
        The current FFI stream as an integer handle.

    Examples
    --------
    .. code-block:: python

        import tvm_ffi

        dev = tvm_ffi.device("cpu:0")
        # Default stream is implementation-defined; set it explicitly
        with tvm_ffi.use_raw_stream(dev, 0):
            assert tvm_ffi.get_raw_stream(dev) == 0

    See Also
    --------
    :py:func:`tvm_ffi.use_raw_stream`
        Set the current stream for a device.

    )r   _env_get_current_streamr   r   r   s    r   get_raw_streamrB      s%    < '(A(A(C(CV\RRRr   )N)r+   r   r   r*   )r	   r   r   r   r   r   )r	   r   r   r=   )r'   
__future__r   ctypesr   typingr    r   _tensorr	   r   r/   r*   r8   r:   r?   rB   r(   r   r   <module>rH      s_  $   " " " " " "                        '
 '
 '
 '
 '
 '
 '
 '
TB1LLL- - - - - - - -0!+ !+ !+ !+ !+ !+F  1 1 11 1 1 1 1 1 11+) +) +) +)\S S S S S Ss   A 
AA