
    &`im"                     
   d dl Z d dlmZ d dlmZmZmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ dZd Zd	 Z G d
 d          Z G d d          Zd Ze	 	 ddededededef
d            ZdededefdZededefd            ZdS )    N)defaultdict)AnyDictTuple)CategoricalDomainFunction)assign_value)DeveloperAPI   c                  *    t          t                    S N)r   list     m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/impl/placeholder.pycreate_resolvers_mapr      s    tr   c                     t          j        t          |                               d                                                    dt
                   S )z>Compute a hash for the specific placeholder based on its path.zutf-8N)hashlibsha1strencode	hexdigestID_HASH_LENGTH)
path_tuples    r   _id_hashr      s;    <J..w7788BBDD_n_UUr   c                   4    e Zd ZdZdZd ZdefdZdefdZ	dS )	_FunctionResolverz*Replaced value for function typed objects.__fn_phc                 "    || _         || _        d S r   )hash_fn)selfr!   fns      r   __init__z_FunctionResolver.__init__   s    	r   configc                 8    | j                             |          S )zSome functions take a resolved spec dict as input.

        Note: Function placeholders are independently sampled during
        resolution. Therefore their random states are not restored.
        )r&   )r"   sample)r#   r&   s     r   resolvez_FunctionResolver.resolve   s     xf---r   returnc                     | j         | j        fS r   TOKENr!   r#   s    r   get_placeholderz!_FunctionResolver.get_placeholder&       
DI&&r   N)
__name__
__module____qualname____doc__r-   r%   r   r)   r   r/   r   r   r   r   r      sd        44E  .d . . . .' ' ' ' ' ' 'r   r   c                   .    e Zd ZdZdZd Zd ZdefdZdS )_RefResolverz3Replaced value for all other non-primitive objects.__ref_phc                 "    || _         || _        d S r   )r!   _value)r#   r!   values      r   r%   z_RefResolver.__init__/   s    	r   c                     | j         S r   )r9   r.   s    r   r)   z_RefResolver.resolve3   s
    {r   r*   c                     | j         | j        fS r   r,   r.   s    r   r/   z_RefResolver.get_placeholder6   r0   r   N)	r1   r2   r3   r4   r-   r%   r)   r   r/   r   r   r   r6   r6   *   sX        ==E    ' ' ' ' ' ' 'r   r6   c                 Z    t          | t          t          t          t          f          p| du S )zeReturns True if x is a primitive type.

    Primitive types are int, float, str, bool, and None.
    N)
isinstanceintfloatr   bool)xs    r   _is_primitiverC   :   s&    
 a#uc4011>Q$Y>r   r   r&   	resolvers	id_prefixpath_prefixr*   c                 .   t          | t                    r?d| v r;t          |           dk    r(fdt          | d                   D             | d<   | S t          | t                    r"fd|                                 D             S t          | t
                    rfdt          |           D             S t          | t                    r*t          fdt          |           D                       S t          |           r| S t          | t                    r)fdt          | j	                  D             | _	        | S t          | t                    rNt                    }t          ||           }                             |           |                                S t          | t                    sNt                    }t!          ||           }                             |           |                                S | S )a  Replaces reference objects contained by a config dict with placeholders.

    Given a config dict, this function replaces all reference objects contained
    by this dict with placeholder strings. It recursively expands nested dicts
    and lists, and properly handles Tune native search objects such as Categorical
    and Function.
    This makes sure the config dict only contains primitive typed values, which
    can then be handled by different search algorithms.

    A few details about id_prefix and path_prefix. Consider the following config,
    where "param1" is a simple grid search of 3 tuples.

    config = {
        "param1": tune.grid_search([
            (Cat, None, None),
            (None, Dog, None),
            (None, None, Fish),
        ]),
    }

    We will replace the 3 objects contained with placeholders. And after trial
    expansion, the config may look like this:

    config = {
        "param1": (None, (placeholder, hash), None)
    }

    Now you need 2 pieces of information to resolve the placeholder. One is the
    path of ("param1", 1), which tells you that the first element of the tuple
    under "param1" key is a placeholder that needs to be resolved.
    The other is the mapping from the placeholder to the actual object. In this
    case hash -> Dog.

    id and path prefixes serve exactly this purpose here. The difference between
    these two is that id_prefix is the location of the value in the pre-injected
    config tree. So if a value is the second option in a grid_search, it gets an
    id part of 1. Injected placeholders all get unique id prefixes. path prefix
    identifies a placeholder in the expanded config tree. So for example, all
    options of a single grid_search will get the same path prefix. This is how
    we know which location has a placeholder to be resolved in the post-expansion
    tree.

    Args:
        config: The config dict to replace references in.
        resolvers: A dict from path to replaced objects.
        id_prefix: The prefix to prepend to id every single placeholders.
        path_prefix: The prefix to prepend to every path identifying
            potential locations of placeholders in an expanded tree.

    Returns:
        The config with all references replaced.
    grid_search   c                 B    g | ]\  }}t          ||fz             S r   inject_placeholders.0ichoicerE   rF   rD   s      r   
<listcomp>z'inject_placeholders.<locals>.<listcomp>~   sD     !
 !
 !

 6  	9t3C[QQ	!
 !
 !
r   c           
      L    i | ] \  }}|t          ||fz   |fz             !S r   rK   )rN   kvrE   rF   rD   s      r   
<dictcomp>z'inject_placeholders.<locals>.<dictcomp>   sN     
 
 
1 "1iaT1A;RSQUCUVV
 
 
r   c           	      J    g | ]\  }}t          ||fz   |fz              S r   rK   rN   rO   elemrE   rF   rD   s      r   rQ   z'inject_placeholders.<locals>.<listcomp>   sK     
 
 
4  iaT1A;RSQUCUVV
 
 
r   c              3   R   K   | ]!\  }}t          ||fz   |fz             V  "d S r   rK   rW   s      r   	<genexpr>z&inject_placeholders.<locals>.<genexpr>   sY       
 
4  iaT1A;RSQUCUVV
 
 
 
 
 
r   c                 B    g | ]\  }}t          ||fz             S r   rK   rM   s      r   rQ   z'inject_placeholders.<locals>.<listcomp>   sD     
 
 

 6  	9t3C[QQ	
 
 
r   )r>   dictlen	enumerateitemsr   tuplerC   r   
categoriesr	   r   r   appendr/   r   r6   )r&   rD   rE   rF   id_hashrT   s    ```  r   rL   rL   B   s   v &$ 2MV$;$;Fq@P@P!
 !
 !
 !
 !
 !

 'vm'<==!
 !
 !
} 	FD	!	! )
 
 
 
 
 

 
 
 	
 
