o
    1 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)Zn_bytes r   k/home/app/PaddleOCR-VL-test/.venv_paddleocr/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   Z	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 ({}))r5   r   r   r   r   r   r   r   r   r   joinr   )r9   summaryZcustom_summaryr   r8   r   summary_string   s0   

z_Resources.summary_stringc                 C      | j | j S N)r   r   r8   r   r   r   	cpu_total      z_Resources.cpu_totalc                 C   r>   r?   )r   r   r8   r   r   r   	gpu_total   rA   z_Resources.gpu_totalc                 C   r>   r?   )r   r   r8   r   r   r   memory_total   rA   z_Resources.memory_totalc                 C   r>   r?   )r   r   r8   r   r   r   object_store_memory_total   rA   z$_Resources.object_store_memory_totalc                 C   s   | j |d| j|d S Nr   )r   getr   r9   keyr   r   r   r6      s   z_Resources.get_res_totalc                 C   s   | j |dS rE   r   rF   rG   r   r   r   rF      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)r9   r/   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   rI   r!   koriginal	to_remover   r   r$          z'_Resources.subtract.<locals>.<dictcomp>c                    rN   rO   )r   rF   rP   rR   r   r   r$      rU   )r   r   r   r   r   r   r   r   r%   r   unionr   )r.   rS   rT   r   r   r   r   r   r   r   r   Zall_resourcesZnew_custom_resZextra_custom_resr   rR   r   subtract   s>   


z_Resources.subtract)	r   r   r   r   r   r   NNF)__name__
__module____qualname____doc__	__slots__floatr   dictboolr-   r=   r@   rB   rC   rD   r6   rF   rM   classmethodrW   __classcell__r   r   r1   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_resourcesr]   osenvironrF   rd   _refresh_period_last_resource_refreshupdate_avail_resources)r9   rc   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   )rayZis_initializedtimeri   rh   loggerdebugrangewarningsleepZcluster_resourcesr&   copypopr   intre   )
r9   rm   rn   	resourcesiZnum_cpusZnum_gpusr   r   r   r   r   r   rj      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 )Nrp   r   rq   c                    sH   i | ] }| ts"|d dksd|vr||d  j|fqS )r   r   Z_group_)r4   r   rF   re   r6   )r!   namer9   r~   r   r   r$   0  s    

z>_ResourceUpdater._get_used_avail_resources.<locals>.<dictcomp>)ry   rz   re   r   r   r   )r9   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 r?   r   )r!   usedtotalr   r   r   rK   I  s    z0_ResourceUpdater.debug_string.<locals>.<genexpr>T)rn   zLogical resource usage: /z CPUs, z GPUsr3   c                 s   s,    | ]\}\}}| d | d| V  qdS )r    Nr   )r!   r   r   r   r   r   r   rK   Z  s
    

z ()zLogical resource usage: ?)ri   r   anyr)   rj   r;   r'   )	r9   r~   r   r   r   r   r   statuscustomsr   r   r   debug_string;  sB   

z_ResourceUpdater.debug_stringc                 C      |    | jjS r?   )rj   re   r   r8   r   r   r   get_num_cpuse     z_ResourceUpdater.get_num_cpusc                 C   r   r?   )rj   re   r   r8   r   r   r   get_num_gpusi  r   z_ResourceUpdater.get_num_gpusc                 C   s   t | jffS r?   )rb   rh   r8   r   r   r   
__reduce__m  s   z_ResourceUpdater.__reduce__r?   )rl   F)rX   rY   rZ   r[   r   r]   rk   r{   r_   rj   r   strr   r   r   r   r   r   r   r   r   r   rb      s    
.*rb   )loggingrf   rs   collectionsr   numbersr   typingr   r   r   rr   Zray._common.constantsr   	getLoggerrX   rt   rd   r   r   rb   r   r   r   r   <module>   s&    

 Q