3
I4W^¥<  ã               @   sä   d dl Z d dlZd dlmZ d dlZd dlZd,dd„Zd-dd„Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ fdd„Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ ZG d(d)„ d)eƒZG d*d+„ d+eƒZdS ).é    N)ÚdefaultdictFc       	      K   sÖ   t d||ƒ}|dkr&| jd | jj S t|tƒr8t|ƒ}|t| ƒkrH| S | jr´|js\t	dƒ‚|jj
ddƒ}|d |j }tj| ƒj||ƒ}|d }tj| ƒj||ƒ}|||| jƒ ƒS |jrÂt	dƒ‚|| jƒ ƒj| |ƒS )	a©  Returns the type if `dtype` is not provided, else casts this object to
    the specified type.

    If this is already of the correct type, no copy is performed and the
    original object is returned.

    Args:
        dtype (type or string): The desired type
        non_blocking (bool): If ``True``, and the source is in pinned memory
            and destination is on the GPU or vice versa, the copy is performed
            asynchronously with respect to the host. Otherwise, the argument
            has no effect.
        **kwargs: For compatibility, may contain the key ``async`` in place of
            the ``non_blocking`` argument. The ``async`` arg is deprecated.
    ÚtypeNÚ.z)Cannot cast sparse tensor to dense tensorz.sparseÚ z.LongTensorz)Cannot cast dense tensor to sparse tensor)Ú_get_async_or_non_blockingÚ
__module__Ú	__class__Ú__name__Ú
isinstanceÚstrÚ_import_dotted_namer   Ú	is_sparseÚRuntimeErrorÚreplaceÚtorchÚ_valuesÚ_indicesÚsizeÚcopy_)	ÚselfÚdtypeÚnon_blockingÚkwargsZnew_module_nameZnew_values_type_nameÚ
new_valuesZnew_indices_type_nameZnew_indices© r   ú-/tmp/pip-build-e66j97b2/torch/torch/_utils.pyÚ_type   s&    
r   c             K   sÎ   t d||ƒ}| jr6|dkr$tjjƒ }| jƒ |krB| S n|dkrBd}tjj|ƒ�v | jrœttjj	| j
jƒ}tj| ƒj||ƒ}tj| ƒj||ƒ}|||| jƒ ƒS ttj| j
jƒ}|| jƒ ƒj| |ƒS W dQ R X dS )aU  Returns a copy of this object in CUDA memory.

    If this object is already in CUDA memory and on the correct device, then
    no copy is performed and the original object is returned.

    Args:
        device (int): The destination GPU id. Defaults to the current device.
        non_blocking (bool): If ``True`` and the source is in pinned memory,
            the copy will be asynchronous with respect to the host. Otherwise,
            the argument has no effect.
        **kwargs: For compatibility, may contain the key ``async`` in place of
            the ``non_blocking`` argument.
    ÚcudaNé   éÿÿÿÿ)r   Zis_cudar   r   Zcurrent_deviceZ
get_deviceÚdevicer   ÚgetattrÚsparser   r	   r   r   r   r   )r   r    r   r   Znew_typeÚindicesÚvaluesr   r   r   Ú_cuda.   s     
r%   c             C   sR   |s|S t |ƒdksd|kr@d}t|jƒ ƒjƒ }t|j| |ƒƒ‚tjdƒ |d S )Nr   Úasyncz,{}() got an unexpected keyword argument '{}'z)'async' is deprecated; use 'non_blocking')ÚlenÚlistÚkeysÚpopÚ	TypeErrorÚformatÚwarningsÚwarn)Zfunction_namer   r   ÚmessageÚargumentr   r   r   r   P   s    
r   c             C   s$   t jg | j| jd�}|j| |||ƒS )N)r   r    )r   Útensorr   r    Úset_)ÚstorageÚstorage_offsetr   ÚstrideÚtr   r   r   Ú_rebuild_tensor€   s    r7   c             C   s   t | |||ƒ}||_||_|S )N)r7   Úrequires_gradÚ_backward_hooks)r3   r4   r   r5   r8   Úbackward_hooksr1   r   r   r   Ú_rebuild_tensor_v2†   s    r;   c             C   s   t j| ƒj||d�}||_|S )N)r   r    )r   Z
from_numpyZtor8   )Údatar   r    r8   r1   r   r   r   Ú_rebuild_xla_tensor�   s    r=   c             C   s®   |d }|t jkr2|\}}	}
t j||	|
| jd�}n\|t jkr€|\}}}}t j|t jd�}t j|t jd�}t j||||| jd�}nt	dj
|ƒƒ‚|j| |||ƒ ||_||_|S )Nr   )ÚscaleÚ
zero_pointr   )r   )ÚscalesÚzero_pointsÚaxisr   z2Can't deserialize quantized tensor with qscheme {})r   Zper_tensor_affineZ_empty_affine_quantizedr   Zper_channel_affiner1   Zfloat64Zint64Z#_empty_per_channel_affine_quantizedr   r,   r2   r8   r9   )r3   r4   r   r5   Zquantizer_paramsr8   r:   ZqschemeÚ_r>   r?   r1   r@   rA   rB   r   r   r   Ú_rebuild_qtensor”   s    


