interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / ng / front / kernels / linear.om

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