interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / x86 / model / llama.om

355 lines · 8346 bytes Raw Blame History
1target x86linux
2note Native decoder for the two pinned TinyStories Llama checkpoints.
3note Inference, attention, normalization, sampling and generation are Omega.
4note Input args: prompt count, new-token ceiling, checkpoint words, token ids.
5note Read-only checkpoint uses the llama2.c version-0 f32 layout.
6note Workspace is private, scratch begins at word 1024; first page is output.
7note fexp is reused from aien-dev/omega-dnn om/dnn.om (Apache-2.0 with LLVM exception), with f32 rounding at use.
8om2
9
10fn fexp x f64 to f64
11if ne x x
12ret x
13end
14if lt x -708.0f64
15ret 0.0f64
16end
17y = fmin x 709.0f64
18t = mul y 1.4426950408889634f64
19n = long 0
20if ge t 0.0f64
21n = long add t 0.5f64
22else
23n = long sub t 0.5f64
24end
25t = f64 n
26y = sub sub y mul t 0.6931471805599453f64 mul t 2.3190468138462996e-17f64
27t = 1.0f64
28for j ulong 13
29t = add 1.0f64 div mul y t f64 sub 13 j
30end
31ret mul t bitcast f64 shl ulong add n 1023 52
32end
33
34fn wf w view uint 16000000 i ulong to f32 inline
35ret bitcast f32 get w i
36end
37fn sf s edit uint 1048576 i ulong to f32
38ret bitcast f32 get s i
39end
40fn sf_fast s edit uint 1048576 i ulong to f32 inline
41ret bitcast f32 get s i
42end
43fn putf s edit uint 1048576 i ulong x f32 to void inline
44put s i bitcast uint x
45end
46
47fn setup w view uint 16000000 c edit ulong 64 nw ulong to void
48for i ulong 7
49put c i ulong get w i
50end
51if eq get c 0 64
52if ne get c 1 172
53trap 3
54end
55if ne get c 2 5
56trap 3
57end
58if ne get c 3 8
59trap 3
60end
61if ne get c 4 4
62trap 3
63end
64if ne get c 5 512
65trap 3
66end
67if ne get c 6 512
68trap 3
69end
70else
71if ne get c 0 288
72trap 3
73end
74if ne get c 1 768
75trap 3
76end
77if ne get c 2 6
78trap 3
79end
80if ne get c 3 6
81trap 3
82end
83if ne get c 4 6
84trap 3
85end
86if ne get c 5 32000
87trap 3
88end
89if ne get c 6 256
90trap 3
91end
92end
93put c 7 div mul get c 0 get c 4 get c 3
94put c 8 div get c 0 get c 3
95put c 9 64
96put c 10 7
97put c 11 add get c 10 mul get c 5 get c 0
98put c 12 add get c 11 mul get c 2 get c 0
99put c 13 add get c 12 mul get c 2 mul get c 0 get c 0
100put c 14 add get c 13 mul get c 2 mul get c 0 get c 7
101put c 15 add get c 14 mul get c 2 mul get c 0 get c 7
102put c 16 add get c 15 mul get c 2 mul get c 0 get c 0
103put c 17 add get c 16 mul get c 2 get c 0
104put c 18 add get c 17 mul get c 2 mul get c 0 get c 1
105put c 19 add get c 18 mul get c 2 mul get c 0 get c 1
106put c 20 add get c 19 mul get c 2 mul get c 0 get c 1
107put c 21 add get c 20 get c 0
108put c 22 add get c 21 div mul get c 6 get c 8 2
109if ne nw add get c 22 div mul get c 6 get c 8 2
110trap 3
111end
112put c 32 1024
113put c 33 add get c 32 get c 0
114put c 34 add get c 33 get c 0
115put c 35 add get c 34 get c 0
116put c 36 add get c 35 get c 1
117put c 37 add get c 36 get c 1
118put c 38 add get c 37 get c 0
119put c 39 add get c 38 mul get c 2 mul get c 9 get c 7
120put c 40 add get c 39 mul get c 2 mul get c 9 get c 7
121put c 41 add get c 40 mul get c 3 get c 9
122if gt add get c 41 get c 5 1048576
123trap 3
124end
125end
126
127fn dot w view uint 16000000 s edit uint 1048576 wi ulong si ulong n ulong to f32
128v = 0.0f32
129for j ulong n
130v = add v mul wf w add wi j sf_fast s add si j
131end
132ret v
133end
134fn linear w view uint 16000000 s edit uint 1048576 wi ulong si ulong di ulong shape ulong to void
135n = shr shape 32
136m = and shape 0xffffffff
137for i ulong m
138putf s add di i dot w s add wi mul i n si n
139end
140end
141fn rms w view uint 16000000 s edit uint 1048576 wi ulong si ulong di ulong n ulong to void
142v = 0.0f32
143for j ulong n
144x = sf s add si j
145v = add v mul x x
146end
147v = div 1.0f32 fsqrt add div v f32 n 0.00001f32
148for j ulong n
149putf s add di j mul wf w add wi j mul v sf s add si j
150end
151end
152fn copyw w view uint 16000000 s edit uint 1048576 wi ulong di ulong n ulong to void
153for j ulong n
154put s add di j get w add wi j
155end
156end
157fn residual s edit uint 1048576 di ulong si ulong n ulong to void
158for j ulong n
159putf s add di j add sf s add di j sf s add si j
160end
161end
162
163fn rotate_values s edit uint 1048576 i ulong cs f32 sn f32 to void
164a = sf s i
165b = sf s add i 1
166putf s i sub mul a cs mul b sn
167putf s add i 1 add mul a sn mul b cs
168end
169fn rotate w view uint 16000000 s edit uint 1048576 c view ulong 64 base ulong n ulong to void
170for pair ulong div n 2
171i = mul pair 2
172off = mul get c 49 get c 8
173off = add off rem i get c 8
174off = div off 2
175cs = wf w add get c 21 off
176sn = wf w add get c 22 off
177rotate_values s add base i cs sn
178end
179end
180fn softmax s edit uint 1048576 base ulong n ulong to void
181m = sf s base
182for j ulong n
183m = fmax m sf s add base j
184end
185total = 0.0f32
186for j ulong n
187v = f32 fexp f64 sub sf s add base j m
188putf s add base j v
189total = add total v
190end
191for j ulong n
192putf s add base j div sf s add base j total
193end
194end
195fn score s edit uint 1048576 a ulong b ulong n ulong to f32
196v = 0.0f32
197for j ulong n
198v = add v mul sf_fast s add a j sf_fast s add b j
199end
200ret div v fsqrt f32 n
201end
202fn weighted s edit uint 1048576 a ulong k ulong n ulong stride ulong to f32
203v = 0.0f32
204for t ulong n
205v = add v mul sf_fast s add a t sf_fast s add k mul t stride
206end
207ret v
208end
209fn attend s edit uint 1048576 c view ulong 64 head ulong to void
210q = mul get c 48 get c 9
211q = mul q get c 7
212k = div get c 3 get c 4
213k = mul div head k get c 8
214k = add add k q get c 38
215q = add get c 37 mul head get c 8
216a = add get c 40 mul head get c 9
217for t ulong add get c 49 1
218putf s add a t score s q add k mul t get c 7 get c 8
219end
220softmax s a add get c 49 1
221k = add k sub get c 39 get c 38
222q = add get c 33 mul head get c 8
223for j ulong get c 8
224v = weighted s a add k j add get c 49 1 get c 7
225putf s add q j v
226end
227end
228fn swiglu s edit uint 1048576 a ulong b ulong n ulong to void
229for j ulong n
230v = sf s add a j
231v = mul v div 1.0f32 add 1.0f32 f32 fexp f64 neg v
232putf s add a j mul v sf s add b j
233end
234end
235
236fn layer w view uint 16000000 s edit uint 1048576 c view ulong 64 to void
237rms w s add get c 11 mul get c 48 get c 0 get c 32 get c 33 get c 0
238off = ulong 0
239dest = ulong 0
240shape = ulong 0
241cache = mul get c 48 get c 9
242cache = mul add cache get c 49 get c 7
243off = mul get c 48 mul get c 0 get c 0
244off = add off get c 12
245dest = get c 37
246shape = or shl get c 0 32 get c 0
247linear w s off get c 33 dest shape
248off = mul get c 48 mul get c 0 get c 7
249off = add off get c 13
250dest = add get c 38 cache
251shape = or shl get c 0 32 get c 7
252linear w s off get c 33 dest shape
253off = mul get c 48 mul get c 0 get c 7
254off = add off get c 14
255dest = add get c 39 cache
256shape = or shl get c 0 32 get c 7
257linear w s off get c 33 dest shape
258rotate w s c get c 37 get c 0
259rotate w s c add get c 38 cache get c 7
260for head ulong get c 3
261attend s c head
262end
263off = mul get c 48 mul get c 0 get c 0
264off = add off get c 15
265dest = get c 34
266shape = or shl get c 0 32 get c 0
267linear w s off get c 33 dest shape
268residual s get c 32 get c 34 get c 0
269off = add get c 16 mul get c 48 get c 0
270rms w s off get c 32 get c 33 get c 0
271off = mul get c 48 mul get c 0 get c 1
272off = add off get c 17
273dest = get c 35
274shape = or shl get c 0 32 get c 1
275linear w s off get c 33 dest shape
276off = mul get c 48 mul get c 0 get c 1
277off = add off get c 19
278dest = get c 36
279shape = or shl get c 0 32 get c 1
280linear w s off get c 33 dest shape
281swiglu s get c 35 get c 36 get c 1
282off = mul get c 48 mul get c 1 get c 0
283off = add off get c 18
284dest = get c 33
285shape = or shl get c 1 32 get c 0
286linear w s off get c 35 dest shape
287residual s get c 32 get c 33 get c 0
288end
289fn forward w view uint 16000000 s edit uint 1048576 c edit ulong 64 token ulong pos ulong to ulong
290if ge token get c 5
291trap 3
292end
293put c 49 pos
294copyw w s add get c 10 mul token get c 0 get c 32 get c 0
295for l ulong get c 2
296put c 48 l
297layer w s c
298end
299rms w s get c 20 get c 32 get c 32 get c 0
300linear w s get c 10 get c 32 get c 41 or shl get c 0 32 get c 5
301best = ulong 0
302for i ulong get c 5
303if gt sf s add get c 41 i sf s add get c 41 best
304best = i
305end
306end
307ret best
308end
309
310fn run args view ulong 512 s edit uint 1048576 count ulong w view uint 16000000 to ulong
311if lt count 32
312trap 3
313end
314c = new ulong 64 0
315setup w c get args 2
316nt = get args 0
317nn = get args 1
318if eq nt 0
319trap 3
320end
321if eq nn 0
322trap 3
323end
324if gt nt 32
325trap 3
326end
327if gt nn 32
328trap 3
329end
330if gt add nt nn 65
331trap 3
332end
333if ne count mul add nt 3 8
334trap 3
335end
336put s 0 0
337put s 1 uint get c 41
338put s 2 uint get c 5
339put s 3 uint get c 9
340token = get args 3
341for pos ulong sub add nt nn 1
342token = forward w s c token pos
343if lt add pos 1 nt
344token = get args add pos 4
345else
346if eq token 1
347break
348end
349i = ulong get s 0
350put s add 16 i uint token
351put s 0 uint add i 1
352end
353end
354ret mul add ulong get s 0 16 4
355end