interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / om / qwen-native / blocks / attention.om

202 lines · 4101 bytes Raw Blame History
1note Two full layers per bank. One uint packs the BF16 K and V of the same lane.
2global qa_cache0 uint 16777216
3global qa_cache1 uint 16777216
4global qa_cache2 uint 16777216
5global qa_cache3 uint 16777216
6global qa_cache4 uint 16777216
7global qa_cache5 uint 16777216
8global qa_cache6 uint 16777216
9global qa_cache7 uint 16777216
10
11fn qa_cache_get bank ulong idx ulong to uint
12if eq bank 0
13ret get qa_cache0 idx
14end
15if eq bank 1
16ret get qa_cache1 idx
17end
18if eq bank 2
19ret get qa_cache2 idx
20end
21if eq bank 3
22ret get qa_cache3 idx
23end
24if eq bank 4
25ret get qa_cache4 idx
26end
27if eq bank 5
28ret get qa_cache5 idx
29end
30if eq bank 6
31ret get qa_cache6 idx
32end
33ret get qa_cache7 idx
34end
35
36fn qa_cache_put bank ulong idx ulong value uint to void
37if eq bank 0
38put qa_cache0 idx value
39ret
40end
41if eq bank 1
42put qa_cache1 idx value
43ret
44end
45if eq bank 2
46put qa_cache2 idx value
47ret
48end
49if eq bank 3
50put qa_cache3 idx value
51ret
52end
53if eq bank 4
54put qa_cache4 idx value
55ret
56end
57if eq bank 5
58put qa_cache5 idx value
59ret
60end
61if eq bank 6
62put qa_cache6 idx value
63ret
64end
65put qa_cache7 idx value
66ret
67end
68
69global qa_row f32 256
70global qa_cos f32 32
71global qa_sin f32 32
72global qa_score f32 8192
73
74fn qa_angles pos ulong to void
75for i ulong 32
76freq = qb_exp mul f64 i -0.50369048909244749f64
77angle = mul f64 pos freq
78put qa_cos i f32 qb_sc angle true
79put qa_sin i f32 qb_sc angle false
80end
81end
82
83fn qa_prepare x view f32 14336 start ulong w view f32 512 off ulong to void
84s = 0.0f32
85for j ulong 256
86v = get x add start j
87s = add s mul v v
88end
89inv = div 1.0f32 fsqrt add div s 256.0f32 1.0e-6f32
90for j ulong 256
91v = mul get x add start j inv
92put qa_row j mul v add 1.0f32 get w add off j
93end
94for i ulong 32
95a = get qa_row i
96b = get qa_row add i 32
97co = get qa_cos i
98si = get qa_sin i
99put qa_row i sub mul a co mul b si
100put qa_row add i 32 add mul b co mul a si
101end
102end
103
104fn qa_bf x f32 to uint
105bits = bitcast uint x
106ret shr add bits add 32767 and shr bits 16 1 16
107end
108
109fn qa_row_offset slot ulong pos ulong nk ulong h ulong to ulong
110ret mul add mul rem slot 2 32768 add mul pos nk h 256
111end
112
113fn qa_store bank ulong row ulong x view f32 14336 vsrc ulong to void
114for j ulong 256
115k = qa_bf get qa_row j
116v = qa_bf get x add vsrc j
117qa_cache_put bank add row j or shl k 16 v
118end
119end
120
121fn qa_append x view f32 14336 w view f32 512 nh ulong nk ulong slot ulong pos ulong to void
122for h ulong nk
123src = add mul nh 512 mul h 256
124qa_prepare x src w 256
125row = qa_row_offset slot pos nk h
126src = add src mul nk 256
127qa_store div slot 2 row x src
128end
129end
130
131fn qa_dot bank ulong row ulong to f32
132s = 0.0f32
133for j ulong 256
134k = bitcast f32 and qa_cache_get bank add row j 4294901760
135s = add s mul get qa_row j k
136end
137ret div s 16.0f32
138end
139
140fn qa_weighted bank ulong row ulong wt f32 to void
141for j ulong 256
142v = bitcast f32 shl qa_cache_get bank add row j 16
143put qa_row j add get qa_row j mul wt v
144end
145end
146
147fn qa_attend bank ulong first ulong stride ulong span ulong to void
148peak = -3.40282346e38f32
149for p ulong span
150score = qa_dot bank add first mul p stride
151put qa_score p score
152peak = fmax peak score
153end
154for j ulong 256
155put qa_row j 0.0f32
156end
157den = 0.0f32
158for p ulong span
159wt = f32 qb_exp f64 sub get qa_score p peak
160den = add den wt
161row = add first mul p stride
162qa_weighted bank row wt
163end
164for j ulong 256
165put qa_row j div get qa_row j den
166end
167end
168
169fn qa_gate h ulong x view f32 14336 y edit f32 6144 to void
170for j ulong 256
171gate = get x add add mul h 512 256 j
172put y add mul h 256 j mul get qa_row j f32 qb_sig f64 gate
173end
174end
175
176fn qa_query slot ulong h ulong nh ulong nk ulong pos ulong to void
177first = qa_row_offset slot 0 nk div h div nh nk
178qa_attend div slot 2 first mul nk 256 add pos 1
179end
180
181fn qwen_attention_step model ulong layer ulong pos ulong x view f32 14336 w view f32 512 y edit f32 6144 to void
182qb_check model layer pos true
183nh = ulong 24
184nk = ulong 4
185if eq model 35
186nh = ulong 16
187nk = ulong 2
188end
189if eq model 38
190nk = ulong 2
191end
192slot = div layer 4
193qa_angles pos
194qa_append x w nh nk slot pos
195for h ulong nh
196qa_prepare x mul h 512 w 0
197qa_query slot h nh nk pos
198qa_gate h x y
199end
200put qb_model layer model
201put qb_next layer add pos 1
202end