~/bend-docscommunity

src/tensor/signed.bend checks

raw source on the hub · import bendtorch-core@0.1.0.0/src/tensor/signed.bend as Signed

7 imports
import Base
import ../shape/core.bend as Shape
import ./core.bend as Core
import ./layout.bend as Layout
import ./transforms.bend as Transforms
import ./transpose_spec.bend as Transpose
import ./slice.bend as Slice

Types

type Position source · line 13 · raw

Data

PyTorch's signed axes, indices and reshape sizes, parsed into the unsigned ones that the tensor operations check. A Position counts from either end, as a PyTorch int does: First{k} is k, and Last{k} is -1 - k, so Last{0} is -1, the last axis or element. Each integer has exactly one Position.

type Extent source · line 18 · raw

Data

A reshape size: an exact size, or -1, the one size PyTorch infers.

type Error source · line 25 · raw

Data

A signed reshape's refusals: the unsigned reshape's, more than one Inferred size, and an Inferred size that any value would fit, on an empty input whose other sizes multiply to 0.

type Product source · line 119 · raw

Data

The exact product against a bound, the input's element count: ZeroProduct when it is 0, Within{p} when it is p > 0 at most the bound, and Beyond when it is past the bound. capped only multiplies a size and a product that are both at most the bound, and a tensor holds at most 2^20 elements, so no number it forms passes 2^40.

Definitions

def wrap source · line 35 · raw

@+limit:U32 -> @position:Position -> U32

PyTorch's wrap of a position among limit axes or elements (maybe_wrap_dim): a position d in -limit..limit-1 becomes d + limit when d is negative and d otherwise. A position outside that range stays outside it, at or past limit, so the operation refuses it with its own check, code and order: First{k} stays k, and an out-of-range Last{k} becomes limit.

def wrap_all source · line 42 · raw

@+limit:U32 -> @positions:List<&2, Position> -> List<&2, U32>

def wrap_each source · line 52 · raw

@dimensions:List<&2, U32> -> @positions:List<&2, Position> -> List<&2, U32>

Each coordinate wrapped among its own axis's extent. A coordinate past the rank has no extent and is wrapped among 0: the read refuses the coordinate count before any range.

def inside source · line 63 · raw

@+limit:U32 -> @position:Position -> Bool

Whether a position lies in PyTorch's range -limit..limit-1. Last{k} is -1 - k, at least -limit exactly when k < limit.

def all_inside source · line 70 · raw

@+limit:U32 -> @positions:List<&2, Position> -> Bool

def axis_limit source · line 79 · raw

@+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> U32

The axes of the input for transpose and sum: its rank, and 1 for a rank-0 input, whose only axis is 0 (or -1).

def inferred_count source · line 83 · raw

@extents:List<&2, Extent> -> Nat

How many sizes are Inferred.

def exact_product source · line 95 · raw

@extents:List<&2, Extent> -> Nat

The product of the exact sizes, in Nat: what the laws state the inference against. The inference never forms it, since it can pass the native Nat limit of 2^48 on an empty input: [-1, 2^24, 2^24] multiplies to 2^48.

def filled source · line 105 · raw

@extents:List<&2, Extent> -> @+size:U32 -> List<&2, U32>

The sizes, each Inferred one replaced by size.

def bounded source · line 124 · raw

@product:Nat -> @below:Bool -> Product

def cap source · line 131 · raw

@+bound:Nat -> @+product:Nat -> Product

def scaled source · line 134 · raw

@+bound:Nat -> @+size:Nat -> @+product:Nat -> @past:Bool -> Product

def times source · line 143 · raw

@+bound:Nat -> @size:Nat -> @rest:Product -> Product

A nonzero size times the product of the sizes after it. A size past the bound makes any nonzero product pass it, so it is never multiplied.

def capped source · line 156 · raw

@+bound:Nat -> @extents:List<&2, Extent> -> Product

def count_mismatch source · line 165 · raw

Error

def unfilled source · line 170 · raw

@count:Nat -> Result<&2, &2, Error, List<&2, U32>>

An Inferred size whose other sizes multiply to 0: ambiguous on an empty input, a count mismatch on any other.

def divided source · line 177 · raw

@+extents:List<&2, Extent> -> @split:Pair(Nat, Nat) -> Result<&2, &2, Error, List<&2, U32>>

def beyond source · line 187 · raw

@count:Nat -> @+extents:List<&2, Extent> -> Result<&2, &2, Error, List<&2, U32>>

A product past the count: the inferred size is 0 on an empty input, and no size fits any other.

def inferred_one source · line 194 · raw

@+count:Nat -> @+extents:List<&2, Extent> -> @product:Product -> Result<&2, &2, Error, List<&2, U32>>

def inferred_from source · line 203 · raw

