Skip to content
fn

custom_bwd

→callable
custom_bwd(bwd: Callable[..., R] | None = None, device_type: str)

Decorate a custom Function.backward to run in its forward's autocast state.

The decorated backward runs inside an autocast scope matching the one custom_fwd recorded — on at the same dtype, or off — whatever scope .backward() was called from, so the arithmetic on the gradient and the tensors forward saved sees the dtypes it saw going forward.

Parameters

bwdcallable= None
The backward to decorate; leave it out to get the decorator.
device_typestr
"metal" or "cpu", the same as forward's. Keyword-only and required.

Returns

callable

The decorated backward (bwd given), or the decorator.

Raises

ValueError
If device_type is neither "metal" nor "cpu".
RuntimeError
When the decorated backward runs: if forward was not decorated with custom_fwd, or was with another device_type.

See Also

  • custom_fwd—the matching forward decorator, with a worked example.

Used by 1