3
I4W^·b  ã               @   sÀ  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZmZ ddlmZ ddlmZ d dlmZ ejd  dkrœd dlZnd dlZd dlZdZejdƒjZejd	ƒjZejd
ƒjZdZdZdZ G dd„ de!ƒZ"edd„ ƒZ#g Z$dd„ Z%dd„ Z&dd„ Z'dd„ Z(dd„ Z)dd„ Z*dd„ Z+e&d e'e)ƒ e&d!e(e+ƒ d"d#„ Z,d$d%„ Z-d&d'„ Z.d(d)„ Z/d*d+„ Z0d,d-„ Z1d.d/„ Z2d0d1„ Z3eefd2d3„Z4d4d5„ Z5defd6d7„Z6d8d9„ Z7dS ):é    N)ÚclosingÚcontextmanageré   )Ú_import_dotted_name)Ústring_classes)Úget_source_lines_and_fileé   z=lz=iz=hl   l|9åQC
 ié  ú,c               @   s   e Zd ZdS )ÚSourceChangeWarningN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú4/tmp/pip-build-e66j97b2/torch/torch/serialization.pyr
       s   r
   c              c   s   t jƒ } | V  tj| ƒ d S )N)ÚtempfileÚmkdtempÚshutilÚrmtree)Úpathr   r   r   r   $   s    r   c       	      C   sÈ   g }| j ƒ }| jdƒ | jdƒ}x.|dkrN|j|ƒ t|ƒdkrBP | jdƒ}q"W | j|ƒ ddddgddd	d
gddddgg}xF|D ]>}d}x,t||ƒD ]\}}t|ƒt|ƒkr–d}P q–W |r‚dS q‚W dS )Nr   r   Ú é   ÚPÚKúúúúúúTF)ÚtellÚseekÚreadÚappendÚlenÚzipÚord)	ÚfZ
read_bytesÚstartZbyteZmagic_numbersÚmagic_numberÚmatchZ
magic_byteZ	read_byter   r   r   Ú_is_zipfile.   s,    







r*   c             C   s    | ||f}t j|ƒ t jƒ  d S )N)Ú_package_registryr"   Úsort)ÚpriorityÚtaggerZdeserializerZ
queue_elemr   r   r   Úregister_packageS   s    

r/   c             C   s   t | ƒjdkrdS d S )NÚtorchÚcpu)Útyper   )Úobjr   r   r   Ú_cpu_tagY   s    r4   c             C   s"   t | ƒjdkrdt| jƒ ƒ S d S )Nz
torch.cudazcuda:)r2   r   ÚstrZ
get_device)r3   r   r   r   Ú	_cuda_tag^   s    r6   c             C   s   |dkr| S d S )Nr1   r   )r3   Úlocationr   r   r   Ú_cpu_deserializec   s    r8   c             C   sŒ   t | tjƒrt| ƒ} t | tƒs&tdƒ‚| dd … dkr<d}ntt| dd … ƒdƒ}tjj	ƒ sdt
dƒ‚|tjjƒ krˆt
dj|tjjƒ ƒƒ‚|S )Nz+location should be a string or torch.deviceé   r   r   zàAttempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.z«Attempting to deserialize object on CUDA device {} but torch.cuda.device_count() is {}. Please use torch.load with map_location to map your storages to an existing device.)Ú
isinstancer0   Údevicer5   Ú_string_classesÚ
ValueErrorÚmaxÚintÚcudaZis_availableÚRuntimeErrorZdevice_countÚformat)r7   r;   r   r   r   Úvalidate_cuda_deviceh   s    

