~/bend-docscommunity

tensor_batch_norm_cells.bend checks

raw source on the hub · import stelliferous@0.0.4.0/tensor_batch_norm_cells.bend as Tensor_batch_norm_cells

Batch normalization on cells [N,H,W,G] whose lanes are the channels, the groups innermost. The statistics read every cell once a sum in the order of Partials and hand the cells back. Every other pass walks the pieces in tasks, and in a piece one loop for each group reads its cells with the parameters of the group.

19 imports
import Base
import ./kernel_batch_norm.bend as Kernel
import ./kernel_tile.bend as Tile
import ./math_sum.bend as Sum
import ./storage_buffer.bend as Storage
import ./tensor.bend as Tensors
import ./tensor_operations.bend as Operations
import ./tensor_partials.bend as Partials
import ./tensor_pieces.bend as Pieces
import ./tensor_tasks.bend as Tasks
import ./tensor_tiled_map.bend as TiledMap
import ./tensor_tiled_reduce.bend as TiledReduce
import ./traversal_array.bend as Traversal
import ./traversal_partition.bend as Partition
import ./proofs/storage_certificate.bend as Certificate
import ./proofs/storage_witness.bend as Witness
import ./proofs/traversal_certificate.bend as TraversalCertificate
import ./proofs/traversal_witness.bend as TraversalWitness
import ./proofs/kernel_batch_norm_certificate.bend as KernelCertificate

Types

type Mapping source · line 37 · raw

@-Group:Data -> Data

The parameters of every group, the number of groups and the padding that zeroes the lanes of the last group past the channels.

Definitions

def Cells source · line 26 · raw

Type

def CellPair source · line 29 · raw

Type

def Pairs source · line 32 · raw

Type

def zero_cells source · line 40 · raw

@+count:U32 -> Cells

def buffers_of source · line 48 · raw

@+input_length:Nat -> @+output_length:Nat -> @result:Pair(Array<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>, Array<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>) -> @certificates:0xa2af805348d03b11eb853850859654f6/proofs/traversal_certificate.Certificates<Array<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8, 0xa2af805348d03b11eb853850859654f6/proofs/storage_certificate.family(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8), result> -> Pair(Cells, Cells)

def padded_output source · line 73 · raw

@+padding:0xa2af805348d03b11eb853850859654f6/tensor_tiled_map.Padding -> @+first:U32 -> @+count:U32 -> @pair:Pair(Cells, Cells) -> Pair(Cells, Cells)

The output of a piece with the lanes past the channels zero.

def padded_both source · line 77 · raw

@+padding:0xa2af805348d03b11eb853850859654f6/tensor_tiled_map.Padding -> @+first:U32 -> @+count:U32 -> @pair:Pair(Cells, Cells) -> Pair(Cells, Cells)

def output_of source · line 91 · raw

@pair:Pair(Cells, Cells) -> Cells

def forward_output source · line 95 · raw

@+groups:List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Normal> -> @+first:U32 -> @+count:U32 -> @+cells:U32 -> @normalized:Pair(Cells, Cells) -> Pair(Cells, Cells)

def forward_leaf source · line 100 · raw

@context:Mapping<0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Normal> -> @first:U32 -> @count:U32 -> @input:Cells -> @resource:Unit -> 0xa2af805348d03b11eb853850859654f6/traversal_partition.Tree<&1, Pair(Cells, Cells), Unit>

A piece normalized and its output; the piece itself is dropped.

def leaves_buffers source · line 110 · raw

@+left_length:Nat -> @+right_length:Nat -> @leaves:0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Leaves -> @certificates:0xa2af805348d03b11eb853850859654f6/proofs/kernel_batch_norm_certificate.LeavesCertificates<leaves> -> Pair(Cells, Cells)

def back_buffers source · line 116 · raw

@context:0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Grouped<0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Back> -> @xhat:Cells -> @dy:Cells -> Pair(Cells, Cells)

dy of a group in place, xhat kept.

def back_pair source · line 122 · raw

@+groups:List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Back> -> @+cells:U32 -> @+padding:0xa2af805348d03b11eb853850859654f6/tensor_tiled_map.Padding -> @+first:U32 -> @+count:U32 -> @pair:CellPair -> 0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8)

def back_leaf source · line 129 · raw

@context:Mapping<0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Back> -> @first:U32 -> @count:U32 -> @pair:CellPair -> @resource:Unit -> 0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8)

