
    9j                     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     r/media/conek/DATA/Code/OCR/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,.AW9===$= !+=  S/	=
  (-=(=>= "&c3h$!6= =r   