Ë
    ÄÓ^jÕ  ã                   óf  — d 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 dd	lmZmZmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ defd„Zdeddfd„Zdedefd„Z e«       ddfdededz  dedz  defd„Z e«       dededfdededz  ded   deedf   dedef   de e!ef   dz  defd„Z"y)a  
Factory functions for creating checkpointer instances with sensible defaults.

This module provides high-level factory functions that simplify the creation
of checkpointer instances by automatically handling component initialization
and configuration with reasonable defaults.
é    )ÚCallable)ÚAnyNé   )Úcreate_barrier_from_config)ÚCheckpointProcess)ÚCheckpointReader)ÚCheckpointWriterÚCheckpointWriterConfigÚ
WriterHook)ÚAsyncCheckpointerÚSyncCheckpointer)ÚCheckpointerConfig)ÚDefaultStager)ÚRankInfoÚreturnc                  ó¦   — t        j                  «       r1t        t        j                  «       t        j                  «       ¬«      S t        dd¬«      S )zá
    Get default rank information from the current distributed environment.

    Returns:
        RankInfo: Rank information from the default process group if initialized,
                 otherwise single-rank fallback.
    )Úglobal_world_sizeÚglobal_rankr   r   )ÚdistÚis_initializedr   Úget_world_sizeÚget_rank© ó    ú}/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/distributed/checkpoint/_experimental/builder.pyÚ_get_default_rank_infor      sA   € ô ×ÑÔÜÜ"×1Ñ1Ó3ÜŸ™›ô
ð 	
ô ¨!¸Ô;Ð;r   Ú_c                   ó   — y)z3Default subprocess initialization function (no-op).Nr   )r   s    r   Údefault_subprocess_init_fnr   *   s   � r   Ú	rank_infoc                 ó,   — t        t        «       | ¬«      S )z2Default checkpoint writer initialization function.)Úconfigr    )r	   r
   ©r    s    r   Údefault_writer_init_fnr$   .   s   € äÜ%Ó'Øôð r   r"   Úcommit_hookc                 ó¨   — |€
t        «       }t        |¬«      }t        | j                  «      }t	        | j
                  |||¬«      }t        ||¬«      S )a‘  
    Factory function to create a SyncCheckpointer instance with sensible defaults.

    This function creates a synchronous checkpointer with default components, automatically
    detecting rank information from the default process group if available, and using the
    provided component configurations.

    Args:
        config: CheckpointerConfig containing component-specific configurations
               (writer_config, staging_config, process_config). Defaults to CheckpointerConfig().
        rank_info: RankInfo for distributed training. Defaults to auto-detection from
                  the default PyTorch distributed process group if initialized, otherwise
                  falls back to single-rank (world_size=1, rank=0).
        commit_hook: Optional hook for custom actions before and after checkpoint commits.

    Returns:
        SyncCheckpointer: A configured synchronous checkpointer instance.

    Examples:
        # Simplest usage - auto-detect rank, default config
        checkpointer = make_sync_checkpointer()

        # Explicit rank configuration
        checkpointer = make_sync_checkpointer(
            rank_info=RankInfo(global_world_size=4, global_rank=0)
        )

        # Disable barrier
        from .barriers import BarrierConfig
        config = CheckpointerConfig(barrier_config=BarrierConfig(barrier_type=None))
        checkpointer = make_sync_checkpointer(config=config)
    r#   )r"   r    Úbarrierr%   )ÚwriterÚreader)r   r   r   Úbarrier_configr	   Úwriter_configr   )r"   r    r%   r)   r'   r(   s         r   Úmake_sync_checkpointerr,   6   sc   € ðJ ÐÜ*Ó,ˆ	äØô€Fô )¨×)>Ñ)>Ó?€GäØ×#Ñ#ØØØô	€Fô ØØôð r   r   Úsubprocess_init_fn).NÚsubprocess_init_args.Úcheckpoint_writer_init_fnÚcheckpoint_writer_init_argsc                 ó¼   — |€
t        «       }t        |¬«      }t        | j                  ¬«      }|xs i }t	        || j
                  ||||¬«      }t        |||¬«      S )aw  
    Factory function to create an AsyncCheckpointer instance with sensible defaults.

    This function creates an asynchronous checkpointer using the provided configuration,
    automatically detecting rank information if not provided.

    Args:
        config: CheckpointerConfig containing component-specific configurations.
        rank_info: RankInfo for distributed training. Defaults to auto-detection.
        subprocess_init_fn: Function to initialize the subprocess. Defaults to no-op.
        subprocess_init_args: Arguments to pass to subprocess_init_fn.
        checkpoint_writer_init_fn: Function to create CheckpointWriter instance.
        checkpoint_writer_init_args: Arguments to pass to checkpoint_writer_init_fn.

    Returns:
        AsyncCheckpointer: A configured asynchronous checkpointer instance.

    Examples:
        # Create with default config
        checkpointer = make_async_checkpointer()

        # Create with custom init functions
        checkpointer = make_async_checkpointer(
            subprocess_init_fn=my_subprocess_init_fn,
            checkpoint_writer_init_fn=my_writer_init_fn
        )
    r#   )r"   )r    r"   r-   r.   r/   r0   )Úcheckpoint_stagerÚcheckpoint_processr)   )r   r   r   Ústaging_configr   Úprocess_configr   )	r"   r    r-   r.   r/   r0   r)   r2   r3   s	            r   Úmake_async_checkpointerr6   q   s~   € ðF ÐÜ*Ó,ˆ	äØô€Fô &Ø×$Ñ$ôÐð #>Ò"CÀÐä*ØØ×$Ñ$Ø-Ø1Ø";Ø$?ôÐô Ø+Ø-Øôð r   )#Ú__doc__Úcollections.abcr   Útypingr   Útorch.distributedÚdistributedr   Úbarriersr   r3   r   Úcheckpoint_readerr   Úcheckpoint_writerr	   r
   r   Úcheckpointerr   r   r"   r   Ústagingr   Útypesr   r   r   r$   r,   ÚtupleÚdictÚstrr6   r   r   r   ú<module>rE      s+  ðñõ %Ý å  å 0Ý 1Ý /ß SÑ Sß =Ý &Ý "Ý ð< ó <ð$> 3ð >¨4ó >ð hð Ð3Có ñ "4Ó!5Ø!%Ø%)ñ8Øð8à˜$‰ð8ð ˜dÑ"ð8ð ó	8ñx "4Ó!5Ø!%Ø.HØ,.ØAWØ9=ñ=Øð=à˜$‰ð=ð ! Ñ+ð=ð    S ™/ð	=ð
  (¨Ð-=Ð(=Ñ>ð=ð "& c¨3 h¡°$Ñ!6ð=ð ô=r   