o
    $iR                     @   sT   d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
 e
G dd deZdS )    )AnyDictListUnion)	StatsBase)single_value_to_cpu)DeveloperAPIc                
       s   e Zd ZdZdZ fddZdeeef f fddZ	deeef dd	f fd
dZ
defddZddedeed f fddZdedd	fddZded  dd	fddZ	ddededeeee f fddZdefddZ  ZS )	ItemStatsa  A Stats object that tracks a single item.

    Note the follwing limitation: That, when calling `ItemStats.merge()`, we replace the current item.
    This is because there can only be a single item tracked by definition.

    This class will check if the logged item is a GPU tensor.
    If it is, it will be converted to CPU memory.

    Use this if you want to track a single item that should not be reduced.
    An example would be to log the total loss.
    itemc                    s   t  j|i | d| _dS )z!Initializes a ItemStats instance.N)super__init___item)selfargskwargs	__class__ _/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/rllib/utils/metrics/stats/item.pyr      s   
zItemStats.__init__returnc                    s   t   }| j|d< |S Nr
   )r   	get_stater   r   stater   r   r   r      s   

zItemStats.get_stater   Nc                    s   t  | |d | _d S r   )r   	set_stater   r   r   r   r   r   "   s   zItemStats.set_statec                 C   s   dS )N   r   r   r   r   r   __len__&   s   zItemStats.__len__Tcompilec                 C   s.   | j }d | _ t|}|r|S |  }||_ |S )N)r   r   clone)r   r   r
   return_statsr   r   r   reduce)   s   zItemStats.reducec                 C   s   t || _dS )zPushes a value into this Stats object.

        Args:
            item: The value to push. Can be of any type.
                GPU tensors are moved to CPU memory.

        Returns:
            None
        Nr   r   )r   r
   r   r   r   push6   s   zItemStats.pushincoming_statsc                 C   s$   t |dks
J d|d j| _dS )zMerges ItemStats objects.

        Args:
            incoming_stats: The list of ItemStats objects to merge.

        Returns:
            None. The merge operation modifies self in place.
        r   z_ItemStats should only be merged with one other ItemStats object which replaces the current itemr   N)lenr   )r   r$   r   r   r   mergeC   s   
zItemStats.mergeFlatest_merged_onlyc                 C   s   t | j}|r	|S |gS )a  Returns the internal item.

        This does not alter the internal item.

        Args:
            compile: If True, return the internal item directly.
                If False, return the internal item as a single-element list.
            latest_merged_only: This parameter is ignored for ItemStats.
                ItemStats tracks a single item, not a series of merged values.
                The current item is always returned regardless of this parameter.

        Returns:
            The internal item.
        r"   )r   r   r'   r
   r   r   r   peekR   s   
zItemStats.peekc                 C   s   d|    S )Nz
ItemStats()r(   r   r   r   r   __repr__k   s   zItemStats.__repr__)T)TF)__name__
__module____qualname____doc__stats_cls_identifierr   r   strr   r   r   intr   boolr   r!   r#   r   r&   r(   r)   __classcell__r   r   r   r   r	      s&    
r	   N)typingr   r   r   r   "ray.rllib.utils.metrics.stats.baser   #ray.rllib.utils.metrics.stats.utilsr   ray.util.annotationsr   r	   r   r   r   r   <module>   s    