3
I4W^R´  ã               @   sæ   d Z ddlZddlZddljZddlmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlZddlZddlmZmZ ejjZejj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G dd„ deƒZdS )zÿDefinition of the DataLoader and it's iterator _DataLoaderIter classes.

To support these two classes, in `./_utils` we define many utility methods and
functions to be run in multiprocessing. E.g., the data loading worker loop is
in `./_utils/worker.py`.
é    Né   )ÚIterableDatasetÚSamplerÚSequentialSamplerÚRandomSamplerÚBatchSampler)Ú_utils)ÚExceptionWrapper)ÚqueueÚstring_classesc               @   s    e Zd ZdZdZedd„ ƒZdS )Ú_DatasetKindr   r   c             C   s2   | t jkrtjj||||ƒS tjj||||ƒS d S )N)r   ÚMapr   ÚfetchZ_MapDatasetFetcherZ_IterableDatasetFetcher)ÚkindÚdatasetZauto_collationÚ
collate_fnÚ	drop_last© r   ú</tmp/pip-build-e66j97b2/torch/torch/utils/data/dataloader.pyÚcreate_fetcher!   s    
z_DatasetKind.create_fetcherN)Ú__name__Ú
__module__Ú__qualname__r   ÚIterableÚstaticmethodr   r   r   r   r   r      s   r   c                   s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )Ú_InfiniteConstantSamplerzwAnalogous to ``itertools.repeat(None, None)``.
    Used as sampler for :class:`~torch.utils.data.IterableDataset`.
    c                s   t t| ƒjd ƒ d S )N)Úsuperr   Ú__init__)Úself)Ú	__class__r   r   r   .   s    z!_InfiniteConstantSampler.__init__c             c   s   x
d V  qW d S )Nr   )r   r   r   r   Ú__iter__1   s    z!_InfiniteConstantSampler.__iter__c             C   s   t dƒ‚d S )Nz;Cannot determine the DataLoader length of a IterableDataset)Ú	TypeError)r   r   r   r   Ú__len__5   s    z _InfiniteConstantSampler.__len__)r   r   r   Ú__doc__r   r    r"   Ú__classcell__r   r   )r   r   r   )   s   r   c                   sp   e Zd ZdZdZddd„Zedd	„ ƒZejd
d	„ ƒZ‡ fdd„Z	dd„ Z
edd„ ƒZedd„ ƒZdd„ Z‡  ZS )Ú
DataLoadera5  
    Data loader. Combines a dataset and a sampler, and provides an iterable over
    the given dataset.

    The :class:`~torch.utils.data.DataLoader` supports both map-style and
    iterable-style datasets with single- or multi-process loading, customizing
    loading order and optional automatic batching (collation) and memory pinning.

    See :py:mod:`torch.utils.data` documentation page for more details.

    Arguments:
        dataset (Dataset): dataset from which to load the data.
        batch_size (int, optional): how many samples per batch to load
            (default: ``1``).
        shuffle (bool, optional): set to ``True`` to have the data reshuffled
            at every epoch (default: ``False``).
        sampler (Sampler, optional): defines the strategy to draw samples from
            the dataset. If specified, :attr:`shuffle` must be ``False``.
        batch_sampler (Sampler, optional): like :attr:`sampler`, but returns a batch of
            indices at a time. Mutually exclusive with :attr:`batch_size`,
            :attr:`shuffle`, :attr:`sampler`, and :attr:`drop_last`.
        num_workers (int, optional): how many subprocesses to use for data
            loading. ``0`` means that the data will be loaded in the main process.
            (default: ``0``)
        collate_fn (callable, optional): merges a list of samples to form a
            mini-batch of Tensor(s).  Used when using batched loading from a
            map-style dataset.
        pin_memory (bool, optional): If ``True``, the data loader will copy Tensors
            into CUDA pinned memory before returning them.  If your data elements
            are a custom type, or your :attr:`collate_fn` returns a batch that is a custom type,
            see the example below.
        drop_last (bool, optional): set to ``True`` to drop the last incomplete batch,
            if the dataset size is not divisible by the batch size. If ``False`` and
            the size of dataset is not divisible by the batch size, then the last batch
            will be smaller. (default: ``False``)
        timeout (numeric, optional): if positive, the timeout value for collecting a batch
            from workers. Should always be non-negative. (default: ``0``)
        worker_init_fn (callable, optional): If not ``None``, this will be called on each
            worker subprocess with the worker id (an int in ``[0, num_workers - 1]``) as
            input, after seeding and before data loading. (default: ``None``)


    .. warning:: If the ``spawn`` start method is used, :attr:`worker_init_fn`
                 cannot be an unpicklable object, e.g., a lambda function. See
                 :ref:`multiprocessing-best-practices` on more details related
                 to multiprocessing in PyTorch.

    .. note:: ``len(dataloader)`` heuristic is based on the length of the sampler used.
              When :attr:`dataset` is an :class:`~torch.utils.data.IterableDataset`,
              an infinite sampler is used, whose :meth:`__len__` is not
              implemented, because the actual length depends on both the
              iterable as well as multi-process loading configurations. So one
              should not query this method unless they work with a map-style
              dataset. See `Dataset Types`_ for more details on these two types
              of datasets.
    Fr   Nr   c             C   sº  t jjdƒ |dk rtdƒ‚|