dy of a piece in place, xhat dropped.

def forward source · line 148 · raw

@groups:List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Normal> -> @+cells:U32 -> @+padding:0xa2af805348d03b11eb853850859654f6/tensor_tiled_map.Padding -> @+shape:List<&2, U32> -> @+workers:U32 -> @pieces:0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8) -> Pair(0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8), 0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8))

xhat and the output of the input, which is dropped. A cell is read twice and written twice, with six operations a lane.

def outputs source · line 152 · raw

@pair:Pair(0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8), 0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8)) -> 0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8)

def evaluated source · line 158 · raw

@groups:List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Normal> -> @+cells:U32 -> @+padding:0xa2af805348d03b11eb853850859654f6/tensor_tiled_map.Padding -> @+shape:List<&2, U32> -> @+workers:U32 -> @pieces:0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8) -> 0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8)

The output of the input, which is dropped. A cell is read once and written once, with four operations a lane.

def backward source · line 164 · raw

@groups:List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Back> -> @+cells:U32 -> @+padding:0xa2af805348d03b11eb853850859654f6/tensor_tiled_map.Padding -> @+shape:List<&2, U32> -> @+workers:U32 -> @pairs:Pairs -> 0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8)

dy in place with xhat dropped. A cell reads both and writes one, with five operations a lane.

def sum_finished source · line 169 · raw

@parts:0xa2af805348d03b11eb853850859654f6/math_sum.TileParts -> 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8

def sums_of source · line 172 · raw

@result:Pair(0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8), List<&2, 0xa2af805348d03b11eb853850859654f6/math_sum.TileParts>) -> Pair(0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8), List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>)

def summed_flags source · line 178 · raw

List<&2, Bool>

Every axis of [N,H,W,G] but the groups is summed. A row is the H x W plane of an image, and the rows of the images merge in the tree.

def sums source · line 183 · raw

@+shape:List<&2, U32> -> @+workers:U32 -> @pieces:0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8) -> Pair(0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8), List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>)

The sums of every group in the order of sum_axes, and the cells. A cell adds to the slot of its place in the row.

def centered_sums source · line 190 · raw

@means:List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8> -> @+shape:List<&2, U32> -> @+workers:U32 -> @pieces:0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8) -> Pair(0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8), List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>)

The sums of (x - mean) * (x - mean) of every group with its mean, in the order of sum_axes, and the cells.

def both_of source · line 194 · raw

@pair:Pair(Cells, Cells) -> CellPair

def product_slotted source · line 198 · raw

@+left_length:Nat -> @+right_length:Nat -> @+slots_length:Nat -> @+phase:U32 -> @result:Pair(0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Leaves, Array<0xa2af805348d03b11eb853850859654f6/math_sum.TileParts>) -> @certificates:0xa2af805348d03b11eb853850859654f6/proofs/traversal_certificate.Certificates<0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Leaves, 0xa2af805348d03b11eb853850859654f6/math_sum.TileParts, 0xa2af805348d03b11eb853850859654f6/proofs/kernel_batch_norm_certificate.leaves_owned, result> -> Pair(CellPair, 0xa2af805348d03b11eb853850859654f6/tensor_operations.TileSlots<Unit>)

def product_buffers source · line 205 · raw

@+count:Nat -> @+position:U32 -> @+step:U32 -> @initial:0xa2af805348d03b11eb853850859654f6/tensor_operations.TileSlots<Unit> -> @left:Cells -> @right:Cells -> Pair(CellPair, 0xa2af805348d03b11eb853850859654f6/tensor_operations.TileSlots<Unit>)

def product_slots source · line 217 · raw

@+count:Nat -> @+position:U32 -> @+step:U32 -> @initial:0xa2af805348d03b11eb853850859654f6/tensor_operations.TileSlots<Unit> -> @pair:CellPair -> Pair(CellPair, 0xa2af805348d03b11eb853850859654f6/tensor_operations.TileSlots<Unit>)

The products of count pairs of cells from position on, step apart, added to the slots of their places in the row, both pieces handed back.

def vacant_pair source · line 222 · raw

@fill:0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8 -> CellPair

def scales_of source · line 226 · raw

@shifts:List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8> -> @result:Pair(Pairs, List<&2, 0xa2af805348d03b11eb853850859654f6/math_sum.TileParts>) -> Pair(Pairs, Pair(List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>, List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>))

def scales_after source · line 230 · raw

