o
    ̳i                     @   s   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d	ZG d
d dee Zdeeef de
e dee fddZG dd dee ZdS )    )AnyCallableDictIterableIteratorMappingOptionalTypeVar)Sampler)BaseNodeT   )Mapper)StatefulKT)	covariantc                       sv   e Zd ZdZdZdZdee f fddZdde	e
eef  f fdd	Zd
efddZd
e
eef fddZ  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                    s   t    || _d | _d S N)super__init__r   _it)selfr   	__class__ L/home/ubuntu/.local/lib/python3.10/site-packages/torchdata/nodes/adapters.pyr   &   s   

zIterableWrapper.__init__Ninitial_statec              
      s   d| _ d | _t | |d urW|| j | _ t| jtr-| j|| j	  t
| j| _d S t
| j| _t| j D ]}zt| j W q8 tyT   td| j  d| d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   r   r   r"   +   s*   
	zIterableWrapper.resetreturnc                 C   s   t | j}|  jd7  _|S Nr   )r)   r   r   r   itemr   r   r   r)   B   s   
zIterableWrapper.nextc                 C   s,   | j | ji}t| jtr| j || j< |S r   )r#   r   r$   r   r   
state_dictr&   r   r2   r   r   r   	get_stateG   s   zIterableWrapper.get_stater   )__name__
__module____qualname____doc__r#   r&   r   r   r   r   r   strr   r"   r)   r4   __classcell__r   r   r   r   r      s     r   map_datasetsamplerr.   c                 C   s   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__)r;   r<   sampler_nodemapper_noder   r   r   MapStyleWrapperN   s   rA   c                
       s   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
dZdde
eeef  f fddZdefddZdeeef fddZede	de	fddZ  ZS )r=   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   Nr<   initial_epochepoch_updaterc                    s8   t    || _|| _d| _d| _|p| j| _d | _d S )Nr   F)	r   r   r<   epochr   _started_default_epoch_updaterrE   r   )r   r<   rD   rE   r   r   r   r   k   s   

zSamplerWrapper.__init__r   c              
      s  t  | |d urb|| j | _|| j | _t| jtr,| j	|| j
  t| j| _nVt| jdr9| j| j t| j| _t| jD ]}zt| j W qD ty`   td| j d| dw n d| _| jro| | j| _t| jdr|| j| j t| j| _d| _d S )N	set_epochr   r    r!   r   F)r   r"   r#   r   	EPOCH_KEYrF   r$   r<   r   r%   SAMPLER_KEYr'   r   hasattrrI   r(   r)   r*   r+   rG   rE   r,   r   r   r   r"   y   s8   
	
zSamplerWrapper.resetr.   c                 C   s"   d| _ t| j}|  jd7  _|S )NTr   )rG   r)   r   r   r0   r   r   r   r)      s   
zSamplerWrapper.nextc                 C   s4   | j | j| j| ji}t| jtr| j || j< |S r   )	r#   r   rJ   rF   r$   r<   r   r2   rK   r3   r   r   r   r4      s   zSamplerWrapper.get_staterF   c                 C   s   |d S r/   r   )clsrF   r   r   r   rH      s   z%SamplerWrapper._default_epoch_updater)r   Nr   )r5   r6   r7   r8   r#   rJ   rK   r
   r   intr   r   r   r   r9   r   r"   r)   r4   classmethodrH   r:   r   r   r   r   r=   [   s&     	r=   N)typingr   r   r   r   r   r   r   r	   torch.utils.datar
   torchdata.nodes.base_noder   r   mapr   typesr   r   r   rA   r=   r   r   r   r   <module>   s   (&9