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 / tokenizer / tokenizer.om

406 lines · 6290 bytes Raw Blame History
1global input uchar *
2global token uint *
3global following ulong *
4global previous ulong *
5global heaprank uint *
6global heappos ulong *
7global heapmeta ulong 1
8text encodecmd "encode"
9text decodecmd "decode"
10
11fn inputsize to ulong
12n = ulong 0
13r = enlarge input 16384
14if lt r 0
15exit 4
16end
17while true bound 9223372036854775807
18if eq n len input
19r = enlarge input mul 2 n
20if lt r 0
21exit 4
22end
23end
24r = read 0 input n sub len input n
25if lt r 0
26exit 5
27end
28if eq r 0
29ret n
30end
31n = add n ulong r
32end
33ret n
34end
35
36fn markerat p ulong id ulong k ulong to bool
37o = get starts id
38for j ulong k
39if ne get input add p j get spelling add o j
40ret false
41end
42end
43ret true
44end
45
46fn special p ulong n ulong to ulong
47if ne get input p 60
48ret 0
49end
50for t ulong sub get meta 1 248044
51id = add 248044 t
52k = ulong get sizes id
53if le k sub n p
54if markerat p id k
55ret id
56end
57end
58end
59ret 0
60end
61
62fn qfold cp uint to uint
63if and ge cp 65 le cp 90
64ret add cp 32
65end
66if eq cp 383
67ret 115
68end
69ret cp
70end
71
72fn contraction p ulong n ulong to ulong
73if or ne get scalars p 39 ge add p 1 n
74ret 0
75end
76a = qfold get scalars add p 1
77if or or eq a 115 eq a 116 or eq a 109 eq a 100
78ret 2
79end
80if lt add p 2 n
81b = qfold get scalars add p 2
82if or and or eq a 114 eq a 118 eq b 101 and eq a 108 eq b 108
83ret 3
84end
85end
86ret 0
87end
88
89fn newline cp uint to bool
90ret or eq cp 10 eq cp 13
91end
92
93fn lettermark i ulong to bool
94k = get uclass get scalars i
95ret or eq k 1 eq k 2
96end
97
98note Return the end of the first matching alternative in the frozen Split regex.
99fn piece p ulong n ulong to ulong
100l = contraction p n
101if ne l 0
102ret add p l
103end
104cp = get scalars p
105k = get uclass cp
106i = p
107if and and ne k 1 ne k 3 not newline cp
108if lt add i 1 n
109if lettermark add i 1
110i = add i 1
111end
112end
113end
114if lettermark i
115while lt i n bound 9223372036854775807
116if not lettermark i
117break
118end
119i = add i 1
120end
121ret i
122end
123if eq k 3
124ret add p 1
125end
126i = p
127if and eq cp 32 lt add i 1 n
128if eq get uclass get scalars add i 1 0
129i = add i 1
130end
131end
132if eq get uclass get scalars i 0
133while lt i n bound 9223372036854775807
134if ne get uclass get scalars i 0
135break
136end
137i = add i 1
138end
139while lt i n bound 9223372036854775807
140if not newline get scalars i
141break
142end
143i = add i 1
144end
145ret i
146end
147i = p
148lastnl = ulong 0
149while lt i n bound 9223372036854775807
150if ne get uclass get scalars i 4
151break
152end
153if newline get scalars i
154lastnl = add i 1
155end
156i = add i 1
157end
158if ne lastnl 0
159ret lastnl
160end
161if and lt i n gt i add p 1
162ret sub i 1
163end
164ret i
165end
166
167fn precedes a uint i ulong b uint j ulong to bool
168ret or lt a b and eq a b lt i j
169end
170
171fn enqueue i ulong n ulong to void
172j = get following i
173if ge j n
174ret
175end
176slot = pairslot pairkey ulong get token i ulong get token j
177rank = get ranks slot
178if eq rank 0
179ret
180end
181h = add get heapmeta 0 1
182put heapmeta 0 h
183while gt h 1
184parent = shr h 1
185if not precedes rank i get heaprank parent get heappos parent
186break
187end
188put heaprank h get heaprank parent
189put heappos h get heappos parent
190h = parent
191end
192put heaprank h rank
193put heappos h i
194end
195
196fn heapbefore a ulong b ulong to bool
197ret precedes get heaprank a get heappos a get heaprank b get heappos b
198end
199
200fn popheap to void
201n = get heapmeta 0
202rank = get heaprank n
203pos = get heappos n
204n = sub n 1
205put heapmeta 0 n
206h = ulong 1
207while le mul h 2 n
208child = mul h 2
209if lt child n
210other = add child 1
211if heapbefore other child
212child = other
213end
214end
215if not precedes get heaprank child get heappos child rank pos
216break
217end
218put heaprank h get heaprank child
219put heappos h get heappos child
220h = child
221end
222put heaprank h rank
223put heappos h pos
224end
225
226fn reservescratch p ulong e ulong to void
227capacity = mul 4 sub e p
228r = enlarge token capacity
229if lt r 0
230exit 4
231end
232r = enlarge following capacity
233if lt r 0
234exit 4
235end
236r = enlarge previous capacity
237if lt r 0
238exit 4
239end
240slots = add 1 mul 3 capacity
241r = enlarge heaprank slots
242if lt r 0
243exit 4
244end
245r = enlarge heappos slots
246if lt r 0
247exit 4
248end
249end
250
251fn initpiece p ulong e ulong to ulong
252buf = new uchar 4 0
253n = ulong 0
254for i ulong sub e p bound 9223372036854775807
255k = js_putcp buf 0 ulong get scalars add p i
256for j ulong k
257put token n get baseids get buf j
258put following n add n 1
259if gt n 0
260put previous n sub n 1
261end
262n = add n 1
263end
264end
265ret n
266end
267
268fn bpe p ulong e ulong to void
269reservescratch p e
270n = initpiece p e
271put heapmeta 0 0
272for i ulong n bound 9223372036854775807
273enqueue i n
274end
275while ne get heapmeta 0 0 bound 9223372036854775807
276i = get heappos 1
277rank = get heaprank 1
278popheap
279j = get following i
280if lt j n
281slot = pairslot pairkey ulong get token i ulong get token j
282if eq get ranks slot rank
283put token i get results slot
284next = get following j
285put following j n
286put following i next
287if lt next n
288put previous next i
289end
290if gt i 0
291enqueue get previous i n
292end
293enqueue i n
294end
295end
296end
297i = ulong 0
298while lt i n bound 9223372036854775807
299outid ulong get token i
300i = get following i
301end
302end
303
304fn normalspan start ulong endpos ulong to void
305put nfcstate 0 0
306p = start
307while lt p endpos bound 9223372036854775807
308c = ulong get input p
309p = add p 1
310cp = c
311more = ulong 0
312if ge c 192
313more = ulong 1
314cp = and c 31
315if ge c 224
316more = ulong 2
317cp = and c 15
318end
319if ge c 240
320more = ulong 3
321cp = and c 7
322end
323end
324for j ulong more
325cp = or shl cp 6 and ulong get input p 63
326p = add p 1
327end
328decompose cp
329end
330n = recompose
331i = ulong 0
332while lt i n bound 9223372036854775807
333e = piece i n
334bpe i e
335i = e
336end
337end
338
339fn encodeinput n ulong to void
340st = ulong 0
341for i ulong n bound 9223372036854775807
342st = js_u8st st get input i
343if eq st 9
344exit 6
345end
346end
347if ne st 0
348exit 6
349end
350r = enlarge scalars mul 4 n
351if lt r 0
352exit 4
353end
354p = ulong 0
355s = ulong 0
356while lt p n bound 9223372036854775807
357id = special p n
358if ne id 0
359normalspan s p
360outid id
361p = add p ulong get sizes id
362s = p
363else
364p = add p 1
365end
366end
367normalspan s n
368end
369
370fn emitword id ulong to void
371for j ulong ulong get sizes id
372tkpc ulong get spelling add get starts id j
373end
374end
375
376fn decodeinput n ulong emit bool to void
377p = ulong 0
378while lt p n bound 9223372036854775807
379id = ulong 0
380digits = ulong 0
381while lt p n bound 9223372036854775807
382c = get input p
383if or lt c 48 gt c 57
384break
385end
386id = add mul id 10 sub ulong c 48
387if ge id get meta 1
388exit 7
389end
390p = add p 1
391digits = add digits 1
392end
393if or eq digits 0 eq get sizes id 0
394exit 7
395end
396if lt p n
397if ne get input p 10
398exit 7
399end
400p = add p 1
401end
402if emit
403emitword id
404end
405end
406end