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/omega-mv/blob/main/om/gpu/q36/serve/tokenizer.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / om / gpu / q36 / serve / tokenizer.om` 370 lines ยท 7011 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note Pure seeded OM-2 ByteLevel BPE tokenizer for Qwen 3.6 35B-A3B FP8 (reads tokenizer.json directly). 3 note Decodes any token ID 0..248319 to exact UTF-8 bytes and encodes prompts via longest-match 4 note over vocab + Qwen 3.6 chat template (<|im_start|>user\n...<|im_end|>\n<|im_start|>assistant\n). 5 6 const Q36_VOCAB_MAX ulong 248320 7 const Q36_STR_POOL ulong `8388608` 8 const Q36_TOK_BUF ulong `16777216` 9 10 global q36TokOff uint 248320 11 global q36TokLen ushort 248320 12 global q36TokPool uchar `8388608` 13 global q36TokPoolPos ulong 1 14 global q36TokRev uchar 512 15 global q36TokFile uchar `16777216` 16 global q36TokFileLen ulong 1 17 18 fn q36TokInitRev to void 19 for i ulong 512 20 put q36TokRev i 0 21 end 22 for b ulong 256 23 if or or and ge b 33 le b 126 and ge b 161 le b 172 and ge b 174 le b 255 24 put q36TokRev b uchar b 25 end 26 end 27 n = ulong 0 28 for b ulong 256 29 if not or or and ge b 33 le b 126 and ge b 161 le b 172 and ge b 174 le b 255 30 put q36TokRev add 256 n uchar b 31 n = add n 1 32 end 33 end 34 end 35 36 fn q36TokHex c uchar to ulong 37 if and ge c 48 le c 57 38 ret ulong sub c 48 39 end 40 if and ge c 97 le c 102 41 ret add 10 ulong sub c 97 42 end 43 if and ge c 65 le c 70 44 ret add 10 ulong sub c 65 45 end 46 ret 0 47 end 48 49 fn q36TokUtf3 c uchar p ulong to ulong 50 a = shl ulong and c 15 12 51 b = shl ulong and get q36TokFile p 63 6 52 d = ulong and get q36TokFile add p 1 63 53 ret or or a b d 54 end 55 56 fn q36TokHex4 p ulong to ulong 57 a = shl q36TokHex get q36TokFile p 12 58 b = shl q36TokHex get q36TokFile add p 1 8 59 c = shl q36TokHex get q36TokFile add p 2 4 60 d = q36TokHex get q36TokFile add p 3 61 ret or or a b or c d 62 end 63 64 fn q36TokReadStr pos ulong lim ulong outStart edit ulong 2 to ulong 65 st = get q36TokPoolPos 0 66 put outStart 0 st 67 p = pos 68 while lt p lim bound 4096 69 c = get q36TokFile p 70 if eq c 34 71 put outStart 1 sub get q36TokPoolPos 0 st 72 ret add p 1 73 end 74 cp = ulong c 75 p = add p 1 76 if eq c 92 77 if ge p lim 78 break 79 end 80 e = get q36TokFile p 81 p = add p 1 82 if eq e 110 83 cp = ulong 10 84 else 85 if eq e 114 86 cp = ulong 13 87 else 88 if eq e 116 89 cp = ulong 9 90 else 91 if eq e 34 92 cp = ulong 34 93 else 94 if eq e 92 95 cp = ulong 92 96 else 97 if eq e 117 98 if le add p 4 lim 99 cp = q36TokHex4 p 100 p = add p 4 101 end 102 else 103 cp = ulong e 104 end 105 end 106 end 107 end 108 end 109 end 110 else 111 if ge c 192 112 if and lt c 224 lt p lim 113 c2 = get q36TokFile p 114 p = add p 1 115 cp = or shl ulong and c 31 6 ulong and c2 63 116 else 117 if and lt c 240 le add p 2 lim 118 cp = q36TokUtf3 c p 119 p = add p 2 120 end 121 end 122 end 123 end 124 if and lt cp 512 lt get q36TokPoolPos 0 Q36_STR_POOL 125 put q36TokPool get q36TokPoolPos 0 get q36TokRev cp 126 put q36TokPoolPos 0 add get q36TokPoolPos 0 1 127 end 128 end 129 put outStart 1 sub get q36TokPoolPos 0 st 130 ret p 131 end 132 133 fn q36TokParseU64 pos ulong lim ulong dst edit ulong 2 to ulong 134 v = ulong 0 135 p = pos 136 while lt p lim bound 64 137 c = get q36TokFile p 138 if and ne c 32 and ne c 58 and ne c 10 ne c 9 139 break 140 end 141 p = add p 1 142 end 143 while lt p lim bound 32 144 c = get q36TokFile p 145 if or lt c 48 gt c 57 146 break 147 end 148 v = add mul v 10 ulong sub c 48 149 p = add p 1 150 end 151 put dst 0 v 152 put dst 1 p 153 ret p 154 end 155 156 fn q36TokReadFile path view uchar * plen ulong to bool 157 fd = fopen path 0 plen 0 0 158 if lt fd 0 159 ret false 160 end 161 sz = fseek int fd 0 2 162 if or le sz 0 gt ulong sz Q36_TOK_BUF 163 rc = fclose int fd 164 ret false 165 end 166 fpos = fseek int fd 0 0 167 done = ulong 0 168 while lt done ulong sz bound 65536 169 r = read int fd q36TokFile done sub ulong sz done 170 if eq r -4 171 continue 172 end 173 if le r 0 174 rc = fclose int fd 175 ret false 176 end 177 done = add done ulong r 178 end 179 rc = fclose int fd 180 put q36TokFileLen 0 done 181 ret true 182 end 183 184 fn q36TokFindVocab done ulong to ulong 185 p = ulong 0 186 lim = sub done 12 187 while lt p lim bound `16777216` 188 if eq get q36TokFile p 34 189 if eq get q36TokFile add p 1 118 190 if eq get q36TokFile add p 2 111 191 if eq get q36TokFile add p 3 99 192 if eq get q36TokFile add p 4 97 193 if eq get q36TokFile add p 5 98 194 if eq get q36TokFile add p 6 34 195 p = add p 7 196 while and lt p done ne get q36TokFile p 123 bound 64 197 p = add p 1 198 end 199 ret add p 1 200 end 201 end 202 end 203 end 204 end 205 end 206 end 207 p = add p 1 208 end 209 ret done 210 end 211 212 fn q36TokLoad path view uchar * plen ulong to bool 213 q36TokInitRev 214 for i ulong Q36_VOCAB_MAX 215 put q36TokLen i 0 216 put q36TokOff i 0 217 end 218 put q36TokPoolPos 0 0 219 if not q36TokReadFile path plen 220 ret false 221 end 222 done = get q36TokFileLen 0 223 p = q36TokFindVocab done 224 if ge p done 225 ret false 226 end 227 sp = new ulong 2 0 228 num = new ulong 2 0 229 while lt p done bound `16777216` 230 c = get q36TokFile p 231 if eq c 125 232 break 233 end 234 if eq c 34 235 p = q36TokReadStr add p 1 done sp 236 p = q36TokParseU64 p done num 237 tid = get num 0 238 if lt tid Q36_VOCAB_MAX 239 put q36TokOff tid uint get sp 0 240 put q36TokLen tid ushort get sp 1 241 end 242 else 243 p = add p 1 244 end 245 end 246 q36TokBuildIndex 247 ret true 248 end 249 250 global q36TokOutIds uint 131072 251 global q36TokInBuf uchar 524288 252 global q36TokSingle uint 256 253 global q36TokHead uint 65536 254 global q36TokNext uint 248320 255 256 fn q36TokBuildIndex to void 257 for b ulong 256 258 put q36TokSingle b 248320 259 end 260 for k ulong 65536 261 put q36TokHead k 248320 262 end 263 for i ulong 248320 264 put q36TokNext i 248320 265 end 266 for rev ulong 248044 267 tid = sub 248043 rev 268 tl = ulong get q36TokLen tid 269 if eq tl 1 270 o = ulong get q36TokOff tid 271 c0 = ulong get q36TokPool o 272 put q36TokSingle c0 uint tid 273 end 274 if ge tl 2 275 o = ulong get q36TokOff tid 276 c0 = ulong get q36TokPool o 277 c1 = ulong get q36TokPool add o 1 278 key = or shl c0 8 c1 279 put q36TokNext tid get q36TokHead key 280 put q36TokHead key uint tid 281 end 282 end 283 end 284 285 fn q36TokMatchGlobal pos ulong o ulong tl ulong to bool 286 for j ulong tl bound 24 287 if ne get q36TokPool add o j get q36TokInBuf add pos j 288 ret false 289 end 290 end 291 ret true 292 end 293 294 fn q36TokBestGlobal pos ulong maxTry ulong res edit ulong 2 to void 295 bestId = ulong 0 296 bestLen = ulong 0 297 c0 = ulong get q36TokInBuf pos 298 s1 = ulong get q36TokSingle c0 299 if lt s1 248044 300 bestId = s1 301 bestLen = ulong 1 302 end 303 if ge maxTry 2 304 c1 = ulong get q36TokInBuf add pos 1 305 key = or shl c0 8 c1 306 tid = ulong get q36TokHead key 307 while lt tid 248044 bound 8192 308 tl = ulong get q36TokLen tid 309 if and gt tl bestLen le tl maxTry 310 o = ulong get q36TokOff tid 311 if q36TokMatchGlobal pos o tl 312 bestId = tid 313 bestLen = tl 314 if eq bestLen maxTry 315 break 316 end 317 end 318 end 319 tid = ulong get q36TokNext tid 320 end 321 end 322 put res 0 bestId 323 put res 1 bestLen 324 end 325 326 note Wrap q36TokInBuf[0..tlen-1] in Qwen 3.6 chat template -> q36TokOutIds[0..131071] (full 128K context!) 327 fn q36TokEncodeChatGlobal tlen ulong to ulong 328 res = new ulong 2 0 329 cnt = ulong 0 330 put q36TokOutIds cnt 248045 331 cnt = add cnt 1 332 put q36TokOutIds cnt 846 333 cnt = add cnt 1 334 put q36TokOutIds cnt 198 335 cnt = add cnt 1 336 pos = ulong 0 337 while and lt pos tlen lt cnt 131060 bound 524288 338 maxTry = sub tlen pos 339 if gt maxTry 24 340 maxTry = ulong 24 341 end 342 q36TokBestGlobal pos maxTry res 343 bestLen = get res 1 344 if eq bestLen 0 345 bestLen = ulong 1 346 end 347 put q36TokOutIds cnt uint get res 0 348 cnt = add cnt 1 349 pos = add pos bestLen 350 end 351 put q36TokOutIds cnt 248046 352 cnt = add cnt 1 353 put q36TokOutIds cnt 198 354 cnt = add cnt 1 355 put q36TokOutIds cnt 248045 356 cnt = add cnt 1 357 put q36TokOutIds cnt 74455 358 cnt = add cnt 1 359 put q36TokOutIds cnt 198 360 cnt = add cnt 1 361 put q36TokOutIds cnt 248068 362 cnt = add cnt 1 363 put q36TokOutIds cnt 271 364 cnt = add cnt 1 365 put q36TokOutIds cnt 248069 366 cnt = add cnt 1 367 put q36TokOutIds cnt 271 368 cnt = add cnt 1 369 ret cnt 370 end