+
    –°³j®#  ã                   óV  a € R t 0 t R t^ RIt^ RIt^ RIHt ^ RIHtHt ^ RI	H
t
 ^ RIHtHt ^ RIHt ^ RIHu Hu Hu Ht ]P,                  ! 4       t/ t] ^ k R R lt] ! R R	4      4       tR
 R lt ! R R]P8                  4      t] ! R R]4      4       t] ! R R]4      4       tR# )a/  
Barrier implementations for synchronizing distributed checkpoint operations.

This module provides abstract and concrete barrier implementations that ensure
all ranks in a distributed training environment complete their checkpoint operations
before proceeding, which is essential for data consistency.
N)ÚCounter)Ú	dataclassÚfield)Ú	timedelta)ÚAnyÚOptionalc                ó0   € V ^8„  d   QhR\         R\         /# )é   Úbarrier_classÚreturn)Útype)Úformats   "Út/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/distributed/checkpoint/_experimental/barriers.pyÚ__annotate__r      s   € ÷ ñ ¤Dð ¬Tñ ó    c                óP   € \        V R4      '       d   V \        V P                  &   V # )z0Register a barrier class in the global registry.Úbarrier_type)ÚhasattrÚBARRIER_REGISTRYr   )r
   s   &r   Úregister_barrierr      s&   € äˆ}˜n×-Ò-Ø7DÔ˜×3Ñ3Ñ4ØÐr   c                   óF   a € ] tR t^"t o RtRt]! ]R7      tV 3R lt	Rt
V tR# )ÚBarrierConfigaÆ  
Configuration for barrier construction.

This class provides a flexible way to configure different barrier implementations
with their specific constructor arguments. The barrier type will be looked up
from a registry and instantiated with rank_info and barrier_args.

Attributes:
    barrier_type: A string identifying the barrier type (e.g., "tcp_store").
                 If None, no barrier will be used.
    barrier_args: Dictionary of arguments to pass to the barrier constructor.
                 rank_info will be automatically injected as the first argument.

Examples:
    # No barrier
    BarrierConfig()

    # TCPStore barrier
    BarrierConfig(
        barrier_type="tcp_store",
        barrier_args={
            'timeout_barrier_init_secs': 30,
            'barrier_prefix_list': ['checkpoint'],
            'use_checkpoint_barrier_tcpstore_libuv': False,
            'tcpstore_port': 12345,
            'master_address': 'localhost'
        }
    )
N)Údefault_factoryc                óV   <€ V ^8„  d   Qh/ S[ R,          ;R&   S[S[ S[3,          ;R&   # )r	   Nr   Úbarrier_args)ÚstrÚdictr   )r   Ú__classdict__s   "€r   r   ÚBarrierConfig.__annotate__"   s/   ø‡ ‚ ñ@ ˜•*Ñ#ñA ñB ‘s™C�x•.Ñ>òC r   © )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   r   r   Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__©r   s   @r   r   r   "   s#   ø‡ € ñð<  $€LÙ#(¸Ô#>€L÷C ƒ r   r   c                ó>   € V ^8„  d   QhR\         R\        R,          /# )r	   Úbarrier_configr   ÚBarrier)r   r   )r   s   "r   r   r   F   s!   € ÷ 8ñ 8Ü!ð8äˆiÕñ8r   c           	     ó
  € V P                   f   R# V P                   \        9  d7   \        RV P                    R\        \        P	                  4       4       24      h\        V P                   ,          pV! R/ V P
                  B # )a  
Create a barrier instance from BarrierConfig.

Args:
    barrier_config: Configuration for barrier construction.

Returns:
    Barrier instance or None if no barrier type is configured.

Raises:
    ValueError: If the barrier_type is not found in the registry.
NzUnknown barrier type: z. Available types: r   )r   r   Ú
ValueErrorÚlistÚkeysr   )r*   r
   s   & r   Úcreate_barrier_from_configr0   F   s„   € ð ×"Ñ"Ò*Ùà×"Ñ"Ô*:Ô:ÜØ$ ^×%@Ñ%@Ð$Að B Ü $Ô%5×%:Ñ%:Ó%<Ó =Ð>ð@ó
ð 	
ô
 % ^×%@Ñ%@ÕA€MÙÑ7˜>×6Ñ6Ñ7Ð7r   c                   ó„   a € ] tR t^bt o Rt]P                  V 3R lR l4       t]P                  V 3R lR l4       tRt	V t
R# )r+   a  
Abstract base class for synchronization barriers.

