pub struct ComputationGraph { /* private fields */ }Expand description
Computation graph that records operations for backward pass.
The graph uses a tape-based approach where operations are recorded in order during the forward pass, then gradients are computed in reverse order during the backward pass.
§Thread Safety
Each thread has its own computation graph (via thread_local storage
in the parent module). This avoids synchronization overhead during
single-threaded training.
Implementations§
Source§impl ComputationGraph
impl ComputationGraph
Sourcepub fn new() -> ComputationGraph
pub fn new() -> ComputationGraph
Create a new empty computation graph.
Sourcepub fn register_tensor(&mut self, tensor: Tensor)
pub fn register_tensor(&mut self, tensor: Tensor)
Register a tensor that requires gradients.
Sourcepub fn record(
&mut self,
output_id: TensorId,
grad_fn: Arc<dyn GradFn>,
input_ids: Vec<TensorId>,
)
pub fn record( &mut self, output_id: TensorId, grad_fn: Arc<dyn GradFn>, input_ids: Vec<TensorId>, )
Record an operation to the tape.
Sourcepub fn get_tensor(&self, id: TensorId) -> Option<&Tensor>
pub fn get_tensor(&self, id: TensorId) -> Option<&Tensor>
Get a tensor by ID.
Sourcepub fn get_tensor_mut(&mut self, id: TensorId) -> Option<&mut Tensor>
pub fn get_tensor_mut(&mut self, id: TensorId) -> Option<&mut Tensor>
Get a mutable tensor by ID.
Sourcepub fn backward(&mut self, output_id: TensorId, grad_output: Tensor)
pub fn backward(&mut self, output_id: TensorId, grad_output: Tensor)
Compute gradients via backpropagation.
This implements the reverse-mode automatic differentiation algorithm:
- Start with
grad_outputfor the output tensor - Iterate through operations in reverse order
- For each operation, compute gradients w.r.t. inputs
- Accumulate gradients for tensors used multiple times
§Arguments
output_id- ID of the tensor to differentiategrad_output- Initial gradient (typically ones for scalar loss)
Sourcepub fn get_grad(&self, id: TensorId) -> Option<Tensor>
pub fn get_grad(&self, id: TensorId) -> Option<Tensor>
Get gradient for a tensor by ID (after backward).
Sourcepub fn clear_grad(&mut self, id: TensorId)
pub fn clear_grad(&mut self, id: TensorId)
Clear gradient for a specific tensor.
Trait Implementations§
Source§impl Default for ComputationGraph
impl Default for ComputationGraph
Source§fn default() -> ComputationGraph
fn default() -> ComputationGraph
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for ComputationGraph
impl !UnwindSafe for ComputationGraph
impl Freeze for ComputationGraph
impl Send for ComputationGraph
impl Sync for ComputationGraph
impl Unpin for ComputationGraph
impl UnsafeUnpin for ComputationGraph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more