src/autograd/sum_to_size_fold_LAWS.bend open laws/TODOs
raw source on the hub · import bendtorch-core@0.1.0.0/src/autograd/sum_to_size_fold_LAWS.bend as Sum_to_size_fold_LAWS
12 imports
import Base import ../shape/core.bend as Shape import ../storage/LAWS.bend as StorageLaws import ../tensor/core.bend as Core import ../tensor/layout.bend as Layout import ../tensor/lifecycle.bend as Lifecycle import ../tensor/contracts/replayed.bend as Replayed import ../tensor/contracts/LAWS.bend as Contracts import ./reduce.bend as Reduce import ./reduce_LAWS.bend as ReduceLaws import ./reduce_fold_LAWS.bend as ReduceFold import ./sum_to_size.bend as SumToSize
Laws
law sum_to_size_clone provedin PROOF.bendsource · line 19 · raw
@+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @same:{0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(target) : List<&2, U32>} -> @+fill:F32 -> @+ops:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/LAWS.Op> -> @-fits:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/lifecycle.logical_length(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/replayed.storage(shape, fill, ops)) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.logical_numel(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape)) : Nat} -> 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/reduce_fold_LAWS.ReduceToContract<target, shape, index => Some{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/replayed.value(shape, fill, ops, index)}, other => 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/replayed.value(shape, fill, ops, other), 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/sum_to_size.sum_to_size(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Packed{shape, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.TTensor{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/replayed.storage(shape, fill, ops), fits}}, target)>sum_to_size through the reduce_to contract, nothing reduced: when the target has the contribution's dimensions, a replayed contribution comes back as its clone. The call succeeds with the target's dimensions, every cell holds the contribution's own word (-0.0 stays -0.0, as in PyTorch's autograd), and the returned contribution keeps every word.
law sum_to_size_fiber provedin PROOF.bendsource · line 36 · raw
@+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @planned:{0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.broadcast_checked(target, shape) == Done{plan} : Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan>} -> @same:{0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.output_shape(plan)) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape) : List<&2, U32>} -> @differ:(@_:{0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(target) : List<&2, U32>} -> Empty) -> @+fill:F32 -> @+ops:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/LAWS.Op> -> @-fits:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/lifecycle.logical_length(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/replayed.storage(shape, fill, ops)) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.logical_numel(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape)) : Nat} -> 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/reduce_fold_LAWS.ReduceToContract<target, shape, index => 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/reduce_LAWS.fiber(plan, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/LAWS.words(shape, fill, ops, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.logical_numel(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape))), index, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.logical_numel(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape))), other => 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/replayed.value(shape, fill, ops, other), 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/sum_to_size.sum_to_size(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Packed{shape, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.TTensor{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/replayed.storage(shape, fill, ops), fits}}, target)>sum_to_size through the reduce_to contract, axes reduced: when the dimensions differ and the target broadcasts to exactly the contribution's dimensions, a replayed contribution gives the ReduceToContract of reduce_to_fiber: every target cell holds ReduceLaws.fiber over the contribution's words in index order from +0.0.
law sum_to_size_replayed provedin PROOF.bendsource · line 93 · raw
@+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @planned:{0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.broadcast_checked(target, shape) == Done{plan} : Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan>} -> @same:{0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.output_shape(plan)) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape) : List<&2, U32>} -> @+fill:F32 -> @+ops:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/LAWS.Op> -> @-fits:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/lifecycle.logical_length(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/replayed.storage(shape, fill, ops)) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.logical_numel(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape)) : Nat} -> @+values:List<&2, F32> -> @words:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/LAWS.words(shape, fill, ops, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.logical_numel(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape))) == values : List<&2, F32>} -> @-P:(@_:Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/reduce.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>) -> Type) -> @h:ReducedWith(target, shape, plan, fill, values, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/layout.words_same(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(shape), 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(target)), P) -> P(0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/sum_to_size.sum_to_size(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Packed{shape, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.TTensor{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/contracts/replayed.storage(shape, fill, ops), fits}}, target))sum_to_size of a replayed contribution is replayed, in the transport form
broadcast_to_replayed uses: when the target broadcasts to exactly the
contribution's dimensions and values lists the contribution's words, then
for every P, if P holds for every result that hands back the contribution
with the same words and gives an output with the target's dimensions whose
cells hold summed, P holds for the result. The output is the clone when
nothing is reduced and reduce_to's output otherwise; both are replayed
tensors, so a later family law applies to either half.
Definitions
def summed source · line 57 · raw
@+same:Bool -> @+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @+count:Nat -> @+values:List<&2, F32> -> @+index:U32 -> Maybe<&2, F32>
What sum_to_size gives at target cell index for a contribution whose words,
in index order, are values, and which has count words. When same is True
(the target has the contribution's dimensions) nothing is reduced and the
cell holds its own word; otherwise it holds ReduceLaws.fiber, the words that
land on the cell added in index order from +0.0.
def ReducedWith source · line 68 · raw
@+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @+fill:F32 -> @+values:List<&2, F32> -> @+same:Bool -> @-P:(@_:Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/autograd/reduce.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>) -> Type) -> Type
What the transport form of sum_to_size_replayed asks of P: P holds for the
returned contribution, replayed with any history kept that keeps every
word, next to an output replayed with any history, with the target's
dimensions, whose every cell holds summed.