dk r,tdƒ‚|| _|| _|| _|
| _|| _|| _	t
|tƒrªtj| _|dk	rztdj|ƒƒ‚q²|d k	r’tdj|ƒƒ‚q²|d k	r²tdj|ƒƒ‚ntj| _|d k	rÆ|rÆtd	ƒ‚|d k	rø|d
ksæ|sæ|d k	sæ|	rîtdƒ‚d }d}	n|d k�r|�s|	�rtdƒ‚|d k�rN| jtjk�r6tƒ }n|�rFt|ƒ}nt|ƒ}|d k	�rn|d k�rnt|||	ƒ}|| _|	| _|| _|| _|d k�rª| j�r¢tjj}ntjj}|| _d| _d S )Nzpython.data_loaderr   zXnum_workers option should be non-negative; use num_workers=0 to disable multiprocessing.z%timeout option should be non-negativeFzXDataLoader with IterableDataset: expected unspecified shuffle option, but got shuffle={}zXDataLoader with IterableDataset: expected unspecified sampler option, but got sampler={}zdDataLoader with IterableDataset: expected unspecified batch_sampler option, but got batch_sampler={}z1sampler option is mutually exclusive with shuffler   z[batch_sampler option is mutually exclusive with batch_size, shuffle, sampler, and drop_lastzcbatch_size=None option disables auto-batching and is mutually exclusive with shuffle, and drop_lastT) ÚtorchZ_CZ_log_api_usage_onceÚ
ValueErrorr   Únum_workersÚ
pin_memoryÚtimeoutÚworker_init_fnÚmultiprocessing_contextÚ
isinstancer   r   r   Ú_dataset_kindÚformatr   r   r   r   r   Ú
batch_sizer   ÚsamplerÚbatch_samplerÚ_auto_collationr   ÚcollateÚdefault_collateZdefault_convertr   Ú_DataLoader__initialized)r   r   r0   Úshuffler1   r2   r(   r   r)   r   r*   r+   r,   r   r   r   r   x   sl    








