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