ordered_matrix.bend
stelliferous@0.0.3.0 · 30 lines · 0 definitions
1# Ordered FP32 matrices with closed lookup templates and runtime data/dimensions.
2import Base
3
4def cat(-A: Data,xs: List<&2,A>,ys: List<&2,A>) -> List<&2,A>:
5 match xs:
6 case Nil{}:
7 ys
8 case h <> tail:
9 h <> cat(A,tail,ys)
10
11def dot(~R: Data,~K: Data,~C: Data,~P: Data,~x: P -> C -> K -> F32,~w: P -> R -> K -> F32,ks: List<&2,K>,+r: R,+c: C,+p: P,acc: F32) -> F32:
12 match ks:
13 case Nil{}:
14 acc
15 case +k <> tail:
16 dot(~R,~K,~C,~P,~x,~w,tail,r,c,p,(acc + x(p,c,k) * w(p,r,k) : F32))
17
18def row(~R: Data,~K: Data,~C: Data,~P: Data,~x: P -> C -> K -> F32,~w: P -> R -> K -> F32,~bias: P -> R -> F32,cs: List<&2,C>,+ks: List<&2,K>,+r: R,+p: P) -> List<&2,F32>:
19 match cs:
20 case Nil{}:
21 Nil{}
22 case c <> tail:
23 (dot(~R,~K,~C,~P,~x,~w,ks,r,c,p,0.0) + bias(p,r) : F32) <> row(~R,~K,~C,~P,~x,~w,~bias,tail,ks,r,p)
24
25def matmul(~R: Data,~K: Data,~C: Data,~P: Data,~x: P -> C -> K -> F32,~w: P -> R -> K -> F32,~bias: P -> R -> F32,rs: List<&2,R>,+ks: List<&2,K>,+cs: List<&2,C>,+p: P) -> List<&2,F32>:
26 match rs:
27 case Nil{}:
28 Nil{}
29 case +r <> tail:
30 cat(F32,row(~R,~K,~C,~P,~x,~w,~bias,cs,ks,r,p),matmul(~R,~K,~C,~P,~x,~w,~bias,tail,ks,cs,p))