TECHNOLOGY

PyTorch custom operator mutation: fresh output, in-place, or out buffer

In this article
THE QUICK TAKE

Output values do not describe ownership: mutation, aliasing, and allocation behavior must agree with the registered schema.

Make memory ownership part of the operation

A kernel that writes the correct numbers into the wrong ownership pattern can break a compiled graph. Two tensors may share storage even though they are different Python objects; modifying a view can therefore alter an input. The tensor-view documentation supplies the basic distinction. Define whether the operation allocates fresh output, mutates an input, or fills a caller-owned buffer before implementing its numerical kernel.

Keep functional outputs genuinely fresh

For a functional custom_op, unchanged input values are only part of the contract. Outputs must not alias inputs or each other. Returning the input unchanged for an empty or special case while allocating new memory otherwise creates unstable alias behavior. A later consumer may modify the output expecting independence. Preserve the allocation contract across branches, including easy cases where returning an existing tensor seems like a harmless shortcut.

Declare mutation precisely

The mutable custom-operator tutorial requires mutates_args to describe the arguments actually modified. This declaration informs transformations that cannot discover hidden writes by inspecting a raw GPU kernel. An undocumented update to a cached buffer can therefore be an integration defect even when a local output comparison passes. Do not label every argument mutated as a substitute for understanding the operation; choose and document the behavior the implementation really needs.

Observe the versioned aliasing patterns

The PyTorch 2.15 library reference documents tagged inplace and out patterns. The conventional inplace form mutates and returns the first positional tensor. The out form writes keyword-only output tensors and returns them in declaration order; it must not read those output buffers. Arbitrary returned aliases are not generally supported by custom_op. Check the installed version rather than copying older advice that categorically forbids every returned mutated tensor, or assuming every alias is now permitted.

Split operations with different ownership

Suppose an activation sometimes allocates and sometimes receives an existing output buffer. Separate these into explicit functional and mutable operators instead of making ownership depend silently on an optional argument. Validate each schema, including view inputs and edge shapes, with registration checks and independent numerical comparisons. The existing guides on tensor axes and GPU acceptance evidence help describe inputs and tests. The crucial additional record is who owns each output and which existing memory can change.

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