
    &`i                         d dl Z d dlZd dlZd dlmZ d dlmZ dZdZ e j	        e
          Z G d d          Z e            ad Zd	 ZdS )
    N)	nccl_util)ENV       c                   $    e Zd ZdZd Zd Zd ZdS )
StreamPoola  The class that represents a stream pool associated with a GPU.

    When multistream is enabled, we will allocate a pool of streams for each
    GPU, and get available stream from this pool when a collective kernel is
    initialized. This enables overlapping computation/communication kernels
    using multiple CUDA streams, given that the streams a appropriately
    synchronized. The class is thread-safe.


    Args:
        device_idx: the absolute index of the device for this pool.
    c                     || _         d| _        t          j                    | _        d gt
          z  | _        d| _        t          j                    | _        d S )NFr   )	
device_idx_initialized	threadingLock_initialized_lockNCCL_STREAM_POOL_SIZE_pool_counter
_pool_lock)selfr
   s     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/collective/collective_group/cuda_stream.py__init__zStreamPool.__init__   sJ    $!!*!1!1V33
#.**    c                 V   | j                                          | j        s|                                  | j                                          | j                                         | j        | j                 }| j        dz   t          z  | _        | j                                         |S )zGet an available stream from the pool.

        The function locks the stream pool and releases the lock before
        returning.

        Returns:
            stream (cupy.cuda.Stream): the returned stream from pool.
           )	r   acquirer   
_init_oncereleaser   r   r   r   )r   streams     r   
get_streamzStreamPool.get_stream'   s     	&&(((  	OO&&((( 	!!!DM**.CC!!!r   c                    t          j        | j                  5  t          t                    D ]}t
          j        j        rDt          	                    d           t          j                            dd          | j        |<   Wt          	                    d           t          j        j        j        | j        |<   	 ddd           n# 1 swxY w Y   d| _        dS )z)Initialize the stream pool only for once.zNCCL multistream enabled.F)nullnon_blockingzNCCL multistream disabled.NT)r   Devicer
   ranger   r   NCCL_USE_MULTISTREAMvalloggerdebugcupycudaStreamr   r   
_init_flag)r   is     r   r   zStreamPool._init_once>   s    do.. 	: 	:011 : :+/ :LL!<===$(I$4$4%e$4$T$TDJqMMLL!=>>>$(I$4$9DJqMM:	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: s   B%CCCN)__name__
__module____qualname____doc__r   r   r    r   r   r   r      sK         + + +  .    r   r   c                  b    t          t                    D ]} t          |           t          | <   d S )N)r"   MAX_GPU_PER_ACTORr   _device_stream_pool_map)r+   s    r   _init_stream_poolr4   Q   s8    $%% 3 3%/]]""3 3r   c                     t          j                    }|                                 t          st	                       |                                 t          |          S )z)Get the CUDA stream pool of a GPU device.)r   r   r   r3   r4   r   )r
   locks     r   get_stream_poolr7   W   sJ     >DLLNNN" LLNNN":..r   )loggingr   r'   $ray.util.collective.collective_groupr   ray.util.collective.constr   r   r2   	getLoggerr,   r%   r   dictr3   r4   r7   r0   r   r   <module>r=      s          : : : : : : ) ) ) ) ) )  		8	$	$: : : : : : : :~ $&& 3 3 3/ / / / /r   