
    &`i!                         U d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
Z
d dlmZ d dlmZ erd dlZ ej                    Zdaed         ed<   daed         ed<    G d	 d          Zd
ede	e         fdZd ZdS )    N)nullcontext)TYPE_CHECKINGContextManagerListOptionalType)$get_accelerator_manager_for_resource)CommunicatorAcceleratorContext_default_accelerator_context_global_custom_contextc                      e Zd ZdZdedee         fdZedd            Z	edd	            Z
ded
         fdZdd
defdZd Zd ZdefdZdefdZedefd            Zedeee                  fd            Zedefd            ZdS )r   z
    Provides a unified interface for managing different accelerator backends
    This includes stream management, event creation, device context control,
    and communicator support for distributed communication.
    torch_module_namecommunicator_clsc                 j    || _         || _        |dk    rt          j        d|           | _        dS dS )a"  
        Initializes an accelerator context with the specified torch device module
        and communicator class.

        Args:
            torch_module_name: Name of the torch device module (e.g., "cuda", "cpu").
            communicator_cls: Class used to handle communication.
        cpuztorch.N)_torch_module_name_communicator_cls	importlibimport_module
_torch_mod)selfr   r   s      /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/experimental/channel/accelerator_context.py__init__zAcceleratorContext.__init__   sI     (95E %%'56R?P6R6RSSDOOO &%    returnc                  6   t           5  t          t          cddd           S t          Qt          t	          j                              dk    rddlm}  t          d|           anddl	m
} t          d|          at          cddd           S # 1 swxY w Y   dS )a  
        Returns the singleton instance of the accelerator context.

        If a custom accelerator has been registered, initializes the context
        based on the registration. Otherwise, selects an appropriate runtime
        based on the available device (CUDA or CPU) and registers the
        corresponding default communicator.

        Returns:
            AcceleratorContext: A singleton instance of the appropriate
            runtime context.
        Nr   )
_NcclGroupcuda)CPUCommunicatorr   )_accelerator_context_lockr   r   lenrayget_gpu_ids#ray.experimental.channel.nccl_groupr   r   )ray.experimental.channel.cpu_communicatorr    )r   r    s     r   getzAcceleratorContext.get-   s'   " ' 	0 	0%1-	0 	0 	0 	0 	0 	0 	0 	0 ,3s(())A--NNNNNN3E
4 400      4F4 40 0)	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   BABBBaccelerator_contextNc                 
    | a dS )z
        Overwrites the default accelerator context.

        Args:
            accelerator_context: The context to register.
        Nr   )r(   s    r   setzAcceleratorContext.setT   s     "5r   ztorch.devicec                     ddl  j        dk    r j        d          gS  j        dk    r-d t          j                    D             }t          d          }nrd t          j                                                     j                                                 D             }t           j                                                  }g }t          |          dk    r||
                                }|g }|D ]`}	 |                    |                    |                     ,# t          $ r( t          |                                 d| d	          w xY wn|                    d            fd
|D             S )z
        Gets the torch device list configured for this process.

        Returns:
            List[torch.device]: The torch device list.
        r   Nr   r   c                 ,    g | ]}t          |          S  str.0ids     r   
<listcomp>z>AcceleratorContext.get_accelerator_devices.<locals>.<listcomp>n   s    CCC2s2wwCCCr   GPUc                 ,    g | ]}t          |          S r.   r/   r1   s     r   r4   z>AcceleratorContext.get_accelerator_devices.<locals>.<listcomp>q   s.        B  r   z& set incorrectly. expected to include zZ. Did you override this environment variable? If not, please help file an issue on Github.c                 D    g | ]} j         j         d |           S ):)devicer   )r2   	device_idr   torchs     r   r4   z>AcceleratorContext.get_accelerator_devices.<locals>.<listcomp>   sF     
 
 
 ELD3AAiAABB
 
 