zDataLoader.__init__c             C   s   | j S )N)Ú$_DataLoader__multiprocessing_context)r   r   r   r   r,   ë   s    z"DataLoader.multiprocessing_contextc             C   sŒ   |d k	r‚| j dkrrtjs tdƒ‚t|tƒrTtjƒ }||krJtdj||ƒƒ‚tj|ƒ}t|t	j
jƒs‚tdj|ƒƒ‚ntdj| j ƒƒ‚|| _d S )Nr   zYmultiprocessing_context relies on Python >= 3.4, with support for different start methodszlmultiprocessing_context option should specify a valid start method in {}, but got multiprocessing_context={}z‹multiprocessing_context option should be a valid context object or a string specifying the start method, but got multiprocessing_context={}zmmultiprocessing_context can only be used with multi-process loading (num_workers > 0), but got num_workers={})r(   ÚmultiprocessingZ_supports_contextr'   r-   r   Zget_all_start_methodsr/   Zget_contextÚpython_multiprocessingÚcontextZBaseContextr8   )r   r,   Zvalid_start_methodsr   r   r   r,   ï   s"    





c                s8   | j r"|dkr"tdj|| jjƒƒ‚tt| ƒj||ƒ d S )Nr0   r2   r1   r   r   z6{} attribute should not be set after {} is initialized)r0   r2   r1   r   r   )r6   r'   r/   r   r   r   r%   Ú__setattr__)r   ÚattrÚval)r   r   r   r<     s    zDataLoader.__setattr__c             C   s   | j dkrt| ƒS t| ƒS d S )Nr   )r(   Ú_SingleProcessDataLoaderIterÚ_MultiProcessingDataLoaderIter)r   r   r   r   r      s    
zDataLoader.__iter__c             C   s
   | j d k	S )N)r2   )r   r   r   r   r3     s    zDataLoader._auto_collationc             C   s   | j r| jS | jS d S )N)r3   r2   r1   )r   r   r   r   Ú_index_sampler  s    zDataLoader._index_samplerc             C   s
   t | jƒS )N)ÚlenrA   )r   r   r   r   r"   (  s    zDataLoader.__len__)r   FNNr   NFFr   NN)r   r   r   r#   r6   r   Úpropertyr,   Úsetterr<   r    r3   rA   r"   r$   r   r   )r   r   r%   <   s   8   
pr%   c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú_BaseDataLoaderIterc             C   s|   |j | _|j| _|j| _|j| _|j| _|j| _|j	o>t
jjƒ | _|j| _|j| _t| jƒ| _t
jf t
jd�jƒ jƒ | _d S )N)Zdtype)r   Ú_datasetr.   r3   r   Ú
_drop_lastrA   r(   Ú_num_workersr)   r&   ÚcudaZis_availableÚ_pin_memoryr*   Ú_timeoutr   Ú_collate_fnÚiterÚ_sampler_iterÚemptyZint64Zrandom_ÚitemÚ
_base_seed)r   Úloaderr   r   r   r   -  s    z_BaseDataLoaderIter.__init__c             C   s   | S )Nr   )r   r   r   r   r    :  s    z_BaseDataLoaderIter.__iter__c             C   s
   t | jƒS )N)ÚnextrN   )r   r   r   r   Ú_next_index=  s    z_BaseDataLoaderIter._next_indexc             C   s   t ‚d S )N)ÚNotImplementedError)r   r   r   r   Ú__next__@  s    z_BaseDataLoaderIter.__next__c             C   s
   t | jƒS )N)rB   rA   )r   r   r   r   r"   C  s    z_BaseDataLoaderIter.__len__c             C   s   t d| jjƒ‚d S )Nz{} cannot be pickled)rU   r   r   )r   r   r   r   Ú__getstate__F  s    z _BaseDataLoaderIter.__getstate__N)	r   r   r   r   r    rT   rV   r"   rW   r   r   r   r   rE   ,  s   rE   c                   s(   e Zd Z‡ fdd„Zdd„ ZeZ‡  ZS )r?   c                sN   t t| ƒj|ƒ | jdkst‚| jdks,t‚tj| j| j	| j
