Every differentiable operation in autograd is aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/autograd/llms.txt
Use this file to discover all available pages before exploring further.
Function — a struct that records its inputs and outputs during the forward pass so the backward pass can retrace the computation.
The Function abstraction
AFunction wraps a Forwarder, which is the interface you implement to define a new operation:
Function struct itself stores the bookkeeping information:
| Field | Type | Description |
|---|---|---|
Input | []*Variable | Variables consumed during the forward pass. |
Output | []*Variable | Variables produced during the forward pass. |
Generation | int | Maximum generation of all inputs. Used to order the backward queue. |
Forwarder | interface | The concrete operation that provides Forward and Backward. |
How functions build the graph
WhenFunction.Forward() runs with backprop enabled, it:
Records the generation
Sets
f.Generation to the maximum generation across all inputs. This establishes a topological order for the backward pass.Links outputs back to this function
Calls
y.SetCreator(f) on each output, which sets y.Creator = f and y.Generation = f.Generation + 1.Calling built-in functions
All built-in operations are top-level functions that construct and call aFunction internally. You never instantiate Function or its Forwarder directly.
Convenience helper: First
Function.First runs the forward pass and returns the first output element — a shorthand for single-output operations.
Available functions
Arithmetic
Arithmetic
| Function | Description |
|---|---|
Add | Element-wise addition with broadcast |
AddC | Add a scalar constant |
Sub | Element-wise subtraction |
SubC | Subtract a scalar constant |
Mul | Element-wise multiplication |
MulC | Multiply by a scalar constant |
Div | Element-wise division |
DivC | Divide by a scalar constant |
Neg | Unary negation |
Pow | Raise to a scalar power |
Square | Square (equivalent to Pow(2.0)) |
Trigonometric and transcendental
Trigonometric and transcendental
| Function | Description |
|---|---|
Sin | Element-wise sine |
Cos | Element-wise cosine |
Tanh | Hyperbolic tangent |
Exp | Element-wise exponential |
Log | Natural logarithm |
Tensor operations
Tensor operations
| Function | Description |
|---|---|
Reshape | Change shape without copying data |
Transpose | Reverse axis order |
MatMul | Matrix multiplication |
TransposeMatMul | Multiply with transposed left operand |
Sum | Sum over specified axes |
SumTo | Sum to a target shape (used in broadcast) |
BroadcastTo | Expand to a target shape |
GetItem | Index into a variable |
Concat | Concatenate along an axis |
Split | Split along an axis |
Reduction and statistics
Reduction and statistics
| Function | Description |
|---|---|
Max | Maximum value (with indices) |
Min | Minimum value (with indices) |
Clip | Clamp values to a range |
Mean | Arithmetic mean |
Variance | Variance |
All functions are exported from both
github.com/itsubaki/autograd/variable and the convenience package github.com/itsubaki/autograd/function, which re-exports the same symbols.Composite functions: an example
Functions compose naturally. Each call returns aVariable that you pass to the next function, building a graph step by step.
Implementing a custom function
To define a new differentiable operation, implement theForwarder interface and wrap it in a Function:
Next steps
Variables
Understand the Variable struct that functions operate on.
Automatic differentiation
See how the backward pass walks the graph functions build.
Function API reference
Full reference for all built-in functions.
Higher-order gradients
Use CreateGraph to differentiate through the backward pass itself.