o
    $iZ2                     @   s   d dl Z d dlZd dl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 e eZdZdd ZG d	d
 d
ed
g dZG dd dZdS )    N)
namedtuple)Number)AnyDictOptional)NODE_ID_PREFIX
   c                 C   s   t | d dS )N   @   round)n_bytes r   \/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/utils/resource_updater.py_to_gb      r   c                       s   e Zd ZdZdZ									d&deded	ed
edededededee dee def fddZ	dd Z
dd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zed$d% Z  ZS )'
_Resourcesa  Ray resources required to schedule a trial.

    Parameters:
        cpu: Number of CPUs to allocate to the trial.
        gpu: Number of GPUs to allocate to the trial.
        memory: Memory to reserve for the trial.
        object_store_memory: Object store memory to reserve.
        extra_cpu: Extra CPUs to reserve in case the trial needs to
            launch additional Ray actors that use CPUs.
        extra_gpu: Extra GPUs to reserve in case the trial needs to
            launch additional Ray actors that use GPUs.
        extra_memory: Memory to reserve for the trial launching
            additional Ray actors that use memory.
        extra_object_store_memory: Object store memory to reserve for
            the trial launching additional Ray actors that use object store
            memory.
        custom_resources: Mapping of resource to quantity to allocate
            to the trial.
        extra_custom_resources: Extra custom resources to reserve in
            case the trial needs to launch additional Ray actors that use
            any of these custom resources.
        has_placement_group: Bool indicating if the trial also
            has an associated placement group.

    r   r           NFcpugpumemoryobject_store_memory	extra_cpu	extra_gpuextra_memoryextra_object_store_memorycustom_resourcesextra_custom_resourceshas_placement_groupc                    sD  |	pi }	|
pi }
t |	t |
A }|D ]}|	|d |
|d qt|d}t|d}t|d}t|d}t|d}t|d}t|d}t|d}dd |	 D }	dd |
 D }
||||||||g}|t|	 7 }|t|
 7 }t|	t|
ksJ |D ]}t|tsJ d|fqt	t
| | |||||||||	|
|S )Nr   r
   c                 S      i | ]
\}}|t |d qS r
   r   .0resourcevaluer   r   r   
<dictcomp>`   s    z&_Resources.__new__.<locals>.<dictcomp>c                 S   r   r    r   r!   r   r   r   r%   c   s    
zImproper resource value.)set
setdefaultr   itemslistvalueslen
isinstancer   superr   __new__)clsr   r   r   r   r   r   r   r   r   r   r   	leftoversr$   
all_valuesentry	__class__r   r   r.   B   s`   









z_Resources.__new__c                    s   d  j j  j j } js jr$|d t j j d d7 } js* j	r:|d t j j	 d d7 }d
 fdd jD }|rP|d	 |7 }|S )
Nz{} CPUs, {} GPUsz, {} GiB heapr	   r
   z, {} GiB objects, c                    s(   g | ]}| tsd  ||qS )z{} {})
startswithr   formatget_res_total)r"   resselfr   r   
<listcomp>   s    z-_Resources.summary_string.<locals>.<listcomp>z ({}))r7   r   r   r   r   r   r   r   r   r   joinr   )r;   summarycustom_summaryr   r:   r   summary_string   s0   

z_Resources.summary_stringc                 C      | j | j S N)r   r   r:   r   r   r   	cpu_total      z_Resources.cpu_totalc                 C   rA   rB   )r   r   r:   r   r   r   	gpu_total   rD   z_Resources.gpu_totalc                 C   rA   rB   )r   r   r:   r   r   r   memory_total   rD   z_Resources.memory_totalc                 C   rA   rB   )r   r   r:   r   r   r   object_store_memory_total   rD   z$_Resources.object_store_memory_totalc                 C   s   | j |d| j|d S Nr   )r   getr   r;   keyr   r   r   r8      s   z_Resources.get_res_totalc                 C   s   | j |dS rH   r   rI   rJ   r   r   r   rI      r   z_Resources.getc                 C   sJ   | j | j| j| jg}|t| j 7 }|t| j 7 }tdd |D S )Nc                 s   s    | ]}|d kV  qdS )r   Nr   )r"   vr   r   r   	<genexpr>   s    z,_Resources.is_nonnegative.<locals>.<genexpr>)	r   r   r   r   r)   r   r*   r   all)r;   r1   r   r   r   is_nonnegative   s   z_Resources.is_nonnegativec                    s    j j  } jj } jj } jj } jj } jj } jj }	 jj }
t j	
tj	} fdd|D } fdd|D }t|||||||	|
||
S )Nc                    *   i | ]}| j |d j |d  qS r   rL   r"   koriginal	to_remover   r   r%          z'_Resources.subtract.<locals>.<dictcomp>c                    rQ   rR   )r   rI   rS   rU   r   r   r%      rX   )r   r   r   r   r   r   r   r   r&   r   unionr   )r/   rV   rW   r   r   r   r   r   r   r   r   all_resourcesnew_custom_resextra_custom_resr   rU   r   subtract   s>   


