Skip to main content

GradientRule

Trait GradientRule 

Source
pub trait GradientRule {
    // Required method
    fn backward(
        &self,
        graph: &SharedGraph,
        op: &Op,
        inputs: &[NodeId],
        output: NodeId,
        upstream: NodeId,
    ) -> GradientList;
}
Expand description

Per-op backward rule. Implementors emit gradient nodes into the shared graph and return one gradient per forward input.

Convention: a rule is responsible for all input slots of its op. If an input is non-differentiable, return None for that slot. Don’t add the upstream into the per-input gradient sum — the caller’s accumulate_grad handles cross-edge accumulation.

Required Methods§

Source

fn backward( &self, graph: &SharedGraph, op: &Op, inputs: &[NodeId], output: NodeId, upstream: NodeId, ) -> GradientList

Build gradient nodes for this op.

  • graph: the shared graph; rule pushes new nodes into it
  • op: a reference to the forward op (for parameter access)
  • inputs: forward-input NodeIds (one per slot)
  • output: the forward output’s NodeId
  • upstream: the upstream gradient flowing into this output

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§