Ë
    ÄÓ^jI  ã                   óÒ   — 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
 d dlmZ d dlmZ d dlmZmZmZmZ e
rd d	lmZ d
dgZ G d„ de«      Z G d„ d
e«      Z G d„ de«      Zy)é    N)Ú	GeneratorÚSequence)Úcontextmanager)ÚPath)ÚTYPE_CHECKING)Ú	url_to_fs)ÚStreamTransformExtension)ÚFileSystemBaseÚFileSystemReaderÚFileSystemWriterÚSerializationFormat)ÚAbstractFileSystemÚFsspecWriterÚFsspecReaderc            
       ó"  — e Zd Zdd„Zedeej                  z  dedee	j                  ddf   fd„«       Zdeej                  z  dedeej                  z  fd„Zdeej                  z  deej                  z  fd	„Zdeej                  z  d
eej                  z  ddfd„Zdeej                  z  ddfd„Zedeej                  z  defd„«       Zdeej                  z  defd„Zdeej                  z  ddfd„Zdeej                  z  dee   fd„Zy)Ú
FileSystemÚreturnNc                 ó   — d | _         y ©N)Úfs)Úselfs    úz/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/distributed/checkpoint/_fsspec_filesystem.pyÚ__init__zFileSystem.__init__!   s	   € Ø-1ˆ�ó    ÚpathÚmodec              #   ó<  ‡K  — | j                   €t        d«      ‚t        j                  |«      }| j                   j	                  |‰«      5 }	 |–— 	 d d d «       y #  t        ˆfd„dD «       «      r	 | j                  |«       ‚ #  Y ‚ xY w‚ xY w# 1 sw Y   y xY w­w)Nzfs should not be Nonec              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­wr   © )Ú.0Úchr   s     €r   ú	<genexpr>z+FileSystem.create_stream.<locals>.<genexpr>3   s   øè ø€ Ò2 b�r˜T”zÑ2ùs   ƒzw+a)r   ÚAssertionErrorÚosÚfspathÚopenÚanyÚrm_file)r   r   r   Ústreams     ` r   Úcreate_streamzFileSystem.create_stream$   sž   øè ø€ ð �7‰7ˆ?Ü Ð!8Ó9Ð9Ü�y‰y˜‹ˆð
 �W‰W�\‰\˜$ Ó%ð 		¨ðØ“÷		ð 		øðÜÓ2¨EÔ2Ô2ðØŸ™ TÔ*ð øðØØú�ú÷		ð 		üsH   ƒA	BÁBÁAÁ	BÁBÁ3BÂBÂB	ÂBÂBÂBÂBÚsuffixc                 óB   — t         j                  j                  ||«      S r   )r$   r   Újoin)r   r   r+   s      r   Úconcat_pathzFileSystem.concat_path:   s   € Ü�w‰w�|‰|˜D &Ó)Ð)r   c                 ó.   — t        |fi |¤Ž\  | _        }|S r   )r   r   )r   r   ÚkwargsÚ_s       r   Ú	init_pathzFileSystem.init_path=   s   € Ü˜tÑ. vÑ.‰
