
    &`iL                         d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlZd dl	m
c mZ d dlmZmZ d dlmZ d dlmZ d dlmZ  ej        e          Z G d d          ZdS )	    N)DictOptionalTuple)HEAD_NODE_RESOURCE_NAMENODE_ID_PREFIX)RESOURCE_CONSTRAINT_PREFIX)accelerators)AcceleratorManagerc                   Z   e Zd ZdZ	 	 	 	 	 	 ddee         dee         dee         dee         deeeef                  deeeef                  fd	Z	d
e
fdZd
e
fdZd Z	 dde
dee         d
d fdZed
eeef         fd            Zedeeef         deeef         fd            Z	 dde
dee         d
dfdZed
eeef         fd            Zedee         d
eeef         fd            Zdee         d
dfdZd Zd Zedee         deeef         d
eeef         fd            ZdS )ResourceAndLabelSpeca(  Represents the resource and label configuration passed to a raylet.

    All fields can be None. Before starting services, resolve() should be
    called to return a ResourceAndLabelSpec with unknown values filled in with
    merged values based on the local machine and user specifications.
    Nnum_cpusnum_gpusmemoryobject_store_memory	resourceslabelsc                 h    || _         || _        || _        || _        || _        || _        d| _        dS )a  
        Initialize a ResourceAndLabelSpec

        Args:
            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.
            resources: The custom resources allocated for this raylet.
            labels: The labels associated with this node. Labels can be used along
                with resources for scheduling.
        FN)r   r   r   r   r   r   _is_resolved)selfr   r   r   r   r   r   s          x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/resource_and_label_spec.py__init__zResourceAndLabelSpec.__init__   s=    * ! #6 "!    returnc                     | j         S )zmReturns if resolve() has been called for this ResourceAndLabelSpec
        and default values are filled out.)r   r   s    r   resolvedzResourceAndLabelSpec.resolved6   s       r   c           	      |    t          d | j        | j        | j        | j        | j        | j        fD                       S )zEReturns whether all fields in this ResourceAndLabelSpec are not None.c              3      K   | ]}|d uV  	d S N ).0vs     r   	<genexpr>z7ResourceAndLabelSpec._all_fields_set.<locals>.<genexpr>=   s:       

 

 TM

 

 

 

 

 

r   )allr   r   r   r   r   r   r   s    r   _all_fields_setz$ResourceAndLabelSpec._all_fields_set;   sS     

 

 (

 

 

 

 

 
	
r   c           	      ,   |                                  sJ t          | j        | j        | j        t          | j                  t          | j                            }d |                                D             }|                                D ]\  }}t          |t
                    s4t          |t                    sJ | dt          |           d|             t          |t                    r7|                                s#t          d                    ||                    |dk     r#t          d                    ||                    |t          j        k    r.t          d                    t          j        ||                    |S )	zReturns a dict suitable to pass to raylet initialization.

        This renames num_cpus / num_gpus to "CPU" / "GPU",
        and check types and values.
        )CPUGPUr   r   c                 &    i | ]\  }}|d k    ||S )r   r    )r!   resource_labelresource_quantitys      r   
<dictcomp>z9ResourceAndLabelSpec.to_resource_dict.<locals>.<dictcomp>Y   s5     
 
 
1 1 A%% -%%%r   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+   s       r   to_resource_dictz%ResourceAndLabelSpec.to_resource_dictI   s    }}Nt{## #D$< = =
 
 
	
 
5>__5F5F
 
 
	 2;1B1B 	 	-N-/55 !5: :   "XXT*;%<%<XXEVXX   ,e44)4466 !77=vni7X7X   !1$$ 77=vni7X7X   !=#FFF 77=v%;^Y8 8   G r   is_headnode_ip_addressc                 X   |                      ||           t                              | j        | j                  \  }}|                     ||           | j        d| _        |                     |           |                                  d| _        | 	                                sJ | S )a  Fills out this ResourceAndLabelSpec instance with merged values from system defaults and user specification.

        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.

        Returns:
            ResourceAndLabelSpec: This instance with all fields resolved.
        )r9   r:   Nr   T)
_resolve_resourcesr   _get_current_node_acceleratorr   r   _resolve_accelerator_resources_resolve_labels_resolve_memory_resourcesr   r%   )r   r9   r:   accelerator_managernum_acceleratorss        r   resolvezResourceAndLabelSpec.resolve}   s     	QQQ !>>M4>
 
	
 	++,?AQRRR = DM 	0111 	&&((( ##%%%%%r   c                  
   i } t          j        t          j                  }|r`	 t	          j        |          } n,# t          $ r t                              d|             w xY wt          	                    d|  d           | S )z9Load resource overrides from the environment, if present.Failed to load z!Autoscaler overriding resources: .)
osgetenvr6   RESOURCES_ENVIRONMENT_VARIABLEjsonloads	Exceptionlogger	exceptiondebug)env_resources
env_strings     r   _load_env_resourcesz(ResourceAndLabelSpec._load_env_resources   s     Y}KLL
 	O $
