o
    9wi?                     @   s  d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
mZmZmZ d dlmZmZ d dlmZmZmZ d dlmZ d dlmZ d dlmZmZmZ eeeef ZG d	d
 d
ee ZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd deZ$e$e%e!de$e&e de$e&e#de$e'e"ddZ(e	d Z)G dd deZ*de+ddfddZ,dee deeee-f  fddZ.dS )     N)Iterable)
AnyCallableDictIteratorListLiteralOptionalTupleTypeUnion)_BaseDataLoaderIter_MultiProcessingDataLoaderIter)Self	TypedDictoverride	sized_len)	_Stateful)_map_and_unflatten_tree_flattentree_unflattenc                	   @   s   e Zd Zddee deeeeef   ddfddZ	e
defddZe
defd	d
ZdefddZdddZdeeef fddZdS )_ModeIteratorN	iterableslimitsreturnc                 C   sR   |d urt |t |krtdt | dt | d|| _g | _d| _|| _d S )NMismatch in number of limits () and number of iterables ()r   )len
ValueErrorr   	iterators_idxr   selfr   r    r%   h/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/lightning/pytorch/utilities/combined_loader.py__init__   s   
z_ModeIterator.__init__c                 C      t NNotImplementedErrorr$   r%   r%   r&   __next__%   s   z_ModeIterator.__next__c                 C   s   dd | j D | _d| _| S )Nc                 S      g | ]}t |qS r%   )iter.0iterabler%   r%   r&   
<listcomp>+       z*_ModeIterator.__iter__.<locals>.<listcomp>r   )r   r!   r"   r,   r%   r%   r&   __iter__)   s   z_ModeIterator.__iter__c                 C   r(   r)   r*   r,   r%   r%   r&   __len__/   s   z_ModeIterator.__len__c                 C   s   g | _ d| _d S Nr   r!   r"   r,   r%   r%   r&   reset2   s   
z_ModeIterator.resetc                 C   s,   | j  }dd t| j|d D |d< |S )Nc                 S   s"   g | ]\}}t |trd n|qS r)   )
isinstancer   )r1   iteratoriterator_stater%   r%   r&   r3   ;   s    z._ModeIterator.__getstate__.<locals>.<listcomp>r!   )__dict__copyzipr!   )r$   stater%   r%   r&   __getstate__6   s
   

z_ModeIterator.__getstate__r)   r   N)__name__
__module____qualname__r   r   r	   r   intfloatr'   r   _ITERATOR_RETURNr-   r   r5   r6   r9   r   strr   rA   r%   r%   r%   r&   r      s    ,
r   c                	       s   e Zd Zddee deeeeef   ddf fddZ	e
defddZe
def fd	d
Ze
defddZe
d fddZ  ZS )_MaxSizeCycleNr   r   r   c                    s   t  || g | _d S r)   )superr'   	_consumedr#   	__class__r%   r&   r'   D      
z_MaxSizeCycle.__init__c              	   C   s   t | j}d g| }t|D ]4}zt| j| ||< W q tyB   d| j|< t| jr- t| j| | j|< t| j| ||< Y qw | j	}|  j	d7  _	||dfS )NT   r   )
r   r!   rangenextStopIterationrL   allr/   r   r"   )r$   noutiindexr%   r%   r&   r-   H   s   




