§
    `ƒiÐ  ã                   ó   — d Z ddlmZ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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e         dee         defd„Z e¦   «         dededfdedee         ded         deedf         dedef         dee e!ef                  defd„Z"dS )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.
é    )ÚAnyÚCallableÚOptionalNé   )Úcreate_barrier_from_config)ÚCheckpointProcess)ÚCheckpointReader)ÚCheckpointWriterÚCheckpointWriterConfigÚ
WriterHook)ÚAsyncCheckpointerÚSyncCheckpointer)ÚCheckpointerConfig)ÚDefaultStager)ÚRankInfoÚreturnc                  ó°   — t          j        ¦   «         r3t          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© ó    ú†/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/distributed/checkpoint/_experimental/builder.pyÚ_get_default_rank_infor      sV   € õ ÔÑÔð <ÝÝ"Ô1Ñ3Ô3Ýœ™œð
ñ 
ô 
ð 	
õ ¨!¸Ð;Ñ;Ô;Ð;r   Ú_c                  ó   — dS )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)
    Nr$   )r#   r!   Úbarrierr&   )ÚwriterÚreader)r   r	   r   Úbarrier_configr
   Úwriter_configr   )r#   r!   r&   r*   r(   r)   s         r   Úmake_sync_checkpointerr-   5   s   € ð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        ¬¦  «        }|p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
        )
    Nr$   )r#   )r!   r#   r.   r/   r0   r1   )Úcheckpoint_stagerÚcheckpoint_processr*   )r   r	   r   Ústaging_configr   Úprocess_configr   )	r#   r!   r.   r/   r0   r1   r*   r3   r4   s	            r   Úmake_async_checkpointerr7   p   s£   € ðF ÐÝ*Ñ,Ô,ˆ	åØðñ ô €Fõ &ØÔ$ðñ ô Ðð #>Ð"CÀÐå*ØØÔ$Ø-Ø1Ø";Ø$?ðñ ô Ðõ Ø+Ø-Øðñ ô ð r   )#Ú__doc__Útypingr   r   r   Útorch.distributedÚdistributedr   Úbarriersr   r4   r   Úcheckpoint_readerr	   Úcheckpoint_writerr
   r   r   Úcheckpointerr   r   r#   r   Ústagingr   Útypesr   r   r    r%   r-   ÚtupleÚdictÚstrr7   r   r   r   ú<module>rE      sK  ððð ð +Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à  Ð  Ð  Ð  Ð  Ð  à 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø /Ð /Ð /Ð /Ð /Ð /Ø SÐ SÐ SÐ SÐ SÐ SÐ SÐ SÐ SÐ SØ =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ø &Ð &Ð &Ð &Ð &Ð &Ø "Ð "Ð "Ð "Ð "Ð "Ø Ð Ð Ð Ð Ð ð< ð <ð <ð <ð <ð$> 3ð >¨4ð >ð >ð >ð >ð hð Ð3Cð ð ð ð ð "4Ð!3Ñ!5Ô!5Ø$(Ø(,ð8ð 8Øð8à˜Ô!ð8ð ˜*Ô%ð8ð ð	8ð 8ð 8ð 8ðx "4Ð!3Ñ!5Ô!5Ø$(Ø.HØ,.ØAWØ<@ð=ð =Øð=à˜Ô!ð=ð ! Ô+ð=ð    S œ/ð	=ð
  (¨Ð-=Ð(=Ô>ð=ð "*¨$¨s°C¨x¬.Ô!9ð=ð ð=ð =ð =ð =ð =ð =r   