: 6 6     !?:!?!?@@@ LLM]MMMNNN	   9 )A"env_dictparams_dictc           
         |                      dd          }|                      dd          }|                      dd          }|                      dd          }|                                }|                    |            t          |                                                               |pi           D ]D}||         | |         k    r0t                              d| d||          d| |          d	           E|||||fS )
zMerge environment and Ray param-provided resources, with env values taking precedence.
        Returns separated special case params (CPU/GPU/memory) and the merged resource dict.
        r'   Nr(   r   r   z(Autoscaler is overriding your resource: : z with rF   )popcopyupdatesetkeysintersectionrM   warning)rT   rU   r   r   r   r   resultkeys           r   _merge_resourcesz%ResourceAndLabelSpec._merge_resources   s'   
 <<t,,<<t,,h--&ll+@$GG!!##hx}}''44[5FBGG 	 	C38C=00@s @ @"3'@ @/7}@ @ @  
 6+>FFr   c                 H   t                                           }t                               || j        pi           \  }}}}}|| j        n|| _        || j        n|| _        || j        n|| _        || j        n|| _        || _        |t          j	        
                                }d| j        t          |z   <   t          | j        v rt          t           d          |rd| j        t          <   | j        *t          j        j                                        | _        dS dS )zResolve CPU, GPU, and custom resources. Merges resources from environment,
        Ray params, and defaults in that order of precedence.Ng      ?z7 is a reserved resource name, use another name instead.)r   rR   ra   r   r   r   r   r   rayutilget_node_ip_addressr   r   r4   _privateutilsget_num_cpus)	r   r9   r:   rP   r   r   r   r   merged_resourcess	            r   r<   z'ResourceAndLabelSpec._resolve_resources   sK    -@@BB !11-AUSUVV	
 *2)9x)1)9x%+^dkk #* $$$ 	 
 *"!h::<<O
 <?~78 #dn44* J J J    	:69DN23 = L.;;==DMMM ! r   c                  
   i } t          j        t          j                  }|r`	 t	          j        |          } n,# t          $ r t                              d|             w xY wt          	                    d|  d           | S )NrE   zAutoscaler overriding labels: rF   )
rG   rH   r6   LABELS_ENVIRONMENT_VARIABLErJ   rK   rL   rM   rN   info)env_override_labelsenv_override_labels_strings     r   _load_env_labelsz%ResourceAndLabelSpec._load_env_labels   s     %'Y5&
 &
" & 	Q&*j1K&L&L##     !O3M!O!OPPP KKO9LOOOPPP""rS   rA   c                    i }t           j                            t          j        j        d          }t           j                            t          j        j        d          }t           j                            t          j        j        d          }t           j                            t          j        j        d          }|r||t          j        j	        <   |r||t          j        j
        <   |r||t          j        j        <   |r||t          j        j        <   | rm|                                 }|r||t          j        j        <   |                                 dk    r+|                                 }|r|                    |           |S )N TPU)rG   environgetrc   _rayletNODE_TYPE_NAME_ENVNODE_MARKET_TYPE_ENVNODE_REGION_ENVNODE_ZONE_ENVRAY_NODE_MARKET_TYPE_KEYRAY_NODE_GROUP_KEYRAY_NODE_ZONE_KEYRAY_NODE_REGION_KEY!get_current_node_accelerator_typeRAY_NODE_ACCELERATOR_TYPE_KEYget_resource_name#get_current_node_accelerator_labelsrZ   )rA   default_labels
node_groupmarket_typeavailability_regionavailability_zoneaccelerator_type
tpu_labelss           r   _get_default_labelsz(ResourceAndLabelSpec._get_default_labels  s^     Z^^CK$BBGG
jnnS[%ErJJ jnnS[-H"MMJNN3;+DbII  	OCNN3;?@ 	H=GN3;9: 	N<MN3;89 	R>QN3;:;  	62TTVV % % K=
 #4466%??0TTVV
 6"))*555r   c                 $   t                               |          }| j        pi                                 D ]U\  }}||v rG||         |k    r;t                              d| d| d||          d| d| j        |          d           |||<   Vt                                           }|pi                                 D ]M\  }}||v r?||         |k    r3t                              d| d||          d| d||          d	           |||<   N|| _        dS )z{Resolve and merge environment override, user-input from params, and Ray default
        labels in that order of precedence.z,User label is overriding Ray default label: rW   z to rF   z$Autoscaler is overriding your label:N)r   r   r   r/   rM   r^   ro   )r   rA   mergedr`   val
env_labelss         r   r?   z$ResourceAndLabelSpec._resolve_labels.  s    &99:MNN *1133 	 	HCf}}!3!323 2 22 2$Sk2 22 2"k#.2 2 2  
 F3KK *::<<
#)r0022 	 	HCf}}!3!311 1$Sk1 11 1(o1 1 1  
 F3KKr   c                    |sdS |                                 }|                                }|C|A|t          |          k    r.t          d| d| d|                                 d| d	          |dk    r|| _        n
