class_docstr: An optimizer that averages gradients across TPU shards.
docstr: Apply gradients to variables.
  
  Calls tpu_ops.cross_replica_sum() to sum gradient contributions across
  replicas, and then applies the real optimizer.
  
  Args:
    grads_and_vars: List of (gradient, variable) pairs as returned by
      compute_gradients().
    global_step: Optional Variable to increment by one after the
      variables have been updated.
    name: Optional name for the returned operation.  Default to the
      name passed to the Optimizer constructor.
  
  Returns:
    An `Operation` that applies the gradients. If `global_step` was not None,
    that operation also increments `global_step`.
  
  Raises:
    ValueError: If the grads_and_vars is malformed.
docstr_tok: ["Apply","gradients","to","variables",".","Calls","tpu_ops.cross_replica_sum","(",")","to","sum","gradient","contributions","across","replicas",",","and","then","applies","the","real","optimizer",".","Args",":","grads_and_vars",":","List","of","(","gradient",",","variable",")","pairs","as","returned","by","compute_gradients","(",")",".","global_step",":","Optional","Variable","to","increment","by","one","after","the","variables","have","been","updated",".","name",":","Optional","name","for","the","returned","operation",".","Default","to","the","name","passed","to","the","Optimizer","constructor",".","Returns",":","An","`","Operation","`","that","applies","the","gradients",".","If","`","global_step","`","was","not","None",",","that","operation","also","increments","`","global_step","`",".","Raises",":","ValueError",":","If","the","grads_and_vars","is","malformed","."]
code: def apply_gradients(
      self, grads_and_vars, global_step=None, name=None
  ):
      """Apply gradients to variables.
  
      Calls tpu_ops.cross_replica_sum() to sum gradient contributions across
      replicas, and then applies the real optimizer.
  
      Args:
        grads_and_vars: List of (gradient, variable) pairs as returned by
          compute_gradients().
        global_step: Optional Variable to increment by one after the
          variables have been updated.
        name: Optional name for the returned operation.  Default to the
          name passed to the Optimizer constructor.
  
      Returns:
        An `Operation` that applies the gradients. If `global_step` was not None,
        that operation also increments `global_step`.
  
      Raises:
        ValueError: If the grads_and_vars is malformed.
      """
      summed_grads_and_vars = []
      for grad, var in grads_and_vars:
          if grad is None:
              summed_grads_and_vars.append((grad, var))
          else:
              with ops.colocate_with(grad):
                  summed_grads_and_vars.append(
                      (
                          tpu_ops.cross_replica_sum(
                              grad, self._group_assignment
                          ),
                          var,
                      )
                  )
      return self._opt.apply_gradients(
          summed_grads_and_vars, global_step, name
      )
  
file_path: tensorflow-upstream/tensorflow/python/tpu/tpu_optimizer.py
imports: from tensorflow.python.framework import ops
  from tensorflow.python.ops.losses import losses
  from tensorflow.python.platform import tf_logging
  from tensorflow.python.tpu import tpu_function
  from tensorflow.python.tpu.ops import tpu_ops
  from tensorflow.python.training import optimizer
  from tensorflow.python.util.tf_export import tf_export
label_desc: Use image node_id 4 for calling the CrossShardOptimizer obj's underlying member method code with example usage: obj.apply_gradients(grads_and_vars, global_step, name) and returns: self