
    &`i]!                     V   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlZd dlmZ d dlmZ eej        j        ej        j        f         Zde
e	eef                  d	e	eef         fd
Ze G d d                      Zee G d de j                                          ZdS )    N)deepcopy)	dataclass)	signature)DictListUnion)placement_group)DeveloperAPIbundlesreturnc                     i }| D ]8}|                                 D ]!\  }}|                    |d          |z   ||<   "9|S )zSum all resources in a list of resource bundles.

    Args:
        bundles: List of resource bundles.

    Returns: Dict containing all resources summed up.
    r   )itemsget)r   	resourcesbundlekvs        w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/air/execution/resources/request.py_sum_bundlesr      s\     I 3 3LLNN 	3 	3DAq$==A..2IaLL	3    c                   f   e Zd ZdZ	 ddeeeeee	f         f                  defdZ
ed             Zeede	fd                        Zeedeeee	f                  fd	                        Zedeee	f         fd
            Zeedefd                        Zd Zd ZddZd Zd Zd ZdefdZdS )ResourceRequesta  Request for resources.

    This class is used to define a resource request. A resource request comprises one
    or more bundles of resources and instructions on the scheduling behavior.

    The resource request can be submitted to a resource manager, which will
    schedule the resources. Depending on the resource backend, this may instruct
    Ray to scale up (autoscaling).

    Resource requests are compatible with the most fine-grained low-level resource
    backend, which are Ray placement groups.

    Args:
        bundles: A list of bundles which represent the resources requirements.
            E.g. ``[{"CPU": 1, "GPU": 1}]``.
        strategy: The scheduling strategy to acquire the bundles.

         - "PACK": Packs Bundles into as few nodes as possible.
         - "SPREAD": Places Bundles across distinct nodes as even as possible.
         - "STRICT_PACK": Packs Bundles into one node. The group is
           not allowed to span multiple nodes.
         - "STRICT_SPREAD": Packs Bundles across distinct nodes.
        *args: Passed to the call of ``placement_group()``, if applicable.
        **kwargs: Passed to the call of ``placement_group()``, if applicable.

    PACKr   strategyc                 P   |st          d          d |D             | _        | j        d         s8d| _        | j                            d           | j        st          d          nd| _        || _        || _        || _        d | _        d | _        | 	                                 d S )Nz6Cannot initialize a ResourceRequest with zero bundles.c                 J    g | ] }d  |                                 D             !S )c                 @    i | ]\  }}|d k    |t          |          S )r   )float).0r   r   s      r   
<dictcomp>z7ResourceRequest.__init__.<locals>.<listcomp>.<dictcomp>G   s)    >>>TQqAvvQavvvr   )r   )r   r   s     r   
<listcomp>z,ResourceRequest.__init__.<locals>.<listcomp>F   s<     
 
 
CI>>V\\^^>>>
 
 
r   r   TzOCannot initialize a ResourceRequest with an empty head and zero worker bundles.F)

ValueError_bundles_head_bundle_is_emptypop	_strategy_args_kwargs_hash_bound_bind)selfr   r   argskwargss        r   __init__zResourceRequest.__init__;   s      	WUVVV
 
MT
 
 
 }Q 	/)-D&Ma   =  /   */D&!



r   c                     | j         S )zReturns True if head bundle is empty while child bundles
        need resources.

        This is considered an internal API within Tune.
        )r$   r,   s    r   head_bundle_is_emptyz$ResourceRequest.head_bundle_is_emptyb   s     ))r   r   c                 V    | j         rdn | j        d                             dd          S )z.Returns the number of cpus in the head bundle.g        r   CPU)r$   r#   r   r1   s    r   	head_cpuszResourceRequest.head_cpusk   s.     0VssdmA6F6J6J5RU6V6VVr   c                 *    t          | j                  S )z'Returns a deep copy of resource bundles)r   r#   r1   s    r   r   zResourceRequest.bundlesq   s     &&&r   c                 *    t          | j                  S )z3Returns a dict containing the sums of all resources)r   r#   r1   s    r   required_resourcesz"ResourceRequest.required_resourcesw   s     DM***r   c                     | j         S )zReturns the placement strategy)r&   r1   s    r   r   zResourceRequest.strategy|   s     ~r   c                     t          t                    }	  |j        | j        | j        g| j        R i | j        | _        dS # t          $ r}t          d          |d}~ww xY w)a  Bind the args and kwargs to the `placement_group()` signature.

        We bind the args and kwargs, so we can compare equality of two resource
        requests. The main reason for this is that the `placement_group()` API
        can evolve independently from the ResourceRequest API (e.g. adding new
        arguments). Then, `ResourceRequest(bundles, strategy, arg=arg)` should
        be the same as `ResourceRequest(bundles, strategy, arg)`.
        ztInvalid definition for resource request. Please check that you passed valid arguments to the ResourceRequest object.N)
r   r	   bindr#   r&   r'   r(   r*   	ExceptionRuntimeError)r,   sigexcs      r   r+   zResourceRequest._bind   s     ((		"#(t~04
  >Bl DKKK  	 	 	  		s   +A 
A"AA"c                 B    t          | j        j        i | j        j        S N)r	   r*   r-   r.   r1   s    r   to_placement_groupz"ResourceRequest.to_placement_group   s     0GDK4FGGGr   otherc                 l    t          |t                    o| j        |j        k    o| j        |j        k    S rA   )
isinstancer   r*   r2   )r,   rC   s     r   __eq__zResourceRequest.__eq__   s:    uo.. Hu|+H)U-GG	
r   c                     | j         sAt          t          j        | j        j        | j        j        dddd                    | _         | j         S )N)r-   r.   Tr   )	sort_keysindentensure_ascii)r)   hashjsondumpsr*   r-   r.   r1   s    r   __hash__zResourceRequest.__hash__   sY    z 		
![-9KLL"!%	   DJ zr   c                     | j                                         }|                    dd            |                    dd            |S )Nr)   r*   )__dict__copyr%   r,   states     r   __getstate__zResourceRequest.__getstate__   sB    ""$$		'4   		(D!!!r   c                 ~    | j                             |           d | _        d | _        |                                  d S rA   )rP   updater)   r*   r+   rR   s     r   __setstate__zResourceRequest.__setstate__   s7    U###


r   c                 (    d| j          d| j         dS )Nz<ResourceRequest (_bound=z, head_bundle_is_empty=z)>)r*   r2   r1   s    r   __repr__zResourceRequest.__repr__   s5    B B B$($=B B B	
r   N)r   )rC   r   )__name__
__module____qualname____doc__r   r   strr   intr   r/   propertyr2   r
   r5   r   r8   r   r+   rB   rF   rN   rT   rW   rY    r   r   r   r      s        < % %d3c5j 1123% % % % %N * * X* W5 W W W \ XW 'd3:./ ' ' ' \ X' +De$4 + + + X+ #    \ X  *H H H
 
 
 
      
# 
 
 
 
 
 
r   r   c                   z    e Zd ZU dZeed<   dee         deee                  fdZ	dede
eef         dedefd	Zd
S )AcquiredResourcesa  Base class for resources that have been acquired.

    Acquired resources can be associated to Ray objects, which can then be
    scheduled using these resources.

    Internally this can point e.g. to a placement group, a placement
    group bundle index, or just raw resources.

    The main API is the `annotate_remote_entities` method. This will associate
    remote Ray objects (tasks and actors) with the acquired resources by setting
    the Ray remote options to use the acquired resources.
    resource_requestentitiesr   c                    | j         j        }t          |          t          | j         j                  z   }t          |          |k    r#t          dt          |           d| d          g }| j         j        r;|                    |                     |d         i d                     |dd         }t          t          ||                    D ]3\  }\  }}|                    |                     |||                     4|S )aA  Return remote ray entities (tasks/actors) to use the acquired resources.

        The first entity will be associated with the first bundle, the second
        entity will be associated with the second bundle, etc.

        Args:
            entities: Remote Ray entities to annotate with the acquired resources.
        z%The number of callables to annotate (z1) cannot exceed the number of available bundles (z).r   )bundle_index   N)
rd   r   lenr_   r2   r=   append_annotate_remote_entity	enumeratezip)r,   re   r   num_bundles	annotatedientityr   s           r   annotate_remote_entitiesz*AcquiredResources.annotate_remote_entities   sC    '/ 'llS)>)S%T%TTx==;&&KH K K;FK K K  
 	 5 	$ ,,Xa["1,MM  
  |H#,S7-C-C#D#D 	 	A,,VV!,LL    r   rq   r   rg   c                     t           rA   )NotImplementedError)r,   rq   r   rg   s       r   rk   z)AcquiredResources._annotate_remote_entity   s
     "!r   N)rZ   r[   r\   r]   r   __annotations__r   RemoteRayEntityr   rr   r   r^   r   r_   rk   ra   r   r   rc   rc      s           &%%%'_-'	eO$	%' ' ' 'R"%"/3CJ/?"OR"	" " " " " "r   rc   )abcrL   rQ   r   dataclassesr   inspectr   typingr   r   r   rayray.utilr	   ray.util.annotationsr
   remote_functionRemoteFunctionactor
ActorClassrv   r^   r   r   r   ABCrc   ra   r   r   <module>r      st   



        ! ! ! ! ! !       $ $ $ $ $ $ $ $ $ $ 



 $ $ $ $ $ $ - - - - - -+:CI<PPQ$tCJ/0 T#u*5E     _
 _
 _
 _
 _
 _
 _
 _
D 
<" <" <" <" <" <" <"  <" <" <"r   