r   )r;   r   r9   r#   r$   r	   get_runtime_contextget_accelerator_idsupperr"   +get_current_process_visible_accelerator_idsappendindex
ValueErrorRuntimeError#get_visible_accelerator_ids_env_var)r   accelerator_idsaccelerator_manager
device_idsaccelerator_visible_listaccelerator_idr;   s   `     @r   get_accelerator_devicesz*AcceleratorContext.get_accelerator_devicesa   s    	"e++ EL''(("f,,CC1B1BCCCO"Fu"M"M 133GGII+1133  O #G'--//# # 
!###OOQQ % (/+-(
 #2 	 	%%&>&D&D^&T&TUUUU!   &.RRTT R R/=R R R  	 a   
 
 
 
 
'
 
 
 	
s   ?(D((2Er9   c                 h    |j         dk    rt                      S | j                            |          S )aW  
        Retrieves the context manager for the specified accelerator device.
        There is no device context for CPU, returning a nullcontext.

        Args:
            device: The target device for which the context manager is required.

        Returns:
            ContextManager: A context manager specific to the device type.
        r   )typer   r   r9   )r   r9   s     r   get_device_contextz%AcceleratorContext.get_device_context   s1     ;%== %%f---r   c                 4    | j                                         S )zT
        Retrieves the current execution stream for the accelerator device.
        )r   current_streamr   s    r   rO   z!AcceleratorContext.current_stream   s     --///r   c                 4    | j                                         S )zE
        Creates an event object for the accelerator device.
        )r   EventrP   s    r   create_eventzAcceleratorContext.create_event   s     $$&&&r   c                 4    | j                                         S )zO
        Generates a communication identifier for communication group.
        )r   generate_communicator_idrP   s    r   rU   z+AcceleratorContext.generate_communicator_id   s     %>>@@@r   c                      | j         |i |S )zJ
        Creates a communication group for collective operations.
        r   )r   argskwargss      r   create_communicatorz&AcceleratorContext.create_communicator   s     &t%t6v666r   c                     | j         S )zL
        Gets the name of the torch module backing the accelerator.
        )r   rP   s    r   module_namezAcceleratorContext.module_name   s    
 &&r   c                     | j         S )z1
        Returns the communicator class.
        rW   rP   s    r   r   z#AcceleratorContext.communicator_cls   s    
 %%r   c                    | j         dk    r t          t          j                              S t          j                                                    }t          |                    | j                                         g                     S )zE
        Returns the number of accelerators assigned by ray.
        r   )r   r"   r#   r$   r<   r=   r'   r>   )r   rE   s     r   accelerator_countz$AcceleratorContext.accelerator_count   so    
 "f,,s(()))!577KKMMO**4+B+H+H+J+JBOOPPPr   )r   r   )r(   r   r   N)__name__
__module____qualname____doc__r0   r   r
   r   staticmethodr'   r+   r   rJ   r   rM   rO   rS   rU   rZ   propertyr\   r   r   intr_   r.   r   r   r   r      s        T# TlAS T T T T& $0 $0 $0 \$0L 
5 
5 
5 \
59
n)= 9
 9
 9
 9
v. .N . . . . 0 0 0' ' 'A# A A A A7l 7 7 7 7 'S ' ' ' X' &(4+="> & & & X& Q3 Q Q Q XQ Q Qr   r   r   c                 Z    t          | |          }t                               |           dS )z
    Registers the accelerator context with the specified device type and communicator.

    Args:
        torch_module_name: The name of the device module under torch.
        communicator_cls: The communicator class associated with the device.
    N)r   r+   )r   r   r(   s      r   register_accelerator_contextrh      s1     -->@PQQ./////r   c                      t           dS dS )z
    Checks whether a custom accelerator context has been registered.

    Returns:
        bool: True if a custom accelerator context is registered
              (_global_custom_context is not None), False otherwise.
    NTFr*   r.   r   r   !is_accelerator_context_registeredrj      s     )t5r   )r   	threading
contextlibr   typingr   r   r   r   r   r#   ray._private.acceleratorsr	   %ray.experimental.channel.communicatorr
   r;   Lockr!   r   __annotations__r   r   r0   rh   rj   r.   r   r   <module>rr      sU            " " " " " " F F F F F F F F F F F F F F 



 J J J J J J > > > > > > LLL +IN,, ?C h';< C C C9= !56 = = =HQ HQ HQ HQ HQ HQ HQ HQV00.2<.@0 0 0 0
 
 
 
 
r   