@+count:Nat -> @+extents:List<&2, Extent> -> @inferred:Nat -> Result<&2, &2, Error, List<&2, U32>>

def inferred_dimensions source · line 218 · raw

@+count:Nat -> @+extents:List<&2, Extent> -> Result<&2, &2, Error, List<&2, U32>>

PyTorch's infer_size: the sizes of a reshape of count elements. With no Inferred size, the sizes themselves; with one, it is count / product of the others when that divides exactly, the product capped at the count; more than one is refused first.

def transformed source · line 221 · 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 reshape_inferred source · line 228 · raw

@input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<input> -> @inferred:Result<&2, &2, Error, List<&2, U32>> -> Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>

def reshape source · line 237 · raw

@+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<input> -> @+extents:List<&2, Extent> -> Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>

PyTorch's reshape with -1: the sizes inferred, then the unsigned reshape.

def reshape_dynamic source · line 240 · raw

@dynamic:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @extents:List<&2, Extent> -> Result<&2, &1, Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>

def permute_copy source · line 245 · raw

@+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @source:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<input> -> @axes:List<&2, Position> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<input>, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/transforms.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)

PyTorch's permute_copy with signed axes, wrapped among the input's rank.

def permute_dynamic source · line 249 · raw

@dynamic:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @axes:List<&2, Position> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/transforms.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)

def transpose_copy source · line 254 · raw

@+input:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @source:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<input> -> @a:Position -> @b:Position -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<input>, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/transforms.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)

PyTorch's transpose_copy with signed axes, wrapped among axis_limit.

def transpose_dynamic source · line 258 · raw

@dynamic:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @a:Position -> @b:Position -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/transforms.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)

def read_coordinate source · line 264 · raw

@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @coordinates:List<&2, Position> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape>, Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, F32>)

A[coordinates] with signed coordinates, each wrapped among its axis's extent; and the write A[coordinates] = value.

def write_coordinate source · line 268 · raw

@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @coordinates:List<&2, Position> -> @value:F32 -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape>, Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Unit>)

def read_flat source · line 274 · raw

@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @index:Position -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape>, Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, F32>)

The element at a signed flat index, PyTorch's A.flatten()[index], wrapped among the element count; and the write at it.

def write_flat source · line 278 · raw

@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @index:Position -> @+value:F32 -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape>, Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Unit>)

def read_dynamic source · line 282 · raw

@dynamic:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @index:Position -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, F32>)

def write_dynamic source · line 286 · raw

@dynamic:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @index:Position -> @value:F32 -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Unit>)

def before source · line 292 · raw

@+limit:U32 -> @position:Position -> Bool

Whether a position lies before -limit: Last{k}, the position -1 - k, with k at or past limit. No First does.

def bound source · line 303 · raw

@+limit:U32 -> @+position:Position -> U32

PyTorch's slice positions (aten::slice, get_slice_bounds) before its clamp to the axis: a negative position d becomes d + limit, the wrap, and one still below 0 becomes 0. First{k} stays k; Slice.bounds then clamps both ends to the extent.

def extent source · line 307 · raw

@dimensions:List<&2, U32> -> @+axis:U32 -> U32

The extent of axis axis, 0 past the rank.

def slice source · line 313 · raw

@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @dim:Position -> @start:Position -> @stop:Position -> @+step:U32 -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/slice.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)

PyTorch's input[..., start:stop:step, ...] with the slice at a signed axis dim, wrapped among the rank, and signed start and stop: the unsigned slice of the bounds. A rank-0 input has no axis, so every dim is refused.

def select source · line 324 · raw

@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @dim:Position -> @index:Position -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/slice.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)

PyTorch's input.select(dim, index) at a signed axis and a signed index, wrapped among the extent: an index outside -extent..extent-1 stays at or past the extent, so the unsigned select refuses it with OutOfRange after its axis check.

def narrow_before source · line 331 · raw

@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @refused:Maybe<&2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/slice.Error> -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/slice.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)

A narrow's start before -extent: the axis check first, as the unsigned narrow does it, then OutOfRange.

def narrow_started source · line 338 · raw

@+shape:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @tensor:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<shape> -> @+axis:U32 -> @+start:U32 -> @+length:U32 -> @early:Bool -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/slice.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)

def narrow source · line 352 · raw

@input:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @dim:Position -> @+start:Position -> @+length:U32 -> Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/slice.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic>)

PyTorch's input.narrow(dim, start, length) at a signed axis with a signed start. PyTorch takes a start in -extent..extent: Last{k} with k below the extent is extent - 1 - k, and one before -extent is OutOfRange. A First start past the extent is refused by the unsigned narrow, which checks start + length against the extent, with the same code. A length is a U32, so PyTorch's negative lengths cannot be written.

def error_code source · line 359 · raw

@error:Error -> U32