z_Resources.subtract)	r   r   r   r   r   r   NNF)__name__
__module____qualname____doc__	__slots__floatr   dictboolr.   r@   rC   rE   rF   rG   r8   rI   rP   classmethodr]   __classcell__r   r   r3   r   r      sZ    	
Dr   )r   r   r   r   r   r   r   r   r   r   r   c                   @   s   e Zd ZdZddee fddZdded	efd
dZ	de
eef fddZde
eef defddZdefddZdefddZdd ZdS )_ResourceUpdaterak  Periodic Resource updater for Tune.

    Initially, all resources are set to 0. The updater will try to update resources
    when (1) init ResourceUpdater (2) call "update_avail_resources", "num_cpus"
    or "num_gpus".

    The update takes effect when (1) Ray is initialized (2) the interval between
    this and last update is larger than "refresh_period"
    Nrefresh_periodc                 C   sD   t ddd| _|d u rttjdt}|| _td| _| 	  d S )Nr   )r   r   TUNE_STATE_REFRESH_PERIODz-inf)
r   _avail_resourcesrc   osenvironrI   rj   _refresh_period_last_resource_refreshupdate_avail_resources)r;   ri   r   r   r   __init__   s   
z_ResourceUpdater.__init__   Fnum_retriesforcec           
      C   s  t  sd S t | j | jk r|sd S td d }t|D ]}|dkr5td|d  d t	d t 
 }|r= nq|sQ|dd |dd td	 | }|dd}|dd}|d
d}|dd}|}	tt|t|t|t||	d| _t | _d S )NzChecking Ray cluster resources.r   z2Cluster resources not detected or are 0. Attempt #   z...g      ?CPUGPUzsCluster resources cannot be detected or are 0. You can resume this experiment by passing in `resume=True` to `run`.r   r   )r   r   r   )rayis_initializedtimero   rn   loggerdebugrangewarningsleepcluster_resourcesr'   copypopr   intrk   )
r;   rs   rt   	resourcesinum_cpusnum_gpusr   r   r   r   r   r   rp      sL   

z'_ResourceUpdater.update_avail_resourcestotal_allocated_resourcesc                    sV     dd} jj}dd} jj} fdd jjD }|||||fS )Nrv   r   rw   c                    sH   i | ] }| ts"|d dksd|vr||d  j|fqS )r   r   _group_)r6   r   rI   rk   r8   )r"   namer;   r   r   r   r%   0  s    

z>_ResourceUpdater._get_used_avail_resources.<locals>.<dictcomp>)r   r   rk   r   r   r   )r;   r   used_cpu	total_cpuused_gpu	total_gpucustom_used_totalr   r   r   _get_used_avail_resources(  s   	z*_ResourceUpdater._get_used_avail_resourcesreturnc           	   	   C   s   | j dkrY| |\}}}}}||ks"||ks"tdd | D r2| jdd | |\}}}}}d| d| d| d| d		}d
dd | D }|rW|d| d7 }|S dS )z=Returns a human readable message for printing to the console.r   c                 s   s    | ]	\}}||kV  qd S rB   r   )r"   usedtotalr   r   r   rN   I  s    z0_ResourceUpdater.debug_string.<locals>.<genexpr>T)rt   zLogical resource usage: /z CPUs, z GPUsr5   c                 s   s,    | ]\}\}}| d | d| V  qdS )r    Nr   )r"   r   r   r   r   r   r   rN   Z  s
    

z ()zLogical resource usage: ?)ro   r   anyr*   rp   r=   r(   )	r;   r   r   r   r   r   r   statuscustomsr   r   r   debug_string;  sB   

z_ResourceUpdater.debug_stringc                 C      |    | jjS rB   )rp   rk   r   r:   r   r   r   get_num_cpuse     z_ResourceUpdater.get_num_cpusc                 C   r   rB   )rp   rk   r   r:   r   r   r   get_num_gpusi  r   z_ResourceUpdater.get_num_gpusc                 C   s   t | jffS rB   )rh   rn   r:   r   r   r   
__reduce__m  s   z_ResourceUpdater.__reduce__rB   )rr   F)r^   r_   r`   ra   r   rc   rq   r   re   rp   r   strr   r   r   r   r   r   r   r   r   r   rh      s    
.*rh   )loggingrl   rz   collectionsr   numbersr   typingr   r   r   rx   ray._common.constantsr   	getLoggerr^   r{   rj   r   r   rh   r   r   r   r   <module>   s&    

 Q