src/autograd/gradient_LAWS.bend open laws/TODOs
raw source on the hub · import bendtorch-core@0.1.0.0/src/autograd/gradient_LAWS.bend as Gradient_LAWS
5 imports
import Base import ../shape/core.bend as Shape import ../tensor/core.bend as Core import ../tensor/elementwise_totality.bend as Totality import ./gradient.bend as Gradient
Laws
law gradient_unset_zeros provedin PROOF.bendsource · line 111 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> ZerosModel<shape, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.gradient(shape, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Unset{})>A slot that never received a contribution reads as zeros of its shape.
law gradient_held provedin PROOF.bendsource · line 116 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @held:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> {0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.gradient(shape, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Held{held}) == Done{held} : Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>}A slot that holds a gradient reads as that gradient.
law accumulate_mapping provedin PROOF.bendsource · line 124 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> MappingModel<slot, contribution, shape, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.accumulate(shape, slot, contribution)>
On success the slot holds a gradient with the slot's dimensions. At every in-bounds position, the first contribution moves in unchanged and every later one is added to the held total with one F32 add, total first.
law accumulate_refusal_keeps provedin PROOF.bendsource · line 132 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> RefusalModel<slot, contribution, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.accumulate(shape, slot, contribution)>
Every refusal is a shape mismatch, decided before any write: the contribution comes back unchanged, and so does the slot.
law accumulate_valid_success provedin PROOF.bendsource · line 140 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @fits:Fits(shape, slot) -> @agrees:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise_totality.dimensions(contribution) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape) : List<&2, U32>} -> Success<0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.accumulate(shape, slot, contribution)>A contribution and a slot that both agree with the slot's shape always accumulate.
law grad_unset_none provedin PROOF.bendsource · line 150 · raw
{0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.grad(0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Unset{}) == None{} : Maybe<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>}Like PyTorch's .grad, a slot that never received a contribution has no gradient: grad reads it as None, not as zeros.
law grad_held_some provedin PROOF.bendsource · line 154 · raw
@held:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> {0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.grad(0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Held{held}) == Some{held} : Maybe<&1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>}A slot that holds a gradient reads as that gradient.
law reset_unset provedin PROOF.bendsource · line 159 · raw
@slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> {0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.reset(slot) == 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Unset{} : 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot}reset returns every slot to Unset, like zero_grad(set_to_none=True).
law reset_then_accumulate_moves_in provedin PROOF.bendsource · line 165 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @agrees:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise_totality.dimensions(contribution) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape) : List<&2, U32>} -> {0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.accumulate(shape, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.reset(slot), contribution) == 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Accumulated{0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Held{contribution}} : 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome}After a reset, a contribution with the slot's dimensions moves in unchanged, so a -0.0 word stays -0.0 instead of becoming +0.0 + -0.0.
law accumulate_wrong_shape_refused provedin PROOF.bendsource · line 175 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @differ:(@_:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise_totality.dimensions(contribution) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape) : List<&2, U32>} -> Empty) -> {0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.accumulate(shape, slot, contribution) == 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Refused{slot, contribution, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.ShapeMismatch{}} : 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome}A contribution whose dimensions differ from the slot's is refused with ShapeMismatch, and the slot and the contribution come back unchanged, even when the element counts agree ([3, 2] or [6] for a [2, 3] slot).
Types
type MappingModel source · line 98 · raw
@-slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @-contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @-shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @-outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> Data
MappingModel@-slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @-contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @-shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @-outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> @-mapping:Mapping(slot, contribution, shape, outcome) -> MappingModel<slot, contribution, shape, outcome>
type RefusalModel source · line 101 · raw
@-slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @-contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @-outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> Data
RefusalModel@-slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @-contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @-outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> @-keeps:RefusalKeeps(slot, contribution, outcome) -> RefusalModel<slot, contribution, outcome>
type Success source · line 104 · raw
@-outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> Data
Success@-outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> @-successful:{accumulated(outcome) == True{} : Bool} -> Success<outcome>
type ZerosModel source · line 107 · raw
@-shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @-result:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic> -> Data
ZerosModel@-shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @-result:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic> -> @-zeros:Zeros(shape, result) -> ZerosModel<shape, result>
Definitions
def add_word source · line 7 · raw
@total:Maybe<&2, F32> -> @value:Maybe<&2, F32> -> Maybe<&2, F32>
def combined source · line 20 · raw
@slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+index:U32 -> Maybe<&2, F32>
The value the specification assigns to one position after a contribution is accumulated into a slot: an Unset slot takes the contribution's word as it is, and a Held slot adds the contribution's word to its own with one F32 add, the held total first.
def held_dimensions source · line 27 · raw
@slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> Maybe<&2, List<&2, U32>>
def held_observe source · line 34 · raw
@slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @+index:U32 -> Maybe<&2, F32>
def Inside source · line 41 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+index:U32 -> Type
def Mapping source · line 44 · raw
@-slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @-contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> Type
def SlotKept source · line 53 · raw
@before:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @after:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> Type
def mismatched source · line 61 · raw
@error:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Error -> Bool
def RefusalKeeps source · line 68 · raw
@-slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> @-contribution:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> Type
def Fits source · line 75 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @slot:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Slot -> Type
def accumulated source · line 82 · raw
@outcome:0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/gradient.Outcome -> Bool
def Zeros source · line 89 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @result:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic> -> Type