
    &`im	                         d dl 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  e            \  ZZ e            \  ZZZe G d de	                      ZdS )    )AnyUnionN)try_import_tftry_import_torch)SeriesStats)DeveloperAPIc                   v    e Zd ZdZdZdej        defdZddZ	de
dd	fd
Zddedee
d f         fdZdefdZd	S )	MeanStatsa  A Stats object that tracks the mean of a series of singular values (not vectors).

    Note the following limitation: When merging multiple MeanStats objects, the resulting mean is not the true mean of all values.
    Instead, it is the mean of the means of the incoming MeanStats objects.
    This is because we calculate the mean in parallel components and potentially merge them multiple times in one reduce cycle.
    The resulting mean of means may differ significantly from the true mean, especially if some incoming means are the result of few outliers.

    Example to illustrate this limitation:
    First incoming mean: [1, 2, 3, 4, 5] -> 3
    Second incoming mean: [15] -> 15
    Mean of both merged means: [3, 15] -> 9
    True mean of all values: [1, 2, 3, 4, 5, 15] -> 5
    meanvaluesreturnc                 *    t          j        |          S )N)npnanmeanselfr   s     v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/utils/metrics/stats/mean.py_np_reduce_fnzMeanStats._np_reduce_fn   s    z&!!!    torch.Tensorc                 Z    t                               |                                          S )z1Reduce function for torch tensors (stays on GPU).)torchr   floatr   s     r   _torch_reduce_fnzMeanStats._torch_reduce_fn"   s    }}V\\^^,,,r   valueNc                     t           r.t                               |          r|                                }| j                            |           dS )a  Pushes a value into this Stats object.

        Args:
            value: The value to be pushed. Can be of any type.
                PyTorch GPU tensors are kept on GPU until reduce() or peek().
                TensorFlow tensors are moved to CPU immediately.
        N)tf	is_tensornumpyr   append)r   r   s     r   pushzMeanStats.push&   sH      	"",,u%% 	"KKMME5!!!!!r   Tcompilec                     |                                  }|                     g            |r|d         S |                                 }||_        |S )Nr   )window_reduce_set_valuescloner   )r   r"   reduced_valuesreturn_statss       r   reducezMeanStats.reduce4   sU    ++-- 	%!!$$zz||,r   c                 b    d|                                   d| j         dt          |            dS )Nz
MeanStats(z	; window=z; len=))peek_windowlen)r   s    r   __repr__zMeanStats.__repr__?   s3    RDIIKKRR$,RRc$iiRRRRr   )r   r   )T)__name__
__module____qualname____doc__stats_cls_identifierr   ndarrayr   r   r   r   r!   boolr   r)   strr/    r   r   r
   r
      s          ""BJ "5 " " " "- - - -"# "$ " " " "	 	d 	eC4D.E 	 	 	 	S# S S S S S Sr   r
   )typingr   r   r   r   ray.rllib.utils.frameworkr   r   $ray.rllib.utils.metrics.stats.seriesr   ray.util.annotationsr   r   _r   r
   r8   r   r   <module>r>      s                E E E E E E E E < < < < < < - - - - - -q=??2q 2S 2S 2S 2S 2S 2S 2S 2S 2S 2Sr   