| j| jƒ| _d S )Nr   )r   r?   r   rK   ÚAssertionErrorrH   r   r   r.   rF   r3   rL   rG   Ú_dataset_fetcher)r   rR   )r   r   r   r   P  s
    z%_SingleProcessDataLoaderIter.__init__c             C   s*   | j ƒ }| jj|ƒ}| jr&tjj|ƒ}|S )N)rT   rY   r   rJ   r   r)   )r   ÚindexÚdatar   r   r   rV   X  s
    z%_SingleProcessDataLoaderIter.__next__)r   r   r   r   rV   rS   r$   r   r   )r   r   r?   O  s   r?   c                   sj   e Zd ZdZ‡ fdd„Zejfdd„Zdd„ Zdd	„ Z	e	Z
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )r@   zHIterates once over the DataLoader's dataset, as specified by the samplerc                sà  t t| ƒj|ƒ | jdkst‚|jd kr.t}n|j}|j| _t	j
t| jƒƒ| _|jƒ | _d| _d| _d| _d| _i | _d| _|jƒ | _g | _g | _g | _xŽt| jƒD ]€}|jƒ }|jtjj| j| j|| j| j| j | j!| j"| j#| | j|| jfd�}d|_$|j%ƒ  | jj&|ƒ | jj&|ƒ | jj&dƒ q¤W | j'�r€t(jƒ | _)t*jƒ | _+t(j,tj-j.| j| j+t/j0j1ƒ | j)fd�}d|_$|j%ƒ  || _2n| j| _+tj3j4t5| ƒt6dd„ | jD ƒƒƒ tj3j7ƒ  d| _x td| j ƒD ]}| j8ƒ  �qÊW d S )Nr   F)ÚtargetÚargsTc             s   s   | ]}|j V  qd S )N)Úpid)Ú.0Úwr   r   r   ú	<genexpr>¿  s    z:_MultiProcessingDataLoaderIter.__init__.<locals>.<genexpr>é   )9r   r@   r   rH   rX   r,   r9   r+   Z_worker_init_fnÚ	itertoolsÚcycleÚrangeÚ_worker_queue_idx_cycleÚQueueÚ_worker_result_queueÚ_worker_pids_setÚ	_shutdownÚ	_send_idxÚ	_rcvd_idxÚ
_task_infoÚ_tasks_outstandingÚEventÚ_workers_done_eventÚ_index_queuesÚ_workersÚ_workers_statusÚProcessr   ÚworkerZ_worker_loopr.   rF   r3   rL   rG   rQ   ÚdaemonÚstartÚappendrJ   Ú	threadingÚ_pin_memory_thread_done_eventr
   Ú_data_queueÚThreadr)   Z_pin_memory_loopr&   rI   Zcurrent_deviceÚ_pin_memory_threadÚsignal_handlingZ_set_worker_pidsÚidÚtupleZ_set_SIGCHLD_handlerÚ_try_put_index)r   rR   r,   ÚiZindex_queuer`   Zpin_memory_threadÚ_)r   r   r   r   {  s`    





"
z'_MultiProcessingDataLoaderIter.__init__c             C   sÂ   y| j j|d�}d|fS  tk
r¼ } zŠg }x>t| jƒD ]0\}}| j| r8|jƒ  r8|j|ƒ | j|ƒ q8W t	|ƒdkršdj
dd„ |D ƒƒ}tdj|ƒƒ‚t|tjƒrªd	S ‚ W Y d d }~X nX d S )
N)r*   Tr   z, c             s   s   | ]}t |jƒV  qd S )N)Ústrr^   )r_   r`   r   r   r   ra   à  s    z?_MultiProcessingDataLoaderIter._try_get_data.<locals>.<genexpr>z1DataLoader worker (pid(s) {}) exited unexpectedlyF)FN)r{   ÚgetÚ	ExceptionÚ	enumeraterr   rs   Úis_aliverx   Ú_shutdown_workerrB   ÚjoinÚRuntimeErrorr/   r-   r
   ÚEmpty)r   r*   r[   ÚeZfailed_workersÚ	worker_idr`   Zpids_strr   r   r   Ú_try_get_dataÇ  s    
