~/bend-docscommunity

src/tensor/batched_matmul.bend checks

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

8 imports
import Base
import ../shape/core.bend as Shape
import ../storage/main.bend as Storage
import ../storage/model.bend as Model
import ./core.bend as Core
import ./elementwise.bend as Elementwise
import ./matmul.bend as Matrix
import ./parallel.bend as Parallel

Types

type Batch source · line 40 · raw

Data

The batch extents of an operand, padded on the left with ones to two, and how many it has.

type Operand source · line 46 · raw

Data

An operand split into its batch and its matrix: the outer extent (rows of the left operand, columns of the right one) and the inner extent. A 1-D operand is a vector: an outer extent of 1 that the output drops.

type Plan source · line 123 · raw

Data

Everything a product and its two gradients need: the output shape, the output's batch dimensions, the matrix extents, both operands' padded batches and the output's, and which operands are vectors.

type Layout source · line 198 · raw

Data

What every cell of a build reads: the orders and extents of the product (as in Matrix.Layout), and each operand's padded batch extents with the output's second batch extent, which splits a batch number into its two indices.

Definitions

def left_operand source · line 49 · raw

@dims:List<&2, U32> -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Operand>

def right_operand source · line 62 · raw

@dims:List<&2, U32> -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Operand>

def stretched source · line 77 · raw

@+l:U32 -> @+r:U32 -> @same:Bool -> @left_one:Bool -> @right_one:Bool -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, U32>

One batch axis: equal extents, or an extent of 1 that stretches to the other one.

def axis source · line 88 · raw

@+l:U32 -> @+r:U32 -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, U32>

def larger source · line 91 · raw

@+a:Nat -> @+b:Nat -> Nat

def batch_dims source · line 95 · raw

@+rank:Nat -> @+first:U32 -> @+second:U32 -> List<&2, U32>

The output's batch dimensions: as many as the operand with more of them.

def column_dims source · line 106 · raw

@+columns:U32 -> @right_vector:Bool -> List<&2, U32>

def matrix_dims source · line 113 · raw

@+rows:U32 -> @left_vector:Bool -> @+columns:U32 -> @right_vector:Bool -> List<&2, U32>

def output_of source · line 127 · raw

@+plan:Plan -> 0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape

def planned_shape source · line 131 · raw

@+batch:List<&2, U32> -> @+rows:U32 -> @+inner:U32 -> @+columns:U32 -> @+l1:U32 -> @+l2:U32 -> @+r1:U32 -> @+r2:U32 -> @+o1:U32 -> @+o2:U32 -> @+lv:Bool -> @+rv:Bool -> @-dims:List<&2, U32> -> @result:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.ConstructResult<dims> -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Plan>

def batched source · line 141 · raw

@+rank:Nat -> @+rows:U32 -> @+inner:U32 -> @+columns:U32 -> @+l1:U32 -> @+l2:U32 -> @+r1:U32 -> @+r2:U32 -> @+lv:Bool -> @+rv:Bool -> @first:Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, U32> -> @second:Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, U32> -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Plan>

def shaped source · line 154 · raw

@+left:Operand -> @+right:Operand -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Plan>

def compatible source · line 159 · raw

@+left:Operand -> @+right:Operand -> @same:Bool -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Plan>

def inner_of source · line 166 · raw

@+operand:Operand -> U32

def both_parsed source · line 170 · raw

@+left:Operand -> @result:Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Operand> -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Plan>

def left_parsed source · line 177 · raw

@right:List<&2, U32> -> @result:Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Operand> -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Plan>

def plan_dims source · line 188 · raw

@left:List<&2, U32> -> @right:List<&2, U32> -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Plan>

The plan of left @ right for operands of dimensions left and right, each of rank 1 to 4: the left operand's rank is checked first, then the right one's, the inner extents, the batch dimensions and the output's capacity.

def plan source · line 191 · raw

@+left:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+right:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Plan>

def layout source · line 202 · raw

@+given:Layout -> Layout

def forward_layout source · line 208 · raw

@+plan:Plan -> Layout

left @ right: left [.., rows, inner] read by rows, right [.., inner, columns] read by columns, as matmul reads them.

def left_layout source · line 215 · raw

@+plan:Plan -> Layout

g @ right^T, the left operand's gradient over the output's batch: g [.., rows, columns] by rows, right [.., inner, columns] by its rows, the columns being the terms.

def right_layout source · line 222 · raw

@+plan:Plan -> Layout

left^T @ g, the right operand's gradient over the output's batch: left [.., rows, inner] by its columns, g [.., rows, columns] by columns, the rows being the terms.

def left_dims source · line 229 · raw

@+plan:Plan -> List<&2, U32>

The dimensions of each operand's gradient before it is reduced to the operand's shape: the output's batch, then the operand's own matrix dimensions.

