~/bend-docscommunity

kernel_input_gradient.bend checks

raw source on the hub · import stelliferous@0.0.3.0/kernel_input_gradient.bend as Kernel_input_gradient

The input gradient of a convolution by phases: output positions congruent modulo the stride read the same taps. Every row of every output channel of the output gradient is cut into eight-lane tiles once for each column shift the phases read, zero outside; a phase entry is then a tile index, and a job stores at the final strided addresses.

20 imports
import Base
import ./storage_buffer.bend as Storage
import ./storage_allocation.bend as Allocation
import ./kernel_tile.bend as Tile
import ./kernel_rows.bend as Rows
import ./kernel_strided_store.bend as Stores
import ./proofs/kernel_strided_store_certificate.bend as StoreCertificate
import ./kernel_gemm_buffer.bend as Gemm
import ./kernel_spatial_packing.bend as Spatial
import ./kernel_window.bend as Window
import ./kernel_pipeline.bend as Pipeline
import ./traversal_loop.bend as Iteration
import ./traversal_array.bend as Traversal
import ./traversal_partition.bend as Partition
import ./tensor_band.bend as Bands
import ./tensor.bend as Tensors
import ./kernel_cost.bend as Cost
import ./default_kernel_cost.bend as DefaultCost
import ./kernel_planning.bend as KernelPlanning
import ./kernel_band_planning.bend as BandPlanning

Types

type Geometry source · line 27 · raw

Data

type Axis source · line 30 · raw

Data

type Phase source · line 39 · raw

Data

type WeightWindow source · line 53 · raw

Data

Tap coordinates and source strides for mapped weight reorder.

type Tiling source · line 92 · raw

Data

The tiling cuts every row of every output channel of the output gradient into eight-lane tiles, once for each column shift the phases read, zero outside. It is built in chunks of at most chunk_rows output rows and chunk_groups groups, about one pipeline block of tiles; one output row of one group is the smallest chunk, and fits bounds it by the capacity. A chunk holds tile rows * chunk_groups blocks of outputs * shifts tiles (output channel outer, shift inner) and one unused tile, so that the rows a step reads do not share cache sets. Tile row t is gradient row t - kernel; shift i starts at gradient column 8 * group + i + column_low - kernel.

type Band source · line 99 · raw

Data

The outputs [first, last) of an axis of the given size. Over the phases that read a tap for one of them: what 0 the lowest tile (lowest output + source + kernel), what 1 one past the highest tile (highest output + source + kernel + taps), what 2 the most outputs of a phase.

type Fill source · line 147 · raw

Type

type Source source · line 171 · raw

Type

Rows [first, first + rows) of every output channel of one image.

type Filling source · line 176 · raw

Data

Tile rows from tile row low and groups [group, group + span) of the tiling, read from a source.

type Jobs source · line 189 · raw

Type

type Context source · line 192 · raw

Data

type Loop source · line 195 · raw

Type

type Plan source · line 235 · raw

Data

One job is four input channels at eight consecutive outputs of one phase row. The loop index runs over the phase table from offset_base; weight_base is the phase weights less offset_base.

type Table source · line 253 · raw

Data

The tile of entry (output o, tap row a, tap column b) of a phase, relative to the tile of the job row and group, at position base + entry of the table.

type TileCopy source · line 261 · raw

Type

type Packed source · line 277 · raw

Type

Every phase weights and table, side by side in phase order.

type PackContext source · line 303 · raw

Data

type Task source · line 332 · raw

Data

type Leaf source · line 335 · raw

Type

type LeafContext source · line 340 · raw

Data

Output rows [first, first + count) go to target + channel * plane + (row - first) * width + column.

type Chunk source · line 345 · raw

Data

Output rows [row, row + rows) and groups [group, group + chunk_groups) of a leaf, over a chunk of the tiling that starts at tile row low.

type Part source · line 381 · raw

Type

The gradient a leaf read and the output it wrote.

type Sweep source · line 385 · raw

Type

A leaf runs its chunks in turn with the source, the weights and the table.