rD   c             C   s   t jj| |ƒ}||_|S )N)r   ÚnnÚ	Parameterr9   )r<   r8   r:   Úparamr   r   r   Ú_rebuild_parameter©   s    rH   c             C   s:   | j dƒ}t|d ƒ}x|dd … D ]}t||ƒ}q$W |S )Nr   r   r   )ÚsplitÚ
__import__r!   )ÚnameÚ
componentsÚobjÚ	componentr   r   r   r   ³   s
    
r   c             C   s   | | S )Nr   )ÚxÚyr   r   r   Ú<lambda>¼   s    rQ   c             c   sR   t | ƒ}yt|ƒ}W n tk
r(   dS X |V  x|D ]}|||ƒ}|V  q6W dS )zReturn running totalsN)ÚiterÚnextÚStopIteration)ÚiterableÚfnÚitÚtotalÚelementr   r   r   Ú_accumulate¼   s    

rZ   c             C   s:   t | ƒdkr| d jƒ jdƒS tjdd„ | D ƒdd�}|S )a§  Flatten dense tensors into a contiguous 1D buffer. Assume tensors are of
    same dense type.

    Since inputs are dense, the resulting tensor will be a concatenated 1D
    buffer. Element-wise operation on this buffer will be equivalent to
    operating individually.

    Arguments:
        tensors (Iterable[Tensor]): dense tensors to flatten.

    Returns:
        A contiguous 1D buffer containing input tensors.
    r   r   c             S   s   g | ]}|j ƒ jdƒ‘qS )r   r   )Ú
contiguousÚview)Ú.0r6   r   r   r   ú
<listcomp>Û   s    z*_flatten_dense_tensors.<locals>.<listcomp>)Zdimr   )r'   r[   r\   r   Úcat)ÚtensorsÚflatr   r   r   Ú_flatten_dense_tensorsË   s    rb   c             C   s,   t dd„ | D ƒƒ}t dd„ | D ƒƒ}||fS )ag  Flatten sparse tensors into two contiguous 1D buffers, one of indices and
    one of values. Assume tensors are of same sparse type.

    Arguments:
        tensors (Iterable[Tensor]): sparse tensors to flatten.

    Returns:
        A tuple of two contiguous 1D buffers, one containing input tensors'
        indices and the other containing the values.
    c             S   s   g | ]}t j|ƒ‘qS r   )r   r   )r]   r6   r   r   r   r^   ê   s    z+_flatten_sparse_tensors.<locals>.<listcomp>c             S   s   g | ]}t j|ƒ‘qS r   )r   r   )r]   r6   r   r   r   r^   ë   s    )rb   )r`   Úflat_indicesÚflat_valuesr   r   r   Ú_flatten_sparse_tensorsß   s    re   c             C   sH   g }d}x6|D ].}|j ƒ }|j| jd||ƒj|ƒƒ ||7 }qW t|ƒS )a³  View a flat buffer using the sizes of tensors. Assume that tensors are of
    same dense type, and that flat is given by _flatten_dense_tensors.

    Arguments:
        flat (Tensor): flattened dense tensors to unflatten.
        tensors (Iterable[Tensor]): dense tensors whose sizes will be used to
          unflatten flat.

    Returns:
        Unflattened dense tensors with sizes same as tensors and values from
        flat.
    r   )ÚnumelÚappendZnarrowZview_asÚtuple)ra   r`   ÚoutputsÚoffsetr1   rf   r   r   r   Ú_unflatten_dense_tensorsï   s    
rk   c       
      C   sp   | \}}t |dd„ |D ƒƒ}t |dd„ |D ƒƒ}g }x2t|||ƒD ]"\}}}	|j|j||	|jƒ ƒƒ qBW t|ƒS )a	  View flat buffer (containing indices and values) using the sizes of
    tensors. Assume that tensors are of same sparse type, and that flat is given
    by _flatten_sparse_tensors.

    Arguments:
        flat (tuple(Tensor, Tensor)): flattened indices and values of sparse
          tensors to unflatten.
        tensors (Iterable[Tensor]): sparse tensors whose sizes will be used to
          unflatten flat.

    Returns:
        Unflattened sparse tensors with sizes same as tensors and values from
        flat.
    c             S   s   g | ]}t j|ƒ‘qS r   )r   r   )r]   r6   r   r   r   r^     s    z-_unflatten_sparse_tensors.<locals>.<listcomp>c             S   s   g | ]}t j|ƒ‘qS r   )r   r   )r]   r6   r   r   r   r^     s    )rk   Úziprg   Únewr   rh   )
