
    &`i,                        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	m
Z
 d dlZd dlmZ d dlmZ d dlmZ  e
d          Z e
d          Z e j        e          Ze G d	 d
                      Ze G d d                      Z G d d          Z ed e             d e             dd           G d d                      ZdS )    N)	dataclass)DictListOptionalTupleTypeTypeVar)get_ray_doc_version)ActorHandle)
DeprecatedTActorMetadatac                   (    e Zd ZU dZeed<   eed<   dS )ActorWrapperz+Class containing an actor and its metadata.actormetadataN)__name__
__module____qualname____doc__r   __annotations__r        h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/actor_group.pyr   r      s0         55r   r   c                   ^    e Zd ZU eed<   eed<   eeeef                  ed<   eed<   eed<   dS )ActorConfignum_cpusnum_gpus	resources	init_argsinit_kwargsN)	r   r   r   floatr   r   r   strr   r   r   r   r   r      sV         OOOOOOS%Z())))r   r   c                   *    e Zd ZdddefdZd Zd ZdS )ActorGroupMethodactor_group
ActorGroupmethod_namec                 F    t          j        |          | _        || _        d S N)weakrefrefr&   _method_name)selfr&   r(   s      r   __init__zActorGroupMethod.__init__#   s"    ";{33'r   c                 B    t          d| j         d| j         d          )NzIActorGroup methods cannot be called directly. Instead of running 'object.z()', try 'object.z.remote()'.)	TypeErrorr-   r.   argskwargss      r   __call__zActorGroupMethod.__call__'   s@    6"&"36 6 (6 6 6
 
 	
r   c                 V      fd                                  j        D             S )Nc                 Z    g | ]'} t          |j        j                  j        i (S r   )getattrr   r-   remote).0ar3   r4   r.   s     r   
<listcomp>z+ActorGroupMethod.remote.<locals>.<listcomp>0   sJ     
 
 
 7GAGT.//6GGG
 
 
r   )r&   actorsr2   s   ```r   r9   zActorGroupMethod.remote/   sI    
 
 
 
 
 
%%''.
 
 
 	
r   N)r   r   r   r#   r/   r5   r9   r   r   r   r%   r%   "   sT        (L (s ( ( ( (
 
 

 
 
 
 
r   r%   zcFor stateless/task processing, use ray.util.multiprocessing, see details in https://docs.ray.io/en/z/ray-more-libs/multiprocessing.html. For stateful/actor processing such as batch prediction, use Datasets.map_batches(compute=ActorPoolStrategy, ...), see details in https://docs.ray.io/en/z4/data/api/dataset.html#ray.data.Dataset.map_batches.T)messagewarningc                       e Zd ZdZ	 	 	 	 	 	 ddedededed	eee	ef                  d
ee
         dee         fdZd Zd Zd Zd ZddefdZdee         fdZdefdZed             ZdS )r'   a  Group of Ray Actors that can execute arbitrary functions.

    ``ActorGroup`` launches Ray actors according to the given
    specification. It can then execute arbitrary Python functions in each of
    these actors.

    If not enough resources are available to launch the actors, the Ray
    cluster will automatically scale up if autoscaling is enabled.

    Args:
        actor_cls: The class to use as the remote actors.
        num_actors: The number of the provided Ray actors to
            launch. Defaults to 1.
        num_cpus_per_actor: The number of CPUs to reserve for each
            actor. Fractional values are allowed. Defaults to 1.
        num_gpus_per_actor: The number of GPUs to reserve for each
            actor. Fractional values are allowed. Defaults to 0.
        resources_per_actor (Optional[Dict[str, float]]):
            Dictionary specifying the resources that will be
            requested for each actor in addition to ``num_cpus_per_actor``
            and ``num_gpus_per_actor``.
        init_args, init_kwargs: If ``actor_cls`` is provided,
            these args will be used for the actor initialization.

       r   N	actor_cls
num_actorsnum_cpus_per_actornum_gpus_per_actorresources_per_actorr    r!   c                    ddl m}  |d           |dk    rt          d| d          |dk     s|dk     rt          d| d| d          g | _        || _        t          ||||pd	|pi 
          | _         t          j        | j        j	        | j        j
        | j        j                  |          | _        |                                  d S )Nr   )record_library_usagezutil.ActorGroupzFThe provided `num_actors` must be greater than 0. Received num_actors=z	 instead.zXThe number of CPUs and GPUs per actor must not be negative. Received num_cpus_per_actor=z and num_gpus_per_actor=.r   )r   r   r   r    r!   )r   r   r   )ray._common.usage.usage_librH   
ValueErrorr=   rC   r   actor_configrayr9   r   r   r   _remote_clsstart)	r.   rB   rC   rD   rE   rF   r    r!   rH   s	            r   r/   zActorGroup.__init__Y   sD    	EDDDDD.///??/9    
 !!%7!%;%;<&8< < '9< < <   $''')o2#)r
 
 

