Core

class stride.core.Variable(*args, **kwargs)[source]

Bases: object

Variables are the inputs and outputs of operators, and track the graph through which they have travelled.

Parameters
  • name (str, optional) – Name of the varible, defaults to automatic name.

  • needs_grad (bool, optional) – Whether or not the gradient wrt to this variable is needed, and thus whether or not the adjoint graph starting from this variable needs to be constructed, defaults to False.

async __call_adjoint__(grad, **kwargs)[source]

Adjoint operation of the variable, which accumulates the given gradient on the Variable.grad attribute.

Parameters

grad (object) – Provided gradient

async adjoint(grad=None, **kwargs)[source]

Run the adjoint graph that has this variable as its root.

Parameters
  • grad (optional) – Gradient seed to start the adjoint run.

  • kwargs (optional) – Extra arguments to pass on through the adjoint run.

alike(*args, **kwargs)[source]

Alias for a copy.

as_parameter(*args, **kwargs)[source]

Create a copy of the variable that is detached from the original graph and re-initialised as a parameter.

Returns

Detached variable.

Return type

Variable

clear_grad()[source]

Clear the gradient buffer of the variable.

copy(*args, **kwargs)[source]

Create a variable that shares its characteristics with this object.

The same parameters as those given to __init__ are valid here. Otherwise the new object will be configured to be like this one.

Returns

Copied variable.

Return type

Variable

detach(*args, **kwargs)[source]

Create a copy of the variable that is detached from the original graph.

Returns

Detached variable.

Return type

Variable

process_grad()[source]

Process the gradient of the variable for its use.

Returns

Processed gradient

Return type

object

class stride.core.Operator(*args, **kwargs)[source]

Bases: object

Operators represent operations that, when performed on Variables, construct an adjoint graph that can then be executed in an adjoint run to calculate necessary gradients.

Parameters

name (str, optional) – Name of the varible, defaults to automatic name.

async __call__(*args, **kwargs)[source]

Operators are executed by calling them. The operator will then take care of tracking all necessary Variables.

Parameters
  • args

  • kwargs

async __call_adjoint__(*output_grads, **kwargs)[source]

This method runs the necessary operations to execute the adjoint of the operator.

Parameters
  • output_grads

  • kwargs

abstract async adjoint(*args, **kwargs)[source]

Method defining the adjoint behaviour of the operator. This method needs to be defined by classes inheriting from the operator.

The method will be called with positional arguments comprised of: the gradients of every output of the forward operation, followed by the arguments originally given when calling the forward method.

The adjoint method needs to return a gradient for each of its Variable inputs (or None if the variable does not needs_grad).

This method should not be called directly from user code.

Parameters
  • args

  • kwargs

abstract async forward(*args, **kwargs)[source]

Method defining the forward behaviour of the operator. This method needs to be defined by classes inheriting from the operator.

The method can take multiple inputs and produce multiple outputs. Outputs of this method should be of type Variable.

Positional and keyword arguments to forward are processed so that present variables are tracked.

This method should not be called directly from user code.

Parameters
  • args

  • kwargs

class stride.core.Graph[source]

Bases: object

Class representing an adjoint graph.

add(node)[source]

Add node to the graph.

Parameters

node (Node) –

print(root=None)[source]

Print the graph.

Parameters

root (Node, optional) – Root to start the printing from if topological sorting is wanted.

static toposort(root)[source]

Iterate over the graph in topological order, starting a the given root and all the way to the leaves.

Parameters

root (Node) –

Returns

Return type

iterable

class stride.core.Node(op, method, idx=0, nxt=None)[source]

Bases: object

Node in the adjoint graph.

Parameters
  • op (Operator) – Operator to which the Node refers.

  • method (str) – Method within the operator that is to be executed in the adjoint pass.

  • idx (int, optional) – Index, within the argument list of the adjoint method, that this node represents.

  • nxt (list, optional) – Nodes to which the result of this one will be propagated to.

add_next(node)[source]

Add node to the list of next nodes.

Parameters

node (Node) –

copy()[source]

Create a copy of the node.

Returns

Return type

Node

property name

Full name of the node.

property name_idx

Name of the node including its index.