o
    bi/,                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlZd dlm  m	Z	 d dl
mZ e eZdZed ZG dd dedg d	ZdS )
    N)
namedtuple)Optional)RESOURCE_CONSTRAINT_PREFIXznode:__internal_head__c                       sT   e Zd ZdZ					d fdd	Zdd Zdd Zdd	ed
ee	 fddZ
  ZS )ResourceSpeca  Represents the resource configuration passed to a raylet.

    All fields can be None. Before starting services, resolve() should be
    called to return a ResourceSpec with unknown values filled in with
    defaults based on the local machine specifications.

    Attributes:
        num_cpus: The CPUs allocated for this raylet.
        num_gpus: The GPUs allocated for this raylet.
        memory: The memory allocated for this raylet.
        object_store_memory: The object store memory allocated for this raylet.
            Note that when calling to_resource_dict(), this will be scaled down
            by 30% to account for the global plasma LRU reserve.
        resources: The custom resources allocated for this raylet.
    Nc                    s   t t| | |||||S N)superr   __new__)clsnum_cpusnum_gpusmemoryobject_store_memory	resources	__class__ N/home/ubuntu/.local/lib/python3.10/site-packages/ray/_private/resource_spec.pyr	   /   s   
zResourceSpec.__new__c                 C   s$   |    D ]	}|du r dS qdS )z;Returns if this ResourceSpec has default values filled out.NFT)_asdictvalues)selfvr   r   r   resolved@   s
   zResourceSpec.resolvedc                 C   s   |   sJ t| j| j| jt| jt| jd}dd | D }| D ]H\}}t	|tsAt	|t
sAJ | dt| d| t	|t
rR| sRtd|||dk r^td|||tjkrmtd	tj||q%|S )
zReturns a dict suitable to pass to raylet initialization.

        This renames num_cpus / num_gpus to "CPU" / "GPU",
        translates memory from bytes into 100MB memory units, and checks types.
        )CPUGPUr   r   c                 S   s   i | ]\}}|d kr||qS )r   r   ).0resource_labelresource_quantityr   r   r   
<dictcomp>W   s
    z1ResourceSpec.to_resource_dict.<locals>.<dictcomp>z (z): zOResource quantities must all be whole numbers. Violated by resource '{}' in {}.r   zIResource quantities must be nonnegative. Violated by resource '{}' in {}.zHResource quantities must be at most {}. Violated by resource '{}' in {}.)r   dictr   r   r   intr   r   items
isinstancefloattype
is_integer
ValueErrorformatray_constantsMAX_RESOURCE_QUANTITY)r   r   r   r   r   r   r   to_resource_dictG   sN   
zResourceSpec.to_resource_dictis_headnode_ip_addressc                 C   s  | j pi  }d|vsJ |d|vsJ |d|vsJ |d|vs'J ||du r0tj }d|t| < t|v rAtt d|rGd|t< | j}|du rTtj	j
 }d}tj	j D ]}tj	j|}d}|dkro| j}n||d}| }	|dur|	dur|t|	krtd	| d
| d|  d|	 d	|du r| }|	durt|t|	}|r|dkr|}n|||< | }
|
rd|t |
 < ddlm} ||
 | }|r|| q\tjj
 }tj	j
 }| j }|du r>t!|t"j# }t$j%dkrt|t"j&}t"j'}t$j%dkst$j%dkr(tj	j
( d }t)t"j*|}t||}|r>||kr>t+,d-|d d  |}| j.}|du ri|| }|dk ri|d| k ritd-t/|d dt!d||  t0|||||}|1 sxJ |S )a  Returns a copy with values filled out with system defaults.

        Args:
            is_head: Whether this is the head node.
            node_ip_address: The IP address of the node that we are on.
                This is used to automatically create a node id resource.
        r   r   r   r   Ng      ?z7 is a reserved resource name, use another name instead.r   z Attempting to start raylet with  z, but z
 contains .   )	usage_libdarwinlinuxlinux2gffffff?z.Warning: Capping object memory store to {}GB. g    eAz]To increase this further, specify `object_store_memory` when calling ray.init() or ray start.g    חAg?zAfter taking into account object store and redis memory usage, the amount of memory on this node available for tasks and actors ({} GB) is less than {}% of total. You can adjust these settings with ray.init(memory=<bytes>, object_store_memory=<bytes>).   d   )2r   copyrayutilget_node_ip_addressNODE_ID_PREFIXHEAD_NODE_RESOURCE_NAMEr&   r   _privateutilsget_num_cpusaccelerators"get_all_accelerator_resource_names$get_accelerator_manager_for_resourcer   get+get_current_process_visible_accelerator_idslen#get_visible_accelerator_ids_env_var!get_current_node_num_acceleratorsmin!get_current_node_accelerator_typer   ray._private.usager0   record_hardware_usage%get_current_node_additional_resourcesupdate_commonget_system_memoryestimate_available_memoryr   r    r(   &DEFAULT_OBJECT_STORE_MEMORY_PROPORTIONsysplatform!MAC_DEGRADED_PERF_MMAP_SIZE_LIMIT%DEFAULT_OBJECT_STORE_MAX_MEMORY_BYTESget_shared_memory_bytesmaxREQUIRE_SHM_SIZE_THRESHOLDloggerdebugr'   r   roundr   r   )r   r+   r,   r   r   r   accelerator_resource_nameaccelerator_managernum_acceleratorsvisible_accelerator_idsaccelerator_typer0   additional_resourcessystem_memoryavail_memoryr   object_store_memory_cap	shm_availshm_capr   specr   r   r   resolve{   s   	







zResourceSpec.resolve)NNNNNr   )__name__
__module____qualname____doc__r	   r   r*   boolr   strrg   __classcell__r   r   r   r   r      s     4r   )r   r   r   r   r   )loggingrQ   collectionsr   typingr   r7   ray._private.ray_constantsr<   r(   ray._common.utilsr   	getLoggerrh   rX   r:   r;   r   r   r   r   r   <module>   s     