type Owners source · line 432 · raw

Type

What one part owns: the gradient rows it reads, the weights and the table.

type Pricing source · line 506 · raw

Data

The split depth, priced with the shared cost configuration. A leaf builds the tiling of the gradient rows it reads, runs its jobs and stores its rows. A split copies the left band and clones the weights and the table. A split image is one parallel region with two wake rounds, so the batch does not change the choice.

type Movement source · line 509 · raw

Data

type Choice source · line 512 · raw

Data

type Images source · line 617 · raw

Data

type ImageState source · line 620 · raw

Type

type Outcome source · line 647 · raw

Type

Definitions

def axis source · line 33 · raw

@+first:U32 -> @+size:U32 -> @+kernel:U32 -> @+stride:U32 -> @+padding:U32 -> Axis

def phase_axes source · line 42 · raw

@geometry:Geometry -> @a:Axis -> @b:Axis -> Phase

def phase source · line 48 · raw

@+geometry:Geometry -> @+y:U32 -> @+x:U32 -> Phase

def weight_window source · line 56 · raw

@context:Phase -> WeightWindow

def weight_window_offset source · line 60 · raw

@window:WeightWindow -> @index:U32 -> U32

def weight_offset source · line 66 · raw

@context:Phase -> @index:U32 -> U32

def window_jobs source · line 69 · raw

@geometry:Geometry -> Nat

def phase_of source · line 73 · raw

@geometry:Geometry -> @index:U32 -> Phase

def phase_inner source · line 79 · raw

@context:Phase -> U32

def band_pick source · line 102 · raw

@used:Bool -> @+low:U32 -> @+high:U32 -> @+count:U32 -> @+value:U32 -> @what:U32 -> U32

def band_measure source · line 107 · raw

@+context:Axis -> @+kernel:U32 -> @+stride:U32 -> @+first:U32 -> @+last:U32 -> @+value:U32 -> @what:U32 -> U32

def band_phase source · line 113 · raw

@index:U32 -> @context:Band -> @value:U32 -> U32

def band_fold source · line 117 · raw

@+context:Band -> @value:U32 -> U32

def taps source · line 123 · raw

@+kernel:U32 -> @+stride:U32 -> U32

Output rows [first, first + rows) read at most rows - 1 + taps tile rows, taps = ceil(kernel / stride), and shifts is at most taps + 1.

def fits source · line 126 · raw

@+geometry:Geometry -> @+capacity:U32 -> Bool

def chunked source · line 131 · raw

@+groups:U32 -> @+shifts:U32 -> @+outputs:U32 -> @+column_low:U32 -> @+kernel:U32 -> @+stride:U32 -> @+height:U32 -> @+width:U32 -> @+limit:U32 -> Tiling

def tiling source · line 140 · raw

@geometry:Geometry -> Tiling

Column shifts come from the first output of every column phase; groups from the most outputs of a column phase.

def filled source · line 150 · raw

@tiles:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @+index:U32 -> @row:U32 -> @+column:U32 -> @first:U32 -> @base:U32 -> @plane:U32 -> @height:U32 -> @width:U32 -> @shifts:Nat -> @read:Pair(Array<F32>, 0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8) -> Fill

def fill_shift source · line 154 · raw

@state:Fill -> Fill

def next_channel source · line 158 · raw

@state:Fill -> Fill

def fill_channel source · line 162 · raw

@state:Fill -> Fill

def fill_finished source · line 166 · raw

@state:Fill -> Pair(Array<F32>, Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>)

def fill_group source · line 180 · raw

@index:U32 -> @context:Filling -> @owners:Pair(Array<F32>, Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>) -> Pair(Array<F32>, Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>)

One tile row and group: every output channel, every shift.

def with_tile source · line 198 · raw

@weights:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @offsets:Array<U32> -> @+index:U32 -> @context:Context -> @accumulators:0xd6aaab858a4fbd1461bd0fee27b97184/kernel_rows.Rows<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @samples:0xd6aaab858a4fbd1461bd0fee27b97184/kernel_rows.Rows<F32> -> @read:Pair(Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>, 0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8) -> Loop

