o
    hw                     @   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   t/var/www/html/construction_image-detection-poc/venv/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torchUntypedStoragepickledumpr   byteswriter   encode_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dtype)_weights_only_unpicklerloadreadr   r   r)   
frombufferuint8untyped_storager   r   )r   r"   r3   entryr    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   r5   )r)   tensorr   r;   )r   r   rC   rD   r   r   r   get_storage_from_recordN   s   z&_PseudoZipFile.get_storage_from_recordc                 C   s   dS )Ntorchftr   r   r   r   r   serialization_idS   s   z_PseudoZipFile.serialization_id)r   N)r   r   r   r   r   objectr   r!   r   r4   r?   r   rA   rB   r)   TensorrF   rH   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_filerL   rM   _disable_byteorder_recordN)r   r   r4   )rK   r"   rL   rM   rN   r   r   r   _streaming_saveW   s   rP   T)weights_onlymap_locationrQ   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$   )rN   rR   rL   r   )RuntimeErrorr7   r+   keysr   r?   r   )r"   rR   rL   rQ   rS   rN   r   r   r   _streaming_loadw   s&   
rW   )NN)r+   dataclassesr   ior   typingr   r)   torch._weights_only_unpicklerr7   torch.serializationr   r   r   r   r	   listr   r   r
   _add_safe_globalsr   rI   r   rP   r   rW   r   r   r   r   <module>   sT   
 B
"