o
    `۷i                     @   sj   d dl Z d dlmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ e \ZZeG dd de
ZdS )    N)AnyDictUnion)try_import_torch)SeriesStats)DeveloperAPIc                       s  e Zd ZdZdZdejdefddZd#dd	Z	d$de
f fddZdeddfddZede
fddZedefddZd%de
deed f f fddZed&deeef 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  ZS )'SumStatszPA Stats object that tracks the sum of a series of singular values (not vectors).sumvaluesreturnc                 C   s
   t |S N)npnansum)selfr
    r   W/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/rllib/utils/metrics/stats/sum.py_np_reduce_fn   s   
zSumStats._np_reduce_fntorch.Tensorc                 C   s:   |t |  }t|dkrt jd|jdS t | S )z1Reduce function for torch tensors (stays on GPU).r   g        )device)torchisnanlentensorr   r	   float)r   r
   clean_valuesr   r   r   _torch_reduce_fn   s   zSumStats._torch_reduce_fnFwith_throughputc                    s&   t  jdi | || _t | _dS )zInitializes a SumStats instance.

        Args:
            throughput: If True, track a throughput estimate based on the time between consecutive calls to reduce().
        Nr   )super__init__track_throughputtimeperf_counter_last_throughput_measure_time)r   r   kwargs	__class__r   r   r      s   zSumStats.__init__r    Nc                 C   s   | j sJ d|| _d S )NzEinitialize_throughput_reference_time can only be called on root stats)is_rootr"   )r   r    r   r   r   $initialize_throughput_reference_time+   s
   
z-SumStats.initialize_throughput_reference_timec                 C   s   | j S r   )r   r   r   r   r   has_throughputs1   s   zSumStats.has_throughputsc                 C   s(   | j sJ d| jddt | j  S )z-Returns the throughput since the last reduce.z7Throughput tracking is not enabled on this Stats objectTcompile)r   peekr    r!   r"   r(   r   r   r   throughputs5   s   
zSumStats.throughputsTr+   c                    s:   t  jdd}| jrt | _|r|S |  }|g|_|S )NTr*   )r   reducer   r    r!   r"   cloner
   )r   r+   reduce_valuereturn_statsr$   r   r   r.   @   s   
zSumStats.reducec                 C   sL   t j| |d}|duri |d|d iS | dur"i |d| jiS td)z;Returns the initialization arguments for this Stats object.)stats_objectstateNr   r   z-Either stats_object or state must be provided)r   _get_init_argsr   
ValueError)r2   r3   
super_argsr   r   r   r4   N   s   zSumStats._get_init_argsc                    s   t   }| j|d< |S )z&Returns the state of the stats object.r   )r   	get_stater   r   r3   r$   r   r   r7   _   s   

zSumStats.get_stater3   c                    s   t  | |d | _d S )Nr   )r   	set_stater   r8   r$   r   r   r9   e   s   zSumStats.set_statec                 C   s"   d|    d| j dt|  dS )Nz	SumStats(z	; window=z; len=))r,   _windowr   r(   r   r   r   __repr__i   s   "zSumStats.__repr__)r
   r   )F)T)NN)__name__
__module____qualname____doc__stats_cls_identifierr   ndarrayr   r   r   boolr   r'   propertyr)   r-   r   r   r.   staticmethodr   strr4   r7   r9   r<   __classcell__r   r   r$   r   r      s"    
 
r   )r    typingr   r   r   numpyr   ray.rllib.utils.frameworkr   $ray.rllib.utils.metrics.stats.seriesr   ray.util.annotationsr   r   _r   r   r   r   r   <module>   s    
