
    Vhw                        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             Z e	j(                  eg        G d	 d
      Ze efdededededdf
dZ	 	 ddddedededededefdZy)    N)	dataclass)BufferedIOBase)Any)_load_saveDEFAULT_PROTOCOLMAP_LOCATION__all__c                   ,    e Zd ZU eed<   eed<   eed<   y)_Entrykey
is_storagelengthN)__name__
__module____qualname__str__annotations__boolint     P/home/dcms/DCMS/lib/python3.12/site-packages/torch/distributed/_serialization.pyr   r      s    	HKr   r   c                       e Zd ZddZdedededdfdZdeddfd	Z	deddfd
Z
dedefdZdedefdZdedededej                  fdZdefdZy)_PseudoZipFilereturnNc                     i | _         y Nrecordsselfs    r   __init__z_PseudoZipFile.__init__   s	    68r   r   datar   c                 &    ||f| j                   |<   y r   r   )r"   r   r$   r   s       r   write_recordz_PseudoZipFile.write_record   s    !6NSr   fc           
      n   g }| j                   j                         D ]=  \  }\  }}|j                  t        |t	        |t
        j                        |             ? t        j                  ||t               | j                   j                         D ]  \  }\  }}t	        |t              r|j                  |       +t	        |t              r!|j                  |j                  d             \t	        |t
        j                        r|j                  |ddd       t        dt!        |              y )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   s         r   write_toz_PseudoZipFile.write_to   s    #'<<#5#5#7 	C$NN)$0D0DE!	 	GQ)9:#'<<#5#5#7 	?C$$&D#&G,-D%"6"67  E5!4.d =>>	?r   c                    t        j                  |      }|D ]  }|j                  |j                        }|j                  rYt        j                  |t
        j                        j                         }||j                  f| j                  |j                  <   ||j                  f| j                  |j                  <    y )Ndtype)_weights_only_unpicklerloadreadr   r   r/   
frombufferuint8untyped_storager    r   )r"   r'   r9   entryr$   storages         r   	read_fromz_PseudoZipFile.read_from6   s    )..q1 	?E66%,,'D**++ "/#  LL+UYY'
 ,0*>UYY'	?r   c                     || j                   v S r   r   r"   r   s     r   
has_recordz_PseudoZipFile.has_recordH   s    dll""r   c                 &    | j                   |   d   S )Nr   r   rH   s     r   
get_recordz_PseudoZipFile.get_recordK   s    ||C ##r   _length_typec                 l    t        j                  | j                  |   d   t         j                        S )Nr   r<   )r/   tensorr    rB   )r"   r   rL   rM   s       r   get_storage_from_recordz&_PseudoZipFile.get_storage_from_recordN   s'     ||DLL-a0DDr   c                      y)Ntorchftr   r!   s    r   serialization_idz_PseudoZipFile.serialization_idS   s    r   )r   N)r   r   r   r#   r   objectr   r&   r   r:   rF   r   rI   rK   r/   TensorrP   rS   r   r   r   r   r      s    9+ +6 +3 +4 +?. ?T ?.?> ?d ?$#c #d #$c $f $EE!$E-0E	E
# r   r   objr'   pickle_modulepickle_protocolr   c                 Z    t               }t        | |||d       |j                  |       y)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_filerW   rX   _disable_byteorder_recordN)r   r   r:   )rV   r'   rW   rX   rZ   s        r   _streaming_saver\   W   s3    * H	#'"' ar   T)weights_onlymap_locationr]   pickle_load_argsc                    |r|t        d      t        }n|t        }d|j                         vrd|d<   t	               }|j                  |        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.
    zDCan not safely load weights when explicit pickle_module is specifiedencodingr*   )rZ   r^   rW   r   )RuntimeErrorr>   r1   keysr   rF   r   )r'   r^   rW   r]   r_   rZ   s         r   _streaming_loadrd   w   s      $V  0 "M)..00'.$Hq !# 	 r   )NN)r1   dataclassesr   ior   typingr   r/   torch._weights_only_unpicklerr>   torch.serializationr   r   r   r	   r
   listr   r   r   _add_safe_globalsr   rT   r   r\   r   rd   r   r   r   <module>rl      s     !    ? L L c     *  ) )6( 3< <D  +		  	
 
D "&$
 $$$ $
 $ $ $r   