src/tensor/slice.bend checks
raw source on the hub · import bendtorch-core@0.1.0.0/src/tensor/slice.bend as Slice
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 ./transforms.bend as Transforms import ./parallel.bend as Parallel import ./elementwise.bend as Elementwise
Types
type Error source · line 41 · raw
Data
TensorError@error:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error -> Error
LayoutError@error:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/layout.Error -> Error
StepNotPositiveError
OutOfRangeError
type Bounds source · line 49 · raw
Data
A slice's start and stop after PyTorch's clamp to an axis of extent n: start' = min(start, n), stop' = max(start', min(stop, n)).
Bounds@start:Nat -> @stop:Nat -> Bounds
type View source · line 74 · raw
Data
The row-major view of the input around the axis: the element count before it, its extent and the element count after it.
View@outer:Nat -> @extent:Nat -> @inner:Nat -> View
type Plan source · line 144 · raw
Data
What every cell of a slice and of its backward reads: the view, the clamped start, the step and the length of the sliced axis.
Plan@outer:Nat -> @extent:Nat -> @inner:Nat -> @start:Nat -> @step:Nat -> @length:Nat -> Plan
type Planned source · line 177 · raw
Data
A slice the checks accept: its plan and its output shape.
Planned@plan:Plan -> @output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> Planned
type Pass source · line 298 · raw
Data
Which build fills the output: the forward copy or the backward scatter.
ForwardPass
BackwardPass
Definitions
def bounds source · line 52 · raw
@+extent:Nat -> @+start:Nat -> @+stop:Nat -> Bounds
def counted source · line 59 · raw
@+span:Nat -> @+step:Nat -> Nat
How many of 0, step, 2 step, ... lie below span: none for an empty span,
else 1 + (span - 1) / step, a ceiling division that never forms a number
past span. It takes one division, not a step per index.
def length source · line 68 · raw
@+clamped:Bounds -> @+step:Nat -> Nat
The length of the sliced axis: the number of indices start', start' + step, ... below stop'.
def view_at source · line 77 · raw
@dimensions:List<&2, U32> -> @+dim:Nat -> @+outer:Nat -> View
def view source · line 86 · raw
@dimensions:List<&2, U32> -> @+dim:Nat -> View
def axis_extent source · line 91 · raw
@dimensions:List<&2, U32> -> @+dim:Nat -> Nat
The extent of axis dim, 0 past the rank. It reads that one dimension and
forms no product.
def extent_at source · line 100 · raw
@dimensions:List<&2, U32> -> @+dim:U32 -> Nat
def holds_zero source · line 104 · raw
@dimensions:List<&2, U32> -> Bool
Whether some dimension is 0, so the input has no element.
def sized source · line 115 · raw
@+dimensions:List<&2, U32> -> @+dim:Nat -> @empty:Bool -> View
The view a plan uses. An input with no element gets no cell in either build, so its outer and inner counts are left at 0 rather than formed: the product of the other extents can pass 2^48 - 1, the largest Nat the runtime holds, as [2^24, 2^24, 0] at axis 2 does.
def resized source · line 123 · raw
@dimensions:List<&2, U32> -> @+dim:Nat -> @+extent:U32 -> List<&2, U32>
The input dimensions with the extent at the axis replaced.
def dropped source · line 133 · raw
@dimensions:List<&2, U32> -> @+dim:Nat -> List<&2, U32>
The input dimensions without the axis, as select returns them.
def planned_view source · line 147 · raw
@+outer:Nat -> @+extent:Nat -> @+inner:Nat -> @+clamped:Bounds -> @+step:Nat -> Plan
def viewed source · line 151 · raw
@+given:View -> @+start:Nat -> @+stop:Nat -> @+step:Nat -> Plan
def plan_of source · line 155 · raw
@+dimensions:List<&2, U32> -> @+dim:U32 -> @+start:U32 -> @+stop:U32 -> @+step:U32 -> Plan
def plan_length source · line 158 · raw
@+plan:Plan -> Nat
def refused source · line 164 · raw
@inside:Bool -> @positive:Bool -> Maybe<&2, Error>
PyTorch's checks, in its order: the axis (a rank-0 input has none), then the step.
def refusal source · line 173 · raw
@dimensions:List<&2, U32> -> @+dim:U32 -> @+step:U32 -> Maybe<&2, Error>
def shaped source · line 180 · raw
@+plan:Plan -> @-dimensions:List<&2, U32> -> @built:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.ConstructResult<dimensions> -> Result<&2, &2, Error, Planned>
def checked source · line 187 · raw
@+dimensions:List<&2, U32> -> @+dim:U32 -> @+start:U32 -> @+stop:U32 -> @+step:U32 -> @refused:Maybe<&2, Error> -> Result<&2, &2, Error, Planned>
def plan source · line 198 · raw
@+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+dim:U32 -> @+start:U32 -> @+stop:U32 -> @+step:U32 -> Result<&2, &2, Error, Planned>
The plan of input[..., start:stop:step, ...] at axis dim, or its refusal.
def source source · line 205 · raw
@+plan:Plan -> @+position:Nat -> Nat
The input index output cell position reads: position is (o * length + k)
* inner + r, and it reads (o * extent + start + k * step) * inner + r.
def same source · line 211 · raw
@+plan:Plan -> Plan
def take source · line 214 · raw
@b:Array<F32> -> @pair:Pair(Array<F32>, F32) -> Pair(F32, Pair(Array<F32>, Array<F32>))
def read source · line 218 · raw
@+plan:Plan -> @+position:Nat -> @state:Pair(Array<F32>, Array<F32>) -> Pair(F32, Pair(Array<F32>, Array<F32>))
def cell source · line 224 · raw
@+plan:Plan -> @+position:Nat -> @state:Pair(Array<F32>, Array<F32>) -> @inside:Bool -> Pair(F32, Pair(Array<F32>, Array<F32>))
The state is the input array and an empty array that fills the second slot of the state pair; a cell past the count is +0.0 padding.
def splits source · line 232 · raw
@+depth:Nat -> Nat
A subtree below the forks holds 2^15 cells, as in the other builds.
def build source · line 235 · raw
@+depth:Nat -> @+splits:Nat -> @+plan:Plan -> @+count:Nat -> @+base:Nat -> @a:Array<F32> -> Array<F32>
def balanced source · line 239 · raw
@+depth:Nat -> @+splits:Nat -> @+plan:Plan -> @+count:Nat -> @+base:Nat -> @a:Array<F32> -> 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/model.Balanced(F32, depth, build(depth, splits, plan, count, base, a))
def picked source · line 250 · raw
@+plan:Plan -> @+row:Nat -> @+within:Nat -> @+i:Nat -> @+k:Nat -> @hit:Bool -> Maybe<&2, Nat>
The gradient index input cell position reads, or None where no output
cell came from it: position is (o * extent + i) * inner + r, and it reads
(o * length + k) * inner + r when i = start + k * step with k below the
length, for k = (i - start) / step.
def hits source · line 258 · raw
@+start:Nat -> @+step:Nat -> @+length:Nat -> @+i:Nat -> @+k:Nat -> Bool
def target source · line 261 · raw
@+plan:Plan -> @+position:Nat -> Maybe<&2, Nat>
def fetched source · line 268 · raw
@+index:Nat -> @state:Pair(Array<F32>, Array<F32>) -> Pair(F32, Pair(Array<F32>, Array<F32>))
def scattered source · line 272 · raw
@state:Pair(Array<F32>, Array<F32>) -> @at:Maybe<&2, Nat> -> Pair(F32, Pair(Array<F32>, Array<F32>))
def scatter_cell source · line 279 · raw
@+plan:Plan -> @+position:Nat -> @state:Pair(Array<F32>, Array<F32>) -> @inside:Bool -> Pair(F32, Pair(Array<F32>, Array<F32>))
def scatter_build source · line 286 · raw
@+depth:Nat -> @+splits:Nat -> @+plan:Plan -> @+count:Nat -> @+base:Nat -> @g:Array<F32> -> Array<F32>
def scatter_balanced source · line 290 · raw
@+depth:Nat -> @+splits:Nat -> @+plan:Plan -> @+count:Nat -> @+base:Nat -> @g:Array<F32> -> 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/model.Balanced(F32, depth, scatter_build(depth, splits, plan, count, base, g))
def built_array source · line 302 · raw
@kind:Pass -> @+depth:Nat -> @+plan:Plan -> @+count:Nat -> @a:Array<F32> -> Array<F32>
def built_balanced source · line 309 · raw
@kind:Pass -> @+depth:Nat -> @+plan:Plan -> @+count:Nat -> @a:Array<F32> -> 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/model.Balanced(F32, depth, built_array(kind, depth, plan, count, a))
def built_cloned source · line 319 · raw
@+kind:Pass -> @+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> @+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>)The build from an owned source: the source comes back as the first half of its clone, and the build reads the second half.
def built_empty source · line 333 · raw
@+kind:Pass -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> @+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, so the build reads an empty array.
def built_owned source · line 343 · raw
@+kind:Pass -> @+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> @+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 356 · raw
@+kind:Pass -> @+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> @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 sliced source · line 369 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @planned:Result<&2, &2, Error, Planned> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def slice source · line 379 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+dim:U32 -> @+start:U32 -> @+stop:U32 -> @+step:U32 -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
PyTorch's input[..., start:stop:step, ...] with the slice at axis dim.
The input owner always comes back unchanged next to the result.
def scatter source · line 386 · raw
@g:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+plan:Plan -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
PyTorch's slice_backward: the gradient g of a planned slice's output, scattered into +0.0 words of the input's shape. g must hold as many words as the slice's output; g comes back unchanged next to the result.
def transformed source · line 390 · raw
@result:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/transforms.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic> -> Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>
def reshaped source · line 399 · raw
@+dimensions:List<&2, U32> -> @pair:Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>) -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def narrow_ranged source · line 408 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @+dim:U32 -> @+start:U32 -> @+length:U32 -> @fits:Bool -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def narrow_checked source · line 416 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @+dim:U32 -> @+start:U32 -> @+length:U32 -> @refused:Maybe<&2, Error> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def narrow source · line 427 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+dim:U32 -> @+start:U32 -> @+length:U32 -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
PyTorch's input.narrow(dim, start, length): the slice start:start + length
at axis dim, refused when start + length passes the extent.
def select_ranged source · line 431 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @+dim:U32 -> @+index:U32 -> @below:Bool -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def select_checked source · line 439 · raw
@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @+dim:U32 -> @+index:U32 -> @refused:Maybe<&2, Error> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
def select source · line 450 · raw
@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @+dim:U32 -> @+index:U32 -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)
PyTorch's input.select(dim, index): the slice index:index + 1 at axis
dim, then a reshape that drops the axis, refused when the index is at or
past the extent.
def error_code source · line 454 · raw
@error:Error -> U32