def with_offset source · line 203 · raw

@tiles:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @weights:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @+index:U32 -> @context:Context -> @+source:U32 -> @accumulators:0xd6aaab858a4fbd1461bd0fee27b97184/kernel_rows.Rows<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @samples:0xd6aaab858a4fbd1461bd0fee27b97184/kernel_rows.Rows<F32> -> @read:Pair(Array<U32>, U32) -> Loop

def execute source · line 221 · raw

@upper:Bool -> @count:Nat -> @state:Loop -> Loop

def stored source · line 227 · raw

@state:Loop -> @+store:0xd6aaab858a4fbd1461bd0fee27b97184/kernel_strided_store.Store -> @output:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> Jobs

Raw stores within a job, one certificate update after them.

def job source · line 239 · raw

@index:U32 -> @plan:Plan -> @state:Jobs -> Jobs

def offset source · line 256 · raw

@index:U32 -> @context:Table -> @offsets:Array<U32> -> Array<U32>

def tile_copied source · line 264 · raw

@target:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @+from:U32 -> @+to:U32 -> @read:Pair(Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>, 0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8) -> TileCopy

def tile_step source · line 268 · raw

@state:TileCopy -> TileCopy

def tile_target source · line 272 · raw

@state:TileCopy -> Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>

def packed_copied source · line 280 · raw

@+context:Phase -> @+layout:Tiling -> @source:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> @offsets:Array<U32> -> @+weight_base:U32 -> @+offset_base:U32 -> @+count:U32 -> @weights:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> Packed

def packed_phase source · line 286 · raw

@+context:Phase -> @+layout:Tiling -> @weights:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @offsets:Array<U32> -> @+weight_base:U32 -> @+offset_base:U32 -> @read:Pair(0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32>, 0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>) -> Packed

def pack_selected source · line 294 · raw

@empty:Bool -> @+context:Phase -> @+layout:Tiling -> @state:Packed -> Packed

def pack_phase source · line 306 · raw

@index:U32 -> @context:PackContext -> @state:Packed -> Packed

def inner_total source · line 311 · raw

@index:U32 -> @geometry:Geometry -> @total:U32 -> U32

def weight_total source · line 314 · raw

@index:U32 -> @geometry:Geometry -> @total:U32 -> U32

def band_low source · line 319 · raw

@+geometry:Geometry -> @+first:U32 -> @+last:U32 -> U32

The tile rows that output rows [first, last) read.

def band_high source · line 323 · raw

@+geometry:Geometry -> @+first:U32 -> @+last:U32 -> U32

def band_rows source · line 327 · raw

@+geometry:Geometry -> @+first:U32 -> @+count:U32 -> U32

def leaf_jobs source · line 348 · raw

@+context:Phase -> @+chunk:Chunk -> @+weight_base:U32 -> @+offset_base:U32 -> @state:Jobs -> Jobs

def leaf_finished source · line 362 · raw

@+weight_base:U32 -> @+offset_base:U32 -> @state:Jobs -> Leaf

def leaf_selected source · line 366 · raw

@empty:Bool -> @+context:Phase -> @+chunk:Chunk -> @state:Leaf -> Leaf

def leaf_phase source · line 375 · raw

@index:U32 -> @chunk:Chunk -> @state:Leaf -> Leaf

def chunk_leaf source · line 388 · raw

@+base:U32 -> @+plane:U32 -> @+first:U32 -> @+rows:U32 -> @data:Array<F32> -> @state:Leaf -> Sweep

def chunk_finished source · line 392 · raw

@+base:U32 -> @+plane:U32 -> @+first:U32 -> @+rows:U32 -> @pair:Pair(Array<F32>, Leaf) -> Sweep

def chunk_run source · line 396 · raw

@+chunk:Chunk -> @weights:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @offsets:Array<U32> -> @output:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> @owners:Pair(Array<F32>, Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>) -> Pair(Array<F32>, Leaf)