rC   c          	   C   s`   |j dƒr\t|ƒ}t| ddƒrRttjt| ƒjƒ}tjj|ƒ� || jƒ ƒS Q R X n
| j|ƒS d S )Nr@   Ú_torch_load_uninitializedF)	Ú
startswithrC   Úgetattrr0   r@   r2   r   r;   Úsize)r3   r7   r;   Ústorage_typer   r   r   Ú_cuda_deserialize�   s    
rI   é
   é   c             C   s:   x"t D ]\}}}|| ƒ}|r|S qW tdtj| ƒ ƒ‚d S )Nz-don't know how to determine data location of )r+   rA   r0   Útypename)ÚstorageÚ_r.   r7   r   r   r   Úlocation_tag�   s    rO   c             C   sL   x(t D ] \}}}|| |ƒ}|d k	r|S qW tdtj| ƒ d | d ƒ‚d S )Nz+don't know how to restore data location of z (tagged with ú))r+   rA   r0   rL   )rM   r7   rN   ÚfnÚresultr   r   r   Údefault_restore_location™   s    
rS   c             C   s   t t| jƒS )N)rF   r0   r   )rH   r   r   r   Únormalize_storage_type£   s    rT   c             C   s&   t | ƒ}t|jƒ}t||jjddƒƒS )NZStorageZTensor)r2   r   r   rF   r   Úreplace)rM   rH   Úmoduler   r   r   Ústorage_to_tensor_type§   s    
rW   c          
   C   sj   d}t | tƒs@tjd dkr&t | tƒs@tjd dkrNt | tjƒrNd}t| |ƒ} z|| ƒS |rd| jƒ  X dS )zq
    Executes a body function with a file object for f, opening
    it in 'mode' if it is a string filename.
    Fr   r   é   TN)	r:   r5   ÚsysÚversion_infoÚunicodeÚpathlibÚPathÚopenÚclose)r&   ÚmodeÚbodyÚnew_fdr   r   r   Ú_with_file_like­   s    

rc   c             C   s*   dg}y
| j |kS  tk
r$   dS X d S )NÚgzipF)r   ÚAttributeError)r&   Zcompress_modulesr   r   r   Ú_is_compressed_file¿   s
    
rf   c             C   sF   t | ƒrdS y| jƒ dkS  tjk
r.   dS  tk
r@   dS X dS )z¹
    Checks if f is a file that should be read directly. It should be read
    directly if it is backed by a real file (has a fileno) and is not a
    a compressed file (e.g. gzip)
    Fr   N)rf   ÚfilenoÚioÚUnsupportedOperationre   )r&   r   r   r   Ú_should_read_directlyÇ   s    rj   c             C   sV   dd„ }y| j | jƒ ƒ dS  tjtfk
rP } z|ddg|ƒ W Y d d }~X nX d S )Nc             S   sB   x8| D ]0}|t |ƒkrt |ƒd d d }t|ƒ|ƒ‚qW |‚d S )Nz7. You can only torch.load from a file that is seekable.z; Please pre-load the data into a buffer like io.BytesIO andz try to load from it instead.)r5   r2   )ÚpatternsÚeÚpÚmsgr   r   r   Úraise_err_msgÙ   s    
z&_check_seekable.<locals>.raise_err_msgTr    r   )r    r   rh   ri   re   )r&   ro   rl   r   r   r   Ú_check_seekable×   s    	rp   c                s   t |d‡ ‡‡fdd„ƒS )a¸  Saves an object to a disk file.

    See also: :ref:`recommend-saving-models`

    Args:
        obj: saved object
        f: a file-like object (has to implement write and flush) or a string
           containing a file name
        pickle_module: module used for pickling metadata and objects
        pickle_protocol: can be specified to override the default protocol

    .. warning::
        If you are using Python 2, :func:`torch.save` does NOT support :class:`StringIO.StringIO`
        as a valid file-like object. This is because the write method should return
        the number of bytes written; :meth:`StringIO.write()` does not do this.

        Please use something like :class:`io.BytesIO` instead.

    Example:
        >>> # Save to file
        >>> x = torch.tensor([0, 1, 2, 3, 4])
        >>> torch.save(x, 'tensor.pt')
        >>> # Save to io.BytesIO buffer
        >>> buffer = io.BytesIO()
        >>> torch.save(x, buffer)
    Úwbc                s   t ˆ | ˆˆƒS )N)Ú_save)r&   )r3   Úpickle_moduleÚpickle_protocolr   r   Ú<lambda>  s    zsave.<locals>.<lambda>)rc   )r3   r&   rs   rt   r   )r3   rs   rt   r   Úsaveé   s    rv   c                s  t jd dkr.dd l}t||jƒr.d}t|ƒ‚dd lj‰ i ‰i ‰‡ ‡‡fdd„}ttt j	dktt
ttd�d�}|jt||d	� |jt||d	� |j|||d	� |j||d	�}||_|j| ƒ tˆjƒ ƒ}	|j|	||d	� |jƒ  x |	D ]}
ˆ|
 j|t|ƒƒ qæW d S )
Nr   r   z»torch.save received unsupported StringIO.StringIO file object, whose write method does not return the number of bytes written. Please use something like io.BytesIO for torch.save instead.c                sú   t | tƒr„t| ˆ jƒr„| ˆkr"d S dˆ| < d  }}yt| ƒ\}}}dj| ƒ}W n( tk
rv   tjd| j	 d ƒ Y nX d| ||fS t
j| ƒrött| ƒƒ}d}t| jƒ}t| ƒ}| ˆ|< | j| jk}	|	rÞt| jƒ|| jƒ f}
nd }
d|||| jƒ |
fS d S )NTr   z4Couldn't retrieve source code for container of type z3. It won't be checked for correctness upon loading.rV   r   rM   )r:   r2   Ú
issubclassÚModuler   ÚjoinÚ	ExceptionÚwarningsÚwarnr   r0   Z
is_storagerT   r5   Z_cdatarO   rG   )r3   Úsource_fileÚsourceZsource_linesrN   rH   ÚoffsetZobj_keyr7   Zis_viewÚview_metadata)ÚnnÚserialized_container_typesÚserialized_storagesr   r   Úpersistent_id  s:    

z_save.<locals>.persistent_idÚlittle)Úshortr?   Úlong)Úprotocol_versionZlittle_endianZ
type_sizes)Úprotocol)rY   rZ   ÚStringIOr:   rA   Ztorch.nnr�   ÚdictÚPROTOCOL_VERSIONÚ	byteorderÚ
SHORT_SIZEÚINT_SIZEÚ	LONG_SIZEÚdumpÚMAGIC_NUMBERÚPicklerr„   ÚsortedÚkeysÚflushZ_write_filerj   )r3   r&   rs   rt   rŠ   rn   r„   Zsys_infoZpicklerZserialized_storage_keysÚkeyr   )r�   r‚   rƒ   r   rr     s6    
)

rr   c          
   K   s    d}t | tƒs&tjd dkr6t | tƒr6d}t| dƒ} n(tjd dkr^t | tjƒr^d}| jdƒ} z.tjd
kr~d|jƒ kr~d|d< t	| ||f|ŽS |rš| j
ƒ  X d	S )a§  Loads an object saved with :func:`torch.save` from a file.

    :func:`torch.load` uses Python's unpickling facilities but treats storages,
    which underlie tensors, specially. They are first deserialized on the
    CPU and are then moved to the device they were saved from. If this fails
    (e.g. because the run time system doesn't have certain devices), an exception
    is raised. However, storages can be dynamically remapped to an alternative
    set of devices using the :attr:`map_location` argument.

    If :attr:`map_location` is a callable, it will be called once for each serialized
    storage with two arguments: storage and location. The storage argument
    will be the initial deserialization of the storage, residing on the CPU.
    Each serialized storage has a location tag associated with it which
    identifies the device it was saved from, and this tag is the second
    argument passed to :attr:`map_location`. The builtin location tags are ``'cpu'``
    for CPU tensors and ``'cuda:device_id'`` (e.g. ``'cuda:2'``) for CUDA tensors.
    :attr:`map_location` should return either ``None`` or a storage. If
    :attr:`map_location` returns a storage, it will be used as the final deserialized
    object, already moved to the right device. Otherwise, :func:`torch.load` will
    fall back to the default behavior, as if :attr:`map_location` wasn't specified.

    If :attr:`map_location` is a :class:`torch.device` object or a string contraining
    a device tag, it indicates the location where all tensors should be loaded.

    Otherwise, if :attr:`map_location` is a dict, it will be used to remap location tags
    appearing in the file (keys), to ones that specify where to put the
    storages (values).

    User extensions can register their own location tags and tagging and
    deserialization methods using :func:`torch.serialization.register_package`.

    Args:
        f: a file-like object (has to implement :meth:`read`, :meth`readline`, :meth`tell`, and :meth`seek`),
            or a string containing a file name
        map_location: a function, :class:`torch.device`, string or a dict specifying how to remap storage
            locations
        pickle_module: module used for unpickling metadata and objects (has to
            match the :attr:`pickle_module` used to serialize file)
        pickle_load_args: (Python 3 only) optional keyword arguments passed over to
            :func:`pickle_module.load` and :func:`pickle_module.Unpickler`, e.g.,
            :attr:`errors=...`.

    .. note::
        When you call :func:`torch.load()` on a file which contains GPU tensors, those tensors
        will be loaded to GPU by default. You can call ``torch.load(.., map_location='cpu')``
        and then :meth:`load_state_dict` to avoid GPU RAM surge when loading a model checkpoint.

    .. note::
        By default, we decode byte strings as ``utf-8``.  This is to avoid a common error
        case ``UnicodeDecodeError: 'ascii' codec can't decode byte 0x...``
        when loading files saved by Python 2 in Python 3.  If this default
        is incorrect, you may use an extra :attr:`encoding` keyword argument to specify how
        these objects should be loaded, e.g., :attr:`encoding='latin1'` decodes them
        to strings using ``latin1`` encoding, and :attr:`encoding='bytes'` keeps them
        as byte arrays which can be decoded later with ``byte_array.decode(...)``.

    Example:
        >>> torch.load('tensors.pt')
        # Load all tensors onto the CPU
        >>> torch.load('tensors.pt', map_location=torch.device('cpu'))
        # Load all tensors onto the CPU, using a function
        >>> torch.load('tensors.pt', map_location=lambda storage, loc: storage)
        # Load all tensors onto GPU 1
        >>> torch.load('tensors.pt', map_location=lambda storage, loc: storage.cuda(1))
        # Map tensors from GPU 1 to GPU 0
        >>> torch.load('tensors.pt', map_location={'cuda:1':'cuda:0'})
        # Load tensor from io.BytesIO object
        >>> with open('tensor.pt', 'rb') as f:
                buffer = io.BytesIO(f.read())
        >>> torch.load(buffer)
        # Load a module with 'ascii' encoding for unpickling
        >>> torch.load('module.pt', encoding='ascii')
    Fr   r   TÚrbrX   Úencodingzutf-8N)rX   r   )r:   r5   rY   rZ   r[   r^   r\   r]   r•   Ú_loadr_   )r&   Úmap_locationrs   Úpickle_load_argsrb   r   r   r   ÚloadU  s    J

r�   c                sÚ  i ‰ˆd krt ‰nVtˆtƒr*‡fdd„‰n>tˆtƒrB‡fdd„‰n&tˆtjƒr\‡fdd„‰n‡fdd„‰dd„ ‰ ‡ ‡‡‡fdd	„}i ‰d
d„ ‰‡ ‡‡‡fdd„}t| ƒ t| ƒ}|o¾| jƒ dk�ry|| ƒS  t	j
k
�r   t| ƒrötdj| jƒƒ‚| jdƒ Y nX ˆj| fˆŽ}|tk�r&tdƒ‚ˆj| fˆŽ}|tk�rJtd| ƒ‚ˆj| fˆŽ}	ˆj| fˆŽ}
||
_|
jƒ }ˆj| fˆŽ}|�r�| jƒ nd }x@|D ]8}|ˆk�s¬t‚ˆ| j| ||ƒ |d k	�rš| jƒ }�qšW |S )Nc                s   ˆ j ||ƒ}t| |ƒS )N)ÚgetrS   )rM   r7   )r›   r   r   Úrestore_location¶  s    z_load.<locals>.restore_locationc                s
   t | ˆ ƒS )N)rS   )rM   r7   )r›   r   r   rŸ   º  s    c                s   t | tˆ ƒƒS )N)rS   r5   )rM   r7   )r›   r   r   rŸ   ½  s    c                s    ˆ | |ƒ}|d krt | |ƒ}|S )N)rS   )rM   r7   rR   )r›   r   r   rŸ   À  s    

c       
      S   sF  ydj t| ƒd ƒ}W n( tk
r>   tjd| j d ƒ d S X ||k�rB| j�r | jd }tj|j	dƒ|j	dƒ||dd�}dj |ƒ}ytt
|dƒ�L}|jdd	ƒ}|jdƒ |dkr¾|j|ƒ n|t|ƒksÖ|jƒ |krÚt‚W d Q R X d
| d | d }	W n" tk
�r   d| d }	Y nX nd}	djtj| ƒ|	ƒ}	tj|	tƒ d S )Nr   r   z4Couldn't retrieve source code for container of type z3. It won't be checked for correctness upon loading.z.patchÚ
)Zlinetermza+r   zSaved a reverse patch to z. Run `patch -p0 < z` to revert your changes.z;Tried to save a patch, but couldn't create a writable file zD. Make sure it doesn't exist and your working directory is writable.z¯you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.z)source code of class '{}' has changed. {})ry   r   rz   r{   r|   r   Zdump_patchesÚdifflibZunified_diffÚsplitr^   r    Úwriter#   r!   ÚIOErrorrB   r0   rL   r
   )
Zcontainer_typer}   Zoriginal_sourceZcurrent_sourceÚ	file_nameZdiffÚlinesr&   Ú	file_sizern   r   r   r   Ú_check_container_sourceÆ  s:    




z&_load.<locals>._check_container_sourcec          "      s6  i ‰ ‡‡ fdd„}t tj| dtjd�ƒ��}tƒ ��î}|jd|d� ttjj|dƒddƒ�š} ˆj	| fˆŽ}xDt
|ƒD ]8}ˆj	| fˆŽ}|\}}}	|	j| ƒ}
ˆ|
|ƒ}
|
ˆ |< qtW ˆj	| fˆŽ}x0|D ](\}}}}ˆ | }|||| … ˆ |< qÄW W d Q R X |jd	|d� ttjj|d	ƒddƒ�Ô} ˆj	| fˆŽ}x¾t
|ƒD ]²}ˆj	| fˆŽ}|\}}}ˆ | }t|ƒ}tjd
| jdƒƒ\}| jdƒ tjdj|ƒ| jd| ƒƒ}tjdj|ƒ| jd| ƒƒ}tjd| jdƒƒ\}|ƒ j||||ƒ}|ˆ |< �q8W W d Q R X |jdƒ}ˆj|fˆŽ}||_|j	ƒ }|S Q R X W d Q R X d S )Nc                s6   t | tƒr*t| dd … ƒr"ˆ | Ž  | d S ˆt| ƒ S )Nr   r   )r:   ÚtupleÚallr?   )Úsaved_id)r¨   Údeserialized_objectsr   r   Úpersistent_loadò  s
    
z3_load.<locals>.legacy_load.<locals>.persistent_loadzr:)Úfileobjr`   rB   Zstorages)r   r˜   r   Ztensorsz<ir   z<{}qé   z<qÚpickle)r   Útarfiler^   Ú
PAX_FORMATr   ÚextractÚosr   ry   r�   ÚrangeZ_new_with_filerW   ÚstructÚunpackr!   rB   Úset_ÚextractfileÚ	Unpicklerr­   )r&   r­   ÚtarZtmpdirZnum_storagesÚiÚargsr—   r7   rH   r3   Zstorage_viewsZtarget_cdataZ
root_cdatar   rG   ÚrootZnum_tensorsrN   Z
storage_idZoriginal_tensor_typerM   Ztensor_typeÚndimZstrideZstorage_offsetZtensorZpickle_fileÚ	unpicklerrR   )r¨   rœ   rs   rŸ   )r¬   r   Úlegacy_loadï  sJ    



