o
    bi]!                     @   s   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
dZeG dd dZeeG dd de jZdS )    N)deepcopy)	dataclass)	signature)DictListUnion)placement_group)DeveloperAPIbundlesreturnc                 C   s8   i }| D ]}|  D ]\}}||d| ||< q
q|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v r   W/home/ubuntu/.local/lib/python3.10/site-packages/ray/air/execution/resources/request.py_sum_bundles   s   r   c                   @   s   e Zd ZdZ	d"deeeeee	f f  defddZ
edd Zeed	e	fd
dZeed	eeee	f  fddZed	eee	f fddZeed	efddZdd Zdd Zd#ddZdd Zdd Zdd Zd	efd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                 O   sx   |st ddd |D | _| jd s$d| _| jd | js#t dnd| _|| _|| _|| _d | _d | _| 	  d S )Nz6Cannot initialize a ResourceRequest with zero bundles.c                 S   s   g | ]}d d |  D qS )c                 S   s"   i | ]\}}|d kr|t |qS )r   )float).0r   r   r   r   r   
<dictcomp>G   s   " z7ResourceRequest.__init__.<locals>.<listcomp>.<dictcomp>)r   )r   r   r   r   r   
<listcomp>F   s    z,ResourceRequest.__init__.<locals>.<listcomp>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kwargsr   r   r   __init__;   s(   
zResourceRequest.__init__c                 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&   r   r   r   head_bundle_is_emptyb   s   z$ResourceRequest.head_bundle_is_emptyr   c                 C   s   | j rdS | jd ddS )z.Returns the number of cpus in the head bundle.g        r   CPU)r   r   r   r+   r   r   r   	head_cpusk   s   zResourceRequest.head_cpusc                 C   
   t | jS )z'Returns a deep copy of resource bundles)r   r   r+   r   r   r   r
   q   s   
zResourceRequest.bundlesc                 C   r/   )z3Returns a dict containing the sums of all resources)r   r   r+   r   r   r   required_resourcesw   s   
z"ResourceRequest.required_resourcesc                 C   r*   )zReturns the placement strategy)r    r+   r   r   r   r   |   s   zResourceRequest.strategyc              
   C   sV   t t}z|j| j| jg| jR i | j| _W dS  ty* } zt	d|d}~w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excr   r   r   r%      s"   	zResourceRequest._bindc                 C   s   t | jji | jjS N)r   r$   r'   r(   r+   r   r   r   to_placement_group   s   z"ResourceRequest.to_placement_groupotherc                 C   s"   t |to| j|jko| j|jkS r6   )
isinstancer   r$   r,   )r&   r8   r   r   r   __eq__   s
   


zResourceRequest.__eq__c                 C   s2   | j sttj| jj| jjddddd| _ | j S )N)r'   r(   Tr   )	sort_keysindentensure_ascii)r#   hashjsondumpsr$   r'   r(   r+   r   r   r   __hash__   s   zResourceRequest.__hash__c                 C   s&   | j  }|dd  |dd  |S )Nr#   r$   )__dict__copyr   r&   stater   r   r   __getstate__   s   
zResourceRequest.__getstate__c                 C   s$   | j | d | _d | _|   d S r6   )rB   updater#   r$   r%   rD   r   r   r   __setstate__   s   zResourceRequest.__setstate__c                 C   s   d| j  d| j dS )Nz<ResourceRequest (_bound=z, head_bundle_is_empty=z)>)r$   r,   r+   r   r   r   __repr__   s   
zResourceRequest.__repr__N)r   )r8   r   )__name__
__module____qualname____doc__r   r   strr   intr   r)   propertyr,   r	   r.   r
   r0   r   r%   r7   r:   rA   rF   rH   rI   r   r   r   r   r      s8    
'

r   c                   @   sZ   e Zd ZU dZeed< dee deee  fddZ	dede
eef d	edefd
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                 C   s   | j j}t|t| j j }t||kr!tdt| d| dg }| j jr:|| j|d i dd |dd }tt	||D ]\}\}}|| j|||d qA|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)
rR   r
   lenrO   r,   r3   append_annotate_remote_entity	enumeratezip)r&   rS   r
   num_bundles	annotatedientityr   r   r   r   annotate_remote_entities   s&   z*AcquiredResources.annotate_remote_entitiesr^   r   rT   c                 C   s   t r6   )NotImplementedError)r&   r^   r   rT   r   r   r   rX      s   z)AcquiredResources._annotate_remote_entityN)rJ   rK   rL   rM   r   __annotations__r   RemoteRayEntityr   r_   r   rN   r   rO   rX   r   r   r   r   rQ      s"   
 

)
rQ   )abcr?   rC   r   dataclassesr   inspectr   typingr   r   r   rayray.utilr   ray.util.annotationsr	   remote_functionRemoteFunctionactor
ActorClassrb   rN   r   r   r   ABCrQ   r   r   r   r   <module>   s"    & #