def chunk_step source · line 402 · raw

@index:U32 -> @context:LeafContext -> @state:Sweep -> Sweep

Chunk index: row chunk outer, group chunk inner.

def swept source · line 420 · raw

@state:Sweep -> Part

def leaf_band source · line 425 · raw

@+context:LeafContext -> @source:Source -> @weights:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @offsets:Array<U32> -> @output:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> Part

Output rows [first, first + count) from a source holding the gradient rows they read.

def leaf source · line 436 · raw

@task:Task -> @first:U32 -> @count:U32 -> @owners:Owners -> 0xd6aaab858a4fbd1461bd0fee27b97184/traversal_partition.Tree<&1, Part, Unit>

A leaf writes output rows [first, first + count), stored [C][count][W].

def copied_pair source · line 445 · raw

@+start:U32 -> @+count:U32 -> @+width:U32 -> @+base:U32 -> @+plane:U32 -> @+first:U32 -> @+rows:U32 -> @owners:Pair(Array<F32>, Array<F32>) -> Pair(Source, Source)

def copy_start source · line 450 · raw

@+geometry:Geometry -> @+first:U32 -> @+rows:U32 -> U32

The gradient rows [start, start + count) that output rows [first, first + rows) read.

def copy_count source · line 455 · raw

@+geometry:Geometry -> @+first:U32 -> @+rows:U32 -> U32

def split source · line 464 · raw

@task:Task -> @row:U32 -> @half:U32 -> @source:Source -> Pair(Source, Source)

The left child, output rows [row, row + half), copies the gradient rows it reads; the right child keeps the source of its parent.

def divided source · line 476 · raw

@sources:Pair(Source, Source) -> @weights:Pair(Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>, Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>) -> @offsets:Pair(Array<U32>, Array<U32>) -> 0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Division<Owners>

def divide source · line 483 · raw

@task:Task -> @left:0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Plan -> @right:0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Plan -> @owners:Owners -> 0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Division<Owners>

The left half reads the rows of its plan; each half owns the weights and the table.

def whole source · line 490 · raw

@task:Task -> @layout:0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Plan -> @owners:Owners -> 0xd6aaab858a4fbd1461bd0fee27b97184/traversal_partition.Tree<&1, Part, Unit>

A split always divides the owners; kept whole, they would compute the whole plan as one part.

def placed source · line 497 · raw

@start:U32 -> @first:U32 -> @count:U32 -> @part:Part -> @gradient:Array<F32> -> Pair(0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Bands(F32), Array<F32>)

A leaf output [C][count][W] is the band of its rows of the output, counted over the batch from start. Its gradient replaces the one carried so far, so the gradient of the last leaf, the original, comes back.

def phase_lanes source · line 516 · raw

@context:Phase -> @total:F32 -> F32

Lanes are computed in eight-wide groups of each phase row.

def phase_cost source · line 520 · raw

@index:U32 -> @geometry:Geometry -> @total:F32 -> F32

def leaf_ns source · line 523 · raw

@+context:Pricing -> @+first:U32 -> @+count:U32 -> F32

def fork_ns source · line 538 · raw

@+context:Pricing -> @+first:U32 -> @+rows:U32 -> F32

def merged source · line 544 · raw

@+fork:F32 -> @left:Movement -> @right:Movement -> Movement

def leaf_movement source · line 549 · raw

@context:Pricing -> @first:U32 -> @count:U32 -> Movement

def fork_movement source · line 553 · raw

@context:Pricing -> @left:0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Plan -> @right:0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Plan -> @first:Movement -> @second:Movement -> Movement

def image_rows source · line 558 · raw

@depth:Nat -> @+context:Pricing -> 0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Plan

The plan the partition runs: the rows of one image halved depth times.

def movement source · line 563 · raw

@depth:Nat -> @+context:Pricing -> Movement

def estimate source · line 566 · raw

@+depth:U32 -> @+workers:U32 -> @+wake:F32 -> @cost:Movement -> F32

