
    &`ia                         d dl mZmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZ  ed          Ze G d deee                               Ze G d d	e                      Ze G d
 dee                               ZdS )    )ABCabstractmethod)AnyDictGenericTypeVar)AcceleratorContext)DeveloperAPITc                   (    e Zd ZdZed             ZdS )DAGOperationFuturez
    A future representing the result of a DAG operation.

    This is an abstraction that is internal to each actor,
    and is not exposed to the DAG caller.
    c                     t           )zM
        Wait for the future and return the result of the operation.
        )NotImplementedErrorselfs    p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/dag/dag_operation_future.pywaitzDAGOperationFuture.wait   s
    
 "!    N)__name__
__module____qualname____doc__r   r    r   r   r   r   
   s9          " " ^" " "r   r   c                       e Zd ZdZd Zd ZdS )ResolvedFuturez~
    A future that is already resolved. Calling `wait()` on this will
    immediately return the result without blocking.
    c                     || _         dS )zl
        Initialize a resolved future.

        Args:
            result: The result of the future.
        N_result)r   results     r   __init__zResolvedFuture.__init__"   s     r   c                     | j         S )zX
        Wait and immediately return the result. This operation will not block.
        r   r   s    r   r   zResolvedFuture.wait+   s     |r   N)r   r   r   r   r    r   r   r   r   r   r      s<         
      r   r   c                       e Zd ZU dZi Zeed f         ed<   ededd ddfd            Z	ededdfd            Z
dd	eded
efdZdefdZddZdS )	GPUFuturea  
    A future for a GPU event on a CUDA stream.

    This future wraps a buffer, and records an event on the given stream
    when it is created. When the future is waited on, it makes the current
    CUDA stream wait on the event, then returns the buffer.

    The buffer must be a GPU tensor produced by an earlier operation launched
    on the given stream, or it could be CPU data. Then the future guarantees
    that when the wait() returns, the buffer is ready on the current stream.

    The `wait()` does not block CPU.
    gpu_futuresfut_idfutreturnNc                     | t           j        v r1t           j                            |                                            |t           j        | <   dS )z
        Cache the GPU future.
        Args:
            fut_id: GPU future ID.
            fut: GPU future to be cached.
        Nr#   r$   popdestroy_event)r%   r&   s     r   add_gpu_futurezGPUFuture.add_gpu_futureN   sH     Y***!%%f--;;===(+	f%%%r   c                     | t           j        v r3t           j                            |                                            dS dS )z{
        Remove the cached GPU future and destroy its CUDA event.
        Args:
            fut_id: GPU future ID.
        Nr)   )r%   s    r   remove_gpu_futurezGPUFuture.remove_gpu_future[   sC     Y***!%%f--;;===== +*r   bufstreamc                 <   |%t          j                                                    }|| _        t          j                                                    | _        | j                            |           || _        d| _        t          
                    ||            dS )an  
        Initialize a GPU future on the given stream.

        Args:
            buf: The buffer to return when the future is resolved.
            fut_id: The future ID to cache the future.
            stream: The torch stream to record the event on, this event is waited
                on when the future is resolved. If None, the current stream is used.
        NF)r	   getcurrent_stream_bufcreate_event_eventrecord_fut_id_waitedr#   r,   )r   r/   r%   r0   s       r   r    zGPUFuture.__init__e   s     >'+--<<>>F	(,..;;==6"""" 	  .....r   c                     t          j                                                    }| j        s@d| _        |                    | j                   t                              | j                   | j	        S )z
        Wait for the future on the current CUDA stream and return the result from
        the GPU operation. This operation does not block CPU.
        T)
r	   r2   r3   r9   
wait_eventr6   r#   r.   r8   r4   )r   r3   s     r   r   zGPUFuture.wait{   sc    
 ,/11@@BB| 	6DL%%dk222''555yr   c                 &    | j         dS d| _         dS )zE
        Destroy the CUDA event associated with this future.
        N)r6   r   s    r   r+   zGPUFuture.destroy_event   s     ;Fr   )N)r'   N)r   r   r   r   r$   r   int__annotations__staticmethodr,   r.   r   r    r   r+   r   r   r   r#   r#   2   s          0 +-Kc;&',,,
,s 
, 
, 
, 
, 
, \
, ># >$ > > > \>/ /C / /c / / / /,c         r   r#   N)abcr   r   typingr   r   r   r   ,ray.experimental.channel.accelerator_contextr	   ray.util.annotationsr
   r   r   r   r#   r   r   r   <module>rD      s5   # # # # # # # # . . . . . . . . . . . . K K K K K K - - - - - -GCLL " " " " "gaj " " "      '   , ] ] ] ] ]"3' ] ] ] ] ]r   