~/bend-docscommunity

src/autograd/tensor_chain_LAWS.bend open laws/TODOs

raw source on the hub · import bendtorch-core@0.1.0.0/src/autograd/tensor_chain_LAWS.bend as Tensor_chain_LAWS

5 imports
import Base
import ../shape/core.bend as Shape
import ../tensor/core.bend as Core
import ./gradient.bend as Gradient
import ./tensor_backward.bend as Backward

Laws

law tensor_sweep_chain_rule provedin PROOF.bendsource · line 139 · raw

@entries:List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry> -> @gradients:List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot> -> @+count:Nat -> @-swept:List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot> -> @succeeded:{result(0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.sweep(entries, Done{gradients}, count)) == Done{swept} : Slots} -> {chained(entries, gradients, count, []) == Done{swept} : Slots}

The chain rule, for any recorded tape and any starting working gradients: when the sweep succeeds, the working gradient it leaves at every entry is the chained one, each slot the accumulation, in the order above, of the VJP values its consumers sent it at their own chained gradients. A refusal is stated by the structure laws in tensor_backward_LAWS.bend.

law tensor_backward_chain_rule provedin PROOF.bendsource · line 158 · raw

@newer:List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry> -> @+node:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Node -> @+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @value:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @saved:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Saved -> @slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @rest:List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry> -> @+older:Nat -> @ones:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @made:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.new(shape, 1.0) == Done{ones} : Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape>>} -> @-gradients:List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot> -> @succeeded:{result(0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.pass(List.append(&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry, newer, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry{node, shape, value, saved, slot} <> rest), 1n+Nat.add(older, List.length(&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry, newer)), older, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.at_head(1n+Nat.add(older, List.length(&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry, newer)), older))) == Done{gradients} : Slots} -> {chained(List.append(&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry, newer, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry{node, shape, value, saved, slot} <> rest), seeds(List.length(&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry, newer), shape, ones, older), 1n+Nat.add(older, List.length(&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry, newer)), []) == Done{gradients} : Slots}

The chain rule for the whole backward: when the pass of a backward from the output with older entries below it succeeds, the working gradients it hands to the merge (tensor_backward_success, tensor_merge_accumulates) are the chained ones from the seeds, for any tape, newer entries included.

Definitions

def Slots source · line 27 · raw

Type

def Log source · line 31 · raw

Type

Contributions sent so far, newest first.

def Taken source · line 35 · raw

Type

The gathered slot of one id, and the contributions left for the others.

def settled source · line 38 · raw

@outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot>

def poured source · line 45 · raw

@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @held:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot> -> @gradient:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot>

def sorted source · line 53 · raw

@+target:Nat -> @+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @gradient:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @mine:Bool -> @pair:Taken -> Taken

def take source · line 64 · raw

@log:Log -> @+id:Nat -> @slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> Taken

Takes out of a newest-first log every contribution to id and accumulates them into slot, oldest first; the others stay in the log, in order.

def joined source · line 71 · raw

@held:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot> -> @rest:Slots -> Slots

def gather_step source · line 78 · raw

@pair:Taken -> @rec:(@_:Log -> Slots) -> Slots

def gather source · line 85 · raw

@slots:List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot> -> @+count:Nat -> @log:Log -> Slots

Each slot of a newest-first list of count slots, the one with k older slots for id k, with what the log sends to it. A list longer than count keeps its slots past count, as the sweep does.

def fired source · line 96 · raw

@pass:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Pass -> @log:Log -> @rec:(@_:Log -> Slots) -> Slots

An entry that holds g after gathering: its VJP's contributions join the log in operand order, so the first operand's arrives first.

def reached source · line 104 · raw

@node:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Node -> @saved:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Saved -> @pair:Taken -> @rec:(@_:Log -> Slots) -> Slots

def chained source · line 118 · raw

@entries:List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Entry> -> @slots:List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot> -> @+count:Nat -> @log:Log -> Slots

The working gradients by the chain rule, pulled, for entries newest first that start with slots and receive the contributions in log: the entry with id k gathers every contribution sent to k into its starting slot; if it then holds g, the contributions of vjp(node, saved, g) join the log.

def result source · line 130 · raw

@pair:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/tensor_backward.Swept -> Slots

The working gradients of a sweep, without its entries.

def seeds source · line 151 · raw

@+newer:Nat -> @+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @ones:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @+older:Nat -> List<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot>

The starting working gradients of a backward from the entry with older entries below it and newer entries above it: no gradient for the newer entries, ones of the output's shape at the output, as PyTorch's backward(torch.ones_like(output)), and no gradient below.