TECHNOLOGY

PyTorch fake kernels: describe output metadata without reading tensor data

In this article
THE QUICK TAKE

A fake implementation models the real output metadata; it does not approximate the output values.

Treat metadata as an executable promise

A GPU kernel can return correct values in eager execution and still fail during compilation because its fake implementation describes a different tensor. The register_fake reference defines this implementation in terms of output properties derived from input metadata. Shape alone is insufficient: device, dtype, layout, and relevant strides and storage offsets belong to the promise. The compiler uses that description while actual tensor contents are unavailable.

Follow the real allocation policy

Suppose a custom image transform accepts a transposed input but always produces a freshly allocated contiguous result. Returning empty_like of the input in the fake path may describe the wrong memory layout. The functional operator tutorial illustrates why fake outputs must agree with real allocation behavior. Write down the output layout independently of the input layout before choosing the metadata construction operation; convenient resemblance is not evidence of an identical contract.

Keep value-dependent shapes genuinely unknown

A filtering kernel may return only elements that pass a content-dependent condition. The input shape cannot determine the number of survivors. Do not inspect tensor data or invent a typical survivor count inside the fake implementation. The documented get_ctx and new_dynamic_size mechanism represents such a count symbolically when supported by the surrounding path. That symbolic dimension describes uncertainty in the actual contract; it is not an allocation cap or a prediction of the future result.

Validate with awkward metadata

Build examples in which shape and layout pull apart: sliced inputs, transposed matrices, empty dimensions, and supported devices. Compare real output metadata with the fake contract, then test the intended compilation path. A fake kernel that only survives a contiguous example can hide an incorrect stride assumption. Keep unsupported cases explicit instead of silently constructing metadata that would authorize the compiler to reason about an output the real kernel never produces.

Separate metadata checks from numerical checks

The custom-operator overview explains that fake kernels may inspect metadata but must not access tensor contents. Successful metadata registration does not validate arithmetic or model quality. Use the existing discussion of tensor axes to define output meaning and benchmark methodology only after correctness is established. The fake path should describe the same operation before a single GPU value is calculated.

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