# torch 2.15.0 (BSD-3-Clause), harvested by tools/harvest_corpus.py
=== input

    Averages parameters periodically after the warm-up stage.

    This can be used for running `post-local SGD <https://arxiv.org/abs/1808.07217>`_,
    by running :class:`~torch.nn.DistributedDataParallel` (DDP)
    using the subgroups created by :meth:`~torch.distributed.new_subgroups`.

    Args:
        period (int): The number of steps per model averaging.
                      Usually the period should be greater than ``1`` to reduce the communication cost.
                      Otherwise, only DDP needs to be used.
        warmup_steps (int): The number of warm-up steps. During this stage,
                            model averaging is skipped.
        process_group: The process group to be used for all-reduce.
                       If ``None``, the default process group, which
                       is created by :func:`torch.distributed.init_process_group`,
                       will be used. (default: ``None``)

    Example::

        >>> # xdoctest: +SKIP("undefined variables")
        >>> import torch
        >>> import torch.distributed as dist
        >>> import torch.distributed.algorithms.ddp_comm_hooks.post_localSGD_hook as post_localSGD
        >>> import torch.distributed.algorithms.model_averaging.averagers as averagers
        >>> import torch.nn as nn
        >>>
        >>> dist.init_process_group("nccl", rank=rank, world_size=16)
        >>> torch.cuda.set_device(rank)
        >>> module = nn.Linear(1, 1, bias=False).cuda()
        >>> 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)
        >>>
        >>> # In the first 100 steps, run global gradient averaging like normal DDP at every step.
        >>> # After 100 steps, run model averaging every 4 steps.
        >>> # Note that ``warmup_steps`` must be the same as ``start_localSGD_iter`` used in ``PostLocalSGDState``.
        >>> averager = averagers.PeriodicModelAverager(period=4, warmup_steps=100)
        >>> for step in range(0, 200):
        >>>    optimizer.zero_grad()
        >>>    loss = loss_fn(output, labels)
        >>>    loss.backward()
        >>>    optimizer.step()
        >>>    # Will average model parameters globally every 4 steps. Thus,
        >>>    # inter-node communication only occurs every 4 iterations after
        >>>    # the initial ``warmup_steps`` period.
        >>>    averager.average_parameters(model.parameters())
    
=== parse google
short_description: 'Averages parameters periodically after the warm-up stage.'
long_description: |
  This can be used for running `post-local SGD <https://arxiv.org/abs/1808.07217>`_,
  by running :class:`~torch.nn.DistributedDataParallel` (DDP)
  using the subgroups created by :meth:`~torch.distributed.new_subgroups`.
  
  Example::
  
      >>> # xdoctest: +SKIP("undefined variables")
      >>> import torch
      >>> import torch.distributed as dist
      >>> import torch.distributed.algorithms.ddp_comm_hooks.post_localSGD_hook as post_localSGD
      >>> import torch.distributed.algorithms.model_averaging.averagers as averagers
      >>> import torch.nn as nn
      >>>
      >>> dist.init_process_group("nccl", rank=rank, world_size=16)
      >>> torch.cuda.set_device(rank)
      >>> module = nn.Linear(1, 1, bias=False).cuda()
      >>> 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)
      >>>
      >>> # In the first 100 steps, run global gradient averaging like normal DDP at every step.
      >>> # After 100 steps, run model averaging every 4 steps.
      >>> # Note that ``warmup_steps`` must be the same as ``start_localSGD_iter`` used in ``PostLocalSGDState``.
      >>> averager = averagers.PeriodicModelAverager(period=4, warmup_steps=100)
      >>> for step in range(0, 200):
      >>>    optimizer.zero_grad()
      >>>    loss = loss_fn(output, labels)
      >>>    loss.backward()
      >>>    optimizer.step()
      >>>    # Will average model parameters globally every 4 steps. Thus,
      >>>    # inter-node communication only occurs every 4 iterations after
      >>>    # the initial ``warmup_steps`` period.
      >>>    averager.average_parameters(model.parameters())
blank_after_short_description: True
blank_after_long_description: True
style: google
- DocstringParam
  description: |
    The number of steps per model averaging.
    Usually the period should be greater than ``1`` to reduce the communication cost.
    Otherwise, only DDP needs to be used.
  kind: param
  key: 'param'
  arg_name: 'period'
  type_name: 'int'
  is_optional: False
- DocstringParam
  description: |
    The number of warm-up steps. During this stage,
    model averaging is skipped.
  kind: param
  key: 'param'
  arg_name: 'warmup_steps'
  type_name: 'int'
  is_optional: False
- DocstringParam
  description: |
    The process group to be used for all-reduce.
    If ``None``, the default process group, which
    is created by :func:`torch.distributed.init_process_group`,
    will be used. (default: ``None``)
  kind: param
  key: 'param'
  arg_name: 'process_group'
  is_optional: True
  default: 'None'
=== detect
google
=== compose google
Averages parameters periodically after the warm-up stage.

This can be used for running `post-local SGD <https://arxiv.org/abs/1808.07217>`_,
by running :class:`~torch.nn.DistributedDataParallel` (DDP)
using the subgroups created by :meth:`~torch.distributed.new_subgroups`.

Example::

    >>> # xdoctest: +SKIP("undefined variables")
    >>> import torch
    >>> import torch.distributed as dist
    >>> import torch.distributed.algorithms.ddp_comm_hooks.post_localSGD_hook as post_localSGD
    >>> import torch.distributed.algorithms.model_averaging.averagers as averagers
    >>> import torch.nn as nn
    >>>
    >>> dist.init_process_group("nccl", rank=rank, world_size=16)
    >>> torch.cuda.set_device(rank)
    >>> module = nn.Linear(1, 1, bias=False).cuda()
    >>> 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)
    >>>
    >>> # In the first 100 steps, run global gradient averaging like normal DDP at every step.
    >>> # After 100 steps, run model averaging every 4 steps.
    >>> # Note that ``warmup_steps`` must be the same as ``start_localSGD_iter`` used in ``PostLocalSGDState``.
    >>> averager = averagers.PeriodicModelAverager(period=4, warmup_steps=100)
    >>> for step in range(0, 200):
    >>>    optimizer.zero_grad()
    >>>    loss = loss_fn(output, labels)
    >>>    loss.backward()
    >>>    optimizer.step()
    >>>    # Will average model parameters globally every 4 steps. Thus,
    >>>    # inter-node communication only occurs every 4 iterations after
    >>>    # the initial ``warmup_steps`` period.
    >>>    averager.average_parameters(model.parameters())

Args:
    period (int): The number of steps per model averaging.
        Usually the period should be greater than ``1`` to reduce the communication cost.
        Otherwise, only DDP needs to be used.
    warmup_steps (int): The number of warm-up steps. During this stage,
        model averaging is skipped.
    process_group: The process group to be used for all-reduce.
        If ``None``, the default process group, which
        is created by :func:`torch.distributed.init_process_group`,
        will be used. (default: ``None``)
