
    &`i^                     |   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	Z	d dl
mZ d dlmZ  e j        e          Z e            defd            Zdedeeef         fd	Zdedefd
Z e            defd            ZdZdZdZdZdZdZ e            	 ddedee         defd            Z G d d          Z dS )    N)	lru_cache)DictOptionalTuple)_is_ray_cluster)Trialreturnc                  (    t          j                    S N)raycluster_resources     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/execution/insufficient_resources_manager.py$_get_cluster_resources_no_autoscalerr      s     """r   trialc                     | j         j                            dd          }| j         j                            dd          }||fS )NCPUr   GPU)placement_group_factoryrequired_resourcesget)r   cpugpus      r   _get_trial_cpu_and_gpur      sA    

'
:
>
>ua
H
HC

'
:
>
>ua
H
HC8Or   c                     | j         t          j        k    sJ t          |           \  }}|t	                                          dd          k    o%|t	                                          dd          k    S )z^Calculates if there is enough resources for a PENDING trial.

    For no autoscaler case.
    r   r   r   )statusr   PENDINGr   r   r   )r   
asked_cpus
asked_gpuss      r   _can_fulfill_no_autoscalerr!      s|    
 <5=((((3E::J
=??CCq   M
<>>BB5!LL
LMr   c                      t                      r-t          t          j                            dd                    S t          t          j                            dd                    S )N5TUNE_WARN_INSUFFICENT_RESOURCE_THRESHOLD_S_AUTOSCALER60*TUNE_WARN_INSUFFICENT_RESOURCE_THRESHOLD_S)r   floatosenvironr   r   r   r   -_get_insufficient_resources_warning_thresholdr)   (   s]     
YJNNG 
 
 	
 RZ^^$PRVWWXXXr   zTraining has not started in the last {wait_time:.0f} seconds. This could be due to the cluster not having enough resources available. zYou asked for {asked_cpus} CPUs and {asked_gpus} GPUs, but the cluster only has {cluster_cpus} CPUs and {cluster_gpus} GPUs available. zStop the training and adjust the required resources (e.g. via the `ScalingConfig` or `resources_per_trial`, or `num_workers` for rllib), or add more resources to your cluster.zNo trial is running and no new trial has been started within the last {wait_time:.0f} seconds. This could be due to the cluster not having enough resources available. zYou asked for {asked_cpus} CPUs and {asked_gpus} GPUs per trial, but the cluster only has {cluster_cpus} CPUs and {cluster_gpus} GPUs available. zStop the tuning and adjust the required resources (e.g. via the `ScalingConfig` or `resources_per_trial`, or `num_workers` for rllib), or add more resources to your cluster.F	for_trainc           
         d}| rt           }t          }t          }nt          }t          }t
          }||                    t                                z  }|rdt          |          \  }}t                      }||                    |||
                    dd          |
                    dd                    z  }||z  }|S )Nz3Ignore this message if the cluster is autoscaling. )	wait_timer   r   r   )r   r    cluster_cpuscluster_gpus)MSG_TRAIN_STARTMSG_TRAIN_INSUFFICIENTMSG_TRAIN_ENDMSG_TUNE_STARTMSG_TUNE_INSUFFICIENTMSG_TUNE_ENDformatr)   r   r   r   )	r*   r   msgstartinsufficientendr   r    r   s	            r   '_get_insufficient_resources_warning_msgr:   V   s     @C -,5<<"O"Q"Q<RRRC 	
!7!>!>
J@BB|""!!*..ua88*..ua88	 # 
 
 	
 3JCJr   c                   &    e Zd ZdZddefdZd ZdS )_InsufficientResourcesManagerzInsufficient resources manager.

    Makes best effort, conservative guesses about if Tune loop is stuck due to
    infeasible resources. If so, outputs usability messages for users to
    act upon.
    Fr*   c                 0    d| _         d| _        || _        d S )N)_no_running_trials_since_last_trial_num
_for_train)selfr*   s     r   __init__z&_InsufficientResourcesManager.__init__   s    (*%!#r   c                    t          |          | j        k    r| j        dk    rt          j                    | _        nt          j                    | j        z
  t                      k    rrt          d |D                       }|r	d| _        dS t          | j        |d                   }t          
                    |           t          j                    | _        nd| _        t          |          | _        dS )a^  Tracks information across the life of Tune loop and makes guesses
        about if Tune loop is stuck due to infeasible resources.
        If so, outputs certain warning messages.
        The logic should be conservative, non-intrusive and informative.
        For example, rate limiting is applied so that the message is not
        spammy.
        r>   c              3   ^   K   | ](}|j         t          j        k    ot          |          V  )d S r   )r   r   r   r!   ).0r   s     r   	<genexpr>zG_InsufficientResourcesManager.on_no_available_trials.<locals>.<genexpr>   sN       & & LEM1W6PQV6W6W& & & & & &r   Nr   )r*   r   )lenr@   r?   time	monotonicr)   anyr:   rA   loggerwarning)rB   
all_trialscan_fulfill_anyr6   s       r   on_no_available_trialsz4_InsufficientResourcesManager.on_no_available_trials   s    z??d222,22040@0@--  4#@@?AAB B #& & &!+& & & # #
 # 46D1F >"oZ]   s###040@0@-,.D)":r   N)F)__name__
__module____qualname____doc__boolrC   rP   r   r   r   r<   r<   w   sM         $ $$ $ $ $ $"/ "/ "/ "/ "/r   r<   )FN)!loggingr'   rI   	functoolsr   typingr   r   r   r   ray.tune.execution.cluster_infor   ray.tune.experimentr   	getLoggerrQ   rL   r   intr   rU   r!   r&   r)   r/   r0   r1   r2   r3   r4   strr:   r<   r   r   r   <module>r^      s    				        ( ( ( ( ( ( ( ( ( ( 



 ; ; ; ; ; ; % % % % % %		8	$	$
 #d # # # #% E#s(O    
Me 
M 
M 
M 
M 
M Yu Y Y Y YO 
B 
- O W 
-  6: $,UO   @0/ 0/ 0/ 0/ 0/ 0/ 0/ 0/ 0/ 0/r   