
    Pi                        d dl mZmZmZmZmZmZmZmZ d dl	m
Z
 d dlmZmZ ddlmZ ddlmZ  edd	          Z G d
 dee                   Zdeeef         de
e         dee         fdZ G d dee                   ZdS )    )AnyCallableDictIterableIteratorMappingOptionalTypeVar)Sampler)BaseNodeT   )Mapper)StatefulKT)	covariantc                        e Zd ZdZdZdZdee         f fdZdde	e
eef                  f fdZdefd	Zde
eef         fd
Z xZS )IterableWrappera  Thin Wrapper that converts any Iterable (including
    torch.utils.data.IterableDataset) in to a BaseNode.

    If iterable implements the Stateful Protocol, it will be saved and restored with its
    state_dict/load_state_dict methods.

    Args:
        iterable (Iterable[T]): Iterable to convert to BaseNode. IterableWrapper calls iter() on it.

    :warning: Note the distinction between state_dict/load_state_dict defined on Iterable, vs Iterator.
        Only the Iterable's state_dict/load_state_dict are used.
    _num_yieldediterablec                 d    t                                                       || _        d | _        d S N)super__init__r   _it)selfr   	__class__s     l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchdata/nodes/adapters.pyr   zIterableWrapper.__init__&   s+     *.    Ninitial_statec           	      T   d| _         d | _        t                                          |           ||| j                 | _         t          | j        t                    r@| j                            || j	                            t          | j                  | _        d S t          | j                  | _        t          | j                   D ]@}	 t          | j                   # t          $ r t          d| j          d| d          w xY wd S t          | j                  | _        d S )Nr   Tried to fast-forward / items during init but hit StopIteration after 4 items, this is likely a bug or malformed state_dict)r   r   r   resetNUM_YIELDED_KEY
isinstancer   r   load_state_dictITERABLE_KEYiterrangenextStopIteration
ValueErrorr   r    ir   s      r   r%   zIterableWrapper.reset+   s>   m$$$$ -d.B CD$-22 --mD<M.NOOO....t011  ATX(   (oT5F o o78o o o    DM**DHHHs   C$$&D
returnc                 N    t          | j                  }| xj        dz  c_        |S Nr   )r,   r   r   r   items     r   r,   zIterableWrapper.nextB   s(    DH~~Qr   c                     | j         | j        i}t          | j        t                    r!| j                                        || j        <   |S r   )r&   r   r'   r   r   
state_dictr)   r   r7   s     r   	get_statezIterableWrapper.get_stateG   sH    &*&:D<M%N
dmX.. 	G,0M,D,D,F,FJt()r   r   )__name__
__module____qualname____doc__r&   r)   r   r   r   r	   r   strr   r%   r,   r9   __classcell__r   s   @r   r   r      s          %OL/! / / / / / /
+ +8DcN#; + + + + + +.a    
4S>        r   r   map_datasetsamplerr1   c                 N    t          |          }t          || j                  }|S )a  Thin Wrapper that converts any MapDataset in to a torchdata.node
    If you want parallelism, copy this and replace Mapper with ParallelMapper.

    Args:
        map_dataset (Mapping[K, T]): - Apply map_dataset.__getitem__ to the outputs of sampler.
        sampler (Sampler[K]):
    )SamplerWrapperr   __getitem__)rA   rB   sampler_nodemapper_nodes       r   MapStyleWrapperrH   N   s)     '5W&=&=L{'>??Kr   c            
            e Zd ZdZdZdZdZ	 	 ddee         de	d	e
ee	ge	f                  f fd
Zdde
eeef                  f fdZdefdZdeeef         fdZede	de	fd            Z xZS )rD   a  
    Convert a sampler into a BaseNode. This is nearly identical to
    IterableWrapper except it includes a hook to call set_epoch on the sampler,
    if it supports it.

    Args:
        sampler (Sampler): Sampler to wrap.
        initial_epoch (int): initial epoch to set on the sampler
        epoch_updater (Optional[Callable[[int], int]] = None): callback to update epoch at start of new iteration. It's called at the beginning of each iterator request, except the first one.
    r   _epoch_samplerr   NrB   initial_epochepoch_updaterc                     t                                                       || _        || _        d| _        d| _        |p| j        | _        d | _        d S )Nr   F)	r   r   rB   epochr   _started_default_epoch_updaterrM   r   )r   rB   rL   rM   r   s       r   r   zSamplerWrapper.__init__k   sS     	"
*Id.I*.r   r    c           	         t                                          |           |!|| j                 | _        || j                 | _        t          | j        t                    r@| j        	                    || j
                            t          | j                  | _        nt          | j        d          r| j                            | j                   t          | j                  | _        t          | j                  D ]@}	 t!          | j                   # t"          $ r t%          d| j         d| d          w xY wnzd| _        | j        r|                     | j                  | _        t          | j        d          r| j                            | j                   t          | j                  | _        d| _        d S )N	set_epochr"   r#   r$   r   F)r   r%   r&   r   	EPOCH_KEYrO   r'   rB   r   r(   SAMPLER_KEYr*   r   hasattrrS   r+   r,   r-   r.   rP   rM   r/   s      r   r%   zSamplerWrapper.resety   s   m$$$$ -d.B CD&t~6DJ$,11 ,,]4;K-LMMM--4<55 7L**4:666--t011  ATX(   (oT5F o o78o o o   !"D} <!//
;;
t|[11 3&&tz222DL))DHs   D&Er1   c                 \    d| _         t          | j                  }| xj        dz  c_        |S )NTr   )rP   r,   r   r   r4   s     r   r,   zSamplerWrapper.next   s/    DH~~Qr   c                     | j         | j        | j        | j        i}t	          | j        t                    r!| j                                        || j        <   |S r   )	r&   r   rT   rO   r'   rB   r   r7   rU   r8   s     r   r9   zSamplerWrapper.get_state   sU     $"3NDJ&

 dlH-- 	E+/<+B+B+D+DJt'(r   rO   c                     |dz   S r3    )clsrO   s     r   rQ   z%SamplerWrapper._default_epoch_updater   s    qyr   )r   Nr   )r:   r;   r<   r=   r&   rT   rU   r   r   intr	   r   r   r   r>   r   r%   r,   r9   classmethodrQ   r?   r@   s   @r   rD   rD   [   s8       	 	 %OIK
 8<	/ // /  # 45	/ / / / / / 8DcN#;      <a    4S>     3 3    [    r   rD   N)typingr   r   r   r   r   r   r	   r
   torch.utils.datar   torchdata.nodes.base_noder   r   mapr   typesr   r   r   rH   rD   rZ   r   r   <module>rc      sQ   W V V V V V V V V V V V V V V V V V V V $ $ $ $ $ $ 1 1 1 1 1 1 1 1            GC4   6 6 6 6 6hqk 6 6 6r
A 
 
QR 
 
 
 
M M M M MXa[ M M M M Mr   