~/bend-docscommunity

tensor.bend checks

raw source on the hub · import bendtorch@1.1.1.0/tensor.bend as Tensor

BendTorch tensors: Tensor<s>, an n-dimensional F32 array whose shape s (a list of sizes) is in its type.

import ../torch/tensor.bend as TT

A Tensor<s> holds product(s) numbers in one flat Array<F32>, row-major (the last axis varies fastest), as bend-ml's Mat<r, c> does for two axes: Tensor<[r, c]> and Mat<r, c> are the same array under two types, and of_mat/to_mat convert without a copy. Every operation hands back what it reads, as the Mat operations do.

Each operation is one of two kernels over an index map (for each position of one tensor, the position of another): gather out[j] = f(x[m(j)]) (and its two-input form, for broadcasting) scatter-add g[m(j)] = g[m(j)] + x[j] (reductions, and the gradient of a broadcast) An index map is a list of axes, each with its size and the stride of the tensor being read (0 on a broadcast axis), or an axis map (the position with one axis taken out). The shape conditions of an operation are Bool functions of the shapes (bc_ok for broadcasting); a caller passes the proof that the condition is True, which the checker computes for literal shapes and a program checks once where shapes read at run time enter (Bool.check).

3 imports
import Base
import bend-ml-tensor-array@0.1.14.0/main.bend as TA
import bend-ml-nat-lemmas@0.1.2.0/main.bend as NL

Types

type Tensor source · line 25 · raw

@-s:List<&2, Nat> -> Type

type Ax source · line 122 · raw

Data

one axis of an index map: its size, and the stride of the tensor read along it (0 where it is broadcast)

type IMap source · line 125 · raw

Data

type P2 source · line 165 · raw

Type

type P3 source · line 168 · raw

Type

type T3 source · line 216 · raw

@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> Type

a binary operation, both operands handed back with the result

type TP source · line 246 · raw

@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> Type

a tensor and the gradient that comes back with it

type P4 source · line 354 · raw

Type

type G2 source · line 358 · raw

@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> @-g:List<&2, Nat> -> Type

dz, x, y and the gradient, handed back

type CatG source · line 600 · raw

@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> Type

the gradients of cat: dz's window of a, and dz's window of b (dz comes back); returned as TP<a, b> after dz

type Geo source · line 623 · raw

Data

where the operands of out[b, i, j] = sum over t of X[b·xb + i·xi + t·xt] · Y[b·yb + t·yt + j·yj] are

type Dot source · line 626 · raw

Type

Definitions

def numel source · line 29 · raw

@+s:List<&2, Nat> -> Nat

the number of elements of a tensor of shape s

def at1 source · line 33 · raw

@s:List<&2, Nat> -> @k:Nat -> Nat

size of axis k (1 past the last axis, so a missing axis counts as a size-1 one)

def remove source · line 43 · raw

@s:List<&2, Nat> -> @k:Nat -> List<&2, Nat>

s without axis k (s itself past the last axis)

def ones_then source · line 53 · raw

@n:Nat -> @s:List<&2, Nat> -> List<&2, Nat>

n copies of 1, then s: s seen with n more leading axes

def pad source · line 61 · raw

@+s:List<&2, Nat> -> @+r:Nat -> List<&2, Nat>

s aligned to rank r by leading size-1 axes, as broadcasting aligns shapes from the right

def bc_pair source · line 67 · raw

@+x:Nat -> @+y:Nat -> @+z:Nat -> Bool

one axis: z is x, and y is x or 1

def bc_dim source · line 71 · raw

@+x:Nat -> @+y:Nat -> @+z:Nat -> Bool

one axis, either way round

def bc_eq source · line 75 · raw

@a:List<&2, Nat> -> @b:List<&2, Nat> -> @o:List<&2, Nat> -> Bool

three shapes of one rank, axis by axis

def bc_rank source · line 85 · raw

@+la:Nat -> @+lb:Nat -> @+lo:Nat -> Bool

the ranks: o's is the larger of a's and b's

def bc_ok source · line 89 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+o:List<&2, Nat> -> Bool

a and b broadcast to o

def Bool.check source · line 93 · raw

@b:Bool -> Maybe<&1, {True{} == b : Bool}>

a proof that b is True, when it is: where shapes read at run time enter, a program checks a condition once

def nat_eq_succ source · line 100 · raw

@-a:Nat -> @-b:Nat -> @m:Maybe<&1, {a == b : Nat}> -> Maybe<&1, {1n+a == 1n+b : Nat}>