A barrier ensures that all ranks in a distributed environment reach a certain
point in execution before any rank proceeds further, which is essential for
coordinating operations like checkpointing across multiple processes.
c                ó6   <€ V ^8„  d   QhRS[ S[S[3,          /# )r	   Úkwargs)r   r   r   )r   r   s   "€r   r   ÚBarrier.__annotate__l   s   ø€ ÷ ñ ¡¡c©3 h¥ñ r   c                ó   € R# )zû
Initialize a barrier.

Args:
    **kwargs: Keyword arguments for specific barrier implementations.
             Common arguments may include rank information, barrier prefixes,
             timeout settings, and other barrier-specific configuration.
Nr   )Úselfr3   s   &,r   Ú__init__ÚBarrier.__init__k   ó   ‚ r   c                ó   <€ V ^8„  d   QhRR/# ©r	   r   Nr   )r   r   s   "€r   r   r4   x   s   ø€ ÷ ñ  ñ r   c                ó   € R# )z˜
Execute a synchronization barrier.

This method uses the barrier_prefix provided during initialization to
coordinate synchronization across processes.
Nr   ©r6   s   &r   Úexecute_barrierÚBarrier.execute_barrierw   r9   r   r   N)r    r!   r"   r#   r$   ÚabcÚabstractmethodr7   r>   r&   r'   r(   s   @r   r+   r+   b   s>   ø‡ € ñð 	×Ñ÷ó ðð 	×Ñ÷ó ör   r+   c                   óL   a € ] tR t^�t o RtRtV 3R lR ltV 3R lR ltRtV t	R# )	ÚDistBarriera  
A barrier implementation using PyTorch's distributed barrier for synchronization.

This barrier uses the built-in torch.distributed.barrier() function to coordinate
synchronization across multiple processes. It's simpler than TCPStoreBarrier but
requires an initialized process group.
Údist_barrierc                ó   <€ V ^8„  d   QhRR/# r;   r   )r   r   s   "€r   r   ÚDistBarrier.__annotate__�   s   ø€ ÷ Wñ Wà	ñWr   c                óR   € \         P                  ! 4       '       g   \        R4      hR# )zÿ
Initialize a DistBarrier.

This barrier requires an initialized PyTorch distributed process group.
No additional arguments are needed as it uses the current process group.

Raises:
    AssertionError: If the distributed process group is not initialized.
z2DistBarrier requires an initialized process group.N)ÚdistÚis_initializedÚAssertionErrorr=   s   &r   r7   ÚDistBarrier.__init__�   s$   € ô ×"Ò"×$Ò$Ü Ð!UÓVÐVñ %r   c                ó   <€ V ^8„  d   QhRR/# r;   r   )r   r   s   "€r   r   rF   œ   s   ø€ ÷ ñ  ñ r   c                ó0   € \         P                  ! 4        R# )zT
Execute a synchronization barrier using the prefix provided during initialization.
N)rH   Úbarrierr=   s   &r   r>   ÚDistBarrier.execute_barrierœ   s   € ô 	�ŠŽr   r   N©
r    r!   r"   r#   r$   r   r7   r>   r&   r'   r(   s   @r   rC   rC   �   s'   ø‡ € ñð "€L÷Wð W÷ö r   rC   c                   óL   a € ] tR t^¥t o RtRtV 3R lR ltV 3R lR ltRtV t	R# )	ÚTCPStoreBarriera;  
A barrier implementation using PyTorch's TCPStore for synchronization.