def movement_work source · line 570 · raw

@cost:Movement -> F32

def chosen source · line 574 · raw

@allowed:Bool -> @+depth:U32 -> @+time:F32 -> @best:Choice -> Choice

def candidate source · line 578 · raw

@+depth:U32 -> @+workers:U32 -> @+context:Pricing -> @best:Choice -> Choice

def depths source · line 587 · raw

@remaining:Nat -> @allowed:Bool -> @+depth:U32 -> @+tasks:U32 -> @+ceiling:U32 -> @+workers:U32 -> @+context:Pricing -> @best:Choice -> Choice

As the other planners: up to five leaves for each worker, at most one leaf a row, one worker never splits.

def baseline source · line 598 · raw

@+context:Pricing -> Choice

One worker, no split: the work of the whole image.

def chosen_depth source · line 601 · raw

@best:Choice -> Nat

def byte_cost source · line 605 · raw

@config:0xd6aaab858a4fbd1461bd0fee27b97184/kernel_cost.Configuration -> F32

def select source · line 609 · raw

@+geometry:Geometry -> @+layout:Tiling -> @+workers:U32 -> @+weight_tiles:U32 -> @+entries:U32 -> Nat

def image_bands source · line 623 · raw

@weights:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @offsets:Array<U32> -> @result:Pair(0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Bands(F32), Array<F32>) -> Pair(ImageState, 0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Bands(F32))

def image_offsets source · line 627 · raw

@+first:U32 -> @+context:Images -> @gradient:Array<F32> -> @weights:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @kept_weights:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @pair:Pair(Array<U32>, Array<U32>) -> Pair(ImageState, 0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Bands(F32))

def image_weights source · line 636 · raw

@+first:U32 -> @+context:Images -> @gradient:Array<F32> -> @offsets:Array<U32> -> @pair:Pair(Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>, Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>) -> Pair(ImageState, 0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Bands(F32))

def image source · line 642 · raw

@context:Images -> @first:U32 -> @rows:U32 -> @state:ImageState -> Pair(ImageState, 0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Bands(F32))

The image whose rows start at first runs its plan, a single leaf when the depth is 0. Its leaves are its bands of the output.

def finished source · line 650 · raw

@source:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> @result:Pair(ImageState, 0xd6aaab858a4fbd1461bd0fee27b97184/tensor_band.Bands(F32)) -> Outcome

def packed source · line 655 · raw

@+geometry:Geometry -> @+layout:Tiling -> @+batch:Nat -> @+depth:Nat -> @gradient:Array<F32> -> @state:Packed -> Outcome

The images in order, each one band of rows of the batch.

def tiled source · line 660 · raw

@+geometry:Geometry -> @batch:Nat -> @+workers:U32 -> @gradient:Array<F32> -> @weights:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> Outcome

def run_selected source · line 670 · raw

@empty:Bool -> @+geometry:Geometry -> @batch:Nat -> @+workers:U32 -> @gradient:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> @weights:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> Outcome

def run source · line 679 · raw

@+geometry:Geometry -> @count:Nat -> @+workers:U32 -> @gradient:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> @weights:0xd6aaab858a4fbd1461bd0fee27b97184/storage_buffer.Buffer<F32> -> Outcome

d [N][O][Ho][Wo] and weights [O][C][k][k] give the input gradient [N][C][H][W] as bands of rows of the batch; count is N * C * H * W.

Templates

template with_weights source · line 208 · raw

@-upper:Bool -> @tiles:Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @offsets:Array<U32> -> @+index:U32 -> @context:Context -> @+source:U32 -> @accumulators:0xd6aaab858a4fbd1461bd0fee27b97184/kernel_rows.Rows<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8> -> @read:Pair(Array<0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8>, 0xd6aaab858a4fbd1461bd0fee27b97184/kernel_tile.Tile8) -> Loop

template step source · line 215 · raw

@-upper:Bool -> @state:Loop -> Loop

One ordered step: the weight tile, the tile of the entry through the table, then the products of four rows at eight outputs.