def Nat.check_eq source · line 108 · raw

@a:Nat -> @b:Nat -> Maybe<&1, {a == b : Nat}>

a proof that a == b, when it holds (reshape's count, read at run time)

def stride_of source · line 131 · raw

@+d:Nat -> @+st:U32 -> U32

def axes source · line 135 · raw

@ra:List<&2, Nat> -> @ro:List<&2, Nat> -> @+st:U32 -> @acc:List<&2, Ax> -> List<&2, Ax>

the axes of reading a tensor of shape a (aligned to o, same rank) at every position of o; the last axis first

def bmap source · line 143 · raw

@+a:List<&2, Nat> -> @+o:List<&2, Nat> -> IMap

reading a (any rank up to o's) along o

def strided source · line 146 · raw

@axs:List<&2, Ax> -> @+j:U32 -> @acc:U32 -> U32

def imap source · line 154 · raw

@+m:IMap -> @+j:U32 -> U32

the position m(j)

def gather_l source · line 180 · raw

@k:Nat -> @+j:U32 -> @+m:IMap -> @rx:Pair(Array<F32>, F32) -> @o:Array<F32> -> P2

o[j] = x[m(j)] for k positions from j

def scatter_l source · line 188 · raw

@k:Nat -> @+j:U32 -> @+m:IMap -> @rx:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> P2

g[m(j)] = g[m(j)] + x[j] for k positions from j; rx holds x[j], rg holds g[m(j)]

def new source · line 195 · raw

@+n:Nat -> Array<F32>

def zeros source · line 202 · raw

@+s:List<&2, Nat> -> Tensor<s>

def of_mat source · line 205 · raw

@-r:Nat -> @-c:Nat -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> Tensor<[r, c]>

def to_mat source · line 210 · raw

@-r:Nat -> @-c:Nat -> @t:Tensor<[r, c]> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>

def t3_of source · line 219 · raw

@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> @p:P3 -> T3<a, b, o>

def f32_add source · line 224 · raw

@+a:F32 -> @+b:F32 -> F32

def f32_sub source · line 227 · raw

@+a:F32 -> @+b:F32 -> F32

def f32_mul source · line 230 · raw

@+a:F32 -> @+b:F32 -> F32

def f32_div source · line 233 · raw

@+a:F32 -> @+b:F32 -> F32

def add source · line 242 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+o:List<&2, Nat> -> @-pf:{True{} == bc_ok(a, b, o) : Bool} -> @x:Tensor<a> -> @y:Tensor<b> -> T3<a, b, o>

def tp_of source · line 249 · raw

@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @p:P2 -> TP<a, b>

def sum_to source · line 255 · raw

@+a:List<&2, Nat> -> @+o:List<&2, Nat> -> @dz:Tensor<o> -> @g:Tensor<a> -> TP<o, a>

g + (dz summed down to a's shape): the gradient of an operand that was broadcast from a to o; dz comes back

def inner source · line 263 · raw

@s:List<&2, Nat> -> @k:Nat -> Nat

the product of the sizes after axis k

def drop_map source · line 272 · raw

@+s:List<&2, Nat> -> @+k:Nat -> IMap

def sum_axis source · line 276 · raw

@+s:List<&2, Nat> -> @+k:Nat -> @x:Tensor<s> -> TP<s, remove(s, k)>

x summed over axis k: shape remove(s, k); x comes back

def sum_axis_bwd source · line 282 · raw

@+s:List<&2, Nat> -> @+k:Nat -> @dy:Tensor<remove(s, k)> -> TP<remove(s, k), s>

the gradient of sum_axis: dy (shape remove(s, k)) copied along axis k; dy comes back

def setd source · line 292 · raw

@s:List<&2, Nat> -> @k:Nat -> @+v:Nat -> List<&2, Nat>

s with the size of axis k replaced by v (s itself past the last axis)

def rank source · line 301 · raw

@+s:List<&2, Nat> -> Nat

def win_axes source · line 306 · raw

@n:Nat -> @+o:List<&2, Nat> -> @+st:List<&2, Nat> -> @+e:Nat -> @acc:List<&2, Ax> -> List<&2, Ax>

the index map of reading a tensor of strides st (given by its shape st) at every position of a tensor of shape o, from a base: axis e has o's size and st's stride (inner(st, e)); the last axis first

def window source · line 313 · raw

@+o:List<&2, Nat> -> @+st:List<&2, Nat> -> @+base:U32 -> IMap

def place_l source · line 317 · raw

@k:Nat -> @+j:U32 -> @+m:IMap -> @rx:Pair(Array<F32>, F32) -> @g:Array<F32> -> P2

g[m(j)] = x[j] for k positions from j (a copy into a window of g; no addition, so -0 stays -0)

def ew_div source · line 332 · raw

@+s:List<&2, Nat> -> @+c:F32 -> @t:Tensor<s> -> Tensor<s>

def n_f32 source · line 337 · raw

@+n:Nat -> F32

def g2_of source · line 361 · raw

@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> @-g:List<&2, Nat> -> @p:P4 -> G2<a, b, o, g>

def d_id source · line 373 · raw

@+d:F32 -> @+p:F32 -> @+q:F32 -> F32

the partial derivatives, dz times d op / d operand (PyTorch's formulas and order of operations)

def d_neg source · line 376 · raw

@+d:F32 -> @+p:F32 -> @+q:F32 -> F32

def d_mul_a source · line 379 · raw

@+d:F32 -> @+p:F32 -> @+q:F32 -> F32

def d_mul_b source · line 382 · raw

@+d:F32 -> @+p:F32 -> @+q:F32 -> F32

def d_div_a source · line 385 · raw

@+d:F32 -> @+p:F32 -> @+q:F32 -> F32

def d_div_b source · line 389 · raw

@+d:F32 -> @+p:F32 -> @+q:F32 -> F32

-dz * ((x / y) / y), as PyTorch's div backward for the divisor

def sub source · line 392 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+o:List<&2, Nat> -> @-pf:{True{} == bc_ok(a, b, o) : Bool} -> @x:Tensor<a> -> @y:Tensor<b> -> T3<a, b, o>

def mul source · line 395 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+o:List<&2, Nat> -> @-pf:{True{} == bc_ok(a, b, o) : Bool} -> @x:Tensor<a> -> @y:Tensor<b> -> T3<a, b, o>

def div source · line 398 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+o:List<&2, Nat> -> @-pf:{True{} == bc_ok(a, b, o) : Bool} -> @x:Tensor<a> -> @y:Tensor<b> -> T3<a, b, o>

def sum_axis_keep source · line 406 · raw

@+s:List<&2, Nat> -> @+k:Nat -> @x:Tensor<s> -> TP<s, setd(s, k, 1n)>

the sum over axis k with the axis kept (size 1): the same numbers as sum_axis, in the same layout

def tp_div source · line 411 · raw

@-s:List<&2, Nat> -> @+u:List<&2, Nat> -> @+c:F32 -> @r:TP<s, u> -> TP<s, u>

def mean_axis source · line 417 · raw

@+s:List<&2, Nat> -> @+k:Nat -> @x:Tensor<s> -> TP<s, remove(s, k)>

the mean over axis k: the sum divided by the axis' size

def mean_axis_bwd source · line 421 · raw

@+s:List<&2, Nat> -> @+k:Nat -> @dy:Tensor<remove(s, k)> -> TP<remove(s, k), s>

the gradient of mean_axis: dy divided by the axis' size, copied along it

def all_map source · line 425 · raw

@+s:List<&2, Nat> -> IMap

every position to position 0: the whole-tensor reductions

def sum_all source · line 429 · raw

@+s:List<&2, Nat> -> @x:Tensor<s> -> TP<s, []>

the sum of every element: a tensor of shape [] (one number)

def sum_all_bwd source · line 434 · raw

@+s:List<&2, Nat> -> @dy:Tensor<[]> -> TP<[], s>

def mean_all source · line 439 · raw

@+s:List<&2, Nat> -> @x:Tensor<s> -> TP<s, []>

def mean_all_bwd source · line 442 · raw

@+s:List<&2, Nat> -> @dy:Tensor<[]> -> TP<[], s>

def reshape source · line 449 · raw

@+s:List<&2, Nat> -> @+t:List<&2, Nat> -> @-e:{numel(s) == numel(t) : Nat} -> @x:Tensor<s> -> Tensor<t>

def flat_numel source · line 455 · raw

@+s:List<&2, Nat> -> {numel(s) == numel([numel(s)]) : Nat}

n * 1 == n, so a shape and its flattening hold the same count

def flatten source · line 458 · raw

@+s:List<&2, Nat> -> @x:Tensor<s> -> Tensor<[numel(s)]>

def permuted source · line 466 · raw

@+s:List<&2, Nat> -> @p:List<&2, Nat> -> List<&2, Nat>

the shape with axes reordered by p: axis d of the result is axis p[d] of s

def count source · line 473 · raw

@+k:Nat -> @p:List<&2, Nat> -> Nat

def all_once source · line 481 · raw

@left:Nat -> @+p:List<&2, Nat> -> @+i:Nat -> Bool

every axis 0..n-1 exactly once, from axis i

def is_perm source · line 489 · raw

@+p:List<&2, Nat> -> @+n:Nat -> Bool

p is a permutation of the axes of a rank-n tensor

def perm_axes source · line 493 · raw

@p:List<&2, Nat> -> @+s:List<&2, Nat> -> @acc:List<&2, Ax> -> List<&2, Ax>

reading s at every position of the permuted shape: axis d has s's size and s's stride of axis p[d]; the last first

def perm_map source · line 500 · raw

@+s:List<&2, Nat> -> @+p:List<&2, Nat> -> IMap

def permute source · line 503 · raw

@+s:List<&2, Nat> -> @+p:List<&2, Nat> -> @-pf:{True{} == is_perm(p, rank(s)) : Bool} -> @x:Tensor<s> -> TP<s, permuted(s, p)>

def permute_bwd source · line 509 · raw

@+s:List<&2, Nat> -> @+p:List<&2, Nat> -> @dy:Tensor<permuted(s, p)> -> TP<permuted(s, p), s>

the gradient of permute: dy put back where each number came from

def swap_perm source · line 515 · raw

@n:Nat -> @+i:Nat -> @+a:Nat -> @+b:Nat -> List<&2, Nat>

0..n-1 with i and j swapped

def transpose_p source · line 522 · raw

@+s:List<&2, Nat> -> @+i:Nat -> @+j:Nat -> List<&2, Nat>

def narrow_ok source · line 530 · raw

@+s:List<&2, Nat> -> @+d:Nat -> @+st:Nat -> @+l:Nat -> Bool

axis d exists and start + len fits in it

def narrow_map source · line 533 · raw

@+s:List<&2, Nat> -> @+d:Nat -> @+st:Nat -> @+l:Nat -> IMap

def narrow source · line 537 · raw

@+s:List<&2, Nat> -> @+d:Nat -> @+st:Nat -> @+l:Nat -> @-pf:{True{} == narrow_ok(s, d, st, l) : Bool} -> @x:Tensor<s> -> TP<s, setd(s, d, l)>

positions st .. st + l - 1 of axis d

def narrow_bwd source · line 543 · raw

@+s:List<&2, Nat> -> @+d:Nat -> @+st:Nat -> @+l:Nat -> @dy:Tensor<setd(s, d, l)> -> TP<setd(s, d, l), s>

the gradient of narrow: dy in its window, zeros elsewhere

def same source · line 549 · raw

@a:List<&2, Nat> -> @b:List<&2, Nat> -> Bool

the same sizes on every axis

def cat_eq source · line 559 · raw

@a:List<&2, Nat> -> @b:List<&2, Nat> -> @d:Nat -> Bool

a and b have the same rank, and the same sizes on every axis but d

def cat_ok source · line 570 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> Bool

def cat_shape source · line 573 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> List<&2, Nat>

def cat_map_a source · line 577 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> IMap

where a's and b's positions go in the result

def cat_map_b source · line 580 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> IMap

def t3_cat source · line 583 · raw

@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> @ax:Array<F32> -> @r:P2 -> T3<a, b, o>

def cat_b source · line 588 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> @rb:Array<F32> -> @pa:P2 -> T3<a, b, cat_shape(a, b, d)>

def cat source · line 594 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> @-pf:{True{} == cat_ok(a, b, d) : Bool} -> @x:Tensor<a> -> @y:Tensor<b> -> T3<a, b, cat_shape(a, b, d)>

a then b along axis d

def catg_c source · line 603 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> @ga:Array<F32> -> @r:P2 -> CatG<a, b, cat_shape(a, b, d)>

def catg_b source · line 608 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> @r:P2 -> CatG<a, b, cat_shape(a, b, d)>

def cat_bwd source · line 613 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> @dz:Tensor<cat_shape(a, b, d)> -> CatG<a, b, cat_shape(a, b, d)>

def Dot.then source · line 629 · raw

@-R:Type -> @d:Dot -> @k:(@_:Array<F32> -> @_:Array<F32> -> @_:F32 -> R) -> R

def dot_l source · line 635 · raw

@k:Nat -> @+xo:U32 -> @+xt:U32 -> @+yo:U32 -> @+yt:U32 -> @rx:Pair(Array<F32>, F32) -> @ry:Pair(Array<F32>, F32) -> @acc:F32 -> Dot

acc + the k products from x[xo] and y[yo], stepping xt and yt; rx and ry hold the first pair, already read

def x_off source · line 642 · raw

@+g:Geo -> @+j:U32 -> U32

def y_off source · line 647 · raw

@+g:Geo -> @+j:U32 -> U32

def geo_k source · line 652 · raw

@+g:Geo -> Nat

def geo_xt source · line 657 · raw

@+g:Geo -> U32

def geo_yt source · line 662 · raw

@+g:Geo -> U32

def bmm_l source · line 668 · raw

@cnt:Nat -> @+j:U32 -> @+g:Geo -> @x:Array<F32> -> @y:Array<F32> -> @o:Array<F32> -> P3

out[j] for cnt positions from j

def u source · line 676 · raw

@+n:Nat -> U32

def bmm source · line 679 · raw

@+bs:Nat -> @+n:Nat -> @+k:Nat -> @+m:Nat -> @x:Tensor<[bs, n, k]> -> @y:Tensor<[bs, k, m]> -> T3<[bs, n, k], [bs, k, m], [bs, n, m]>

def bmm_dx source · line 685 · raw

@+bs:Nat -> @+n:Nat -> @+k:Nat -> @+m:Nat -> @dz:Tensor<[bs, n, m]> -> @y:Tensor<[bs, k, m]> -> T3<[bs, n, m], [bs, k, m], [bs, n, k]>

dX = dZ · Yᵀ per batch: [bs, n, m] · [bs, k, m]ᵀ = [bs, n, k]; dZ and Y come back

def bmm_dy source · line 691 · raw

@+bs:Nat -> @+n:Nat -> @+k:Nat -> @+m:Nat -> @x:Tensor<[bs, n, k]> -> @dz:Tensor<[bs, n, m]> -> T3<[bs, n, k], [bs, n, m], [bs, k, m]>

dY = Xᵀ · dZ per batch: [bs, n, k]ᵀ · [bs, n, m] = [bs, k, m]; X and dZ come back

Templates

template gather2_l source · line 172 · raw

@-f:(@+a:F32 -> @+b:F32 -> F32) -> @k:Nat -> @+j:U32 -> @+m:IMap -> @+n:IMap -> @rx:Pair(Array<F32>, F32) -> @ry:Pair(Array<F32>, F32) -> @o:Array<F32> -> P3

o[j] = f(x[m(j)], y[n(j)]) for k positions from j; rx and ry hold x[m(j)] and y[n(j)], already read

template map2 source · line 237 · raw

@-f:(@+a:F32 -> @+b:F32 -> F32) -> @+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+o:List<&2, Nat> -> @-pf:{True{} == bc_ok(a, b, o) : Bool} -> @x:Tensor<a> -> @y:Tensor<b> -> T3<a, b, o>

z = f(x, y) element by element, x and y broadcast to o

template ewc_l source · line 325 · raw

@-f:(@+a:F32 -> @+c:F32 -> F32) -> @k:Nat -> @+j:U32 -> @+c:F32 -> @ra:Pair(Array<F32>, F32) -> Array<F32>

a[j] = f(a[j]) for k positions from j, with a constant c

template grad2_l source · line 346 · raw

@-f:(@+d:F32 -> @+p:F32 -> @+q:F32 -> F32) -> @k:Nat -> @+j:U32 -> @+mg:IMap -> @+ma:IMap -> @+mb:IMap -> @rd:Pair(Array<F32>, F32) -> @rx:Pair(Array<F32>, F32) -> @ry:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> P4

g[mg(j)] = g[mg(j)] + f(dz[j], x[ma(j)], y[mb(j)]) for k positions from j: the gradient of one operand of map2, with both operands at hand (mul and div need them)

template grad2 source · line 367 · raw

@-f:(@+d:F32 -> @+p:F32 -> @+q:F32 -> F32) -> @+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+o:List<&2, Nat> -> @+gs:List<&2, Nat> -> @dz:Tensor<o> -> @x:Tensor<a> -> @y:Tensor<b> -> @g:Tensor<gs> -> G2<a, b, o, gs>

g + the gradient, through f, of the operand of shape gs (a or b) of z = op(x, y): f(dz, x, y) at each position of o