"


z_load.<locals>.legacy_loadc             S   s   t | tƒr| jdƒS | S )NÚascii)r:   ÚbytesÚdecode)Z	bytes_strr   r   r   Úmaybe_decode_ascii%  s    

z!_load.<locals>.maybe_decode_asciic                sè   t | tƒst‚ˆ| d ƒ}| dd … }|dkrNt|dd … ƒrFˆ |Ž  |d S |dkrÔ|\}}}}}ˆ|ƒ}|ˆkr�||ƒ}d|_ˆ||ƒˆ|< ˆ| }	|d k	rÎ|\}
}}|
ˆkrÆ|	||| … ˆ|
< ˆ|
 S |	S ntd| d  ƒ‚d S )Nr   r   rV   rM   TzUnknown saved id type: %s)r:   r©   ÚAssertionErrorrª   rD   rA   )r«   rL   ÚdataZ	data_typeZroot_keyr7   rG   r€   r3   rM   Zview_keyr   Z	view_size)r¨   r¬   rÅ   rŸ   r   r   r­   0  s,    
z_load.<locals>.persistent_loadr   z;{} is a zip archive (did you mean to use torch.jit.load()?)z#Invalid magic number; corrupt file?zInvalid protocol version: %s)rS   r:   r‹   r<   r0   r;   rp   rj   r   r±   ÚTarErrorr*   rA   rB   Únamer    r�   r’   rŒ   rº   r­   rÆ   Z_set_from_file)r&   r›   rs   rœ   rÁ   r­   Zf_should_read_directlyr(   rˆ   Z	_sys_inforÀ   rR   Zdeserialized_storage_keysr   r—   r   )r¨   r¬   r›   rÅ   rœ   rs   rŸ   r   rš   °  sT    

)4



rš   )8r¡   r´   rh   r   r¶   rY   r0   r±   r   r{   Ú
contextlibr   r   Ú_utilsr   Z_sixr   r<   Ztorch._utils_internalr   rZ   ÚcPickler°   r\   ÚDEFAULT_PROTOCOLÚStructrG   r�   r�   rŽ   r’   rŒ   ZSTORAGE_KEY_SEPARATORÚWarningr
   r   r+   r*   r/   r4   r6   r8   rC   rI   rO   rS   rT   rW   rc   rf   rj   rp   rv   rr   r�   rš   r   r   r   r   Ú<module>   s`   
%	
N[