o
    Zhw                     @   s   U d dl Z d dlmZ d dlmZ d dlmZ d dlZd dlm	Z	 d dl
mZmZmZmZ g Zee ed< eG dd dZe	eg G d	d
 d
Ze efdededededdf
ddZ		ddddedededededefddZdS )    N)	dataclass)BufferedIOBase)Any)_load_saveDEFAULT_PROTOCOLMAP_LOCATION__all__c                   @   s&   e Zd ZU eed< eed< eed< dS )_Entrykey
is_storagelengthN)__name__
__module____qualname__str__annotations__boolint r   r   O/var/www/auris/lib/python3.10/site-packages/torch/distributed/_serialization.pyr
      s   
 r
   c                   @   s   e Zd ZdddZdedededdfdd	Zd
eddfddZ	d
eddfddZ
dedefddZdedefddZdedededejfddZdefddZdS )_PseudoZipFilereturnNc                 C   s
   i | _ d S Nrecordsselfr   r   r   __init__      
z_PseudoZipFile.__init__r   datar   c                 C   s   ||f| j |< d S r   r   )r   r   r    r   r   r   r   write_record   s   z_PseudoZipFile.write_recordfc                 C   s   g }| j  D ]\}\}}|t|t|tj|d qtj||t	d | j  D ]6\}\}}t|t
r:|| q)t|trH||d q)t|tjrW||ddd q)tdt| d S )N)r   r   r   )protocolutf-8F   zunknown type: )r   itemsappendr
   
isinstancetorchZUntypedStoragepickledumpr   byteswriter   encodeZ_write_file	TypeErrortype)r   r"   entriesr   r    r   r   r   r   write_to   s&   


z_PseudoZipFile.write_toc                 C   sd   t |}|D ](}||j}|jr&tj|tjd }||jf| j	|j
< q||jf| j	|j
< qd S )NZdtype)_weights_only_unpicklerloadreadr   r   r)   Z
frombufferuint8Zuntyped_storager   r   )r   r"   r1   entryr    Zstorager   r   r   	read_from6   s   
z_PseudoZipFile.read_fromc                 C   s
   || j v S r   r   r   r   r   r   r   
has_recordH   r   z_PseudoZipFile.has_recordc                 C   s   | j | d S )Nr   r   r:   r   r   r   
get_recordK   s   z_PseudoZipFile.get_record_length_typec                 C   s   t j| j| d t jdS )Nr   r3   )r)   Ztensorr   r7   )r   r   r=   r>   r   r   r   get_storage_from_recordN   s   z&_PseudoZipFile.get_storage_from_recordc                 C   s   dS )NZtorchftr   r   r   r   r   serialization_idS   s   z_PseudoZipFile.serialization_id)r   N)r   r   r   r   r   objectr   r!   r   r2   r9   r   r;   r<   r)   ZTensorr?   r@   r   r   r   r   r      s"    

r   objr"   pickle_modulepickle_protocolr   c                 C   s&   t  }t| |||dd || dS )a4  
    Save the object to a file-like object in a streaming fashion compatible with
    network sockets.

    This behaves similarly to :func:`torch.save` with a few notable differences:

    * A non-seekable file like object can be used when loading.
    * No forwards/backwards compatiblity is provided for the serialization
      format. This is only intended to be used with a single version of PyTorch
      with transient storage (i.e. sockets or temp files).
    * mmap is not supported

    See :func:`torch.save` for more details on specific arguments.
    F)zip_filerC   rD   Z_disable_byteorder_recordN)r   r   r2   )rB   r"   rC   rD   rE   r   r   r   _streaming_saveW   s   rF   T)weights_onlymap_locationrG   pickle_load_argsc                K   s`   |r|dur
t dt}n|du rt}d| vrd|d< t }||  td|||d|S )a  
    Load the object from a file-like object in a streaming fashion compatible with
    network sockets.

    See :func:`_streaming_save` for more details about the streaming behavior.

    See :func:`torch.load` for more details on specific arguments.
    NzDCan not safely load weights when explicit pickle_module is specifiedencodingr$   )rE   rH   rC   r   )RuntimeErrorr4   r*   keysr   r9   r   )r"   rH   rC   rG   rI   rE   r   r   r   _streaming_loadw   s&   
rM   )NN)r*   dataclassesr   ior   typingr   r)   Ztorch._weights_only_unpicklerr4   Ztorch.serializationr   r   r   r   r	   listr   r   r
   Z_add_safe_globalsr   rA   r   rF   r   rM   r   r   r   r   <module>   sT   
 B
"