
    &`i              
          U d dl Z d dlZd dlmZmZmZmZ d dlZd dlm	c m
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 daded	<    e j                    Z G d
 d          Z ed          	 ddeej        j                 dee         dee         fd            Z ed          	 ddeej        j                 dedee         defd            Z ed          deeef         fd            Z ed          d             Z dS )    N)DictListOptionalUnion)CommunicatorHandle)	PublicAPIget_address_and_port)get_master_address_metadata_key)Backendz#Optional[RemoteCommunicatorManager]_remote_communicator_managerc                       e Zd ZdZd Zedd            ZdefdZde	fdZ
	 	 dd
eeej        j                          dee         fdZd	S )RemoteCommunicatorManagerzmSingleton class to store the mapping between actors and communicators
    that the actors are a part of.
    c                     i | _         d S N)_remote_communicatorsselfs    z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/experimental/collective/collective.py__init__z"RemoteCommunicatorManager.__init__   s     EG"""    returnc                  |    t           5  t          t                      at          cd d d            S # 1 swxY w Y   d S r   )!_remote_communicator_manager_lockr   r    r   r   getzRemoteCommunicatorManager.get   s     / 	0 	0+3/H/J/J,/	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   155comm_handlec                 $    || j         |j        <   d S r   )r   name)r   r   s     r   add_remote_communicatorz1RemoteCommunicatorManager.add_remote_communicator%   s    7B";#3444r   r   c                 8    | j                             |d           S r   )r   popr   r   s     r   remove_remote_communicatorz4RemoteCommunicatorManager.remove_remote_communicator(   s    )--dD999r   Nactorsbackendc                     |pg }t          |          }g }| j                                        D ]K}|                    t          |j                            r"||j        |k    r|                    |           L|S )zy
        Get the collective groups that the given actors are a subset of. Filter by
        backend if provided.
        )setr   valuesissubsetr%   r&   append)r   r%   r&   collectives
collectives        r   get_collective_groupsz/RemoteCommunicatorManager.get_collective_groups+   s     2V 4;;== 	3 	3Js:#45566 3?j&8G&C&C&&z222r   )r   r   )NN)__name__
__module____qualname____doc__r   staticmethodr   r   r    strr$   r   r   rayactorActorHandler   r.   r   r   r   r   r      s         G G G
 0 0 0 \0C3E C C C C:s : : : :
 9=%) ci345 '"     r   r   alpha)	stabilityr%   r&   r   c                     t                                           }|t          |          nd}|                    | |          S )a  
    Get the collective groups that the given actors are a subset of. Filter by
    backend if provided.

    Args:
        actors: List of actors. Return handles to all collective groups that
            these actors are a subset of.
        backend: An optional backend to filter by. See
            ray.util.collective.types.Backend for valid backends.

    Returns:
        A list of communicator handles that the actors are a subset of.
    N)r   r   r   r.   )r%   r&   managers      r   r.   r.   A   sB    " (++--G")"5gg4G((999r   r   c                    t                                           } t          t          j                              t                    t          |           | D ]/}|                    |g          rt          d| d d          0d | D             }t          t          |                    t          |          k    rt          d|            d}t
          j        k    r_t          j        | d         j                            d                     \  }}t                    }t!          j        || d	|            d
t$          ffd	 fdt'          |           D             }	t          j        |	           |t!          j        |           n# |t!          j        |           w w xY wt+          |           }
|                    |
           |
S )a  Create a collective group on the given list of actors. If this function
    returns successfully, then the collective group has been initialized on all
    actors, using the given order of actors as the ranks.

    Currently, an actor can only participate in one collective group per
    backend at a time. To reuse an actor, destroy its collective group and
    create a new one.

    Args:
        actors: The actors to participate in the collective group.
        backend: The backend to use. See ray.util.collective.types.Backend for
            valid backends.
        name: A name to use for the collective group. If None is provided, a
            random name will be generated.

    Returns:
        Handle to the communicator.
    NzActor z already in group for backend zI. Actors can currently only participate in at most one group per backend.c                     g | ]	}|j         
S r   )_ray_actor_id).0r6   s     r   
<listcomp>z+create_collective_group.<locals>.<listcomp>   s    999$999r   z All actors must be unique, got: r   c                     t                      S r   r	   r   s    r   <lambda>z)create_collective_group.<locals>.<lambda>   s    7K7M7M r   :rankc                 X    t           j        j                            |           d S )N)
group_name)r5   utilr-   init_collective_group)r   rD   r&   r   
world_sizes     r   _do_init_collective_groupz:create_collective_group.<locals>._do_init_collective_group   s8    11g$ 	2 	
 	
 	
 	
 	
r   c                 L    g | ] \  }}|j                             |          !S r   )__ray_call__remote)r?   rD   r6   rJ   s      r   r@   z+create_collective_group.<locals>.<listcomp>   sH     
 
 

 e	 %%) 
 
 
r   )r   r   r4   uuiduuid4r   lenr.   RuntimeErrorr(   
ValueErrorGLOOr5   rL   rM   r   internal_kv_internal_kv_putint	enumerate_internal_kv_delr   r    )r%   r&   r   r;   r6   	actor_idsmetadata_keymaster_addrmaster_port
init_taskscommrJ   rI   s    ``        @@r   create_collective_groupr_   W   sW   0 (++--G|4:<<   gGVJ  ((%':: 	 Q  Q  Qg  Q  Q  Q  	
 :9&999I
3y>>c)nn,,DFDDEEEL', $'71I"))*M*MNN$
 $
 [ 7t<<$\k3Q3QK3Q3QRRR
c 
 
 
 
 
 
 
 

7
 
 
 

  )00
 
 

 	
 #(666 #(6666 $ fdG44D##D)))Ks   /F" "F;group_or_namec                    t          | t                    r| j        n't          | t                    r| nt	          d          t
                                          }|                              }|Jfdfd|j        D             }	 t          j        |           dS # t          j
        j        $ r Y dS w xY wt	          d d          )a!  
    Destroy a collective group. If this functions returns successfully, then
    the actors that were in the collective can be reused to create a new
    collective group.

    Args:
        group_or_name: Either a communicator handle or the name of the group to
            destroy.
    z0Expected CommunicatorHandle or str (group name).Nc                 P    t           j        j                                       d S r   )r5   rG   r-   destroy_collective_groupr#   s    r   _do_destroy_collective_groupz>destroy_collective_group.<locals>._do_destroy_collective_group   s"    H88>>>>>r   c                 l    g | ]0}|j                             d                                         1S )_ray_system)concurrency_group)rL   optionsrM   )r?   r6   rd   s     r   r@   z,destroy_collective_group.<locals>.<listcomp>   sR     
 
 
  &&&GGNN, 
 
 
r   zNo group with name z found.)
isinstancer   r   r4   rR   r   r   r$   r%   r5   
exceptionsActorDiedError)r`   r;   groupdestroy_tasksrd   r   s       @@r   rc   rc      s    -!344 M!	M3	'	' MKLLL'++--G..t44E	? 	? 	? 	? 	?
 
 
 
 	
 
 
	GM"""""~, 	 	 	DD	 <t<<<===s   B& &B>=B>c                      t                                           } |                                 D ]}t          |j                   dS )a  
    Destroy all collective groups. This will destroy all collective groups that
    were previously created by this process. After this function returns, the
    actors participating in those collective groups can be reused to create a
    new collective group.
    N)r   r   r.   rc   r   )r;   r-   s     r   destroy_all_collective_groupsro      sM     (++--G3355 2 2
 11112 2r   r   )!	threadingrN   typingr   r   r   r   r5   ray.experimental.internal_kvexperimentalrT   (ray.experimental.collective.communicatorr   ray.util.annotationsr   ray.util.collective.collectiver
   @ray.util.collective.collective_group.torch_gloo_collective_groupr   ray.util.collective.typesr   r   __annotations__Lockr   r   r6   r7   r4   r.   r_   rc   ro   r   r   r   <module>r{      sb         . . . . . . . . . . . . 



 2 2 2 2 2 2 2 2 2 G G G G G G * * * * * * ? ? ? ? ? ?      . - - - - -FJ C J J J$2IN$4$4 !+ + + + + + + +\ WBF: :&':2:3-:	
: : : :* W P P&'PP 3-P 	P P P Pf W#>E2Dc2I,J #> #> #> #>L W	2 	2 	2 	2 	2r   