FD	!	! $
 
 
 
 
 
$V,,
 
 
 	
 
FE	"	"  
 
 
 
 
 
$V,,
 
 
 
 
 	
 
v		 	FK	(	( 
 
 
 
 
 

 'v'899
 
 
 	FH	%	% 9%%gv..+%%a(((  """'' 9%%&))+%%a(((  """ r   prefixpathc                    |s|| fS |d         }t          | t                    rb| d         t          j        t          j        fv r|| fS |t          |           k     r*t          | |         ||d         fz   |dd                    S nkt          | t                    r|| v s(t          | t                    r=|t          |           k     r*t          | |         ||d         fz   |dd                    S dS )Nr   rI   )rd   re   )NN)	r>   r`   r   r-   r6   r]   _get_placeholderr\   r   )r&   rd   re   keys       r   rg   rg      s    v~
q'C&%   X!9*0,2DEEE 6>!3v;;#sFd1gZ$7d122h     VT
"
" Xsf}}64   (5%(3v;;%6%6  sFd1gZ4GdSTSUSUhWWWW :r   replacedc                 `      fd} |t           d            |t           f           dS )zReplaces placeholders contained by a config dict with the original values.

    Args:
        config: The config to replace placeholders in.
        replaced: A dict from path to replaced objects.
    c           	                                           D ]g\  }}|sJ t          |d         |           s t          d|          \  }}|s7|D ]-}|j        |d         k    rt	          | |j        |            .hd S )Nr   r   rI   )r_   r>   rg   r!   r
   r)   )	resolver_typeargsre   rD   rd   phresolverr&   ri   s	          r   	__resolvez'resolve_placeholders.<locals>.__resolve   s    '~~// 	F 	FOD)9ilM:: )&"d;;JFB % F F=BqE))VV-=X-=t-DEEEE	F	F 	Fr   r   )rm   N)r6   r   )r&   ri   rp   s   `` r   resolve_placeholdersrq      s_    F F F F F F& Il$$$$ Ivi000000r   )r   r   )r   collectionsr   typingr   r   r   ray.tune.search.sampler   r   r	   !ray.tune.search.variant_generatorr
   ray.util.annotationsr   r   r   r   r   r6   rC   rL   rg   rq   r   r   r   <module>rw      s    # # # # # # # # # # # # # # # # @ @ @ @ @ @ @ @ @ @ : : : : : : - - - - - -  V V V
' ' ' ' ' ' ' '*' ' ' ' ' ' ' ' ? ? ?  	l lll l 	l
 
l l l l^ S  %  u        F 1 1 1 1 1 1 1 1r   