src/tensor/gather.bend checks
raw source on the hub · import bendtorch-core@0.1.0.0/src/tensor/gather.bend as Gather
11 imports
import Base import ../shape/core.bend as Shape import ../storage/main.bend as Storage import ../storage/model.bend as Model import ../storage/certificates.bend as Certificates import ./core.bend as Core import ./layout.bend as Layout import ./lifecycle.bend as Lifecycle import ./parallel.bend as Parallel import ./slice.bend as Slice import ./signed.bend as Signed
Types
type Error source · line 41 · raw
Data
TensorError@error:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error -> Error
LayoutError@error:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/layout.Error -> Error
OutOfRangeError
IndexRankMismatchError
IndexShapeMismatchError
type Index source · line 51 · raw
Data
PyTorch's index tensor for gather: its shape, a Shape as every tensor's (rank at most 4, at most 2^20 elements), and its entries in row-major order.
Index@shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @entries:List<&2, U32> -> Index
type Lookup source · line 69 · raw
Data
Which entry a cell reads: entry k of an index_select, the entry at the output position itself for a gather.
SelectedLookup
GatheredLookup
type Plan source · line 75 · raw
Data
What every cell of both builds reads: the kind, the view [outer, extent, inner] of the input around the axis, and the length of the output's axis.
Plan@kind:Lookup -> @outer:Nat -> @extent:Nat -> @inner:Nat -> @length:Nat -> Plan
type Planned source · line 127 · raw
Data
A plan the checks accept, and its output shape.
Planned@plan:Plan -> @output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> Planned
type Pass source · line 358 · raw
Data
ForwardPass
BackwardPass
Definitions
def built_index source · line 54 · raw
@+entries:List<&2, U32> -> @-dimensions:List<&2, U32> -> @built:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.ConstructResult<dimensions> -> Result<&2, &2, Error, Index>
def index source · line 64 · raw
@+dimensions:List<&2, U32> -> @+entries:List<&2, U32> -> Result<&2, &2, Error, Index>
An Index from an index tensor's dimensions and entries, refused as a tensor of those dimensions is: past rank 4 (RankExceeded, 1) or 2^20 elements (CapacityExceeded, 2). Its entry count is checked by gather.
def plan_length source · line 78 · raw
@+plan:Plan -> Nat
def count source · line 82 · raw
@entries:List<&2, U32> -> Nat
def below source · line 90 · raw
@entries:List<&2, U32> -> @+extent:Nat -> Bool
Whether every entry is below the extent.
def equal source · line 98 · raw
@left:List<&2, U32> -> @right:List<&2, U32> -> Bool
Whether two lists of dimensions are equal.
def matches source · line 108 · raw
@dimensions:List<&2, U32> -> @index:List<&2, U32> -> @+dim:Nat -> Bool
Whether the index's dimensions are the input's at every axis but dim.
def viewed source · line 119 · raw
@+kind:Lookup -> @+given:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/slice.View -> @+length:Nat -> Plan
The plan of a gather or an index_select of length entries at an axis in
range.
def planned_at source · line 123 · raw
@+kind:Lookup -> @+dimensions:List<&2, U32> -> @+axis:Nat -> @+length:Nat -> Plan
def shaped source · line 130 · raw
@+plan:Plan -> @-dimensions:List<&2, U32> -> @built:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.ConstructResult<dimensions> -> Result<&2, &2, Error, Planned>
def accepted source · line 139 · raw
@+kind:Lookup -> @+dimensions:List<&2, U32> -> @+axis:Nat -> @+length:Nat -> Result<&2, &2, Error, Planned>
The output keeps every axis of the input but axis, whose extent becomes
the length.
def ranged source · line 143 · raw
@+kind:Lookup -> @+dimensions:List<&2, U32> -> @+axis:Nat -> @+entries:List<&2, U32> -> @+length:Nat -> @inside:Bool -> Result<&2, &2, Error, Planned>
def selecting source · line 152 · raw
@+dimensions:List<&2, U32> -> @+axis:Nat -> @+entries:List<&2, U32> -> @in_rank:Bool -> Result<&2, &2, Error, Planned>
The axis check, then the entries against the extent.
def select_plan source · line 160 · raw
@+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @dim:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/signed.Position -> @+entries:List<&2, U32> -> Result<&2, &2, Error, Planned>
The plan of index_select(dim, index), or its refusal.
def gather_shaped source · line 165 · raw
@+dimensions:List<&2, U32> -> @+axis:Nat -> @+index:List<&2, U32> -> @+entries:List<&2, U32> -> @same:Bool -> Result<&2, &2, Error, Planned>
def gather_ranked source · line 173 · raw
@+dimensions:List<&2, U32> -> @+axis:Nat -> @+index:List<&2, U32> -> @+entries:List<&2, U32> -> @same:Bool -> Result<&2, &2, Error, Planned>
def gather_axis source · line 181 · raw
@+dimensions:List<&2, U32> -> @+axis:Nat -> @+index:List<&2, U32> -> @+entries:List<&2, U32> -> @in_rank:Bool -> Result<&2, &2, Error, Planned>
def gather_counted source · line 189 · raw
@+dimensions:List<&2, U32> -> @+axis:Nat -> @+index:List<&2, U32> -> @+entries:List<&2, U32> -> @exact:Bool -> Result<&2, &2, Error, Planned>
def gather_plan source · line 201 · raw
@+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @dim:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/signed.Position -> @+index:Index -> Result<&2, &2, Error, Planned>
The plan of gather(dim, index), or its refusal, in this order: an entry count other than the index shape's element count, the axis, the index's rank, its extents at the other axes, then its entries. The element count is the Shape's own, so no product of its extents is formed here.
def written source · line 209 · raw
@entries:List<&2, U32> -> @array:Array<U32> -> @+at:U32 -> Array<U32>
The entries as a balanced U32 array that a cell reads in a few steps: the smallest depth that holds them, as Storage allocates.
def index_depth source · line 216 · raw
@pair:Pair(Nat, Nat) -> Nat
def index_array source · line 220 · raw
@+entries:List<&2, U32> -> Array<U32>
def position source · line 227 · raw
@+rows:Nat -> @+inner:Nat -> @+o:Nat -> @+k:Nat -> @+r:Nat -> Nat
The position of cell (o, k, r) of the view [outer, rows, inner].
def entry_of source · line 232 · raw
@+kind:Lookup -> @+length:Nat -> @+inner:Nat -> @+o:Nat -> @+k:Nat -> @+r:Nat -> Nat
The entry output cell (o, k, r) reads: k for an index_select, the cell's own position for a gather.
def entry source · line 241 · raw
@+plan:Plan -> @+at:Nat -> Nat
Output cell at is (o, k, r) of [outer, length, inner]; with entry j, it
reads input cell (o, j, r) of [outer, extent, inner].
def source source · line 246 · raw
@+plan:Plan -> @+at:Nat -> @+j:Nat -> Nat
def same source · line 250 · raw
@+plan:Plan -> Plan
def take source · line 253 · raw
@index:Array<U32> -> @pair:Pair(Array<F32>, F32) -> Pair(F32, Pair(Array<F32>, Array<U32>))
def looked source · line 257 · raw
@+plan:Plan -> @+at:Nat -> @a:Array<F32> -> @pair:Pair(Array<U32>, U32) -> Pair(F32, Pair(Array<F32>, Array<U32>))
def read source · line 261 · raw
@+plan:Plan -> @+at:Nat -> @state:Pair(Array<F32>, Array<U32>) -> Pair(F32, Pair(Array<F32>, Array<U32>))
def cell source · line 266 · raw
@+plan:Plan -> @+at:Nat -> @state:Pair(Array<F32>, Array<U32>) -> @inside:Bool -> Pair(F32, Pair(Array<F32>, Array<U32>))
A cell past the count is +0.0 padding.
def halves source · line 273 · raw
@a:Pair(Array<F32>, Array<F32>) -> @b:Pair(Array<U32>, Array<U32>) -> Pair(Pair(Array<F32>, Array<U32>), Pair(Array<F32>, Array<U32>))
def split source · line 280 · raw
@state:Pair(Array<F32>, Array<U32>) -> Pair(Pair(Array<F32>, Array<U32>), Pair(Array<F32>, Array<U32>))
Each half of a fork gets its own copy of both arrays.
def build source · line 284 · raw
@+depth:Nat -> @+splits:Nat -> @+plan:Plan -> @+count:Nat -> @+base:Nat -> @a:Array<F32> -> @index:Array<U32> -> Array<F32>
def balanced source · line 287 · raw
@+depth:Nat -> @+splits:Nat -> @+plan:Plan -> @+count:Nat -> @+base:Nat -> @a:Array<F32> -> @index:Array<U32> -> 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/model.Balanced(F32, depth, build(depth, splits, plan, count, base, a, index))
def accumulate source · line 293 · raw
@acc:F32 -> @index:Array<U32> -> @pair:Pair(Array<F32>, F32) -> Pair(F32, Pair(Array<F32>, Array<U32>))
def summed source · line 297 · raw
@hit:Bool -> @+at:Nat -> @acc:F32 -> @g:Array<F32> -> @index:Array<U32> -> Pair(F32, Pair(Array<F32>, Array<U32>))
def compared source · line 304 · raw
@+i:Nat -> @+at:Nat -> @acc:F32 -> @g:Array<F32> -> @pair:Pair(Array<U32>, U32) -> Pair(F32, Pair(Array<F32>, Array<U32>))
def stepped source · line 309 · raw
@+i:Nat -> @+at:Nat -> @+slot:Nat -> @acc:F32 -> @state:Pair(Array<F32>, Array<U32>) -> Pair(F32, Pair(Array<F32>, Array<U32>))
One gradient cell (o, k, r): its word is added when its entry is i.
def cell_at source · line 316 · raw
@+plan:Plan -> @+o:Nat -> @+k:Nat -> @+r:Nat -> Nat
The gradient cells (o, k, r), (o, k + 1, r), ... for left more k, added
to acc in increasing k.
Gradient cell (o, k, r) and the index slot it reads.
def slot_at source · line 320 · raw
@+plan:Plan -> @+o:Nat -> @+k:Nat -> @+r:Nat -> Nat
def stepped_pair source · line 324 · raw
@+i:Nat -> @+at:Nat -> @+slot:Nat -> @pair:Pair(F32, Pair(Array<F32>, Array<U32>)) -> Pair(F32, Pair(Array<F32>, Array<U32>))
def added source · line 328 · raw
@+plan:Plan -> @+o:Nat -> @+i:Nat -> @+r:Nat -> @left:Nat -> @+k:Nat -> @pair:Pair(F32, Pair(Array<F32>, Array<U32>)) -> Pair(F32, Pair(Array<F32>, Array<U32>))
def scattered source · line 337 · raw
@+plan:Plan -> @+at:Nat -> @state:Pair(Array<F32>, Array<U32>) -> Pair(F32, Pair(Array<F32>, Array<U32>))
Input cell at is (o, i, r) of [outer, extent, inner].
def scatter_cell source · line 342 · raw
@+plan:Plan -> @+at:Nat -> @state:Pair(Array<F32>, Array<U32>) -> @inside:Bool -> Pair(F32, Pair(Array<F32>, Array<U32>))
def scatter_build source · line 349 · raw
@+depth:Nat -> @+splits:Nat -> @+plan:Plan -> @+count:Nat -> @+base:Nat -> @g:Array<F32> -> @index:Array<U32> -> Array<F32>
def scatter_balanced source · line 352 · raw
@+depth:Nat -> @+splits:Nat -> @+plan:Plan -> @+count:Nat -> @+base:Nat -> @g:Array<F32> -> @index:Array<U32> -> 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/model.Balanced(F32, depth, scatter_build(depth, splits, plan, count, base, g, index))
def built_array source · line 362 · raw
@kind:Pass -> @+depth:Nat -> @+plan:Plan -> @+count:Nat -> @a:Array<F32> -> @index:Array<U32> -> Array<F32>
def built_balanced source · line 369 · raw
@kind:Pass -> @+depth:Nat -> @+plan:Plan -> @+count:Nat -> @a:Array<F32> -> @index:Array<U32> -> 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/model.Balanced(F32, depth, built_array(kind, depth, plan, count, a, index))
def built_cloned source · line 377 · raw
@+kind:Pass -> @+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> @+entries:List<&2, U32> -> @+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(output)) : Nat} -> @+d:Nat -> @+n:Nat -> @-array:Array<F32> -> @pair:Pair(Array<F32>, Array<F32>) -> @-same:{pair == Array.clone(F32, array) : Pair(Array<F32>, Array<F32>)} -> @-balanced:0x1689e5deb278c16abd3bd45eac101ef3/src/storage/model.Balanced(F32, d, array) -> @-ok:{0x1689e5deb278c16abd3bd45eac101ef3/src/storage/main.bounds(d, n) == True{} : Bool} -> @-fits:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/lifecycle.logical_length(0x1689e5deb278c16abd3bd45eac101ef3/src/storage/main.Owned{d, n, array, balanced, ok}) == 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.logical_numel(0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.dimensions(input)) : Nat} -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def built_empty source · line 391 · raw
@+kind:Pass -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> @+entries:List<&2, U32> -> @+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(output)) : Nat} -> Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>An empty source: the forward copy never has cells then, and the backward of an empty gradient reads nothing.
def built_owned source · line 402 · raw
@+kind:Pass -> @+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> @+entries:List<&2, U32> -> @+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(output)) : Nat} -> @source:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<input> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def built source · line 415 · raw
@+kind:Pass -> @+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> @+entries:List<&2, U32> -> @source:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<input> -> @allocated:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<output>> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
Every output cell at once, into the storage that Core.new allocates. An
output with no cell keeps that storage, which is empty.
def copied source · line 428 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @+entries:List<&2, U32> -> @planned:Result<&2, &2, Error, Planned> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def index_select source · line 438 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @dim:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/signed.Position -> @+entries:List<&2, U32> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
PyTorch's input.index_select(dim, index). The input owner always comes back unchanged next to the result.
def gather source · line 443 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @dim:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/signed.Position -> @+index:Index -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
PyTorch's input.gather(dim, index).
def scatter source · line 451 · raw
@g:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> @+entries:List<&2, U32> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
The backward of a planned index_select or gather: the gradient g of its output, added into +0.0 words of the input's shape at the entries, PyTorch's index_add and scatter_add. g comes back unchanged next to the result.
def error_code source · line 455 · raw
@error:Error -> U32