TECHNOLOGY

Custom GPU autograd: the backward registration is a separate implementation

In this article
THE QUICK TAKE

A fast forward kernel does not define its derivative, and incoming gradient layouts need their own contract.

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 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 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 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 explains the mathematics, while GPU acceptance evidence helps structure reproducible validation. This guide concerns the concrete registration boundary that connects those layers.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles