
    &`iZ2              	           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 j        e          ZdZd Z G d d	 ed	g d
                    Z G d d          ZdS )    N)
namedtuple)Number)AnyDictOptional)NODE_ID_PREFIX
   c                 (    t          | dz  d          S )N   @   round)n_bytess    s/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/utils/resource_updater.py_to_gbr      s    G$a(((    c                        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Z	d Z
d Zd Zd Zd Zd Zd Zd Zed             Z x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           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                    |	pi }	|
pi }
t          |	          t          |
          z  }|D ].}|	                    |d           |
                    |d           /t          |d          }t          |d          }t          |d          }t          |d          }t          |d          }t          |d          }t          |d          }t          |d          }d |	                                D             }	d |
                                D             }
||||||||g}|t	          |	                                          z  }|t	          |
                                          z  }t          |	          t          |
          k    sJ |D ]#}t          |t                    sJ d|f            $t          t          |                               | |||||||||	|
|          S )Nr   r   c                 6    i | ]\  }}|t          |d           S r   r   .0resourcevalues      r   
<dictcomp>z&_Resources.__new__.<locals>.<dictcomp>`   s3     
 
 
*9(EHeE1oo
 
 
r   c                 6    i | ]\  }}|t          |d           S r$   r   r%   s      r   r)   z&_Resources.__new__.<locals>.<dictcomp>c   s6     "
 "
 "
% eE1oo"
 "
 "
r   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__s                   r   r3   z_Resources.__new__B   sE    ,1r!7!=2())C0F,G,GG	 	8 	8E''q111"--eQ7777CmmCmmvq!!#$7;;)Q''	)Q''	\1--$)*CQ$G$G!
 
=M=S=S=U=U
 
 
"
 "
#9#?#?#A#A"
 "
 "
 %	

 	d+2244555
d188::;;;
#$$,B(C(CCCCC 	R 	REeV,,QQ/I5.QQQ,QZ%%--%"
 
 	
r   c                     d                      j         j        z    j         j        z             } j        s j        r6|d                     t           j         j        z   dz  d                    z  } j        s j	        r6|d                     t           j         j	        z   dz  d                    z  }d
                     fd j        D                       }|r|d                     |          z  }|S )	Nz{} CPUs, {} GPUsz, {} GiB heapr   r   z, {} GiB objects, c                     g | ]E}|                     t                    d                                         |          |          FS )z{} {})
startswithr   formatget_res_total)r&   resselfs     r   
<listcomp>z-_Resources.summary_string.<locals>.<listcomp>   sW       ~~n55t11#66<<  r   z ({}))r=   r   r   r   r   r   r   r   r   r   joinr   )r@   summarycustom_summarys   `  r   summary_stringz_Resources.summary_string   s:   $++Ht~%tx$.'@
 
 ; 	$+ 	--t{T%667CQGG  G # 	t'E 	)00-0NN    G    0  
 
  	6w~~n555Gr   c                      | j         | j        z   S N)r   r   r@   s    r   	cpu_totalz_Resources.cpu_total       x$.((r   c                      | j         | j        z   S rG   )r   r   rH   s    r   	gpu_totalz_Resources.gpu_total   rJ   r   c                      | j         | j        z   S rG   )r   r   rH   s    r   memory_totalz_Resources.memory_total   s    {T...r   c                      | j         | j        z   S rG   )r   r   rH   s    r   object_store_memory_totalz$_Resources.object_store_memory_total   s    '$*HHHr   c                 p    | j                             |d          | j                            |d          z   S Nr   )r   getr    r@   keys     r   r>   z_Resources.get_res_total   s<    $((a0043N3R3R4
 4
 
 	
r   c                 8    | j                             |d          S rR   r   rS   rT   s     r   rS   z_Resources.get   s    $((a000r   c                    | j         | j        | j        | j        g}|t	          | j                                                  z  }|t	          | j                                                  z  }t          d |D                       S )Nc              3   "   K   | ]
}|d k    V  dS )r   Nr   )r&   vs     r   	<genexpr>z,_Resources.is_nonnegative.<locals>.<genexpr>   s&      ..a16......r   )	r   r   r   r   r.   r   r/   r    all)r@   r6   s     r   is_nonnegativez_Resources.is_nonnegative   sw    h$.$.I
d407799:::
d46==??@@@
..:......r   c                    j         j         z
  }j        j        z
  }j        j        z
  }j        j        z
  }j        j        z
  }j        j        z
  }j        j        z
  }	j        j        z
  }
t          j	                  
                    t          j	                            }fd|D             }fd|D             }t          |||||||	|