ra   r`   rc   rd   r#   r$   ri   r6   ÚiÚvr   r   r   Ú_unflatten_sparse_tensors  s    rp   c                sP   t tƒ‰ x| D ]}ˆ |jƒ  j|ƒ qW dd„ ˆ jƒ D ƒ‰ t‡ fdd„|D ƒƒS )a  Assume that tensors are of same order as ordered_tensors within their
    types, e.g., from _take_tensors. Reorder them to be of same order as
    ordered_tensors.

    Arguments:
        tensors (Iterable[Tensor]): tensors to be reordered. They should be of
          the same order as ordered_tensors within their own types.
        ordered_tensors (Iterable[Tensor]): tensors whose order will be the
          reference.

    Returns:
        Ordered tuple of tensors with contents from tensors and order of
        ordered_tensors.
    c             S   s   i | ]\}}t |ƒ|“qS r   )rR   )r]   r6   Zcollr   r   r   ú
<dictcomp>/  s    z'_reorder_tensors_as.<locals>.<dictcomp>c             3   s   | ]}t ˆ |jƒ  ƒV  qd S )N)rS   r   )r]   r1   )Ú	type_dictr   r   ú	<genexpr>0  s    z&_reorder_tensors_as.<locals>.<genexpr>)r   r(   r   rg   Úitemsrh   )r`   Zordered_tensorsr1   r   )rr   r   Ú_reorder_tensors_as  s
    
ru   c             c   sö   t dd„ ƒ}x¼| D ]´}|jƒ }|jrZtj|ƒ}tj|ƒ}|jƒ |jƒ  |jƒ |jƒ   }n|jƒ |jƒ  }|| }|d | |kr¨|d dkr¨|d V  g dg }||< |d j|ƒ |d  |7  < qW x&|j	ƒ D ]\}	}
t
|	ƒdkrÔ|	V  qÔW dS )aÕ  Group tensors into chunks. This generator yields a chunk at each time,
    each containing tensors of same type up to certain byte limit in total size.

    Args:
        tensors (Sequence): A sequence of tensors to be separated into chunks.
        size_limit (int): The limit of each chunk in bytes.

    Yields:
        Blocks of tensors of same type and within size_limit. The yielded
        tensors are only ordered as the original sequence within its types.
    c               S   s   g dgS )Nr   r   r   r   r   r   rQ   ?  s    z_take_tensors.<locals>.<lambda>r   r   N)r   r   r   r   r   r   rf   Zelement_sizerg   r$   r'   )r`   Z
size_limitZbuf_dictr1   r6   r#   r$   r   Zbuf_and_sizeÚbufrC   r   r   r   Ú_take_tensors3  s"    


"
rw   c                s   ‡ ‡fdd„}|S )Nc                s   t ˆ ƒ| _ˆ| jd< | S )NÚreturn)ÚdictÚ__annotations__)Zfun)r   Úretr   r   ÚdecV  s    

zannotate.<locals>.decr   )r{   r   r|   r   )r   r{   r   ÚannotateU  s    r}   c               @   s   e Zd ZdZdd„ ZdS )ÚKeyErrorMessagez(str subclass that returns itself in reprc             C   s   | S )Nr   )r   r   r   r   Ú__repr__g  s    zKeyErrorMessage.__repr__N)r	   r   Ú__qualname__Ú__doc__r   r   r   r   r   r~   e  s   r~   c               @   s"   e Zd ZdZddd„Zdd„ ZdS )	ÚExceptionWrapperz?Wraps an exception plus traceback to communicate across threadsNúin backgroundc             C   s6   |d krt jƒ }|d | _djtj|Ž ƒ| _|| _d S )Nr   r   )ÚsysÚexc_infoÚexc_typeÚjoinÚ	tracebackÚformat_exceptionÚexc_msgÚwhere)r   r…   r‹   r   r   r   Ú__init__m  s
    
zExceptionWrapper.__init__c             C   s6   dj | jj| j| jƒ}| jtkr(t|ƒ}| j|ƒ‚dS )z4Reraises the wrapped exception in the current threadzCaught {} {}.
Original {}N)r,   r†   r	   r‹   rŠ   ÚKeyErrorr~   )r   Úmsgr   r   r   Úreraisev  s
    
zExceptionWrapper.reraise)Nrƒ   )r	   r   r€   r�   rŒ   r�   r   r   r   r   r‚   k  s   
	r‚   )NF)NF)r   r-   Úcollectionsr   r„   rˆ   r   r%   r   r7   r;   r=   rD   rH   r   rZ   rb   re   rk   rp   ru   rw   r}   r   r~   Úobjectr‚   r   r   r   r   Ú<module>   s.   
&
"0	
	"