﻿# Custom GPU autograd: the backward registration is a separate implementation

Add a traceable backward formula to a custom GPU operator, save the necessary forward context, and handle gradient strides independently.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-pytorch-custom-op-autograd-backward-contract/
Language: en

## Do not infer training support from the forward result

An inference extension may calculate exactly the intended activation while providing no usable derivative. Registering a CUDA implementation tells PyTorch how to execute the forward operator, not how to differentiate it. The [custom C++ and CUDA tutorial](https://docs.pytorch.org/tutorials/advanced/cpp_custom_ops.html) adds training support explicitly. Keep the claim that an extension supports inference separate from the claim that the same operator is valid inside a trainable graph.

## Save only the forward information backward needs

For a custom gating operation, the derivative may need the original gate input, an output, or an auxiliary quantity. Define that dependency before implementing setup_context. Saving every intermediate retains unnecessary memory; saving only a detached summary may discard information the derivative requires. Tensor values needed later belong in the appropriate saved-tensor mechanism, while ordinary non-tensor attributes can hold configuration. The context should describe this invocation, not a mutable global cache shared by unrelated calls.

## Make the derivative traceable

The [register_autograd reference](https://docs.pytorch.org/docs/2.15/library.html) requires traceable setup and backward functions. A derivative expressed with understood PyTorch operations provides a straightforward path. If the derivative needs its own external kernel, expose that computation through an appropriate registered operator rather than reaching directly into raw storage from the backward function. Returning a plausible gradient shape does not establish the correct derivative, particularly around branches, broadcasting, or nondifferentiable input arguments.

## Incoming gradients have independent layouts

A contiguous forward output does not guarantee a contiguous gradient arriving from later operations. A backward kernel that assumes contiguous gradients can therefore fail only when embedded inside a larger graph. Either handle incoming strides or deliberately normalize the gradient layout before launching the specialized kernel. Test a composition that creates non-contiguous gradient views, rather than checking the custom operation solely as the final scalar loss's immediate producer.

## Validate both mathematics and integration

The [optional-registration tutorial](https://docs.pytorch.org/tutorials/advanced/python_custom_ops_registrations.html) separates training registration from the base operator. Check forward results against a reference, derivative mathematics with suitable gradient checks, and the supported compiled training path. State higher-order derivative support only when it has actually been established. Existing material on [the chain rule in autograd](/journal/autograd-chain-rule-neural-graphs/) explains the mathematics, while [GPU acceptance evidence](/journal/gpu-acceptance-evidence/) helps structure reproducible validation. This guide concerns the concrete registration boundary that connects those layers.