||
  
        S )Nc                     i | ]:}|j                             |d           j                             |d           z
  ;S r   rW   r&   koriginal	to_removes     r   r)   z'_Resources.subtract.<locals>.<dictcomp>   s[     
 
 
  x(,,Q22(,,Q223
 
 
r   c                     i | ]:}|j                             |d           j                             |d           z
  ;S r`   )r    rS   ra   s     r   r)   z'_Resources.subtract.<locals>.<dictcomp>   s[     
 
 
  x.221a88.221a889
 
 
r   )r   r   r   r   r   r   r   r   r+   r   unionr   )r4   rc   rd   r   r   r   r   r   r   r   r   all_resourcesnew_custom_resextra_custom_ress    ``           r   subtractz_Resources.subtract   sH   lY]*lY]*9#33(9+HH 	 &)<<	&)<<	,y/EE.1TT 	" H566<<	*++
 

 
 
 
 
 #
 
 


 
 
 
 
 #
 
 

 %
 
 	
r   )	r   r   r   r   r   r   NNF)__name__
__module____qualname____doc__	__slots__floatr   dictboolr3   rE   rI   rL   rN   rP   r>   rS   r]   classmethodrj   __classcell__)r8   s   @r   r   r      s       $ 4 I %(!+.+/15$)B
 B
B
 B
 	B

 #B
 B
 B
 B
 $)B
 #4.B
 !)B
 "B
 B
 B
 B
 B
 B
H  6) ) )) ) )/ / /I I I
 
 

1 1 1/ / / %
 %
 [%
 %
 %
 %
 %
r   r   )r   r   r   r   r   r   r   r   r   r    r!   c                       e Zd ZdZddee         fdZddedefd	Z	d
e
eef         fdZd
e
eef         defdZdefdZdefdZ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                     t          dd          | _        |2t          t          j                            dt                              }|| _        t          d          | _        | 	                                 d S )Nr   )r   r   TUNE_STATE_REFRESH_PERIODz-inf)
r   _avail_resourcesrp   osenvironrS   ry   _refresh_period_last_resource_refreshupdate_avail_resources)r@   rw   s     r   __init__z_ResourceUpdater.__init__   sq     *qa 8 8 8!"
:<UVV N  .&+Fmm###%%%%%r      Fnum_retriesforcec                    t          j                    sd S t          j                    | j        z
  | j        k     r|sd S t
                              d           d }t          |          D ]T}|dk    r5t
                              d|dz    d           t          j	        d           t          j
                    }|r nU|sF|                    dd           |                    dd           t
                              d	           |                                }|                    dd          }|                    dd          }|                    d
d          }|                    dd          }|}	t          t          |          t          |          t          |          t          |          |	          | _        t          j                    | _        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timer~   r}   loggerdebugrangewarningsleepcluster_resourcesr,   copypopr   intrz   )
r@   r   r   	resourcesinum_cpusnum_gpusr   r   r   s
             r   r   z'_ResourceUpdater.update_avail_resources   s   !## 	FIKK$558LLL M F6777	{## 	 	A1uuSQSSS   
3-//I   	   ***  ***NNW  
 NN$$	==**==**x++'mm,A1EE$ *MMMMv;; #$7 8 8-!
 !
 !
 '+ikk###r   total_allocated_resourcesc                                                                           dd          } j        j        }                    dd          } j        j        } fd j        j        D             }|||||fS )Nr   r   r   c                     i | ]k}|                     t                    sO                    |d           dk    sd|v:|                    |d           j                            |          flS )r   r   _group_)r<   r   rS   rz   r>   )r&   namer@   r   s     r   r)   z>_ResourceUpdater._get_used_avail_resources.<locals>.<dictcomp>0  s     
 
 

 ??>22
 +..tS99A==RVAVAV )--dC88%33D99 BWAVAVr   )r   r   rz   r   r   r   )r@   r   used_cpu	total_cpuused_gpu	total_gpucustom_used_totals   ``     r   _get_used_avail_resourcesz*_ResourceUpdater._get_used_avail_resources(  s    $=$B$B$D$D!,00::)-	,00::)-	
 
 
 
 

 ->
 
 
 Hi9JJJr   returnc           	         | j         dk    r|                     |          \  }}}}}||k    s1||k    s+t          d |                                D                       r1|                     d           |                     |          \  }}}}}d| d| d| d| d	}d	                    d
 |                                D                       }|r	|d| dz  }|S dS )z=Returns a human readable message for printing to the console.r   c              3   (   K   | ]\  }}||k    V  d S rG   r   )r&   usedtotals      r   r[   z0_ResourceUpdater.debug_string.<locals>.<genexpr>I  s*      TTute|TTTTTTr   T)r   zLogical resource usage: /z CPUs, z GPUsr:   c              3   6   K   | ]\  }\  }}| d | d| V  dS )r    Nr   )r&   r   r   r   s       r   r[   z0_ResourceUpdater.debug_string.<locals>.<genexpr>Z  sT          'D-4 ((%(($((           r   z ()zLogical resource usage: ?)r~   r   anyr/   r   rB   r-   )	r@   r   r   r   r   r   r   statuscustomss	            r   debug_stringz_ResourceUpdater.debug_string;  sb   &** ../HII! 9$$i''TT9J9Q9Q9S9STTTTT (
 ++$+777 223LMM%/8 / /i / // /'/ / /  ii    +<+B+B+D+D       G
  */w///)M..r   c                 B    |                                   | j        j        S rG   )r   rz   r   rH   s    r   get_num_cpusz_ResourceUpdater.get_num_cpuse       ##%%%$((r   c                 B    |                                   | j        j        S rG   )r   rz   r   rH   s    r   get_num_gpusz_ResourceUpdater.get_num_gpusi  r   r   c                      t           | j        ffS rG   )rv   r}   rH   s    r   
__reduce__z_ResourceUpdater.__reduce__m  s      $"6!888r   rG   )r   F)rk   rl   rm   rn   r   rp   r   r   rr   r   r   strr   r   r   r   r   r   r   r   r   rv   rv      s        	& 	&x 	& 	& 	& 	&,2 ,2# ,2$ ,2 ,2 ,2 ,2\K4S> K K K K&(/d38n (/ (/ (/ (/ (/T)c ) ) ) ))c ) ) ) )9 9 9 9 9r   rv   )loggingr{   r   collectionsr   numbersr   typingr   r   r   r   ray._common.constantsr   	getLoggerrk   r   ry   r   r   rv   r   r   r   <module>r      s2    				  " " " " " "       & & & & & & & & & & 



 0 0 0 0 0 0		8	$	$ ) ) )M
 M
 M
 M
 M
J	
 	
 	
 M
 M
 M
`M9 M9 M9 M9 M9 M9 M9 M9 M9 M9r   