# tensorplay.autograd.function API Source: https://www.tensorplay.cn/docs/api/tensorplay.autograd.function.html ## Functions 1 [#](#api-tensorplay.autograd.function.once_differentiable) ### once_differentiable function[Full reference ↗](/docs/generated/tensorplay.autograd.function.once_differentiable.html) ```python tensorplay.autograd.function.once_differentiable(fn) ``` Decorator to make a custom autograd Function’s backward run once, with gradients detached and grad-mode disabled inside. ## Classes 4 [#](#api-tensorplay.autograd.function.Function) ### Function class[Full reference ↗](/docs/generated/tensorplay.autograd.function.Function.html) ```python class tensorplay.autograd.function.Function ``` Records operation history and defines formulas for differentiating ops. - Legacy style: forward(ctx, ...) / backward(ctx, ...) (forward receives a context object). - Combined-forward style: define forward(*args, **kwargs), setup_context(ctx, inputs, output) and use save_for_backward/save_for_forward inside setup_context instead of receiving a ctx argument in forward. ```python classmethod apply(*args, **kwargs) ``` Runs the operation and attaches gradient bookkeeping to outputs. flat arguments computes needs_input_grad and wires next-edges BEFORE forward; outputs are marked and attached AFTER setup_context. When the fused C++ apply is present the hot path makes a single pybind crossing that also wires the backward entry; otherwise a generic Python fallback runs. ```python static backward(ctx, *grad_outputs) ``` Defines a formula for differentiating the operation. ```python static forward(ctx, *args, **kwargs) ``` Performs the operation. This function is to be overridden by all subclasses. There are two ways to define forward: Usage 1 (Combined forward and ctx): ``` @staticmethod def forward(ctx, input1, input2): ... return output ``` Usage 2 (Separated forward and ctx): ``` @staticmethod def forward(input1, input2): ... return output @staticmethod def setup_context(ctx, inputs, output): ... ``` ```python static jvp(ctx, *grad_inputs) ``` Defines a formula for computing the jacobian-vector product. Called by forward-mode AD when at least one input of this Function carries a tangent: grad_inputs holds the tangent of each input (None for inputs without one), and the returned tangents are attached to the outputs one-for-one. ```python static setup_context(ctx, inputs, output) ``` Sets up the context object (Usage 2 above). Parameters: - ctx (_Context) – context object to modify in-place - inputs ([tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)) – inputs to [forward()](#tensorplay.autograd.function.Function.forward) - output (Any) – output of [forward()](#tensorplay.autograd.function.Function.forward) ```python static vmap(info, in_dims, *args) ``` Defines a formula for vectorizing the operation. Not yet supported by this engine; provided for API compatibility. [#](#api-tensorplay.autograd.function.FunctionMeta) ### FunctionMeta class[Full reference ↗](/docs/generated/tensorplay.autograd.function.FunctionMeta.html) ```python class tensorplay.autograd.function.FunctionMeta(name, bases, namespace, **kwds) ``` the name classproperty ("Backward", used for node naming) and a friendlier repr for subclasses. ```python mro() ``` Return a type’s method resolution order. [#](#api-tensorplay.autograd.function.InplaceFunction) ### InplaceFunction class[Full reference ↗](/docs/generated/tensorplay.autograd.function.InplaceFunction.html) ```python class tensorplay.autograd.function.InplaceFunction ``` In-place operations must call ctx.mark_dirty on the mutated inputs inside forward; this subclass exists only so historical code that subclasses it keeps working. ```python classmethod apply(*args, **kwargs) ``` Runs the operation and attaches gradient bookkeeping to outputs. flat arguments computes needs_input_grad and wires next-edges BEFORE forward; outputs are marked and attached AFTER setup_context. When the fused C++ apply is present the hot path makes a single pybind crossing that also wires the backward entry; otherwise a generic Python fallback runs. ```python static backward(ctx, *grad_outputs) ``` Defines a formula for differentiating the operation. ```python static forward(ctx, *args, **kwargs) ``` Performs the operation. This function is to be overridden by all subclasses. There are two ways to define forward: Usage 1 (Combined forward and ctx): ``` @staticmethod def forward(ctx, input1, input2): ... return output ``` Usage 2 (Separated forward and ctx): ``` @staticmethod def forward(input1, input2): ... return output @staticmethod def setup_context(ctx, inputs, output): ... ``` ```python static jvp(ctx, *grad_inputs) ``` Defines a formula for computing the jacobian-vector product. Called by forward-mode AD when at least one input of this Function carries a tangent: grad_inputs holds the tangent of each input (None for inputs without one), and the returned tangents are attached to the outputs one-for-one. ```python static setup_context(ctx, inputs, output) ``` Sets up the context object (Usage 2 above). Parameters: - ctx (_Context) – context object to modify in-place - inputs ([tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)) – inputs to [forward()](#tensorplay.autograd.function.InplaceFunction.forward) - output (Any) – output of [forward()](#tensorplay.autograd.function.InplaceFunction.forward) ```python static vmap(info, in_dims, *args) ``` Defines a formula for vectorizing the operation. Not yet supported by this engine; provided for API compatibility. [#](#api-tensorplay.autograd.function.NestedIOFunction) ### NestedIOFunction class[Full reference ↗](/docs/generated/tensorplay.autograd.function.NestedIOFunction.html) ```python class tensorplay.autograd.function.NestedIOFunction ``` Kept only for import compatibility; the modern contract is to define forward + backward on [Function](/docs/generated/tensorplay.autograd.function.Function.html#tensorplay.autograd.function.Function) directly. ```python classmethod apply(*args, **kwargs) ``` Runs the operation and attaches gradient bookkeeping to outputs. flat arguments computes needs_input_grad and wires next-edges BEFORE forward; outputs are marked and attached AFTER setup_context. When the fused C++ apply is present the hot path makes a single pybind crossing that also wires the backward entry; otherwise a generic Python fallback runs. ```python static jvp(ctx, *grad_inputs) ``` Defines a formula for computing the jacobian-vector product. Called by forward-mode AD when at least one input of this Function carries a tangent: grad_inputs holds the tangent of each input (None for inputs without one), and the returned tangents are attached to the outputs one-for-one. ```python static setup_context(ctx, inputs, output) ``` Sets up the context object (Usage 2 above). Parameters: - ctx (_Context) – context object to modify in-place - inputs ([tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)) – inputs to forward() - output (Any) – output of forward() ```python static vmap(info, in_dims, *args) ``` Defines a formula for vectorizing the operation. Not yet supported by this engine; provided for API compatibility.