Skip to content
class

UnaryKernel

extendsAutogradNode<Derived, 1>kernel::IKernel
UnaryKernel

CRTP base for single-input, single-output op kernels.

Inherits AutogradNode\<Derived, 1\> (one saved input slot) and kernel::IKernel so an instance can be held polymorphically while still exposing the typed static forward() trampoline.

Concrete ops declare themselves as class FooBackward : public UnaryKernel<FooBackward> and provide:

  • static constexpr OpSchema schema_v1 — op name + AMP policy.
    • static cpu_kernel(a, out_shape, dtype) -> CpuStorage and/or static gpu_kernel(a, out_shape, dtype) -> GpuStorage, or static dispatch(IBackend&, a, out_shape, dtype) -> Storage.
    • Storage grad_formula(Storage grad_out) — the local Jacobian product computed at backward time.
    • Optional grad_formula_impl(grad_out, a_impl, out_impl) -> TensorImplPtr for graph-mode (create_graph=True) higher-order differentiation.

Template Parameters

Derived : class The concrete CRTP self-type.

See Also

BinaryKernel, NaryKernel, VariadicKernel. IKernel — the abstract base above the CRTP layer.

Attributes

kSavesInputstatic constexpr bool
Whether forward() snapshots a->storage() into saved_inputs_[0] for use in grad_formula. Defaults to true; set false when the gradient formula uses only the saved output (e.g. ReLU keyed on output sign).
kSavesOutputstatic constexpr bool
Whether forward() additionally snapshots the output storage into saved_output_. Defaults to false. Set true for ops whose backward is cheap in terms of y (e.g. Exp, Sigmoid).
kHasGradientstatic constexpr bool
Whether the op participates in autograd. Defaults to true; set false for non-differentiable ops to skip all graph wiring.

Notes

AMP policy and dtype promotion are forwarded through SchemaGuard using Derived::schema_v1; the effective dtype returned drives both the input cast and the saved-input dtype.

Static methods

2
static

compute_storage

int compute_storage(const int & a_ptr, Dtype eff_dt)

The result's storage at a_ptr's shape and eff_dt: the backend's dispatch, or the Derived CPU / GPU kernel. A phantom under phantom mode, where no kernel runs.

static

forward

→std::shared_ptr<TensorImpl>
int forward(const int & a)

Typed forward trampoline for a single-input op.

Parameters

aconst std::shared_ptr<TensorImpl>&
The input tensor.

Returns

std::shared_ptr<TensorImpl>

The output tensor. If a requires a gradient (and grad mode is enabled) the result has its grad_fn set to a freshly constructed Derived backward node.

Raises

ShapeMismatch
If the schema's shape contract is violated.
DtypeMismatch
If the input dtype is not compatible with the schema's accepted dtype set.

Notes

Behaviour

  1. Validate a is non-null.
  2. Resolve the effective dtype via SchemaGuard.
  3. Materialise contiguous on CPU when a is non-contiguous.
  4. Cast to the effective dtype if needed.
  5. Dispatch to dispatch / gpu_kernel / cpu_kernel (see file header for priority).
  6. Wire the autograd graph when kHasGradient and any input requires a gradient.

Methods

5
fn

apply

→std::vector<Storage>
int apply(int grad_out)

Backward implementation invoked by the autograd engine.

Parameters

grad_outStorage
The upstream gradient with respect to this op's output.

Returns

std::vector<Storage>

Single-element vector containing grad_input reduced back to input_shapes_[0] via reduce_grad_to_shape.

Notes

The reduction step is needed when the forward broadcast the input to a larger output shape; for purely shape-preserving unary ops it is a no-op.

fn

apply_for_graph

→std::vector<TensorImplPtr>
int apply_for_graph(const int & grad_out)

Graph-mode backward — supports create_graph=True.

Parameters

grad_outconst TensorImplPtr&
The upstream gradient, retained as a TensorImpl with its own grad_fn for higher-order differentiation.

Returns

std::vector<TensorImplPtr>

Single-element vector containing the fully-traced grad_input reduced back to the original input shape via sum_op / reshape_op.

Raises

:runtime_error
If the saved input pointer was not captured (i.e. the forward ran without create_graph=True), or if the concrete op did not override grad_formula_impl.
fn

grad_formula_impl

→TensorImplPtr
int grad_formula_impl(const int &, const int &, const int &)

Default graph-mode gradient formula — concrete ops override.

Parameters

gconst TensorImplPtr&
Gradient of the loss with respect to this op's output, as a TensorImpl that itself carries grad_fn so the backward computation is differentiable.
aconst TensorImplPtr&
The saved forward input (possibly broadcast / cast to the effective dtype).
outconst TensorImplPtr&
The saved forward output (populated only when kSavesOutput == true).

Returns

TensorImplPtr

grad_input as a fully-traced TensorImpl.

Raises

:runtime_error
The default base implementation always throws. Concrete ops must override to support create_graph=True.
fnconst

name

int name()

Return the canonical schema name of the concrete op.

fnconst

node_name

int node_name()

Return the autograd node label (same string as name).