Skip to main content

A correct forward pass can have the wrong gradient

Updated 5 min readMarkdown

A small exact-rational proof separates forward equality from the backward rule a task actually requires.

A forward check can pass while a required backward rule is wrong. For f(x) = x² + 3x − 1, the value at x = 2 is 9. If the incoming gradient is g = −2, the required backward result is g(2x + 3) = −14. A rule that forgets g still returns the correct forward value and sends 7 backward.

The contract is explicit. We treat f as a real polynomial and evaluate it at rational points. When backward is required, the candidate must return both f(x) and the scalar vector-Jacobian product g f′(x). The incoming gradient matters: the derivative of the local function is 7 at x = 2, while this backward call must return −14. A forward-only API has a different contract; this example leaves backward unevaluated in that mode.

Four authored implementations share the same correct forward function. Their backward rules are g(2x + 3), 2x + 3, −g(2x + 3), and zero. At x = 2 and g = −2, they return −14, 7, 14 and 0. Only the first satisfies the declared backward contract. This is a constructed example, not a defect found in a framework or production grader.

The fixture fixes twelve (x,g) pairs before execution: x is −2, −3/2, 0 or 2, and g is 0, 1 or −2. Every forward value, derivative, required backward value and candidate backward value is written as a literal rational string. All 48 main forward comparisons pass. Across the twelve pairs, the correct rule passes twelve backward checks; each deliberately wrong rule passes six.

Authored backward rule Forward at x=2 Backward at g=−2 Passes across12 pairs
g(2x+3) 9 −14 12/12
2x+3: ignores g 9 7 6/12
−g(2x+3) 9 14 6/12
0 9 0 6/12

All four forward functions are identical. Required backward at the main point is−14.

Those six passes are useful evidence about the tests. At x = −3/2 the derivative is zero, so every rule returns the required zero for every chosen incoming gradient. A zero incoming gradient also masks the flipped-sign and zero rules. Testing only g = 1 masks the rule that ignores g. The full result retains these agreements beside the disagreements; six of twelve is not an estimate of failure prevalence.

The ground truth here is the closed-form derivative, 2x + 3. The program also forms the central difference [f(x+h) − f(x−h)]/(2h) from forward values with h = 1/4. For this quadratic, exact rational arithmetic makes that expression equal to the derivative. This convenient equality is specific to the function and arithmetic used here.

Two cubic controls make the distinction visible. For f(x) = x³ at x = 1, the derivative is 3. The central difference is 13/4 at h = 1/2 and 49/16 at h = 1/4. The errors are exactly 1/4 and 1/16. Finite differences remain a diagnostic with step-size behavior; they are not an exact derivative oracle for arbitrary functions. This example does not model floating-point rounding or non-smooth functions.

The standard-library program ran once on CPython 3.9.6 on 2026-11-28 UTC. The saved output contains twelve literal case checks, 48 main candidate evaluations, two cubic diagnostics, four cross-case checks, four forward-only evaluations and eight selected input refusals. No candidate or old experiment was rerun.

The method follows a familiar distinction in PyTorch’s documentation: backward-mode differentiation computes a vector-Jacobian product, and gradient checking compares analytical and numerical derivatives. The custom-operation guide also recommends checking backward separately. We credit those explanations; the polynomial fixture and intentionally wrong rules are original teaching material, and this program does not execute PyTorch.

For a benchmark that requires backward, make its derivative contract explicit and retain the incoming gradient with the result. Include non-unit and negative incoming gradients, and keep stationary and zero-gradient controls visible. Report forward and backward decisions separately. A finite check can expose a disagreement; it does not establish all-input correctness or a training benefit.

Limits

  • Original finite, authored scalar mathematical example at rational points; not a sampled defect rate or an existing framework, kernel or grader defect.
  • Backward is required only under the stated first-order scalar vector-Jacobian-product contract. A forward-only API is not failed for leaving it unevaluated.
  • Closed-form derivatives supply ground truth. Finite differences are diagnostics; the exact quadratic identity does not generalize to arbitrary functions.
  • No tensor, automatic-differentiation engine, saved-context, higher-order, complex, stochastic, non-smooth or floating-point behavior is tested.
  • Twelve pairs and selected refusal controls do not prove universal gradient correctness, secure validation or complete input coverage.
  • No GPU, training run, measured performance, cost, reward improvement or training uplift.

Original credit

About the author

No comments yet. Be the first to comment.