3:&/&/'1
 
 
 	  	

r   c                 p    t          | j                  dk    rt          d          t          | |          S )Nr   z9This ActorGroup has been shutdown. Please start it again.)lenr=   RuntimeErrorr%   r.   items     r   __getattr__zActorGroup.__getattr__   s>    t{q  K    d+++r   c                 *    t          | j                  S r*   )rQ   r=   r.   s    r   __len__zActorGroup.__len__   s    4;r   c                     | j         |         S r*   r=   rS   s     r   __getitem__zActorGroup.__getitem__   s    {4  r   c                 :   | j         r't          | j                   dk    rt          d          t                              d| j         d           |                     | j                   t                              t          | j                    d           dS )z*Starts all the actors in this actor group.r   z_The actors have already been started. Please call `shutdown` first if you want to restart them.z	Starting  actors.z" actors have successfully started.N)r=   rQ   rR   loggerdebugrC   
add_actorsrW   s    r   rO   zActorGroup.start   s    ; 	3t{++a//    	::::;;;(((DK((LLLMMMMMr      
patience_sc                    t                               dt          | j                   d           |dk    r$| j        D ]}t	          j        |j                   nid | j        D             }t	          j        ||          \  }}|r=t                               d           | j        D ]}t	          j        |j                   t                               d           g | _        dS )	aq  Shutdown all the actors in this actor group.

        Args:
            patience_s: Attempt a graceful shutdown
                of the actors for this many seconds. Fallback to force kill
                if graceful shutdown is not complete after this time. If
                this is less than or equal to 0, immediately force kill all
                actors.
        zShutting down r]   r   c                 J    g | ] }|j         j                                        !S r   )r   __ray_terminate__r9   )r:   ws     r   r<   z'ActorGroup.shutdown.<locals>.<listcomp>   s)    QQQ299;;QQQr   )timeoutz8Graceful termination failed. Falling back to force kill.zShutdown successful.N)r^   r_   rQ   r=   rM   killr   wait)r.   rb   r   	done_refsdonenot_dones         r   shutdownzActorGroup.shutdown   s     	@c$+&6&6@@@AAA?? & &%%%%& RQT[QQQI XiDDDND( *WXXX![ * *EHU[))))+,,,r   actor_indexesc                     g }t          t          | j                            D ]&}||vr |                    | j        |                    '|| _        dS )zRemoves the actors with the specified indexes.

        Args:
            actor_indexes (List[int]): The indexes of the actors to remove.
        N)rangerQ   r=   append)r.   rn   
new_actorsis       r   remove_actorszActorGroup.remove_actors   s[     
s4;''(( 	2 	2A%%!!$+a.111 r   c                 <   g }g }t          |          D ]y} | j        j        | j        j        i | j        j        }|                    |           t          |d          r,|                    |j                                                   zt          j
        |          }t          |          dk    rdgt          |          z  }t          t          |                    D ]7}| j                            t          ||         ||                              8dS )ztAdds ``num_actors`` to this ActorGroup.

        Args:
            num_actors: The number of actors to add.
        get_actor_metadatar   N)r   r   )rp   rN   r9   rL   r    r!   rq   hasattrrv   rM   getrQ   r=   r   )r.   rC   rr   new_actor_metadata_r   r   rs   s           r   r`   zActorGroup.add_actors   s3    
z"" 	M 	MA+D$+",040A0M E e$$$u233 M"))%*B*I*I*K*KLLL 7-..x==AvJ/Hs:'' 	X 	XAK|*Q-(ST+VVVWWWW	X 	Xr   c                 $    d | j         D             S )Nc                     g | ]	}|j         
S r   )r   )r:   r;   s     r   r<   z-ActorGroup.actor_metadata.<locals>.<listcomp>   s    000q
000r   rZ   rW   s    r   actor_metadatazActorGroup.actor_metadata   s    00DK0000r   )rA   rA   r   NNN)ra   )r   r   r   r   r   intr"   r   r   r#   r   r/   rU   rX   r[   rO   rm   r   rt   r`   propertyr}   r   r   r   r'   r'   6   s_        : $%$%:>%)&*. .. . "	.
 ". &d3:&67. E?. d^. . . .`, , ,     ! ! !N N N 5    6
!49 
! 
! 
! 
!XS X X X X2 1 1 X1 1 1r   r'   )loggingr+   dataclassesr   typingr   r   r   r   r   r	   rM   ray._private.utilsr
   	ray.actorr   ray.util.annotationsr   r   r   	getLoggerr   r^   r   r   r%   r'   r   r   r   <module>r      s     ! ! ! ! ! ! = = = = = = = = = = = = = = = = 



 2 2 2 2 2 2 ! ! ! ! ! ! + + + + + +GCLL((		8	$	$                
 
 
 
 
 
 
 
( j!4!4!6!6j j 2133	j j j
   h1 h1 h1 h1 h1 h1 h1 h1 h1 h1r   