
    Ph                         d dl Z d dlmZmZ d dlmZmZmZ d dlZd dl	m
Z d dlm
c mc mc mZ ddgZ G d de      Z G d de      Zy)    N)ABCabstractmethod)UnionIterableDictModelAveragerPeriodicModelAveragerc                   (    e Zd ZdZddZed        Zy)r   aH  Base class for all model averagers.

    Args:
        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``)
    Nc                 X    ||nt         j                  j                  | _        d| _        y )Nr   )distgroupWORLDprocess_groupstep)selfr   s     C:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/algorithms/model_averaging/averagers.py__init__zModelAverager.__init__   s'    *6MDJJ<L<L 	 	    c                     t         N)NotImplementedErrorr   paramss     r   average_parametersz ModelAverager.average_parameters   s    !!r   r   )__name__
__module____qualname____doc__r   r   r    r   r   r   r   
   s      " "r   c                        e Zd ZdZ	 	 d fd	Zdeeej                  j                     ee
eej                  j                  f      f   fdZ xZS )r	   ak
  
    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())
    c                     t         |   |       |dk  rt        d      || _        |dk  rt        d      |dk(  rt	        j
                  d       || _        y )Nr   z3Arg ``warmup_steps`` must be a non-negative number.   z(Arg ``period`` must be a positive value.a  When period is 1, no need to use model averaging because the communication cost of all-reducing parameters will be no less than the cost of all-reducing gradients by DistributedDataParallel in the backward pass. Therefore, only DistributedDataParallel should be used for this case.)superr   
ValueErrorwarmup_stepswarningswarnperiod)r   r(   r%   r   	__class__s       r   r   zPeriodicModelAverager.__init__T   sc     	'!RSS(A:GHHq[MMH r   r   c                     | j                   | j                  k\  rI| j                   | j                  z
  | j                  z  dk(  r t        j                  || j
                         | xj                   dz  c_         y)aV  
        Averages parameters or parameter groups of an optimizer if ``step`` is no less than ``warmup_steps``
        and it can be divided by ``period``, where ``step`` is increased by 1
        at each iteration in the training loop.
        Args:
            params: The parameters of a model or parameter groups of an optimizer.

        r   r"   N)r   r%   r(   utils&average_parameters_or_parameter_groupsr   r   s     r   r   z(PeriodicModelAverager.average_parametersi   sY     II***T...$++=B88ASAST		Q	r   )r   N)r   r   r   r   r   r   r   torchnn	Parameterr   strr   __classcell__)r)   s   @r   r	   r	      sY    2n 	*x8J8J/KXVZ[^`e`h`h`r`r[rVsMt/t)u r   )r&   abcr   r   typingr   r   r   r-   torch.distributeddistributedr   2torch.distributed.algorithms.model_averaging.utils
algorithmsmodel_averagingr+   __all__r   r	   r   r   r   <module>r:      sB     # ( (    B B3
4"C "*XM Xr   