|| j        |<   |                                }|rd| j        t           | <   |	                                }|r| j        
                    |           dS dS )	z2Detect and update accelerator resources on a node.Nz Attempting to start raylet with  z, but z
 contains rF   r(      )r   +get_current_process_visible_accelerator_idslenr4   #get_visible_accelerator_ids_env_varr   r   r~   r   %get_current_node_additional_resourcesrZ   )r   rA   rB   accelerator_resource_namevisible_accelerator_idsr   additional_resourcess          r   r>   z3ResourceAndLabelSpec._resolve_accelerator_resourcesN  sQ   " 	F$7$I$I$K$K!KKMM 	  ('3 3'>#?#???73C 7 7,7 7*NNPP7 7 47 7 7   %--,DMM8HDN45.PPRR 	RPQDN8L:JLLMEEGG 	   	8N!!"677777	8 	8r   c           
         t           j        j                                        }t           j        j                                        }| j        }|t          |t          j	        z            }t          j        dk    rt          |t          j                  }t          j        }t          j        dk    st          j        dk    rPt           j        j                                        dz  }t!          t          j        |          }t          ||          }|r;||k    r5t$                              d                    |dz            dz              |}| j        }|[||z
  }|dk     rP|d	|z  k     rGt-          d
                    t/          |dz  d          t          d||z  z                                || _        || _        d S )N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   )rc   _commonrg   get_system_memoryrf   estimate_available_memoryr   r.   r6   &DEFAULT_OBJECT_STORE_MEMORY_PROPORTIONsysplatformmin!MAC_DEGRADED_PERF_MMAP_SIZE_LIMIT%DEFAULT_OBJECT_STORE_MAX_MEMORY_BYTESget_shared_memory_bytesmaxREQUIRE_SHM_SIZE_THRESHOLDrM   rO   r5   r   r4   round)r   system_memoryavail_memoryr   object_store_memory_cap	shm_availshm_capr   s           r   r@   z.ResourceAndLabelSpec._resolve_memory_resourcest  s   );;==|)CCEE"6&"%}SS# # |x''&)')X' '#
 C $ |w&&#,(*B*B  L.FFHH4O	mF	RR*-.Ew*O*O' (>'*AAADKK/36 <<   '>#>!$77F~~&4-+?"?"? 4
 5;FfslA..C6M;Q4R0S0S5 5	 	 	 $7 r   c                 F   t          j                    D ]}t          j        |          }||dk    r| }n|                    |          }|G|                                }|                                }|t          |t          |                    }|dk    r||fc S dS )a2  
        Returns the AcceleratorManager and accelerator count for the accelerator
        associated with this node. This assumes each node has at most one accelerator type.
        If no accelerators are present, returns None.

        The resolved accelerator count uses num_gpus (for GPUs) or resources if set, and
        otherwise falls back to the count auto-detected by the AcceleratorManager. The
        resolved accelerator count is capped by the number of visible accelerators.

        Args:
            num_gpus: GPU count (if provided by user).
            resources: Resource dictionary containing custom resource keys.

        Returns:
            Tuple[Optional[AcceleratorManager], int]: A tuple containing the accelerator
            manager (or None) the final resolved accelerator count.
        Nr(   r   )Nr   )r	   "get_all_accelerator_resource_names$get_accelerator_manager_for_resourcert   !get_current_node_num_acceleratorsr   r   r   )r   r   resource_namerA   rB   r   s         r   r=   z2ResourceAndLabelSpec._get_current_node_accelerator  s    * *LNN 	= 	=M"."S# # #*%%#+  #,==#?#? ''IIKK ! (SSUU ( +6'*(#.E*F*F( ($  !##*,<<<<< $ wr   )NNNNNNr   )__name__
__module____qualname____doc__r   r.   r1   r   strr   boolr   r%   r8   rC   staticmethodrR   ra   r<   ro   r
   r   r?   r>   r@   r   r=   r    r   r   r   r      s         #'"&"&/304+/" "3-" 3-" 	"
 &e_" De,-" c3h(" " " ":!$ ! ! ! !

 
 
 
 
2 2 2j ?C% %%.6sm%	% % % %N c5j!1    \ G4U
#3 G$sEzBR G G G \G, ?C.> .>.>.6sm.>	.> .> .> .>` #d38n # # # \# #%&89#	c3h# # # \#J#+,>#?	   @$8 $8 $8L? ? ?B .3-.,0e,<.	!3&	'. . . \. . .r   r   )rJ   loggingrG   r   typingr   r   r   rc   ray._private.ray_constantsrf   r6   ray._common.constantsr   r   ray._common.utilsr   ray._privater	   ray._private.acceleratorsr
   	getLoggerr   rM   r   r    r   r   <module>r      s     				 



 ( ( ( ( ( ( ( ( ( ( 



 2 2 2 2 2 2 2 2 2 I I I I I I I I 8 8 8 8 8 8 % % % % % % 8 8 8 8 8 8		8	$	$S S S S S S S S S Sr   