
    &`i                         d dl mZ d dlmZ d dlmZmZ e G d d                      ZdedefdZ	ded	edefd
Z
dedededefdZdS )    )List)OldAPIStack)TensorStructType
TensorTypec                   z    e Zd ZdZdedee         defdZdeee                  fdZdee         fdZ	d	 Z
d
 ZdS )RepeatedValuesa  Represents a variable-length list of items from spaces.Repeated.

    RepeatedValues are created when you use spaces.Repeated, and are
    accessible as part of input_dict["obs"] in ModelV2 forward functions.

    Example:
        Suppose the gym space definition was:
            Repeated(Repeated(Box(K), N), M)

        Then in the model forward function, input_dict["obs"] is of type:
            RepeatedValues(RepeatedValues(<Tensor shape=(B, M, N, K)>))

        The tensor is accessible via:
            input_dict["obs"].values.values

        And the actual data lengths via:
            # outer repetition, shape [B], range [0, M]
            input_dict["obs"].lengths
                -and-
            # inner repetition, shape [B, M], range [0, N]
            input_dict["obs"].values.lengths

    Attributes:
        values: The padded data tensor of shape [B, max_len, ..., sz],
            where B is the batch dimension, max_len is the max length of this
            list, followed by any number of sub list max lens, followed by the
            actual data size.
        lengths (List[int]): Tensor of shape [B, ...] that represents the
            number of valid items in each list. When the list is nested within
            other lists, there will be extra dimensions for the parent list
            max lens.
        max_len: The max number of items allowed in each list.

    TODO(ekl): support conversion to tf.RaggedTensor.
    valueslengthsmax_lenc                 >    || _         || _        || _        d | _        d S N)r	   r
   r   _unbatched_repr)selfr	   r
   r   s       t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/models/repeated_values.py__init__zRepeatedValues.__init__-   s%    #    returnc           	      h   | j         $t          | j                  }|t          d          t	          |          }|                                 }g }t          |          D ]}t          | j        |         d          r-t	          | j        |         	                                          }n,t	          | j        |         
                                          }g }t          |          D ]&}|                    t          |||                     '|                    |           || _         | j         S )a  Unbatch both the repeat and batch dimensions into Python lists.

        This is only supported in PyTorch / TF eager mode.

        This lets you view the data unbatched in its original form, but is
        not efficient for processing.

        .. testcode::
            :skipif: True

            batch = RepeatedValues(<Tensor shape=(B, N, K)>)
            items = batch.unbatch_all()
            print(len(items) == B)

        .. testoutput::

            True

        .. testcode::
            :skipif: True

            print(max(len(x) for x in items) <= N)

        .. testoutput::

            True

        .. testcode::
            :skipif: True

            print(items)

        .. testoutput::

            [[<Tensor_1 shape=(K)>, ..., <Tensor_N, shape=(K)>],
             ...
             [<Tensor_1 shape=(K)>, <Tensor_2 shape=(K)>],
             ...
             [<Tensor_1 shape=(K)>],
             ...
             [<Tensor_1 shape=(K)>, ..., <Tensor_N shape=(K)>]]
        NzjCannot call unbatch_all() when batch_dim is unknown. This is probably because you are using TF graph mode.item)r   _get_batch_dim_helperr	   
ValueErrorintunbatch_repeat_dimrangehasattrr
   r   numpyappend_batch_index_helper)r   Bslicesresultidynamic_lendynamic_slicejs           r   unbatch_allzRepeatedValues.unbatch_all3   s2   X '%dk22Ay L  
 FF,,..FF1XX - -4<?F33 ?"%dl1o&:&:&<&<"="=KK"%dl1o&;&;&=&=">">K "{++ L LA!(()<VQ)J)JKKKKm,,,,#)D ##r   c                 6    t          | j        | j                  S )ak  Unbatches the repeat dimension (the one `max_len` in size).

        This removes the repeat dimension. The result will be a Python list of
        with length `self.max_len`. Note that the data is still padded.

        .. testcode::
            :skipif: True

            batch = RepeatedValues(<Tensor shape=(B, N, K)>)
            items = batch.unbatch()
            len(items) == batch.max_len

        .. testoutput::

            True

        .. testcode::
            :skipif: True

            print(items)

        .. testoutput::

            [<Tensor_1 shape=(B, K)>, ..., <Tensor_N shape=(B, K)>]
        )_unbatch_helperr	   r   r   s    r   r   z!RepeatedValues.unbatch_repeat_dimw   s    4 t{DL999r   c                     d                     t          | j                  t          | j                  | j                  S )Nz0RepeatedValues(value={}, lengths={}, max_len={}))formatreprr	   r
   r   r)   s    r   __repr__zRepeatedValues.__repr__   s7    AHHtDL114<
 
 	
r   c                      t          |           S r   )r,   r)   s    r   __str__zRepeatedValues.__str__   s    Dzzr   N)__name__
__module____qualname____doc__r   r   r   r   r&   r   r-   r/    r   r   r   r      s        " "H$z $DI $ $ $ $ $B$T$z"23 B$ B$ B$ B$H:D$4 : : : :8
 
 

    r   r   vr   c                 p   t          | t                    r)|                                 D ]}t          |          c S dS t          | t                    rt          | d                   S t          | t
                    rt          | j                  S | j        d         }t          |d          r|j        }|S )z5Tries to find the batch dimension size of v, or None.r   valueN)	
isinstancedictr	   r   tupler   shaper   r7   )r5   ur   s      r   r   r      s    !T  	, 	,A(+++++	, 	,	Au		 $QqT***	A~	&	& $QX...GAJ1g 	Ar   r   c                     t           t                    r fd                                 D             S t           t                    rt          fd D                       S t           t                    r0t           j                  } fdt          |          D             S  fdt                    D             S )z3Recursively unpacks the repeat dimension (max_len).c                 8    i | ]\  }}|t          |          S r4   r(   ).0kr<   r   s      r   
<dictcomp>z#_unbatch_helper.<locals>.<dictcomp>   s)    GGG6Aq?1g..GGGr   c              3   8   K   | ]}t          |          V  d S r   r?   )r@   r<   r   s     r   	<genexpr>z"_unbatch_helper.<locals>.<genexpr>   s-      <<Q_Q00<<<<<<r   c           	      b    g | ]+\  }}t          |j        d d |df         j                  ,S N.)r   r
   r   )r@   r"   r<   r5   s      r   
<listcomp>z#_unbatch_helper.<locals>.<listcomp>   sL     
 
 
1 1ai1c	2AI>>
 
 
r   c                 *    g | ]}d d |df         S rF   r4   )r@   r"   r5   s     r   rG   z#_unbatch_helper.<locals>.<listcomp>   s'    555!!!Q)555r   )	r8   r9   itemsr:   r   r(   r	   	enumerater   )r5   r   	unbatcheds   `` r   r(   r(      s    !T 6GGGGQWWYYGGGG	Au		 	6<<<<!<<<<<<	A~	&	& 6#AHg66	
 
 
 
!),,
 
 
 	

 6555eGnn5555r   r"   r%   c                    t          | t                    r!fd|                                 D             S t          | t                    rt          fd| D                       S t          | t                    rt          |                    S t          | t                    r|                                 }|         S | df         S )z;Selects the item at the ith batch index and jth repetition.c                 :    i | ]\  }}|t          |          S r4   r   )r@   rA   r<   r"   r%   s      r   rB   z'_batch_index_helper.<locals>.<dictcomp>   s,    HHHFQ&q!Q//HHHr   c              3   :   K   | ]}t          |          V  d S r   rN   )r@   r<   r"   r%   s     r   rD   z&_batch_index_helper.<locals>.<genexpr>   s0      ==a(Aq11======r   .)r8   r9   rI   r:   listr   r   r&   )r5   r"   r%   rK   s    `` r   r   r      s    !T HHHHHaggiiHHHH	Au		 =====1======	At		 
 #1Q4A...	A~	&	& MMOO	|Cyr   N)typingr   ray.rllib.utils.annotationsr   ray.rllib.utils.typingr   r   r   r   r   r(   r   r4   r   r   <module>rT      s         3 3 3 3 3 3 ? ? ? ? ? ? ? ? Q Q Q Q Q Q Q Qh- #     6' 6# 6:J 6 6 6 6 +   @P      r   