o
    8wi?                     @   st  d dl Z d dlmZmZ d dl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Iterator)AnyCallableLiteralOptional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/pytorch_lightning/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	   )r-   iteratoriterator_stater!   r!   r"   r/   ;   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__listr   r   r   intfloatr#   r   _ITERATOR_RETURNr)   r   r1   r2   r5   dictstrr   r=   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StopIterationrJ   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)rI   r1   r   r   rJ   r(   rK   r!   r"   r1   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r-   lengthlimitr!   r!   r"   	<genexpr>d       z(_MaxSizeCycle.__len__.<locals>.<genexpr>_get_iterables_lengthsr   r   maxr;   r    lengthsr!   r!   r"   r2   `      

z_MaxSizeCycle.__len__c                    s   t    g | _d S r%   )rI   r5   rJ   r(   rK   r!   r"   r5   g      

z_MaxSizeCycle.resetr%   r>   )r?   r@   rA   rB   r   r   r   rC   rD   r#   r   rE   r)   r   r1   r2   r5   __classcell__r!   r!   rK   r"   rH   C   s    0rH   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!   )rP   )r-   itr!   r!   r"   r/   p   r0   z%_MinSize.__next__.<locals>.<listcomp>rN   r   r4   )r    rT   rV   r!   r!   r"   r)   n   s   
z_MinSize.__next__c                 C   s*   t | j}| jd urt|| j S t|S r%   )ra   r   r   rZ   rc   r!   r!   r"   r2   u   s   
 z_MinSize.__len__Nr?   r@   rA   r   rE   r)   rC   r2   r!   r!   r!   r"   ri   m   s
    ri   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 r3   )rI   r#   _iterator_idxr   rK   r!   r"   r#   |   rM   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   rN   )
r   r   rm   rQ   r   r   _use_next_iteratorrP   r   r)   )r    rS   rT   rV   r!   r!   r"   r)      s$   


z_Sequential.__next__c                 C   s   d| _ d| _|   | S r3   rm   r   _load_current_iteratorr(   r!   r!   r"   r1      s   z_Sequential.__iter__c                 C   rW   )Nc                 s   rX   r%   rY   r[   r!   r!   r"   r^      r_   z&_Sequential.__len__.<locals>.<genexpr>)ra   r   r   sumr;   rc   r!   r!   r"   r2      re   z_Sequential.__len__c                    s   t    d| _d S r3   )rI   r5   rm   r(   rK   r!   r"   r5      rf   z_Sequential.resetc                 C   s2   | j t| jk rt| j| j  g| _d S g | _d S r%   )rm   r   r   r+   r   r(   r!   r!   r"   rp      s   
z"_Sequential._load_current_iteratorc                 C   s    |  j d7  _ d| _|   d S )NrN   r   ro   r(   r!   r!   r"   rn      s   z_Sequential._use_next_iteratorr%   r>   )r?   r@   rA   rB   r   r   r   rC   rD   r#   r   rE   r)   r   r1   r2   r5   rp   rn   rg   r!   r!   rK   r"   rl   {   s    0
rl   c                   @   rh   )_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 )NTFrN   r   )r   r   rO   
contextlibsuppressrQ   rP   r   )r    rS   rT   all_exhaustedrU   rV   r!   r!   r"   r)      s   


z_MaxSize.__next__c                 C   rW   )Nc                 s   rX   r%   rY   r[   r!   r!   r"   r^      r_   z#_MaxSize.__len__.<locals>.<genexpr>r`   rc   r!   r!   r"   r2      re   z_MaxSize.__len__Nrk   r!   r!   r!   r"   rr      s
    rr   c                   @   s0   e Zd ZU eee gef ed< ee ed< dS )_CombinationModefnr7   N)	r?   r@   rA   r   rB   rC   __annotations__typer   r!   r!   r!   r"   rv      s   
 rv   )rw   r7   )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

    rz   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   rB   
_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   )r6   rC   rD   r   r   rB   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   rP   r6   rl   r   r   )r    rT   	batch_idxdataloader_idxr!   r!   r"   r)   S  s   

zCombinedLoader.__next__c                 C   s.   t | j d }|| j| j}t| || _| S )Nr7   )r   r   r   r   r+   r   )r    clsr7   r!   r!   r"   r1   [  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"   r2   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   r5   r   $_shutdown_workers_and_reset_iterator)r    r.   r!   r!   r"   r5   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   )r-   dlr!   r!   r"   r/   s  s    z2CombinedLoader._dataset_length.<locals>.<listcomp>c                    s    g | ]}t |  d ur qS r%   r   )r-   dsr\   r!   r"   r/   t  s     z)All datasets are iterable-style datasets.rw   )r   r'   r   r   )r    datasetsrd   rw   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!   )r6   r   
state_dictr-   loaderr!   r!   r"   r/   |  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!   )r6   r   r   r!   r!   r"   r/     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)rz   r>   )r?   r@   rA   __doc__r   _LITERAL_SUPPORTED_MODESr#   propertyr   r   r   rB   r   setterr   r   rC   rD   r   rE   r)   r   r   r1   r2   r5   r   rF   rG   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   )hasattrr6   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   rD   r,   r   r!   r"   r/     s   ( z*_get_iterables_lengths.<locals>.<listcomp>r!   )r   r!   r   r"   ra     s   ra   )-rs   collections.abcr   r   typingr   r   r   r   r   torch.utils.data.dataloaderr	   r
   typing_extensionsr   r   r   lightning_fabric.utilities.datar    lightning_fabric.utilities.typesr   #pytorch_lightning.utilities._pytreer   r   r   tuplerC   rE   r   rH   ri   rl   rr   rv   rZ   rb   rq   r   r   r~   objectr   rB   rD   ra   r!   r!   r!   r"   <module>   s2   '*=



 0&