@+shape:List<&2, U32> -> @+workers:U32 -> @xhat:0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8) -> @result:Pair(0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8), List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>) -> Pair(Pairs, Pair(List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>, List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>))

def gradient_sums source · line 240 · raw

@+shape:List<&2, U32> -> @+workers:U32 -> @dy:0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8) -> @xhat:0xa2af805348d03b11eb853850859654f6/tensor_pieces.Pieces(0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8) -> Pair(Pairs, Pair(List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>, List<&2, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>))

The sums of dy and of dy * xhat of every group in the order of sum_axes, and the pairs: the sums of dy as the sums of the statistics, then the pairs fold dy * xhat.

Templates

template grouped source · line 44 · raw

@-Group:Data -> @+parameters:Group -> @+first:U32 -> @+count:U32 -> @+cells:U32 -> @+group:U32 -> 0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Grouped<Group>

The loop of group group in the piece of count cells from cell first.

template mapped_buffers source · line 54 · raw

@-Group:Data -> @-read:(@_:Array<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8> -> @_:0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Grouped<Group> -> @_:U32 -> Pair(Array<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8)) -> @-read_preserves:(@-input:Array<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8> -> @context:0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Grouped<Group> -> @index:U32 -> @_:0xa2af805348d03b11eb853850859654f6/proofs/storage_witness.ArrayWitness<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8, input> -> 0xa2af805348d03b11eb853850859654f6/proofs/kernel_batch_norm_certificate.Read(input, read(input, context, index))) -> @context:0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Grouped<Group> -> @input:Cells -> @output:Cells -> Pair(Cells, Cells)

template each_group source · line 64 · raw

@-Group:Data -> @-step:(@_:0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Grouped<Group> -> @_:Cells -> @_:Cells -> Pair(Cells, Cells)) -> @groups:List<&2, Group> -> @+first:U32 -> @+count:U32 -> @+cells:U32 -> @+group:U32 -> @left:Cells -> @right:Cells -> Pair(Cells, Cells)

Every group of a piece from group on, each a step on the two buffers.

template map_leaf source · line 82 · raw

@-Group:Data -> @-read:(@_:Array<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8> -> @_:0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Grouped<Group> -> @_:U32 -> Pair(Array<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8>, 0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8)) -> @-read_preserves:(@-input:Array<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8> -> @context:0xa2af805348d03b11eb853850859654f6/kernel_batch_norm.Grouped<Group> -> @index:U32 -> @_:0xa2af805348d03b11eb853850859654f6/proofs/storage_witness.ArrayWitness<0xa2af805348d03b11eb853850859654f6/kernel_tile.Tile8, input> -> 0xa2af805348d03b11eb853850859654f6/proofs/kernel_batch_norm_certificate.Read(input, read(input, context, index))) -> @context:Mapping<Group> -> @first:U32 -> @count:U32 -> @input:Cells -> @resource:Unit -> 0xa2af805348d03b11eb853850859654f6/traversal_partition.Tree<&1, Pair(Cells, Cells), Unit>

A piece and the cells read from it.

template tasked source · line 135 · raw

@-Block:Type -> @-Output:Type -> @-Context:Data -> @-leaf:(@_:Context -> @_:U32 -> @_:U32 -> @_:Block -> @_:Unit -> 0xa2af805348d03b11eb853850859654f6/traversal_partition.Tree<&1, Output, Unit>) -> @decision:0xa2af805348d03b11eb853850859654f6/tensor_tasks.Decision -> @context:Context -> @tree:0xa2af805348d03b11eb853850859654f6/traversal_partition.Tree<&1, Block, Unit> -> 0xa2af805348d03b11eb853850859654f6/traversal_partition.Tree<&1, Output, Unit>

template pass source · line 142 · raw

@-Block:Type -> @-Output:Type -> @-Context:Data -> @-leaf:(@_:Context -> @_:U32 -> @_:U32 -> @_:Block -> @_:Unit -> 0xa2af805348d03b11eb853850859654f6/traversal_partition.Tree<&1, Output, Unit>) -> @context:Context -> @+shape:List<&2, U32> -> @+workers:U32 -> @+rows:F32 -> @+steps:F32 -> @tree:0xa2af805348d03b11eb853850859654f6/traversal_partition.Tree<&1, Block, Unit> -> 0xa2af805348d03b11eb853850859654f6/traversal_partition.Tree<&1, Output, Unit>

The leaf on every piece of cells of a shape, in tasks. rows and steps count one cell.