def _begin_averaging_gradients(self, grad_scaler: Optional[GradScaler]) -> bool:
    'Begin an all-reduce round to average gradients; return True if succeeded, False if failed'
    if (grad_scaler is not None):
        with grad_scaler.running_global_step():
            assert grad_scaler.unscale_(self)
    began_averaging_gradients = False
    if ((self.scheduled_grads is not None) and (self.scheduled_grads.triggered or self.scheduled_grads.done())):
        logger.log(self.status_loglevel, f'Not using pre-scheduled group for state averaging because itwas already used elsewhere: {self.scheduled_state}')
        self.scheduled_grads = None
    elif (self.tracker.global_progress.num_peers > 1):
        try:
            self.scheduled_grads = self.grad_averager.step(control=self.scheduled_grads, reset_accumulators=True, wait=False)
            began_averaging_gradients = True
        except BaseException as e:
            logger.exception(e)
    if ((not began_averaging_gradients) and (self.scheduled_grads is not None) and (not self.scheduled_grads.done())):
        if (self.tracker.global_progress.num_peers > 1):
            logger.log(self.status_loglevel, f'Tagging along for a pre-scheduled gradient averaging round')
            self._tag_along_with_zero_weight(self.scheduled_grads)
        else:
            logger.log(self.status_loglevel, f'Skipping pre-scheduled averaging round: there are no other peers')
            self._load_local_gradients_into_optimizer()
            self.scheduled_grads.cancel()
        self.scheduled_grads = None
    return began_averaging_gradients
Begin an all-reduce round to average gradients; return True if succeeded, False if failed
def _begin_averaging_gradients(self, grad_scaler: Optional[GradScaler]) -> bool:
    
    if (grad_scaler is not None):
        with grad_scaler.running_global_step():
            assert grad_scaler.unscale_(self)
    began_averaging_gradients = False
    if ((self.scheduled_grads is not None) and (self.scheduled_grads.triggered or self.scheduled_grads.done())):
        logger.log(self.status_loglevel, f'Not using pre-scheduled group for state averaging because itwas already used elsewhere: {self.scheduled_state}')
        self.scheduled_grads = None
    elif (self.tracker.global_progress.num_peers > 1):
        try:
            self.scheduled_grads = self.grad_averager.step(control=self.scheduled_grads, reset_accumulators=True, wait=False)
            began_averaging_gradients = True
        except BaseException as e:
            logger.exception(e)
    if ((not began_averaging_gradients) and (self.scheduled_grads is not None) and (not self.scheduled_grads.done())):
        if (self.tracker.global_progress.num_peers > 1):
            logger.log(self.status_loglevel, f'Tagging along for a pre-scheduled gradient averaging round')
            self._tag_along_with_zero_weight(self.scheduled_grads)
        else:
            logger.log(self.status_loglevel, f'Skipping pre-scheduled averaging round: there are no other peers')
            self._load_local_gradients_into_optimizer()
            self.scheduled_grads.cancel()
        self.scheduled_grads = None
    return began_averaging_gradients