src/autograd/reduce.bend checks
raw source on the hub · import bendtorch-core@0.1.0.0/src/autograd/reduce.bend as Reduce
9 imports
import Base import ../shape/core.bend as Shape import ../storage/main.bend as Storage import ../storage/model.bend as Model import ../tensor/core.bend as Core import ../tensor/elementwise.bend as Elementwise import ../tensor/layout.bend as Layout import ../tensor/matmul.bend as Matrix import ../tensor/parallel.bend as Parallel
Types
type Error source · line 17 · raw
Data
TensorError@error:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error -> Error
NotReducibleError
type Fibers source · line 177 · raw
Data
What every cell of a build needs: the index rows of the plan and the contribution extent of each row.
Fibers@rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @extents:List<&2, Nat> -> Fibers
Definitions
def leaf_add source · line 31 · raw
@accumulator:F32 -> @pair:Pair(Array<F32>, F32) -> Pair(F32, Array<F32>)
def span source · line 37 · raw
@+left:Nat -> @+extent:Nat -> Nat
How many digits a row walks for a target cell: every digit of an axis the target reduces (left stride 0), else one.
def first_span source · line 44 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> Nat
def first_digit source · line 53 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+target:Nat -> Nat
The first digit a row walks: 0 on a reduced axis (Nat.div by 0 is 0),
else the digit target names there.
def walk_next source · line 60 · raw
@rec:(@_:F32 -> @_:Array<F32> -> Pair(F32, Array<F32>)) -> @pair:Pair(F32, Array<F32>) -> Pair(F32, Array<F32>)
def fiber source · line 70 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> @fuel:Nat -> @+target:Nat -> @+base:Nat -> @+digit:Nat -> @accumulator:F32 -> @array:Array<F32> -> Pair(F32, Array<F32>)
The words of the fiber of target below these rows, added to
accumulator in increasing position. extents lists the contribution
extent of each row and base is the contribution offset the rows above
chose. The first row walks fuel digits from digit, each at offset
base + right * digit, and the rows below get target % left (all of
target on a reduced axis, as Nat.mod by 0 keeps it).
def single_rows source · line 87 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> Bool
Whether every row here takes one digit (span 1), so that the fiber below a digit of the row above is one word.
def single_below source · line 97 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> Bool
single_rows of the rows below the first one.
def single_offset source · line 105 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> @+target:Nat -> @+base:Nat -> Nat
The offset of the one word that rows of span 1 reach from base.
def step_offset source · line 113 · raw
@+start:Nat -> @+step:Nat -> @+d:Nat -> Nat
The contribution offset of digit d of a row whose right stride is step.
def strided_offset source · line 116 · raw
@+step:Nat -> @+base:Nat -> @+digit:Nat -> @+lift:Nat -> Nat
def strided_run source · line 122 · raw
@fuel:Nat -> @+step:Nat -> @+base:Nat -> @+digit:Nat -> @+lift:Nat -> @accumulator:F32 -> @pair:Pair(Array<F32>, F32) -> Pair(F32, Array<F32>)
The fuel + 1 words at offsets base + step * d + lift for d = digit, digit +
1, ..., added to accumulator in that order; pair is the read of the
first. Each step reads the next word and hands it on as an argument.
def strided source · line 131 · raw
@fuel:Nat -> @+step:Nat -> @+base:Nat -> @+digit:Nat -> @+lift:Nat -> @accumulator:F32 -> @array:Array<F32> -> Pair(F32, Array<F32>)
def fiber_fast source · line 144 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> @fuel:Nat -> @single:Bool -> @+target:Nat -> @+base:Nat -> @+digit:Nat -> @accumulator:F32 -> @array:Array<F32> -> Pair(F32, Array<F32>)
fiber, the same words added in the same order, except that a row whose
rows below all take one digit (single, which is single_below of the
rows; a later digit of the same row passes False) walks its digits in one
strided loop: below digit d it reads the one word at base + right * d +
single_offset(rest, others, target % left, 0), with no closure per word.
Column sums, row sums and batch sums over one axis take that loop.
def fiber_of source · line 162 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> @+target:Nat -> @array:Array<F32> -> Pair(F32, Array<F32>)
Cell target of the reduction: its whole fiber from +0.0.
def cell source · line 167 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> @+position:Nat -> @array:Array<F32> -> @inside:Bool -> Pair(F32, Array<F32>)
One target cell: its fiber sum from +0.0 when the cell is below count,
else +0.0.
def fibers source · line 181 · raw
@+given:Fibers -> Fibers
Each thread below the forks reads its own copy.
def keep source · line 187 · raw
@pair:Pair(F32, Array<F32>) -> @none:Unit -> Pair(F32, Pair(Array<F32>, Unit))
The cell function Parallel.build runs: its state is the contribution
array, next to a Unit that fills the build's second state slot.
def cell_at source · line 191 · raw
@+fibers:Fibers -> @+position:Nat -> @state:Pair(Array<F32>, Unit) -> @inside:Bool -> Pair(F32, Pair(Array<F32>, Unit))
def halves source · line 197 · raw
@pair:Pair(Array<F32>, Array<F32>) -> Pair(Pair(Array<F32>, Unit), Pair(Array<F32>, Unit))
Each half of a fork gets its own clone of the contribution array.
def split source · line 201 · raw
@state:Pair(Array<F32>, Unit) -> Pair(Pair(Array<F32>, Unit), Pair(Array<F32>, Unit))
def build source · line 205 · raw
@+depth:Nat -> @+splits:Nat -> @+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> @+count:Nat -> @+base:Nat -> @array:Array<F32> -> Array<F32>
def build_balanced source · line 211 · raw
@+depth:Nat -> @+splits:Nat -> @+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> @+count:Nat -> @+base:Nat -> @array:Array<F32> -> 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/model.Balanced(F32, depth, build(depth, splits, rows, extents, count, base, array))
The parallel build returns a balanced tree of its depth, whatever the split.
def terms source · line 217 · raw
@+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @+length:Nat -> Nat
A target cell costs one term per contribution word that lands on it.
def kept_after source · line 222 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> Bool
Whether some axis below these rows has extent above 1 and is kept by the target (left stride above 0).
def contiguous source · line 233 · raw
@+rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> @+extents:List<&2, Nat> -> Bool
Whether every fiber is one contiguous run of the contribution: no axis the target keeps comes after an axis it reduces, counting only axes of extent above 1. Row sums such as [n, m] to [n, 1] are contiguous; column and batch sums such as [n, m] to [1, m] or [b, n, m] to [n, m] are strided.
def splits source · line 246 · raw
@+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @+depth:Nat -> @+length:Nat -> Nat
How many top levels of the target tree fork. Every fork clones the whole
contribution. A target whose fibers are contiguous splits by work, as
Matrix.splits does with the terms of a cell; a target with strided fibers
never forks, since there the forked build ran slower at 8 and 24 threads
than one thread. The proofs hold for any split.
def parallel_rows source · line 251 · raw
@+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+depth:Nat -> @+length:Nat -> @-valid:{0x1689e5deb278c16abd3bd45eac101ef3/src/storage/main.bounds(depth, length) == True{} : Bool} -> @-exact:{length == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.logical_numel(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(target)) : Nat} -> @taken:Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Array<F32>) -> @rows:List<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Row> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def parallel_indexed source · line 264 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @output:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<target> -> @+depth:Nat -> @+length:Nat -> @-valid:{0x1689e5deb278c16abd3bd45eac101ef3/src/storage/main.bounds(depth, length) == True{} : Bool} -> @-exact:{length == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.logical_numel(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(target)) : Nat} -> @map:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Index<0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.extents(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.left_shape(plan)), 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.extents(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.right_shape(plan)), 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.extents(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.output_shape(plan))> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)An empty contribution leaves every target cell at +0.0.
def parallel_allocated source · line 275 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @result:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<target>> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def fitted source · line 288 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan -> @same:Bool -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def planned source · line 296 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @result:Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.BroadcastPlan> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def reduce_to source · line 310 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+target:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
The contribution owner comes back unchanged next to the result. A target that does not broadcast with the contribution fails with the planner's broadcast error; a target whose broadcast with the contribution is larger than the contribution (for example [2, 3] for a [3] contribution) fails with NotReducible.
def reduce_to_dimensions_result source · line 314 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @-dimensions:List<&2, U32> -> @result:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.ConstructResult<dimensions> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def reduce_to_dimensions source · line 322 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+dimensions:List<&2, U32> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def error_code source · line 325 · raw
@error:Error -> U32