
    &`i                     D   d dl Z d dlZ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	m
Z
mZmZmZmZmZ d dlZd dlmZmZ d dlmZ d dlmZmZ d dlmZ d dlmZ  ed          Z ej        e          Z d	ee         d
ee!ee"         f         fdZ#d
ee$e%f         fdZ&de
e$ef         fdZ'ded
e%fdZ(	 	 ddeeg ef         ee
e$ef         gef         f         dee
e$ef                  de	dee$         de!d
eg ef         fdZ) G d de j*                  Z+ G d d          Z,dS )    N)	AnyCallableContextManagerDictListOptionalTupleTypeVarUnion)find_free_portis_ipv6)ActorHandle)StartTracebackStartTracebackWithWorkerRank)RayActorError)	ObjectRefTremote_valuesreturnc                    |                                  }t          |          dk    rt          j        |          \  }}|D ]}	 t          j        |           # t
          $ rC}|                     |          }t                              d| d           d|fcY d}~c S d}~wt          $ r+}|                     |          }t          |          |d}~ww xY wt          |          dk    dS )a$  Check for actor failure when retrieving the remote values.

    Args:
        remote_values: List of object references from Ray actor methods.

    Returns:
        A tuple of (bool, Exception). The bool is
        True if evaluating all object references is successful, False otherwise.
    r   zWorker z has failed.FN)worker_rank)TN)copylenraywaitgetr   indexloggerinfo	Exceptionr   )r   
unfinishedfinished
object_refexcfailed_actor_rankfailed_worker_ranks          m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/train/_internal/utils.pycheck_for_failurer(   "   s5    ##%%J
j//A

"x
33* # 	 	J
####  " " "$1$7$7
$C$C!E&7EEEFFFcz!!!!!!!!   %2%8%8%D%D"2 2   j//A

* :s*   A
C#6B!C!C.&CCc                      t           j                                        } t          t	          |           rt
          j        nt
          j                  }| |fS )z4Returns the IP address and a free port on this node.)r   utilget_node_ip_addressr   r   socketAF_INET6AF_INET)addrports     r'   get_address_and_portr1   H   s?    8''))DWT]]N&//OOD:    env_varsc                     d |                                  D             }t          j                            |           dS )zyUpdates the environment variables on this worker process.

    Args:
        env_vars: Environment variables to set.
    c                 4    i | ]\  }}|t          |          S  )str).0kvs      r'   
<dictcomp>z#update_env_vars.<locals>.<dictcomp>U   s$    888tq!CFF888r2   N)itemsosenvironupdate)r3   	sanitizeds     r'   update_env_varsrA   O   s=     98x~~'7'7888IJi     r2   fnc                     t          j        |           j                                        }t           j        j        t           j        j        t           j        j        ht          fd|D                       S )a  Counts the number of required parameters of a function.

    NOTE: *args counts as 1 required parameter.

    Examples
    --------

    >>> def fn(a, b, /, c, *args, d=1, e=2, **kwargs):
    ...    pass
    >>> count_required_parameters(fn)
    4

    >>> fn = lambda: 1
    >>> count_required_parameters(fn)
    0

    >>> def fn(config, a, b=1, c=2):
    ...     pass
    >>> from functools import partial
    >>> count_required_parameters(partial(fn, a=0))
    1
    c                 Z    g | ]'}|j         t          j        j        k    |j        v %|(S r6   )defaultinspect	Parameteremptykind)r8   ppositional_param_kindss     r'   
<listcomp>z-count_required_parameters.<locals>.<listcomp>x   sC     	
 	
 	
yG-333BX8X8X 8X8X8Xr2   )	rF   	signature
parametersvaluesrG   POSITIONAL_ONLYPOSITIONAL_OR_KEYWORDVAR_POSITIONALr   )rB   paramsrK   s     @r'   count_required_parametersrT   Y   s    . r""-4466F 	)/(
 	
 	
 	
 	
	
 	
 	
  r2   
train_funcFconfigtrain_func_contextfn_arg_namediscard_returnsc                 N   	 t                     }|r"t          j                    fd            }|	n 	|dk    r| d| d}t          |          |dk    r(i nt          j        	          	fd            }n t          j        	          	fd            }|S )a  Validates and constructs the training function to execute.
    Args:
        train_func: The training function to execute.
            This can either take in no arguments or a ``config`` dict.
        config (Optional[Dict]): Configurations to pass into
            ``train_func``. If None then an empty Dict will be created.
        train_func_context: Context manager for user's `train_func`, which executes
            backend-specific logic before and after the training function.
        fn_arg_name (Optional[str]): The name of training function to use for error
            messages.
        discard_returns: Whether to discard any returns from train_func or not.
    Returns:
        A valid training function.
    Raises:
        ValueError: if the input ``train_func`` is invalid.
    c                  N    	  | i | d S # t           $ r}t          |d }~ww xY wNr    r   )argskwargserU   s      r'   discard_return_wrapperz4construct_train_func.<locals>.discard_return_wrapper   sJ    ,
D+F+++++ , , ,$!+,s    
$$   z: should take in 0 or 1 required arguments, but it accepts z required arguments instead.Nc                      	              5             cd d d            S # 1 swxY w Y   d S # t           $ r} t          | d } ~ ww xY wr\   r]   )r`   rV   rW   wrapped_train_funcs    r'   train_fnz&construct_train_func.<locals>.train_fn   s    ,'')) 6 6--f556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 , , ,$!+,s,   
2 %2 )2 )2 
A	AA	c                      	              5               cd d d            S # 1 swxY w Y   d S # t           $ r} t          | d } ~ ww xY wr\   r]   )r`   rW   rd   s    r'   re   z&construct_train_func.<locals>.train_fn   s    ,'')) 0 0--//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 , , ,$!+,s,   
1 
$1 (1 (1 
AAA)rT   	functoolswraps
ValueError)
rU   rV   rW   rX   rY   num_required_paramsra   err_msgre   rd   s
   ```      @r'   construct_train_funcrl      s6   . 4J?? ( 
	$	$	, 	, 	, 	, 
%	$	, 4'Q A A"A A A 	 !!!		!	!~6	+	,	,	, 	, 	, 	, 	, 	, 
-	,	, 	, 
+	,	,	, 	, 	, 	, 	, 
-	,	, Or2   c                   &     e Zd ZdZi Z fdZ xZS )	SingletonzSingleton Abstract Base Class

    https://stackoverflow.com/questions/33364070/implementing
    -singleton-as-metaclass-but-for-abstract-classes
    c                     | | j         vr( t          t          |           j        |i || j         | <   | j         |          S r\   )
_instancessuperrn   __call__)clsr^   r_   	__class__s      r'   rr   zSingleton.__call__   sE    cn$$"@%	3"7"7"@$"Q&"Q"QCN3~c""r2   )__name__
__module____qualname____doc__rp   rr   __classcell__)rt   s   @r'   rn   rn      sI          J# # # # # # # # #r2   rn   c                   $    e Zd ZdZdefdZd ZdS )ActorWrapperzDWraps an actor to provide same API as using the base class directly.actorc                     || _         d S r\   )r|   )selfr|   s     r'   __init__zActorWrapper.__init__   s    


r2   c                 8    t          | j        |          fdS )Nc                  B    t          j         j        | i |          S r\   )r   r   remote)r^   r_   actor_methods     r'   <lambda>z*ActorWrapper.__getattr__.<locals>.<lambda>   s$    sw/B|/BD/SF/S/S'T'T r2   )getattrr|   )r~   itemr   s     @r'   __getattr__zActorWrapper.__getattr__   s%     tz400TTTTTr2   N)ru   rv   rw   rx   r   r   r   r6   r2   r'   r{   r{      sI        NNk    U U U U Ur2   r{   )rU   F)-abcrg   rF   loggingr=   r,   typingr   r   r   r   r   r   r	   r
   r   r   ray._common.network_utilsr   r   	ray.actorr   ray.air._internal.utilr   r   ray.exceptionsr   	ray.typesr   r   	getLoggerru   r   boolr    r(   r7   intr1   rA   rT   rl   ABCMetarn   r{   r6   r2   r'   <module>r      s   



       				 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


 = = = = = = = = ! ! ! ! ! !        ) ( ( ( ( (      GCLL		8	$	$#	?#
4)$$%# # # #LeCHo    !d38n ! ! ! !$( $s $ $ $ $V ".!C Chr1uoxc3h0@!0C'DDECT#s(^$C 'C #	C
 C b!e_C C C CL# # # # # # # #
U 
U 
U 
U 
U 
U 
U 
U 
U 
Ur2   