Separate forward and backward contracts

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.[4][5]

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.[4][5]

  • Carry the incoming gradient: At x=2 the derivative is7; with g=−2, the required backward result is−14.

Keep the incoming gradient

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.[4][5]

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.[4][5]

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.[4][5]

Use an exact oracle

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.[4][5]

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.[4][5]

  • Keep the contract visible: A forward-only evaluation reports backward as unevaluated, not passed.

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

Authored backward ruleForward at x=2Backward at g=−2Passes across12 pairs
g(2x+3)9−1412/12
2x+3: ignores g976/12
−g(2x+3)9146/12
0906/12
  • The twelve pairs retain stationary, zero, unit and negative-gradient cases. Counts describe authored fixtures, not prevalence.
  • Analytic derivatives are the ground truth. Full finite-difference and forward-only controls are retained in the saved result.

Original execution, credit and scope

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. The complete source, fixed fixture, protocol and saved output are in the MIT package. No candidate or old experiment was rerun.[4][5][6]

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.[1][2][3][4]

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.[4][5]

  • Keep forward and required backward verdicts separate in the benchmark record.

Sources

  1. PyTorch contributors — Gradcheck mechanics docs.pytorch.org
  2. PyTorch contributors — Extending PyTorch docs.pytorch.org
  3. Python Software Foundation and contributors — fractions docs.python.org
  4. MIT proof and complete saved output data.nodus-compute.ai
  5. Saved result JSON data.nodus-compute.ai
  6. Original execution and saved-result provenance data.nodus-compute.ai
  7. Vector figure data.nodus-compute.ai