Skip to content
class

FunctionCtx

FunctionCtx

Per-call context shared between a custom op's forward and backward passes.

FunctionCtx is the C++-side counterpart of lucid.autograd.Function's Python ctx object. A fresh instance is created on each Function.apply invocation and is passed unchanged as the first argument to both forward(ctx, ...) and backward(ctx, grad_output), providing the only legal channel for state to cross the forward/backward boundary.

See Also

lucid.autograd.FunctionCtx — Python-facing wrapper class. PythonBackwardNode — owns one FunctionCtx per custom op call.

Attributes

saved_tensors_std::vector<std::shared_ptr<TensorImpl>>
Tensors stored via save_for_backward as strong references so they survive until the backward pass executes.
extras_std::unordered_map<std::string, py::object>
Generic Python key/value bag mirroring ctx.<name> = value / ctx.<name> access from Python. Used for scalars, shape tuples, hyperparameters, and other non-tensor state.

Notes

The object is heap-allocated and jointly owned by the Python wrapper and the associated PythonBackwardNode, so it outlives the forward call and remains valid for the entire backward traversal. Capturing tensors through Python closures instead of saving them through ctx bypasses this ownership tracking and leaks memory.

Constructors

1
ctor

FunctionCtx

Construct a FunctionCtx for a custom Function::forward call.

The newly created context starts with empty saved_tensors_ and extras_ containers; the user's forward body populates them via save_for_backward and store before returning. Defaulted — no resources need acquisition at construction.

Destructor

1

Methods

8
fnconst

for_each_owned_tensor

void for_each_owned_tensor(const int & fn)

Visit each owned TensorImpl held in saved_ for cyclic GC traversal without constructing temporary shared_ptr copies.

fnconst

load

→py::object
int load(const int & key)

Retrieve a previously stored Python object.

Parameters

keyconst std::string&
Attribute name previously passed to store.

Returns

py::object

The stored object, or py::none() when key is absent. The __getattr__ binding translates the None sentinel into a Python AttributeError.

fnconst

materialize_grads

bool materialize_grads()
fn

save_for_backward

void save_for_backward(int)

Save tensors needed for the backward pass.

Non-output tensors are stored by strong shared_ptr<TensorImpl>. Outputs of this context's own PythonBackwardNode are stored as a detached storage alias plus a weak pointer to the live output, a weak pointer to the node, and the output slot index (the reference framework's SavedVariable for outputs), breaking the output -> node -> ctx -> output cycle so forward-only calls are freed immediately by refcount without waiting for cyclic GC.

Parameters

tensorsstd::vector<std::shared_ptr<TensorImpl>>
Tensors to save, in the order saved_tensors() should return them. Moved into the context.

Notes

Each call overwrites the previously saved list; saving twice does not accumulate. To stash non-tensor data (shapes, axes, scalars) use the store / load extras bag instead.

fn

set_owner_node

void set_owner_node(const int & owner)

Record the owning PythonBackwardNode so save_for_backward can recognise outputs that belong to this node and hold the node weakly.

fn

store

void store(const int & key, int val)

Store an arbitrary Python object under a string key.

Used by the pybind11 __setattr__ binding so that Python code assigning ctx.shape = x.shape lands here.

Parameters

keyconst std::string&
Attribute name.
valpy::object
Value to store; ownership is transferred into the extras map.