tensorflow/models · error · ValueError

No gradients provided for any variable

Error message

No gradients provided for any variable: %s.

What it means

Gradient sanity guard in _filter_grads: every (grad, var) pair had grad None, i.e. the loss is disconnected from all trainable variables — typically a loss computed without referencing model variables or a broken tape/gradient function.

Solutions

  1. Check the loss function actually uses the model's trainable variables
  2. Ensure the forward pass inside the gradient tape produces the loss
  3. Verify variables are not disconnected by a stop_gradient or numpy conversion in the loss path
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/modeling/grad_utils.py:36 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/49941e89fcfa2782. Report an issue: GitHub.

Appendix: source

Thrown at official/modeling/grad_utils.py:36

import tensorflow as tf, tf_keras


def _filter_grads(grads_and_vars):
  """Filter out iterable with grad equal to None."""
  grads_and_vars = tuple(grads_and_vars)
  if not grads_and_vars:
    return grads_and_vars
  filtered = []
  vars_with_empty_grads = []
  for grad, var in grads_and_vars:
    if grad is None:
      vars_with_empty_grads.append(var)
    else:
      filtered.append((grad, var))
  filtered = tuple(filtered)
  if not filtered:
    raise ValueError("No gradients provided for any variable: %s." %
                     ([v.name for _, v in grads_and_vars],))
  if vars_with_empty_grads:
    logging.warning(
        ("Gradients do not exist for variables %s when minimizing the loss."),
        ([v.name for v in vars_with_empty_grads]))
  return filtered


def _filter_and_allreduce_gradients(grads_and_vars,
                                    allreduce_precision="float32",
                                    bytes_per_pack=0):
  """Filter None grads and then allreduce gradients in specified precision.

  This utils function is used when users intent to explicitly allreduce
  gradients and customize gradients operations before and after allreduce.
  The allreduced gradients are then passed to optimizer.apply_gradients(
  experimental_aggregate_gradients=False).

View on GitHub (pinned to e006f5f0d5)