z,_MultiProcessingDataLoaderIter._try_get_datac             C   s†   | j dkr4| j| j ƒ\}}|r"|S tdj| j ƒƒ‚nN| jrhxF| jjƒ r\| jƒ \}}|r<|S q<W tdƒ‚nx| jƒ \}}|rj|S qjW d S )Nr   z%DataLoader timed out after {} secondsz%Pin memory thread exited unexpectedly)rK   r�   r‹   r/   rJ   r}   rˆ   )r   Úsuccessr[   r   r   r   Ú	_get_dataæ  s    

z(_MultiProcessingDataLoaderIter._get_datac             C   s2  �x*x`| j | jk rX| j| j  }|d }t|ƒdks<| j| r>P | j| j = |  j d7  _ qW | jƒ  t‚t| j| j  ƒdkr–| jj| j ƒd }| j|ƒS | j	 r¨| j
dks¬t‚| jƒ \}}|  j
d8  _
| jtjkröt|tjjƒrö| j|jƒ | jƒ  q|| j k�r| j|  |f7  < q| j|= | j|ƒS qW d S )Nr   rb   r   )rl   rk   rm   rB   rs   Ú_shutdown_workersÚStopIterationÚpopÚ_process_datarj   rn   rX   r‘   r.   r   r   r-   r   ru   Z_IterableDatasetStopIterationr‰   rŽ   r�   )r   ÚinforŽ   r[   Úidxr   r   r   rV     s2    

z'_MultiProcessingDataLoaderIter.__next__c             C   s¨   | j d| j k st‚y| jƒ }W n tk
r4   d S X x,t| jƒD ]}t| jƒ}| j| rBP qBW d S | j	| j
| j|fƒ |f| j| j< |  j d7  _ |  jd7  _d S )Nrb   r   )rn   rH   rX   rT   r“   re   rS   rf   rs   rq   Úputrk   rm   )r   rZ   rƒ   Zworker_queue_idxr   r   r   r�   7  s    

z-_MultiProcessingDataLoaderIter._try_put_indexc             C   s,   |  j d7  _ | jƒ  t|tƒr(|jƒ  |S )Nr   )rl   r�   r-   r	   Úreraise)r   r[   r   r   r   r•   J  s
    
z,_MultiProcessingDataLoaderIter._process_datac             C   s0   | j | st‚| j| }|jd ƒ d| j |< d S )NF)rs   rX   rq   r˜   )r   rŽ   Úqr   r   r   r‰   Q  s    


z/_MultiProcessingDataLoaderIter._shutdown_workerc             C   sð   t j}|dks|d krd S | jsìd| _z¦t| dƒr\| jjƒ  | jjdƒ | jj	ƒ  | jj
ƒ  | jjƒ  x*tt| jƒƒD ]}| j| rv| j|ƒ qvW x| jD ]}|j	ƒ  qšW x| jD ]}|jƒ  |j
ƒ  q²W W d | jrêt jjt| ƒƒ d| _X d S )NTr}   F)NN)r   Úpython_exit_statusrj   Úhasattrrz   Úsetrh   r˜   r}   rŠ   Úcloserp   re   rB   rr   rs   r‰   rq   Zcancel_join_threadri   r~   Z_remove_worker_pidsr   )r   r›   rŽ   r`   rš   r   r   r   r’   h  s.    





z0_MultiProcessingDataLoaderIter._shutdown_workersc             C   s   | j ƒ  d S )N)r’   )r   r   r   r   Ú__del__�  s    z&_MultiProcessingDataLoaderIter.__del__)r   r   r   r#   r   r   ZMP_STATUS_CHECK_INTERVALr�   r‘   rV   rS   r�   r•   r‰   r’   rŸ   r$   r   r   )r   r   r@   b  s     L!.5r@   )r#   r&   r9   r:   Ztorch.multiprocessingÚ r   r   r   r   r   r   Ztorch._utilsr	   ry   rc   Z
torch._sixr
   r   ru   Zget_worker_infor4   r5   Úobjectr   r   r%   rE   r?   r@   r   r   r   r   Ú<module>   s$   
 q#