z_MaxSizeCycle.__next__c                    s    t    dgt| j | _| S )NF)rK   r5   r   r   rL   r,   rM   r%   r&   r5   Z   s   
z_MaxSizeCycle.__iter__c                 C   6   t | j}| jd urtdd t|| jD S t|S )Nc                 s       | ]
\}}t ||V  qd S r)   minr1   lengthlimitr%   r%   r&   	<genexpr>d       z(_MaxSizeCycle.__len__.<locals>.<genexpr>_get_iterables_lengthsr   r   maxr?   r$   lengthsr%   r%   r&   r6   `      

z_MaxSizeCycle.__len__c                    s   t    g | _d S r)   )rK   r9   rL   r,   rM   r%   r&   r9   g      

z_MaxSizeCycle.resetr)   rB   )rC   rD   rE   r   r   r	   r   rF   rG   r'   r   rH   r-   r   r5   r6   r9   __classcell__r%   r%   rM   r&   rJ   C   s    0rJ   c                   @   0   e Zd ZedefddZedefddZdS )_MinSizer   c                 C   s.   dd | j D }| j}|  jd7  _||dfS )Nc                 S   r.   r%   )rR   )r1   itr%   r%   r&   r3   p   r4   z%_MinSize.__next__.<locals>.<listcomp>rP   r   r8   )r$   rV   rX   r%   r%   r&   r-   n   s   
z_MinSize.__next__c                 C   s*   t | j}| jd urt|| j S t|S r)   )rc   r   r   r\   re   r%   r%   r&   r6   u   s   
 z_MinSize.__len__NrC   rD   rE   r   rH   r-   rF   r6   r%   r%   r%   r&   rk   m   s
    rk   c                	       s   e Zd Zddee deeeeef   ddf fddZ	e
defddZe
defd	d
Ze
defddZe
d fddZdddZdddZ  ZS )_SequentialNr   r   r   c                    s   t  || d| _d S r7   )rK   r'   _iterator_idxr#   rM   r%   r&   r'   |   rO   z_Sequential.__init__c                 C   s   t | j}|dks| j|krt| jd ur2| j| j | jkr2|   | j|kr)t| j| j | jksz	t| jd }W n tyL   |   | 	  Y S w | j}|  jd7  _||| jfS )Nr   rP   )
r   r   ro   rS   r   r"   _use_next_iteratorrR   r!   r-   )r$   rU   rV   rX   r%   r%   r&   r-      s$   


z_Sequential.__next__c                 C   s   d| _ d| _|   | S r7   ro   r"   _load_current_iteratorr,   r%   r%   r&   r5      s   z_Sequential.__iter__c                 C   rY   )Nc                 s   rZ   r)   r[   r]   r%   r%   r&   r`      ra   z&_Sequential.__len__.<locals>.<genexpr>)rc   r   r   sumr?   re   r%   r%   r&   r6      rg   z_Sequential.__len__c                    s   t    d| _d S r7   )rK   r9   ro   r,   rM   r%   r&   r9      rh   z_Sequential.resetc                 C   s2   | j t| jk rt| j| j  g| _d S g | _d S r)   )ro   r   r   r/   r!   r,   r%   r%   r&   rr      s   
z"_Sequential._load_current_iteratorc                 C   s    |  j d7  _ d| _|   d S )NrP   r   rq   r,   r%   r%   r&   rp      s   z_Sequential._use_next_iteratorr)   rB   )rC   rD   rE   r   r   r	   r   rF   rG   r'   r   rH   r-   r   r5   r6   r9   rr   rp   ri   r%   r%   rM   r&   rn   {   s    0
rn   c                   @   rj   )_MaxSizer   c              	   C   s   t | j}d g| }d}t|D ]"}tt t| j| ||< d}W d    n1 s-w   Y  q|r7t| j}|  jd7  _||dfS )NTFrP   r   )r   r!   rQ   
contextlibsuppressrS   rR   r"   )r$   rU   rV   all_exhaustedrW   rX   r%   r%   r&   r-      s   