ˆŒ�Øˆr   Únew_pathc                 ó<   — | j                   j                  ||«       y r   )r   Úrename)r   r   r3   s      r   r5   zFileSystem.renameA   s   € Ø�‰�‰�t˜XÕ&r   c                 ó>   — | j                   j                  |d¬«       y )NT)Úexist_ok)r   Úmakedirs©r   r   s     r   ÚmkdirzFileSystem.mkdirD   s   € Ø�‰×Ñ˜¨ÐÕ-r   Úcheckpoint_idc                 ó\   — t        |t        «      ry	 t        |«       y# t        $ r Y yw xY w)NFT)Ú
isinstancer   r   Ú
ValueError©Úclsr;   s     r   Úvalidate_checkpoint_idz!FileSystem.validate_checkpoint_idG   s5   € ä�m¤TÔ*Øð	Ü�mÔ$ð øô ò 	Ùð	ús   “ Ÿ	+ª+c                 ó8   — | j                   j                  |«      S r   )r   Úexistsr9   s     r   rC   zFileSystem.existsS   s   € Ø�w‰w�~‰~˜dÓ#Ð#r   c                 ó:   — | j                   j                  |«       y r   )r   Úrmr9   s     r   r(   zFileSystem.rm_fileV   s   € Ø�‰�
‰
�4Õr   c                 ó<   — | j                   j                  |d¬«      S )NF)Údetail)r   Úlsr9   s     r   rH   zFileSystem.lsY   s   € ð �w‰w�z‰z˜$ uˆzÓ-Ð-r   )r   N)Ú__name__Ú
__module__Ú__qualname__r   r   Ústrr$   ÚPathLiker   ÚioÚIOBaser*   r.   r2   r5   r:   ÚclassmethodÚboolrA   rC   r(   ÚlistrH   r   r   r   r   r       sh  „ ó2ð ðØ˜"Ÿ+™+Ñ%ðØ-0ðà	�2—9‘9˜d DÐ(Ñ	)òó ðð**  b§k¡kÑ 1ð *¸3ð *À3ÈÏÉÑCTó *ð˜c B§K¡KÑ/ð ¸cÀBÇKÁKÑ>Oó ð'˜3 §¡Ñ,ð '¸¸b¿k¹kÑ8Ið 'Èdó 'ð.˜# §¡Ñ+ð .°ó .ð ð	°3¸¿¹Ñ3Dð 	Èò 	ó ð	ð$˜3 §¡Ñ,ð $°ó $ð˜C "§+¡+Ñ-ð °$ó ð.�s˜RŸ[™[Ñ(ð .¨T°#©Yô .r   r   c                   óÆ   ‡ — e Zd ZdZddddddej
                  fdeej                  z  de	de	d	e
d
e
de	dee   dz  deddfˆ fd„Zedeej                  z  de	fd„«       Zˆ xZS )r   a`  
    Basic implementation of StorageWriter using FFspec.

    This implementation makes the following assumptions and simplifications:

    * The checkpoint path is an empty or non-existing directory.
    * File creation is atomic

    The checkpoint consist of one file per write request plus
    a `.metadata` file with the serialized metadata.

    Té   i€–˜ Nr   Úsingle_file_per_rankÚ
sync_filesÚthread_countÚper_thread_copy_aheadÚ	overwriteÚ_extensionsÚserialization_formatr   c	           
      ó–   •— t         ‰
| �  ||||||||¬«       t        «       | _         | j                  j                  |fi |	¤Ž| _        y)a=  
        Initialize the writer pointing to `path`.

        Args:
            path: directory where the checkpoint will be written to.
            single_file_per_rank: Produce one file per rank instead of one file per tensor/blob. Default to True.
            sync_files : force files to be synced to permanent storage. Default to True.
            thread_count: Number of IO threads to use to write. Default to 1.
            per_thread_copy_ahead: How many bytes to copy from the GPU ahead of saving them. Default 10Mb.
            overwrite: Whether to allow overwriting existing checkpoints. Defaults to True.
            _extensions: Extensions to apply to output streams (EXPERIMENTAL)

        N. B. If sync_files is disabled, there's no guarantee that the checkpoint will be consistent in the case of a failure.
        )rY   rZ   r[   N©Úsuperr   r   r   r2   r   )r   r   rU   rV   rW   rX   rY   rZ   r[   r0   Ú	__class__s             €r   r   zFsspecWriter.__init__n   sW   ø€ ô4 	‰ÑØØ ØØØ!ØØ#Ø!5ð 	ô 		
ô “,ˆŒØ%�D—G‘G×%Ñ% dÑ5¨fÑ5ˆ�	r   r;   c                 ó,   — t         j                  |«      S r   ©r   rA   r?   s     r   rA   z#FsspecWriter.validate_checkpoint_id•   ó   € ä×0Ñ0°Ó?Ð?r   )rI   rJ   rK   Ú__doc__r   Ú
TORCH_SAVErL   r$   rM   rQ   Úintr   r	   r   rP   rA   Ú__classcell__©r_   s   @r   r   r   `   sÆ   ø„ ñð  &*ØØØ%/ØØAEØ4G×4RÑ4Rñ%6à�B—K‘KÑð%6ð #ð%6ð ð	%6ð
 ð%6ð  #ð%6ð ð%6ð Ð6Ñ7¸$Ñ>ð%6ð 2ð%6ð 
õ%6ðN ð@°3¸¿¹Ñ3Dð @Èò @ó ô@r   c                   óv   ‡ — e Zd Zdeej
                  z  ddfˆ fd„Zedeej
                  z  defd„«       Z	ˆ xZ
S )r   r   r   Nc                 ó†   •— t         ‰| �  |«       t        «       | _         | j                  j                  |fi |¤Ž| _        y r   r]   )r   r   r0   r_   s      €r   r   zFsspecReader.__init__›   s6   ø€ Ü‰Ñ˜ÔÜ“,ˆŒØ%�D—G‘G×%Ñ% dÑ5¨fÑ5ˆ�	r   r;   c                 ó,   — t         j                  |«      S r   ra   r?   s     r   rA   z#FsspecReader.validate_checkpoint_id    rb   r   )rI   rJ   rK   rL   r$   rM   r   rP   rQ   rA   rf   rg   s   @r   r   r   š   sN   ø„ ð6˜S 2§;¡;Ñ.ð 6¸Tõ 6ð
 ð@°3¸¿¹Ñ3Dð @Èò @ó ô@r   )rN   r$   Úcollections.abcr   r   Ú
contextlibr   Úpathlibr   Útypingr   Úfsspec.corer   Ú'torch.distributed.checkpoint._extensionr	   Ú'torch.distributed.checkpoint.filesystemr
   r   r   r   Úfsspecr   Ú__all__r   r   r   r   r   r   ú<module>rt      sl   ðó 
Û 	ß /Ý %Ý Ý  å !å L÷ó ñ Ý)ð Øð€ô<.�ô <.ô@7@Ð#ô 7@ôt@Ð#õ @r   