tensor.bend checks
raw source on the hub · import bendtorch@1.3.0.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.18.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
Tensor@-s:List<&2, Nat> -> @data:Array<F32> -> Tensor<s>
type Ax source · line 124 · raw
Data
one axis of an index map: its size, and the stride of the tensor read along it (0 where it is broadcast)
Ax@n:U32 -> @st:U32 -> Ax
type IMap source · line 127 · raw
Data
Strided@base:U32 -> @axes:List<&2, Ax> -> IMap
base plus, for every axis (the last one first), its coordinate times its stride
Drop@inner:U32 -> @size:U32 -> IMap
the position with one axis taken out: inner is the product of the sizes after it, size its own size
type P2 source · line 167 · raw
Type
P2@a:Array<F32> -> @b:Array<F32> -> P2
type P3 source · line 170 · raw
Type
P3@a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> P3
type T3 source · line 218 · raw
@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> Type
a binary operation, both operands handed back with the result
T3@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> @x:Tensor<a> -> @y:Tensor<b> -> @z:Tensor<o> -> T3<a, b, o>
type TP source · line 248 · raw
@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> Type
a tensor and the gradient that comes back with it
TP@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @x:Tensor<a> -> @y:Tensor<b> -> TP<a, b>
type P4 source · line 356 · raw
Type
P4@a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> @d:Array<F32> -> P4
type G2 source · line 360 · 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
G2@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> @-g:List<&2, Nat> -> @dz:Tensor<o> -> @x:Tensor<a> -> @y:Tensor<b> -> @g:Tensor<g> -> G2<a, b, o, g^3>
type CatG source · line 607 · 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
CatG@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> @dz:Tensor<o> -> @ga:Tensor<a> -> @gb:Tensor<b> -> CatG<a, b, o>
type Geo source · line 630 · 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
Geo@n:U32 -> @m:U32 -> @k:Nat -> @xb:U32 -> @xi:U32 -> @xt:U32 -> @yb:U32 -> @yt:U32 -> @yj:U32 -> Geo
type Dot source · line 633 · raw
Type
Dot@x:Array<F32> -> @y:Array<F32> -> @v:F32 -> Dot
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 86 · 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 90 · 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 95 · 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 102 · raw
@-a:Nat -> @-b:Nat -> @m:Maybe<&1, {a == b : Nat}> -> Maybe<&1, {1n+a == 1n+b : Nat}>
def Nat.check_eq source · line 110 · 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 133 · raw
@+d:Nat -> @+st:U32 -> U32
def axes source · line 137 · 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 145 · raw
@+a:List<&2, Nat> -> @+o:List<&2, Nat> -> IMap
reading a (any rank up to o's) along o
def strided source · line 148 · raw
@axs:List<&2, Ax> -> @+j:U32 -> @acc:U32 -> U32
def imap source · line 156 · raw
@+m:IMap -> @+j:U32 -> U32
the position m(j)
def gather_l source · line 182 · 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 190 · 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 197 · raw
@+n:Nat -> Array<F32>
def zeros source · line 204 · raw
@+s:List<&2, Nat> -> Tensor<s>
def of_mat source · line 207 · raw
@-r:Nat -> @-c:Nat -> @m:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c> -> Tensor<[r, c]>
def to_mat source · line 212 · raw
@-r:Nat -> @-c:Nat -> @t:Tensor<[r, c]> -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>
def t3_of source · line 221 · raw
@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @-o:List<&2, Nat> -> @p:P3 -> T3<a, b, o>
def f32_add source · line 226 · raw
@+a:F32 -> @+b:F32 -> F32
def f32_sub source · line 229 · raw
@+a:F32 -> @+b:F32 -> F32
def f32_mul source · line 232 · raw
@+a:F32 -> @+b:F32 -> F32
def f32_div source · line 235 · raw
@+a:F32 -> @+b:F32 -> F32
def add source · line 244 · 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 251 · raw
@-a:List<&2, Nat> -> @-b:List<&2, Nat> -> @p:P2 -> TP<a, b>
def sum_to source · line 257 · 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 265 · raw
@s:List<&2, Nat> -> @k:Nat -> Nat
the product of the sizes after axis k
def drop_map source · line 274 · raw
@+s:List<&2, Nat> -> @+k:Nat -> IMap
def sum_axis source · line 278 · 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 284 · 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 294 · 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 303 · raw
@+s:List<&2, Nat> -> Nat
def win_axes source · line 308 · 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 315 · raw
@+o:List<&2, Nat> -> @+st:List<&2, Nat> -> @+base:U32 -> IMap
def place_l source · line 319 · 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 334 · raw
@+s:List<&2, Nat> -> @+c:F32 -> @t:Tensor<s> -> Tensor<s>
def n_f32 source · line 339 · raw
@+n:Nat -> F32
def g2_of source · line 363 · 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 375 · 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 378 · raw
@+d:F32 -> @+p:F32 -> @+q:F32 -> F32
def d_mul_a source · line 381 · raw
@+d:F32 -> @+p:F32 -> @+q:F32 -> F32
def d_mul_b source · line 384 · raw
@+d:F32 -> @+p:F32 -> @+q:F32 -> F32
def d_div_a source · line 387 · raw
@+d:F32 -> @+p:F32 -> @+q:F32 -> F32
def d_div_b source · line 391 · raw
@+d:F32 -> @+p:F32 -> @+q:F32 -> F32
-dz * ((x / y) / y), as PyTorch's div backward for the divisor
def sub source · line 394 · 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 397 · 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 400 · 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 408 · 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 413 · raw
@-s:List<&2, Nat> -> @+u:List<&2, Nat> -> @+c:F32 -> @r:TP<s, u> -> TP<s, u>
def mean_axis source · line 419 · 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 423 · 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 427 · raw
@+s:List<&2, Nat> -> IMap
every position to position 0: the whole-tensor reductions
def sum_all source · line 431 · 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 436 · raw
@+s:List<&2, Nat> -> @dy:Tensor<[]> -> TP<[], s>
def mean_all source · line 441 · raw
@+s:List<&2, Nat> -> @x:Tensor<s> -> TP<s, []>
def mean_all_bwd source · line 444 · raw
@+s:List<&2, Nat> -> @dy:Tensor<[]> -> TP<[], s>
def reshape source · line 451 · 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 457 · 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 460 · raw
@+s:List<&2, Nat> -> @x:Tensor<s> -> Tensor<[numel(s)]>
def permuted source · line 468 · 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 475 · raw
@+k:Nat -> @p:List<&2, Nat> -> Nat
def all_once source · line 483 · 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 492 · 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 497 · 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 504 · raw
@+s:List<&2, Nat> -> @+p:List<&2, Nat> -> IMap
def permute source · line 507 · 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 513 · 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 519 · 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 526 · raw
@+s:List<&2, Nat> -> @+i:Nat -> @+j:Nat -> List<&2, Nat>
def narrow_ok source · line 534 · 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 537 · raw
@+s:List<&2, Nat> -> @+d:Nat -> @+st:Nat -> @+l:Nat -> IMap
def narrow source · line 541 · 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 547 · 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 553 · raw
@a:List<&2, Nat> -> @b:List<&2, Nat> -> Bool
the same sizes on every axis
def cat_eq source · line 564 · 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 576 · raw
@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> Bool
def cat_shape source · line 580 · raw
@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> List<&2, Nat>
def cat_map_a source · line 584 · 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 587 · raw
@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+d:Nat -> IMap
def t3_cat source · line 590 · 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 595 · 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 601 · 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 610 · 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 615 · 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 620 · 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 636 · raw
@-R:Type -> @d:Dot -> @k:(@_:Array<F32> -> @_:Array<F32> -> @_:F32 -> R) -> R
def dot_l source · line 642 · 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 649 · raw
@+g:Geo -> @+j:U32 -> U32
def y_off source · line 654 · raw
@+g:Geo -> @+j:U32 -> U32
def geo_k source · line 659 · raw
@+g:Geo -> Nat
def geo_xt source · line 664 · raw
@+g:Geo -> U32
def geo_yt source · line 669 · raw
@+g:Geo -> U32
def bmm_l source · line 675 · 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 683 · raw
@+n:Nat -> U32
def bmm source · line 686 · 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 692 · 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 698 · 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 174 · 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 239 · 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 327 · 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 348 · 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 369 · 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