z_MaxSize.__next__c                 C   rY   )Nc                 s   rZ   r)   r[   r]   r%   r%   r&   r`      ra   z#_MaxSize.__len__.<locals>.<genexpr>rb   re   r%   r%   r&   r6      rg   z_MaxSize.__len__Nrm   r%   r%   r%   r&   rt      s
    rt   c                   @   s0   e Zd ZU eee gef ed< ee ed< dS )_CombinationModefnr;   N)	rC   rD   rE   r   r   rF   __annotations__r   r   r%   r%   r%   r&   rx      s   
 rx   )ry   r;   )min_sizemax_size_cyclemax_size
sequentialc                
   @   sb  e Zd ZdZd&dededdfddZedefd	d
ZedefddZ	edefddZ
edee fddZejdee ddfddZedeeeeef   fddZejdeeeeeeeef  f  ddfddZdefddZedefddZdefddZd'ddZdefdd Zdeeeef  fd!d"Zd#eeeef  ddfd$d%ZdS )(CombinedLoadera  Combines different iterables under specific sampling modes.

    Args:
        iterables: the iterable or collection of iterables to sample from.
        mode: the mode to use. The following modes are supported:

            * ``min_size``: stops after the shortest iterable (the one with the lowest number of items) is done.
            * ``max_size_cycle``: stops after the longest iterable (the one with most items) is done, while cycling
              through the rest of the iterables.
            * ``max_size``: stops after the longest iterable (the one with most items) is done, while returning None
              for the exhausted iterables.
            * ``sequential``: completely consumes each iterable sequentially, and returns a triplet
              ``(data, idx, iterable_idx)``

    Examples:
        >>> from torch.utils.data import DataLoader
        >>> iterables = {'a': DataLoader(range(6), batch_size=4),
        ...              'b': DataLoader(range(15), batch_size=5)}
        >>> combined_loader = CombinedLoader(iterables, 'max_size_cycle')
        >>> _ = iter(combined_loader)
        >>> len(combined_loader)
        3
        >>> for batch, batch_idx, dataloader_idx in combined_loader:
        ...     print(f"{batch}, {batch_idx=}, {dataloader_idx=}")
        {'a': tensor([0, 1, 2, 3]), 'b': tensor([0, 1, 2, 3, 4])}, batch_idx=0, dataloader_idx=0
        {'a': tensor([4, 5]), 'b': tensor([5, 6, 7, 8, 9])}, batch_idx=1, dataloader_idx=0
        {'a': tensor([0, 1, 2, 3]), 'b': tensor([10, 11, 12, 13, 14])}, batch_idx=2, dataloader_idx=0

        >>> combined_loader = CombinedLoader(iterables, 'max_size')
        >>> _ = iter(combined_loader)
        >>> len(combined_loader)
        3
        >>> for batch, batch_idx, dataloader_idx in combined_loader:
        ...     print(f"{batch}, {batch_idx=}, {dataloader_idx=}")
        {'a': tensor([0, 1, 2, 3]), 'b': tensor([0, 1, 2, 3, 4])}, batch_idx=0, dataloader_idx=0
        {'a': tensor([4, 5]), 'b': tensor([5, 6, 7, 8, 9])}, batch_idx=1, dataloader_idx=0
        {'a': None, 'b': tensor([10, 11, 12, 13, 14])}, batch_idx=2, dataloader_idx=0

        >>> combined_loader = CombinedLoader(iterables, 'min_size')
        >>> _ = iter(combined_loader)
        >>> len(combined_loader)
        2
        >>> for batch, batch_idx, dataloader_idx in combined_loader:
        ...     print(f"{batch}, {batch_idx=}, {dataloader_idx=}")
        {'a': tensor([0, 1, 2, 3]), 'b': tensor([0, 1, 2, 3, 4])}, batch_idx=0, dataloader_idx=0
        {'a': tensor([4, 5]), 'b': tensor([5, 6, 7, 8, 9])}, batch_idx=1, dataloader_idx=0

        >>> combined_loader = CombinedLoader(iterables, 'sequential')
        >>> _ = iter(combined_loader)
        >>> len(combined_loader)
        5
        >>> for batch, batch_idx, dataloader_idx in combined_loader:
        ...     print(f"{batch}, {batch_idx=}, {dataloader_idx=}")
        tensor([0, 1, 2, 3]), batch_idx=0, dataloader_idx=0
        tensor([4, 5]), batch_idx=1, dataloader_idx=0
        tensor([0, 1, 2, 3, 4]), batch_idx=0, dataloader_idx=1
        tensor([5, 6, 7, 8, 9]), batch_idx=1, dataloader_idx=1
        tensor([10, 11, 12, 13, 14]), batch_idx=2, dataloader_idx=1

    r{   r   moder   Nc                 C   sN   |t vrtd|dtt  d|| _t|\| _| _|| _d | _d | _	d S )NzUnsupported mode z, please select one of: .)
_SUPPORTED_MODESr    list
_iterablesr   
_flattened_spec_mode	_iterator_limits)r$   r   r   r%   r%   r&   r'     s   
zCombinedLoader.__init__c                 C      | j S )z,Return the original collection of iterables.)r   r,   r%   r%   r&   r   %     zCombinedLoader.iterablesc                 C      t dd | j| jS )z:Return a collections of samplers extracted from iterables.c                 S      t | dd S )Nsamplergetattrxr%   r%   r&   <lambda>-      z(CombinedLoader.sampler.<locals>.<lambda>r   	flattenedr   r,   r%   r%   r&   r   *     zCombinedLoader.samplerc                 C   r   )z@Return a collections of batch samplers extracted from iterables.c                 S   r   )Nbatch_samplerr   r   r%   r%   r&   r   2  r   z.CombinedLoader.batch_sampler.<locals>.<lambda>r   r,   r%   r%   r&   r   /  r   zCombinedLoader.batch_samplerc                 C   r   )z"Return the flat list of iterables.)r   r,   r%   r%   r&   r   4  r   zCombinedLoader.flattenedr   c                 C   sJ   t |t | jkrtdt | dt | j dt|| j| _|| _dS )z4Setter to conveniently update the list of iterables.zMismatch in flattened length (z) and existing length (r   N)r   r   r    r   r   r   )r$   r   r%   r%   r&   r   9  s   
c                 C   r   )zOptional limits per iterator.)r   r,   r%   r%   r&   r   D  r   zCombinedLoader.limitsr   c                 C   sf   t |ttfr|gt| j }nt |tr.t|t| jkr.tdt| dt| j d|| _d S )Nr   r   r   )r:   rF   rG   r   r   r   r    r   )r$   r   r%   r%   r&   r   I  s   
c                 C   sD   | j d usJ t| j }t| j tr|S |\}}}t|| j||fS r)   )r   rR   r:   rn   r   r   )r$   rV   	batch_idxdataloader_idxr%   r%   r&   r-   S  s   

zCombinedLoader.__next__c                 C   s.   t | j d }|| j| j}t| || _| S )Nr;   )r   r   r   r   r/   r   )r$   clsr;   r%   r%   r&   r5   [  s
   zCombinedLoader.__iter__c                 C   s   | j du r	tdt| j S )zCompute the number of batches.Nz*Please call `iter(combined_loader)` first.)r   RuntimeErrorr   r,   r%   r%   r&   r6   c  s   

zCombinedLoader.__len__c                 C   s2   | j dur| j   d| _ | jD ]}t| qdS )z)Reset the state and shutdown any workers.N)r   r9   r   $_shutdown_workers_and_reset_iterator)r$   r2   r%   r%   r&   r9   i  s   



zCombinedLoader.resetc                    sD   dd | j D } fdd|D }|stdt| j d }||S )zGCompute the total length of the datasets according to the current mode.c                 S   s   g | ]}t |d dqS )datasetNr   )r1   dlr%   r%   r&   r3   s  s    z2CombinedLoader._dataset_length.<locals>.<listcomp>c                    s    g | ]}t |  d ur qS r)   r   )r1   dsr^   r%   r&   r3   t  s     z)All datasets are iterable-style datasets.ry   )r   r+   r   r   )r$   datasetsrf   ry   r%   r   r&   _dataset_lengthq  s   zCombinedLoader._dataset_lengthc                 C   s   dd | j D S )zTReturns the list of state dicts for iterables in `self.flattened` that are stateful.c                 S   s   g | ]}t |tr| qS r%   )r:   r   
state_dictr1   loaderr%   r%   r&   r3   |  s    z/CombinedLoader._state_dicts.<locals>.<listcomp>)r   r,   r%   r%   r&   _state_dictsz  s   zCombinedLoader._state_dictsstatesc                 C   sh   |sdS dd | j D }t|t|kr#tdt| dt| dt||D ]	\}}|| q(dS )zJLoads the state dicts for iterables in `self.flattened` that are stateful.Nc                 S   s   g | ]	}t |tr|qS r%   )r:   r   r   r%   r%   r&   r3     s    z4CombinedLoader._load_state_dicts.<locals>.<listcomp>zThe CombinedLoader has z stateful loaders, but found zv states in the checkpoint. Please make sure you define the same dataloaders that were used when saving the checkpoint.)r   r   r   r?   load_state_dict)r$   r   stateful_loadersr   r   r%   r%   r&   _load_state_dicts~  s   z CombinedLoader._load_state_dicts)r{   rB   )rC   rD   rE   __doc__r   _LITERAL_SUPPORTED_MODESr'   propertyr   r   r   r   r   setterr	   r   rF   rG   r   rH   r-   r   r   r5   r6   r9   r   r   rI   r   r   r%   r%   r%   r&   r      s2    =	
 .	
	"r   
dataloaderr   c                 C   s.   t | drt| jtr| j  d | _d S d S )Nr   )hasattrr:   r   r   _shutdown_workers)r   r%   r%   r&   r     s
   


r   r   c                    s    fdd| D S )Nc                    s(   g | ]}t |  d u rtdn qS )Ninf)r   rG   r0   r   r%   r&   r3     s   ( z*_get_iterables_lengths.<locals>.<listcomp>r%   )r   r%   r   r&   rc     s   rc   )/ru   collections.abcr   typingr   r   r   r   r   r   r	   r
   r   r   torch.utils.data.dataloaderr   r   typing_extensionsr   r   r   lightning.fabric.utilities.datar    lightning.fabric.utilities.typesr   #lightning.pytorch.utilities._pytreer   r   r   rF   rH   r   rJ   rk   rn   rt   rx   r\   rd   rs   r   r   r   objectr   rG   rc   r%   r%   r%   r&   <module>   s2   0'*=



 0&