class_docstr: LocalGradientAggregationHelper aggregates gradient updates locally,
  and communicates the updates across machines only once every
  backward_passes_per_step. Only supports graph mode execution.
class_docstr_tok: ["LocalGradientAggregationHelper","aggregates","gradient","updates","locally",",","and","communicates","the","updates","across","machines","only","once","every","backward_passes_per_step",".","Only","supports","graph","mode","execution","."]
code: def _allreduce_grads_helper(self, vars):
      def __filtered_reduce_grads(grads, vars):
          rv = []
          rg = []
          if _IS_TF2:
              v2g = {var.ref(): grad for var, grad in zip(vars, grads)}
              for var, grad in zip(vars, grads):
                  if var.ref() not in self._local_vars:
                      rv.append(var)
                      rg.append(grad)
          else:
              v2g = {var: grad for var, grad in zip(vars, grads)}
              for var, grad in zip(vars, grads):
                  if var not in self._local_vars:
                      rv.append(var)
                      rg.append(grad)
  
          rg = self._allreduce_grads(rg, rv)
          horovod_size = (
              size_op(process_set_id=self.process_set.process_set_id)
              if int(os.environ.get("HOROVOD_ELASTIC", 0))
              else self.process_set.size()
          )
          if _IS_TF2:
              for rv, rg in zip(rv, rg):
                  v2g[rv.ref()] = rg
  
              if self.scale_local_gradients and len(self._local_vars):
                  # Scale local gradients by a size factor. See pull/3695 and discussions/3705 for context.
                  for v_ref in v2g:
                      if (
                          v_ref in self._local_vars
                          and v2g[v_ref] is not None
                      ):
                          v2g[v_ref] /= float(horovod_size)
  
              return [v2g[rv.ref()] for rv in vars]
          else:
              for rv, rg in zip(rv, rg):
                  v2g[rv] = rg
  
              if self.scale_local_gradients and len(self._local_vars):
                  # Scale local gradients by a size factor. See pull/3695 and discussions/3705 for context.
                  for v in v2g:
                      if v in self._local_vars and v2g[v] is not None:
                          v2g[v] /= float(horovod_size)
  
              return [v2g[rv] for rv in vars]
  
      # Read in latest variables values.
      aggregated_grads = []
      aggregation_read_ops_list = []
      for idx, locally_aggregated_grad in enumerate(
          self.locally_aggregated_grads
      ):
          aggregated_grads.append(locally_aggregated_grad.read_value())
          aggregation_read_ops_list.append(aggregated_grads[idx])
      aggregation_read_ops = tf.group(*aggregation_read_ops_list)
  
      with tf.control_dependencies([aggregation_read_ops]):
          averaged_gradients = __filtered_reduce_grads(
              aggregated_grads, vars
          )
  
          # Reset counter.
          with tf.control_dependencies(
              [g.op for g in averaged_gradients if g is not None]
          ):
              reset_op = self.counter.assign(
                  tf.constant(0), use_locking=True
              )
  
          # Divide by backward_passes_per_step if
          # average_aggregated_gradients is True.
          with tf.control_dependencies([reset_op]):
              gradient_divisor = (
                  self.backward_passes_per_step
                  if self.average_aggregated_gradients
                  else 1
              )
  
              averaged_gradients = apply_op_to_not_none_tensors(
                  tf.divide,
                  averaged_gradients,
                  gradient_divisor,
              )
              return averaged_gradients
  
file_path: horovod/horovod/tensorflow/gradient_aggregation.py
imports: import os
  import tensorflow
  from packaging import version
  from horovod.tensorflow.mpi_ops import size_op
  from horovod.tensorflow.mpi_ops import global_process_set
label_desc: Use image node_id 7 for calling the LocalGradientAggregationHelper obj's underlying member method code with example usage: obj._allreduce_grads_helper(vars) and returns: unknown, unknown, averaged_gradients