traversal_partition.bend
stelliferous@0.0.2.0 · 43 lines · 3 definitions
1
2
3import Base
4import stelliferous@0.0.2.0/storage_buffer.bend as Storage
5
6type Tree<ownership,-Block: Kind(ownership),-Details: Data> is Kind(ownership):
7 Leaf{data: Block,offset: U32,span: U32,details: Details}
8 Fork{left: Tree<ownership,Block,Details>,right: Tree<ownership,Block,Details>}
9
10
11
12
13
14
15
16
17
18def run(~Input: Type,~Weights: Type,~Bias: Type,~Output: Type,~Context: Data,
19 ~split_input: Context -> U32 -> U32 -> Input -> Input & Input,~clone_weights: Weights -> Weights & Weights,~clone_bias: Bias -> Bias & Bias,
20 ~leaf: Context -> U32 -> U32 -> Input -> Weights -> Bias -> Tree<&1,Output,U32>,
21 depth: Nat,+context: Context,+offset: U32,+span: U32,input: Input,weights: Weights,bias: Bias) -> Tree<&1,Output,U32>:
22 match depth:
23 case 0n: leaf(context,offset,span,input,weights,bias)
24 case 1n:
25 +half = (span / 16 * 8 : U32)
26 Storage.bind_pair(~Input,~Input,~Tree<&1,Output,U32>,split_input(context,offset,half,input),left_input => right_input =>
27 Storage.bind_pair(~Weights,~Weights,~Tree<&1,Output,U32>,clone_weights(weights),left_weights => right_weights =>
28 Storage.bind_pair(~Bias,~Bias,~Tree<&1,Output,U32>,clone_bias(bias),left_bias => right_bias =>
29 left right = leaf(context,offset,half,left_input,left_weights,left_bias) leaf(context,(offset + half : U32),(span - half : U32),right_input,right_weights,right_bias)
30 Fork{left,right})))
31 case 2n+ +rest:
32 +half = (span / 16 * 8 : U32)
33 Storage.bind_pair(~Input,~Input,~Tree<&1,Output,U32>,split_input(context,offset,half,input),left_input => right_input =>
34 Storage.bind_pair(~Weights,~Weights,~Tree<&1,Output,U32>,clone_weights(weights),left_weights => right_weights =>
35 Storage.bind_pair(~Bias,~Bias,~Tree<&1,Output,U32>,clone_bias(bias),left_bias => right_bias =>
36 left right = run(~Input,~Weights,~Bias,~Output,~Context,~split_input,~clone_weights,~clone_bias,~leaf,1n+rest,context,offset,half,left_input,left_weights,left_bias) run(~Input,~Weights,~Bias,~Output,~Context,~split_input,~clone_weights,~clone_bias,~leaf,1n+rest,context,(offset + half : U32),(span - half : U32),right_input,right_weights,right_bias)
37 Fork{left,right})))
38
39def gather(~Output: Type,~copy: U32 -> U32 -> U32 -> U32 -> Output -> Output -> Output,
40 partitions: Tree<&1,Output,U32>,output: Output,+positions: U32) -> Output:
41 match partitions:
42 case Leaf{input,offset,span,rows}: copy(rows,offset,positions,span,input,output)
43 case Fork{left,right}: gather(~Output,~copy,right,gather(~Output,~copy,left,output,positions),positions)