def right_dims source · line 233 · raw

@+plan:Plan -> List<&2, U32>

def stretch source · line 239 · raw

@+index:Nat -> @one:Bool -> Nat

Index index of a batch axis of extent: the index itself, or 0 where the extent is 1 and stretches.

def batch_number source · line 246 · raw

@+first:Nat -> @+second:Nat -> @+b1:Nat -> @+b2:Nat -> Nat

def run_from source · line 252 · raw

@fuel:Nat -> @+ls:Nat -> @+rs:Nat -> @+lp:Nat -> @+rp:Nat -> @la:Array<F32> -> @ra:Array<F32> -> Pair(F32, Pair(Array<F32>, Array<F32>))

The fuel terms of a cell from offsets lp and rp, from +0.0, each next term ls and rs further on: Matrix.product_run after the first reads.

def left_start source · line 263 · raw

@+layout:Layout -> @+position:Nat -> Nat

The first term's offsets of cell position: batch b = position / (rows * columns) splits into (b / o2, b % o2), and each operand reads its own batch, a stretched axis at 0, then the cell's row and column at term 0.

def right_start source · line 269 · raw

@+layout:Layout -> @+position:Nat -> Nat

def dot_at source · line 276 · raw

@+layout:Layout -> @+position:Nat -> @la:Array<F32> -> @ra:Array<F32> -> Pair(F32, Pair(Array<F32>, Array<F32>))

Cell position: all inner terms, from +0.0.

def cell_at source · line 282 · raw

@+layout:Layout -> @+position:Nat -> @inputs:Pair(Array<F32>, Array<F32>) -> @inside:Bool -> Pair(F32, Pair(Array<F32>, Array<F32>))

The cell function Parallel.build runs: a cell at or past the count is +0.0 padding.

def build source · line 290 · raw

@+depth:Nat -> @+splits:Nat -> @+given:Layout -> @+count:Nat -> @+base:Nat -> @la:Array<F32> -> @ra:Array<F32> -> Array<F32>

def build_balanced source · line 293 · raw

@+depth:Nat -> @+splits:Nat -> @+given:Layout -> @+count:Nat -> @+base:Nat -> @la:Array<F32> -> @ra:Array<F32> -> 0x1689e5deb278c16abd3bd45eac101ef3/src/storage/model.Balanced(F32, depth, build(depth, splits, given, count, base, la, ra))

def terms_of source · line 297 · raw

@+given:Layout -> Nat

def filled source · line 304 · raw

@+given:Layout -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+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} -> @left:Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Array<F32>) -> @right:Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Array<F32>) -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

Both inputs come back to the caller as the kept halves of their clones, and the build reads the other halves. It forks by work as Matrix.splits says.

def allocated source · line 316 · raw

@+given:Layout -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @left:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @right:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @result:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<output>> -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

def product source · line 330 · raw

@+given:Layout -> @+output:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @left:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @right:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

The product of the storages of left and right read as given says, into a new tensor of shape output.

def tensor_failed source · line 334 · raw

@result:Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))> -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

def planned source · line 342 · raw

@+left:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+right:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @a:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<left> -> @b:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<right> -> @result:Result<&2, &2, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Plan> -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

def matrices source · line 350 · raw

@dims:List<&2, U32> -> @other:List<&2, U32> -> Bool

def both_matrices source · line 353 · raw

@+left:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+right:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> Bool

def dispatched source · line 356 · raw

@+left:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+right:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @a:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<left> -> @b:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<right> -> @+dims:List<&2, U32> -> @+other:List<&2, U32> -> @both:Bool -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

def routed source · line 366 · raw

@+left:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @+right:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.Shape -> @a:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<left> -> @b:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Tensor<right> -> @+dims:List<&2, U32> -> @+other:List<&2, U32> -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

The operands' dimensions dims and other decide: two rank-2 operands go to Matrix.matmul, every other pair to the plan.

def matmul source · line 371 · raw

@left:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @right:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/matmul.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

torch.matmul(left, right). Both inputs come back to the caller.

def gradient_shaped source · line 376 · raw

@+given:Layout -> @first:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @second:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @-dims:List<&2, U32> -> @result:0x1689e5deb278c16abd3bd45eac101ef3/src/shape/core.ConstructResult<dims> -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

def left_gradient source · line 387 · raw

@+plan:Plan -> @g:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @right:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

The left operand's gradient over the output's batch, g @ right^T, of dimensions left_dims(plan): g has the output's shape and right the right operand's. A broadcast left operand still has to be summed to its shape.

def right_gradient source · line 393 · raw

@+plan:Plan -> @left:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> @g:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic -> Result<&2, &1, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Error, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, Pair(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/core.Dynamic))>

The right operand's gradient over the output's batch, left^T @ g, of dimensions right_dims(plan).