U
    <c'                     @   sV   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 dd Z
dd	d
ZdS )z"Contains definitions of the methods used by the _BaseDataLoaderIter to put
fetched tensors into pinned memory.

These **needs** to be in global scope since Py2 doesn't support serializing
static methods.
    N)string_classes   )MP_STATUS_CHECK_INTERVAL)ExceptionWrapperc                    s>   t d t j  fdd} s:|  q*d S )Nr   c                     s   zj td} W n tjk
r(   Y d S X | \}} st|tszt| }W n$ tk
rv   td	d}Y nX ||f}  szj
| td W qW q tjk
r   Y qY qX qd S )N)timeoutz"in pin memory thread for device {})where)getr   queueEmptyis_set
isinstancer   
pin_memory	ExceptionformatputFull)ridxdatadevice	device_id
done_eventin_queue	out_queue F/tmp/pip-unpacked-wheel-gikjz4vx/torch/utils/data/_utils/pin_memory.pydo_one_step   s&    z%_pin_memory_loop.<locals>.do_one_step)torchZset_num_threadsZcudaZ
set_devicer   )r   r   r   r   r   r   r   r   r   _pin_memory_loop   s
    
r   c                    s>  t | tjr|  S t | tr$| S t | tjjrz t|  fdd| 	 D W S  t
k
r|    fdd| 	 D  Y S X nt | trt| drt|  fdd| D  S t | trʇ fdd| D S t | tjjr"zt|  fd	d| D W S  t
k
r    fd
d| D  Y S X nt| dr6|  S | S d S )Nc                    s   i | ]\}}|t | qS r   r   .0ksampler   r   r   
<dictcomp>:   s      zpin_memory.<locals>.<dictcomp>c                    s   i | ]\}}|t | qS r   r    r!   r%   r   r   r&   =   s      _fieldsc                 3   s   | ]}t | V  qd S )Nr    r"   r$   r%   r   r   	<genexpr>?   s     zpin_memory.<locals>.<genexpr>c                    s   g | ]}t | qS r   r    r(   r%   r   r   
<listcomp>A   s     zpin_memory.<locals>.<listcomp>c                    s   g | ]}t | qS r   r    r(   r%   r   r   r*   D   s     c                    s   g | ]}t | qS r   r    r(   r%   r   r   r*   G   s     r   )r   r   ZTensorr   r   collectionsabcMappingtypeitems	TypeErrortuplehasattrSequence)r   r   r   r%   r   r   3   s*    

 
r   )N)__doc__r+   r	   r   Z
torch._sixr    r   Ztorch._utilsr   r   r   r   r   r   r   <module>   s   "