o
    $i<                  
   @   s  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlZd dlmZ d dlmZmZmZ d dlmZ d dlmZ d d	lmZmZmZmZ d d
lmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+m,Z, d dl-m.Z.m/Z/m0Z0 d dl1m2Z2m3Z3 ed"ddZ4e	d#de	e de5deee
f fddZ6ed$dee7ef dee7ef fddZ8ed%dee7ef dee7ef fddZ9eG dd de j:Z;ee6dd Z<g d!Z=dS )&    N)deque)partial)AnyDictListOptionalTupleUnion)deprecation_warning)DeveloperAPI	PublicAPIoverride)Filter)FilterManager)try_import_jaxtry_import_tftry_import_tfptry_import_torch)
LARGE_INTEGERMAX_LOG_NN_OUTPUTMIN_LOG_NN_OUTPUTSMALL_NUMBERfclstmone_hotrelusigmoidsoftmax)ConstantScheduleExponentialScheduleLinearSchedulePiecewiseSchedulePolynomialSchedule)checkcheck_compute_single_actioncheck_train_results)deep_updatemerge_dictsFc                 C   sN   t |pg }|r%|rG dd d| | }nG dd d| | }|} |s| S )z:Returns a new class with mixins applied in priority order.c                   @      e Zd ZdS zadd_mixins.<locals>.new_baseN__name__
__module____qualname__ r.   r.   U/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/rllib/utils/__init__.pynew_base6       r0   c                   @   r(   r)   r*   r.   r.   r.   r/   r0   ;   r1   )listpop)basemixinsreversedr0   r.   r.   r/   
add_mixins-   s   r7   elementsto_tuplereturnc                 C   sD   t }|du rt}| du r| S t| t tttfv r|| S || gS )a;  
    Makes sure `elements` is returned as a list, whether `elements` is a single
    item, already a list, or a tuple.

    Args:
        elements: The inputs as a single item, a list/tuple/deque of items, or None,
            to be converted to a list/tuple. If None, returns empty list/tuple.
        to_tuple: Whether to use tuple (instead of list).

    Returns:
        The provided item in a list of size 1, or the provided items as a
        list. If `elements` is None, returns an empty list. If `to_tuple` is True,
        returns a tuple instead of a list.
    TN)r2   tupletypesetr   )r8   r9   ctorr.   r.   r/   
force_listC   s   r?   /nestedc                 C   s4   i }t | D ]\}}|tt|}|||< q|S )a   
    Flattens a nested dict into a flat dict with joined keys.

    Note, this is used for better serialization of nested dictionaries
    in `OfflinePreLearner.__call__` when called inside
    `ray.data.Dataset.map_batches`.

    Note, this is used to return a `Dict[str, numpy.ndarray] from the
    `__call__` method which is expected by Ray Data.

    Args:
        nested: A nested dictionary.
        sep: Separator to use when joining keys.

    Returns:
        A flat dictionary where each key is a path of keys in the nested dict.
    )treeflatten_with_pathjoinmapstr)rA   sep	env_stepsflatpathleafkeyr.   r.   r/   flatten_dicta   s
   
rM   rI   c                 C   s^   i }|   D ]&\}}||}|}|dd D ]}||vr!i ||< || }q|||d < q|S )a  
    Reconstructs a nested dict from a flat dict with joined keys.

    Note, this is used for better deserialization ofr nested dictionaries
    in `Learner.update' calls in which a `ray.data.DataIterator` is used.

    Args:
        flat: A flat dictionary with keys that are paths joined by `sep`.
        sep: The separator used in the flat dictionary keys.

    Returns:
        A nested dictionary.
    N)itemssplit)rI   rG   rA   compound_keyvaluekeyscurrentrL   r.   r.   r/   unflatten_dict~   s   

rU   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	NullContextManagerzNo-op context managerc                 C      d S Nr.   selfr.   r.   r/   __init__      zNullContextManager.__init__c                 C   rW   rX   r.   rY   r.   r.   r/   	__enter__   r\   zNullContextManager.__enter__c                 G   rW   rX   r.   )rZ   argsr.   r.   r/   __exit__   r\   zNullContextManager.__exit__N)r+   r,   r-   __doc__r[   r]   r_   r.   r.   r.   r/   rV      s
    rV   T)r9   )#r7   r#   r$   r%   r&   r
   r   r?   force_tuplerM   rU   r   r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r!   r"   r   r   )F)NF)r@   r   )r@   )>
contextlibcollectionsr   	functoolsr   typingr   r   r   r   r   r	   rB   ray._common.deprecationr
   ray.rllib.utils.annotationsr   r   r   ray.rllib.utils.filterr   ray.rllib.utils.filter_managerr   ray.rllib.utils.frameworkr   r   r   r   ray.rllib.utils.numpyr   r   r   r   r   r   r   r   r   r   ray.rllib.utils.schedulesr   r   r    r!   r"   ray.rllib.utils.test_utilsr#   r$   r%   ray.tune.utilsr&   r'   r7   boolr?   rF   rM   rU   AbstractContextManagerrV   ra   __all__r.   r.   r.   r/   <module>   sB     0
&&