This barrier uses a TCP-based distributed key-value store to coordinate
synchronization across multiple processes. It uses a single TCP store
for all barrier operations, with different prefixes to distinguish between
different barrier types.
Ú	tcp_storec                óJ   <€ V ^8„  d   QhRS[ RS[ RS[RS[ RS[RS[ RS[RS[ /# )	r	   Úglobal_rankÚglobal_world_sizeÚbarrier_prefixÚtimeout_barrier_init_secsÚ%use_checkpoint_barrier_tcpstore_libuvÚtcpstore_portÚmaster_addressÚtimeout_secs)Úintr   Úbool)r   r   s   "€r   r   ÚTCPStoreBarrier.__annotate__²   s[   ø€ ÷ 6
ñ 6
áð6
ñ ð6
ñ ð	6
ñ
 $'ð6
ñ 04ð6
ñ ð6
ñ ð6
ñ ñ6
r   c	                ó"  € \         P                  RVVVVVVVV4	       \        4       V n        W0n        Wn        W n        W€n        \        P                  ! V\        V4      V P                  \        VR7      V P
                  ^ 8H  R7      V n        R# )a~  
Initialize a TCPStoreBarrier.

Args:
    global_rank: The rank of the current process in the distributed environment.
    global_world_size: The total number of processes in the distributed environment.
    barrier_prefix: A string prefix to identify this specific barrier.
    timeout_barrier_init_secs: Timeout in seconds for initializing the TCPStore.
    use_checkpoint_barrier_tcpstore_libuv: Whether to use libuv for the TCPStore.
    tcpstore_port: Port number for the TCPStore.
    master_address: Address of the master node for the TCPStore.
    timeout_secs: Maximum time in seconds to wait for all ranks to reach the barrier.
z¶Initializing TCPStore master_address=%s tcpstore_port=%s rank=%s world_size=%s barrier_prefix=%s timeout_barrier_init_secs=%s use_checkpoint_barrier_tcpstore_libuv=%s timeout_secs=%s)Úseconds)Ú
world_sizeÚtimeoutÚ	is_masterN)ÚloggerÚinfor   Ú_tcp_store_barrier_seqÚ_barrier_prefixÚ_global_rankÚ_global_world_sizeÚ_timeout_secsrH   ÚTCPStorer]   r   Ú
_tcp_store)	r6   rU   rV   rW   rX   rY   rZ   r[   r\   s	   &&&&&&&&&r   r7   ÚTCPStoreBarrier.__init__²   s“   € ô0 	�‰ðGð ØØØØØ%Ø1Øô	
ô 07«yˆÔ#Ø-Ôð (ÔØ"3ÔØ)Ôô Ÿ-š-ØÜ�ÓØ×.Ñ.ÜÐ&?Ô@Ø×(Ñ(¨AÑ-ô
ˆŽr   c                ó   <€ V ^8„  d   QhRR/# r;   r   )r   r   s   "€r   r   r_   ê   s   ø€ ÷ !9ñ !9 ñ !9r   c           	     óÒ  € V P                   p\        P                  RVV P                  4       R R lpV P                  P                  V! V P                  4      \        V P                  V,          4      4       \        P                  ! V P                  V P                  V\        V P                  V,          4      ,           R7       V P                  V;;,          ^,          uu&   R# )a  
Execute a synchronization barrier using the prefix provided during initialization.

The implementation uses a sequence number that is incremented every time
a barrier is reached. The sequence number is per barrier prefix to allow
different barriers to operate concurrently.
z3Executing barrier barrier_prefix=%s timeout_secs=%sc                ó0   € V ^8„  d   QhR\         R\        /# )r	   Úrankr   )r]   r   )r   s   "r   r   Ú5TCPStoreBarrier.execute_barrier.<locals>.__annotate__ú   s   € ÷ 	!ñ 	!œCð 	!¤Cñ 	!r   c                 ó   € R V  2# ©rr   r   ru   s   &r   Ú	_rank_keyÚ2TCPStoreBarrier.execute_barrier.<locals>._rank_keyú   s   € Ø˜$˜�=Ð r   )Ústorerb   Ú
key_prefixN)rh   re   rf   rk   rm   Úsetri   r   rg   Ú
store_utilrN   rj   )r6   rW   rv   s   &  r   r>   ÚTCPStoreBarrier.execute_barrierê   s´   € ð ×-Ñ-ˆä�‰ØAØØ×Ñô	
õ	!ð 	�‰×ÑÙ�d×'Ñ'Ó(Ü�×+Ñ+¨NÕ;Ó<ô	
ô 	×ÒØ—/‘/Ø×.Ñ.à¤ T×%@Ñ%@ÀÕ%PÓ!QÕQõ		
ð 	×#Ñ# N×3°qÕ8Õ3r   )rh   ri   rj   rm   rg   rk   NrP   r(   s   @r   rR   rR   ¥   s&   ø‡ € ñð €L÷6
ð 6
÷p!9ö !9r   rR   c                ób   € V ^8„  d   Qh/ ^ \         9   d   \        \        \        3,          ;R&   # )r	   r   )Ú__conditional_annotations__r   r   r   )r   s   "r   r   r      s&   € ÷ ñ ÷. 'Ò &”$”sœD�y•/Ñ &ò/r   )!r~   r$   r@   ÚloggingÚcollectionsr   Údataclassesr   r   Údatetimer   Útypingr   r   Útorch.distributedÚdistributedrH   Ú%torch.distributed.elastic.utils.storeÚelasticÚutilsrx   r{   Ú	getLoggerre   r   r   r   r0   ÚABCr+   rC   rR   r   )r~   s   @r   Ú<module>r‹      s±   øðöó Û Ý ß (Ý ß  å  ß :Ö :ð 
×	Ò	Ó	€ð %'Ð Ó &õð ÷ ?ð  ?ó ð ?õF8ô8ˆc�g‰gô ð> ô �'ó  ó ð ðF ôe9�gó e9ó òe9r   