+
    –°³j’  ã                   ór   € ^ RI t ^ RIt^ RIHu Hu Hu Ht  ! R R]P                  P                  4      t	R# )é    Nc                   óz   a € ] tR t^t o RtV 3R lR lt]R 4       tR tR t	R t
R tRV 3R	 lR
 lltR tRtV tR# )ÚPostLocalSGDOptimizera<  
Wraps an arbitrary :class:`torch.optim.Optimizer` and runs `post-local SGD <https://arxiv.org/abs/1808.07217>`_,
This optimizer runs local optimizer at every step.
After the warm-up stage, it averages parameters periodically after the local optimizer is applied.

Args:
    optim: The local optimizer.
    averager: A model averager instance to run post-localSGD algorithm.

Example::

    >>> # xdoctest: +SKIP("undefined variables")
    >>> import torch
    >>> import torch.distributed as dist
    >>> import torch.distributed.algorithms.model_averaging.averagers as averagers
    >>> import torch.nn as nn
    >>> from torch.distributed.optim import PostLocalSGDOptimizer
    >>> from torch.distributed.algorithms.ddp_comm_hooks.post_localSGD_hook import (
    >>>   PostLocalSGDState,
    >>>   post_localSGD_hook,
    >>> )
    >>>
    >>> model = nn.parallel.DistributedDataParallel(
    >>>    module, device_ids=[rank], output_device=rank
    >>> )
    >>>
    >>> # Register a post-localSGD communication hook.
    >>> state = PostLocalSGDState(process_group=None, subgroup=None, start_localSGD_iter=100)
    >>> model.register_comm_hook(state, post_localSGD_hook)
    >>>
    >>> # Create a post-localSGD optimizer that wraps a local optimizer.
    >>> # Note that ``warmup_steps`` used in ``PostLocalSGDOptimizer`` must be the same as
    >>> # ``start_localSGD_iter`` used in ``PostLocalSGDState``.
    >>> local_optim = torch.optim.SGD(params=model.parameters(), lr=0.01)
    >>> opt = PostLocalSGDOptimizer(
    >>>     optim=local_optim,
    >>>     averager=averagers.PeriodicModelAverager(period=4, warmup_steps=100)
    >>> )
    >>>
    >>> # In the first 100 steps, DDP runs global gradient averaging at every step.
    >>> # After 100 steps, DDP runs gradient averaging within each subgroup (intra-node by default),
    >>> # and post-localSGD optimizer runs global model averaging every 4 steps after applying the local optimizer.
    >>> for step in range(0, 200):
    >>>    opt.zero_grad()
    >>>    loss = loss_fn(output, labels)
    >>>    loss.backward()
    >>>    opt.step()
c                ób   <€ V ^8„  d   QhRS[ P                  P                  RS[P                  /# )é   ÚoptimÚaverager)Útorchr   Ú	OptimizerÚ	averagersÚModelAverager)ÚformatÚ__classdict__s   "€Úp/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/distributed/optim/post_localSGD_optimizer.pyÚ__annotate__Ú"PostLocalSGDOptimizer.__annotate__:   s+   ø€ ÷ !ñ !™eŸk™k×3Ñ3ð !¹y×?VÑ?Vñ !ó    c                óT   € Wn         V P                   P                  V n        W n        R # ©N)r   Úparam_groupsr   )Úselfr   r   s   &&&r   Ú__init__ÚPostLocalSGDOptimizer.__init__:   s   € ØŒ
Ø ŸJ™J×3Ñ3ˆÔØ Žr   c                ó.   € V P                   P                  # r   )r   Ústate©r   s   &r   r   ÚPostLocalSGDOptimizer.state?   s   € à�z‰z×ÑÐr   c                ó6   € V P                   P                  4       # r   )r   Ú__repr__r   s   &r   r   ÚPostLocalSGDOptimizer.__repr__C   s   € Ø�z‰z×"Ñ"Ó$Ð$r   c                ól   € V P                   P                  4       pV P                  P                  VR&   V# )zÍ
This is the same as :class:`torch.optim.Optimizer` :meth:`state_dict`,
but adds an extra entry to record model averager's step to the checkpoint
to ensure reload does not cause unnecessary warm up again.
Ústep)r   Ú
state_dictr   r!   )r   Úoptim_state_dicts   & r   r"   Ú PostLocalSGDOptimizer.state_dictF   s2   € ð  Ÿ:™:×0Ñ0Ó2ÐØ#'§=¡=×#5Ñ#5Ð˜Ñ ØÐr   c                óÐ   € V P                   P                  V4       RV9   d   VR,          V P                  n        R# \        P
                  ! R^R7       ^ V P                  n        R# )a'  
This is the same as :class:`torch.optim.Optimizer` :meth:`load_state_dict`,
but also restores model averager's step value to the one
saved in the provided ``state_dict``.

If there is no ``"step"`` entry in ``state_dict``,
it will raise a warning and initialize the model averager's step to 0.
r!   z]Loaded state dict does not contain a step counter for an averager. Setting step counter to 0.)Ú
stacklevelN)r   Úload_state_dictr   r!   ÚwarningsÚwarn)r   r"   s   &&r   r'   Ú%PostLocalSGDOptimizer.load_state_dictP   sQ   € ð 	�
‰
×"Ñ" :Ô.Ø�ZÔØ!+¨FÕ!3ˆD�M‰MÖä�MŠMð-àõð
 "#ˆD�M‰MÖr   c                ó†   € V P                   P                  4        V P                  P                  V P                  R7       R# )z9
Performs a single optimization step (parameter update).
)ÚparamsN)r   r!   r   Úaverage_parametersr   r   s   &r   r!   ÚPostLocalSGDOptimizer.stepd   s-   € ð 	�
‰
�‰ÔØ�‰×(Ñ(°×0AÑ0AÐ(ÖBr   c                ó    <€ V ^8„  d   QhRS[ /# )r   Úset_to_none)Úbool)r   r   s   "€r   r   r   k   s   ø€ ÷ 6ñ 6¡Tñ 6r   c                ó>   € V P                   P                  VR 7       R# ))r0   N)r   Ú	zero_grad)r   r0   s   &&r   r3   ÚPostLocalSGDOptimizer.zero_gradk   s   € Ø�
‰
×Ñ¨ÐÖ5r   c                ó<   € V P                   P                  V4       R # r   )r   Úadd_param_group)r   Úparam_groups   &&r   r6   Ú%PostLocalSGDOptimizer.add_param_groupn   s   € Ø�
‰
×"Ñ" ;Ö/r   )r   r   r   N)T)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Úpropertyr   r   r"   r'   r!   r3   r6   Ú__static_attributes__Ú__classdictcell__)r   s   @r   r   r      sR   ø‡ € ñ/÷b!ð !ð
 ñ ó ð ò%ò ò#ò(C÷6ò 6÷0ð 0r   r   )
r(   r	   Ú6torch.distributed.algorithms.model_averaging.averagersÚdistributedÚ
algorithmsÚmodel_averagingr   r   r
   r   © r   r   Ú<module>rF      s)   ðã ã ß JÖ Jôg0˜EŸK™K×1Ñ1ö g0r   