tensor_band_pool.bend

stelliferous@0.0.2.0 · 89 lines · 10 definitions

1# Each band leaf assembles the rows its outputs read into one contiguous
2# window and runs the two sweeps of the dense pool on it: along the columns of
3# every window row, then along the rows, with output row zero's window
4# starting at the window row that the image row would. Sample order is the
5# dense order.
6import Base
7import stelliferous@0.0.2.0/tensor_access.bend as Access
8import stelliferous@0.0.2.0/tensor_operations.bend as Operations
9import stelliferous@0.0.2.0/tensor_band.bend as Bands
10import stelliferous@0.0.2.0/tensor_band_window.bend as BandWindow
11import stelliferous@0.0.2.0/traversal_partition.bend as Partition
12import stelliferous@0.0.2.0/storage_buffer.bend as Storage
13import stelliferous@0.0.2.0/tensor.bend as Tensors
14import stelliferous@0.0.2.0/tensor_band_schedule.bend as BandSchedule
15import stelliferous@0.0.2.0/kernel_cost.bend as Cost
16import stelliferous@0.0.2.0/default_kernel_cost.bend as DefaultCost
17
18type Context<-Element: Data> is Data:
19  Context{geometry: BandWindow.Geometry,pad: Element,fill: Element,workers: U32}
20
21def window_size(window: BandWindow.RowWindow) -> BandSchedule.Size:
22  BandWindow.RowWindow{BandWindow.Rows{first,+count},above,below,rows} = window
23  BandSchedule.Size{1,count,count}
24
25def plan_size(layout: Bands.Plan,+window: BandWindow.Geometry) -> BandSchedule.Size:
26  match layout:
27    case Bands.Rows{first,rows}: window_size(BandWindow.window(window,first,rows))
28    case Bands.Split{left,right}: BandSchedule.merge(plan_size(left,window),plan_size(right,window))
29
30# Bytes price loading, storing and the loop that moves them (copy_byte_ns is
31# measured on copy loops); operations count the rest. Per sample
32# (Access.read_line and the operation): an offset and a subtraction, one bound
33# check, a two-operation index, the padding select and the compare-and-select
34# maximum: 8. Each line adds ten for its origin (Access.line_at). An output is
35# one line along the rows; the lines along the columns are one for every
36# window row, stride for every output row.
37def parallel(window: BandWindow.Geometry,workers: U32,size: BandSchedule.Size) -> Bool:
38  BandWindow.Geometry{Bands.Geometry{batch,channels,ih,iw},kernel,stride,padding,oh,ow} = window
39  +config = DefaultCost.configuration()
40  +samples = U32.to_f32(kernel)
41  +lines = (U32.to_f32(stride) + 1.0 : F32)
42  +line_bytes = (samples * 4.0 + 8.0 : F32)
43  +line_operations = (samples * 8.0 + 10.0 : F32)
44  BandSchedule.sized(Bands.Geometry{batch,channels,oh,ow},workers,Cost.element_ns((lines * line_bytes : F32),(lines * line_operations : F32),config),config,size)
45
46# The destination of the two sweeps of channels stored planes: along the
47# columns into middle, [channels,height,out_width], then along the rows from
48# origin into output.
49def pooled(~Element: Data,~operation: Element -> Element -> Element,+pool: Access.Pool,+pad: Element,origin: U32,+channels: U32,
50  input: Storage.Buffer<Element>,middle: Storage.Buffer<Element>,output: Storage.Buffer<Element>) -> Storage.Buffer<Element>:
51  Access.Pool{+height,+width,+kernel,+stride,+padding,+out_height,+out_width} = pool
52  +pool = {Access.Pool{height,width,kernel,stride,padding,out_height,out_width} : Access.Pool}
53  Bands.destination(~Element,Operations.swept(~Element,~operation,U32.to_nat((channels * out_height * out_width : U32)),
54    Access.SweepContext{Access.pool_rows(pool,origin),pad},
55    Bands.destination(~Element,Operations.swept(~Element,~operation,U32.to_nat((channels * height * out_width : U32)),
56      Access.SweepContext{Access.pool_columns(pool),pad},input,middle)),output))
57
58def windowed(~Element: Data,~operation: Element -> Element -> Element,+geometry: BandWindow.Geometry,+pad: Element,+fill: Element,+first: U32,+rows: U32,
59  +rows_read: BandWindow.RowWindow,core: Storage.Buffer<Element>,above: Storage.Buffer<Element>,below: Storage.Buffer<Element>) -> Bands.Bands(~Element):
60  BandWindow.Geometry{Bands.Geometry{batch,+channels,+height,+width},+kernel,+stride,+padding,+output_height,+output_width} = geometry
61  BandWindow.RowWindow{BandWindow.Rows{+output_first,+output_rows},+above_range,+below_range,+window} = rows_read
62  BandWindow.Rows{+window_first,+window_rows} = window
63  Partition.Leaf{pooled(~Element,~operation,Access.Pool{window_rows,width,kernel,stride,padding,output_rows,output_width},pad,
64    ((output_first - first / height * output_height) * stride + first / height * height - window_first : U32),channels,
65    BandWindow.assemble(~Element,fill,BandWindow.input_geometry(geometry),window,BandWindow.Rows{first,rows},above_range,below_range,core,above,below),
66    Tensors.buffer(~Element,fill,(channels * window_rows * output_width : U32)),
67    Tensors.buffer(~Element,fill,(channels * output_rows * output_width : U32))),output_first,output_rows,Unit{}}
68
69def leaf(~Element: Data,~operation: Element -> Element -> Element,context: Context<Element>,first: U32,rows: U32,core: Storage.Buffer<Element>,halo: BandWindow.Halo<Element>) -> Bands.Bands(~Element):
70  Context{+geometry,+pad,+fill,workers} = context
71  BandWindow.Halo{above,below,+rows_read} = halo
72  +first = first
73  +rows = rows
74  windowed(~Element,~operation,geometry,pad,fill,first,rows,rows_read,core,above,below)
75
76def prepared(~Element: Data,~operation: Element -> Element -> Element,+context: Context<Element>,+layout: Bands.Plan,owners: Bands.Bands(~Element) & BandWindow.Halos(~Element)) -> Bands.Bands(~Element):
77  Context{+window,pad,fill,workers} = context
78  (bands,halos) = owners
79  +geometry = BandWindow.input_geometry(window)
80  +enabled = parallel(window,workers,plan_size(layout,window))
81  BandWindow.apply(~Element,~Context<Element>,~leaf(~Element,~operation),layout,enabled && Bands.same_image(geometry,layout),enabled,geometry,context,bands,halos)
82
83def planned(~Element: Data,~operation: Element -> Element -> Element,+context: Context<Element>,owners: Bands.Bands(~Element) & Bands.Plan) -> Bands.Bands(~Element):
84  Context{window,pad,fill,workers} = context
85  (bands,+layout) = owners
86  prepared(~Element,~operation,context,layout,BandWindow.prepare(~Element,window,pad,layout,bands))
87
88def execute(~Element: Data,~operation: Element -> Element -> Element,context: Context<Element>,bands: Bands.Bands(~Element)) -> Bands.Bands(~Element):
89  planned(~Element,~operation,context,Bands.plan(~Element,bands))