omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / ng / front / kernels / linear.om
| 1 | note linear: the torch.nn.Linear addmm kernel of omega-bridge pytorch/kernels/linear.om (main d8174edd, |
| 2 | note blob f7d5231a) at M 64, K 256, N 256, verbatim below the consts, plus a bench main: 40 passes, the |
| 3 | note bias changes by 1.0 at one column before each pass, and every pass folds the f32 bits of all |
| 4 | note outputs into one ulong checksum, printed as one decimal line. |
| 5 | const M ulong 64 |
| 6 | const K ulong 256 |
| 7 | const N ulong 256 |
| 8 | const MK ulong 16384 |
| 9 | const NK ulong 65536 |
| 10 | const MN ulong 16384 |
| 11 | note linear: o[M,N] = c[N] + a[M,K] @ w[N,K]^T, the addmm of a torch.nn.Linear whose weight transpose |
| 12 | note (aten.permute [1,0]) feeds only this addmm; w is read in place, nothing is copied. Each sum starts |
| 13 | note at the bias, is summed in f64 and rounded once to f32. Consts M K N MK NK MN come first. |
| 14 | fn lindot c view f32 N a view f32 MK w view f32 NK i ulong j ulong to f64 |
| 15 | s = f64 get c j |
| 16 | ia = mul i K |
| 17 | iw = mul j K |
| 18 | for k ulong K |
| 19 | s = add s mul f64 get a ia f64 get w iw |
| 20 | ia = add ia 1 |
| 21 | iw = add iw 1 |
| 22 | end |
| 23 | ret s |
| 24 | end |
| 25 | fn linear c view f32 N a view f32 MK w view f32 NK o edit f32 MN to void |
| 26 | for i ulong M |
| 27 | for j ulong N |
| 28 | put o add mul i N j f32 lindot c a w i j |
| 29 | end |
| 30 | end |
| 31 | end |
| 32 | global out uchar 32 |
| 33 | fn putnum v ulong to void |
| 34 | put out 31 10 |
| 35 | p = ulong 31 |
| 36 | x = v |
| 37 | while gt x 9 |
| 38 | p = sub p 1 |
| 39 | put out p uchar add rem x 10 48 |
| 40 | x = div x 10 |
| 41 | end |
| 42 | p = sub p 1 |
| 43 | put out p uchar add x 48 |
| 44 | w = write 1 out p sub 32 p |
| 45 | end |
| 46 | fn main to int |
| 47 | c = new f32 256 0.0f32 |
| 48 | a = new f32 16384 0.0f32 |
| 49 | w = new f32 65536 0.0f32 |
| 50 | o = new f32 16384 0.0f32 |
| 51 | for i ulong 256 |
| 52 | put c i div f32 sub long rem mul i 5 11 5 4.0f32 |
| 53 | end |
| 54 | for i ulong 16384 |
| 55 | put a i div f32 sub long rem mul i 7 13 6 8.0f32 |
| 56 | end |
| 57 | for i ulong 65536 |
| 58 | put w i div f32 sub long rem mul i 3 17 8 16.0f32 |
| 59 | end |
| 60 | s = ulong 0 |
| 61 | for r ulong 40 |
| 62 | q = rem r 256 |
| 63 | put c q add get c q 1.0f32 |
| 64 | linear c a w o |
| 65 | for i ulong 16384 |
| 66 | s = add s mul ulong bitcast uint get o i add i 1 |
| 67 | end |
| 68 | end |
| 69 | putnum s |
| 70 | ret 0 |
| 71 | end |