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/flash/serve.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 / flash / serve.om` 600 lines ยท 11989 bytes Raw Blame History 1 note flash-serve 38 MODEL_DIR PORT KEYFILE [SLOTS]: one Flash-Next text model behind HTTP/1.1 on 127.0.0.1. 2 note Writes `PORT n` to stderr once the model is loaded and the socket listens. 3 note GET /health 200 application/json 4 note POST /v1/chat/completions messages, max_tokens (default 256, at most 1024), stream; Authorization: Bearer KEY 5 note One request at a time. Each request is a fresh sequence. Greedy decoding, reasoning off. 6 text snhealth "/health" 7 text snchat "/v1/chat/completions" 8 text sget "GET" 9 text spost "POST" 10 text sjson "application/json" 11 text sctype "Content-Type" 12 text sauth "authorization" 13 text sbearer "Bearer " 14 text shealth "{\"status\":\"ok\",\"model\":\"qwen3.8-flash-next-fp8\",\"engine\":\"omega\"}" 15 text skmsg "messages" 16 text skrole "role" 17 text skcontent "content" 18 text skmax "max_tokens" 19 text skstream "stream" 20 text sims "<|im_start|>" 21 text sime "<|im_end|>\n" 22 text sgen "<|im_start|>assistant\n\n\n\n\n" 23 text sport "PORT " 24 text sc0 "{\"id\":\"flash-next\",\"object\":\"chat.completion.chunk\",\"model\":\"qwen3.8-flash-next-fp8\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"" 25 text sc1 "\"},\"finish_reason\":null}]}" 26 text sf0 "{\"id\":\"flash-next\",\"object\":\"chat.completion.chunk\",\"model\":\"qwen3.8-flash-next-fp8\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"" 27 text sf1 "\"}],\"usage\":{\"prompt_tokens\":" 28 text sstop "stop" 29 text slength "length" 30 text su1 ",\"completion_tokens\":" 31 text su2 ",\"total_tokens\":" 32 text su3 "},\"timings\":{\"first_token_ms\":" 33 text su4 ",\"decode_ms\":" 34 text su5 ",\"expert_loads\":" 35 text su6 ",\"expert_evictions\":" 36 text su7 "}}" 37 text sdone "[DONE]" 38 text sm0 "{\"id\":\"flash-next\",\"object\":\"chat.completion\",\"model\":\"qwen3.8-flash-next-fp8\",\"choices\":[{\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\"" 39 text sm1 "\"},\"finish_reason\":\"" 40 global skey uchar 160 41 note sreq: 0 max tokens, 1 stream, 2 rendered bytes, 3 start ns, 4 first-token ns, 5 end ns, 6 completion tokens, 7 stopped at end of turn, 8 key bytes. 42 global sreq ulong 9 43 global sacc uchar 4096 44 global saccn ulong 1 45 46 fn sputc c uchar to bool 47 n = get sreq 2 48 if ge n len input 49 r = enlarge input add mul 2 n 4096 50 if lt r 0 51 ret false 52 end 53 end 54 put input n c 55 put sreq 2 add n 1 56 ret true 57 end 58 59 fn sputs t view uchar * to bool 60 for i ulong len t 61 if not sputc get t i 62 ret false 63 end 64 end 65 ret true 66 end 67 68 note Value token of member name in object obj, or jsnone. 69 fn sfind src view uchar `1048576` tape edit uint 262160 obj ulong name view uchar * to ulong 70 if ne js_type tape obj jsobj 71 ret jsnone 72 end 73 k = js_first tape obj 74 while ne k jsnone bound 4096 75 if js_streq src js_off tape k name 76 ret add k 1 77 end 78 k = js_mnext tape k 79 end 80 ret jsnone 81 end 82 83 fn smessage src view uchar `1048576` tape edit uint 262160 m ulong txt edit uchar `1048576` to bool 84 r = sfind src tape m skrole 85 c = sfind src tape m skcontent 86 if or eq r jsnone eq c jsnone 87 ret false 88 end 89 if or ne js_type tape r jsstr ne js_type tape c jsstr 90 ret false 91 end 92 n = js_sget src js_off tape r txt 93 if or le n 0 gt n 16 94 ret false 95 end 96 ok = sputs sims 97 for i ulong ulong n 98 ch = get txt i 99 if or lt ch 97 gt ch 122 100 ret false 101 end 102 ok = and ok sputc ch 103 end 104 ok = and ok sputc 10 105 n = js_sget src js_off tape c txt 106 if lt n 0 107 ret false 108 end 109 for i ulong ulong n 110 ok = and ok sputc get txt i 111 end 112 ret and ok sputs sime 113 end 114 115 note Parse the request body of nb bytes and render the chat into the tokenizer input. False refuses the request. 116 fn srender src view uchar `1048576` tape edit uint 262160 nb ulong txt edit uchar `1048576` to bool 117 put sreq 0 256 118 put sreq 1 0 119 put sreq 2 0 120 if lt js_init tape nb 32 0 121 ret false 122 end 123 if lt js_parse src tape nb 0 124 ret false 125 end 126 v = sfind src tape 0 skmax 127 if ne v jsnone 128 if ne js_type tape v jsnum 129 ret false 130 end 131 if not js_isu src js_off tape v 132 ret false 133 end 134 mx = js_tou src js_off tape v 135 if or eq mx 0 gt mx 1024 136 ret false 137 end 138 put sreq 0 mx 139 end 140 v = sfind src tape 0 skstream 141 if ne v jsnone 142 if eq js_type tape v jstrue 143 put sreq 1 1 144 end 145 end 146 v = sfind src tape 0 skmsg 147 if eq v jsnone 148 ret false 149 end 150 if or ne js_type tape v jsarr eq js_nkids tape v 0 151 ret false 152 end 153 m = js_first tape v 154 while ne m jsnone bound 4096 155 if not smessage src tape m txt 156 ret false 157 end 158 m = js_enext tape m 159 end 160 ret sputs sgen 161 end 162 163 fn sdec b edit uchar 8192 p ulong v ulong to ulong 164 d = new uchar 20 0 165 i = ulong 20 166 x = v 167 for it ulong 20 168 i = sub i 1 169 put d i uchar add rem x 10 48 170 x = div x 10 171 if eq x 0 172 break 173 end 174 end 175 q = p 176 for k ulong sub 20 i 177 put b q get d add i k 178 q = add q 1 179 end 180 ret q 181 end 182 183 fn scat b edit uchar 8192 p ulong t view uchar * to ulong 184 for i ulong len t 185 put b add p i get t i 186 end 187 ret add p len t 188 end 189 190 note JSON string escape of one byte into b at p. 191 fn sesc b edit uchar 8192 p ulong c uchar to ulong 192 if or eq c 34 eq c 92 193 put b p 92 194 put b add p 1 c 195 ret add p 2 196 end 197 if ge c 32 198 put b p c 199 ret add p 1 200 end 201 put b p 92 202 put b add p 1 117 203 put b add p 2 48 204 put b add p 3 48 205 put b add p 4 hhexd shr ulong c 4 206 put b add p 5 hhexd and ulong c 15 207 ret add p 6 208 end 209 210 note Bytes at the start of sacc that end on a UTF-8 character boundary. 211 fn swhole to ulong 212 n = get saccn 0 213 back = ulong 0 214 while and lt back 4 lt back n bound 4 215 c = get sacc sub sub n 1 back 216 if lt c 128 217 ret n 218 end 219 if ge c 192 220 need = ulong 2 221 if ge c 224 222 need = ulong 3 223 end 224 if ge c 240 225 need = ulong 4 226 end 227 if ge add back 1 need 228 ret n 229 end 230 ret sub sub n 1 back 231 end 232 back = add back 1 233 end 234 ret n 235 end 236 237 fn stoken id ulong to void 238 if ge id len sizes 239 ret 240 end 241 w = ulong get sizes id 242 n = get saccn 0 243 if gt add n w len sacc 244 ret 245 end 246 o = get starts id 247 for j ulong w 248 c = get spelling add o j 249 put sacc add n j c 250 end 251 put saccn 0 add n w 252 end 253 254 note Drop the first k bytes of sacc. 255 fn sdrop k ulong to void 256 n = get saccn 0 257 for j ulong sub n k 258 put sacc j get sacc add k j 259 end 260 put saccn 0 sub n k 261 end 262 263 note Usage and timing members shared by both response forms, appended to b at p. 264 fn susage b edit uchar 8192 p ulong to ulong 265 count = get qnids 0 266 made = get sreq 6 267 q = sdec b p count 268 q = scat b q su1 269 q = sdec b q made 270 q = scat b q su2 271 q = sdec b q add count made 272 q = scat b q su3 273 q = sdec b q div sub get sreq 4 get sreq 3 `1000000` 274 q = scat b q su4 275 q = sdec b q div sub get sreq 5 get sreq 4 `1000000` 276 q = scat b q su5 277 q = sdec b q get qecfg 4 278 q = scat b q su6 279 q = sdec b q get qecfg 5 280 ret scat b q su7 281 end 282 283 fn sreason b edit uchar 8192 p ulong to ulong 284 if eq get sreq 7 1 285 ret scat b p sstop 286 end 287 ret scat b p slength 288 end 289 290 note One streamed content event from the whole characters now in sacc. 291 fn sevent cs edit ulong 320 io edit uchar 131072 ev edit uchar 8192 to long 292 k = swhole 293 if eq k 0 294 ret 0 295 end 296 if gt k 1024 297 k = 1024 298 end 299 q = scat ev 0 sc0 300 for j ulong k 301 q = sesc ev q get sacc j 302 end 303 q = scat ev q sc1 304 sdrop k 305 r = hssedat cs io view part ev 0 q 306 ret hfe r hsseend cs io 307 end 308 309 note sst: 0 current token, 1 escaped text bytes gathered in txt. 310 global sst ulong 2 311 fn sprefill to void 312 count = get qnids 0 313 put sreq 3 ulong monons 314 put sreq 6 0 315 put sreq 7 0 316 put saccn 0 0 317 put sst 1 0 318 id = ulong 0 319 for j ulong count 320 id = qtoken ulong get qids j j eq add j 1 count 321 end 322 put sst 0 id 323 put sreq 4 ulong monons 324 end 325 326 fn sgather ev edit uchar 8192 txt edit uchar `1048576` to bool 327 k = get saccn 0 328 n = get sst 1 329 if gt add n mul k 6 len txt 330 ret false 331 end 332 q = ulong 0 333 for i ulong k 334 q = sesc ev q get sacc i 335 end 336 for i ulong q 337 put txt add n i get ev i 338 end 339 put sst 1 add n q 340 put saccn 0 0 341 ret true 342 end 343 344 note Prefill the prompt, then decode. With stream set each token is sent as it is made; otherwise text gathers in txt. 345 fn sdecode cs edit ulong 320 io edit uchar 131072 ev edit uchar 8192 txt edit uchar `1048576` to long 346 sprefill 347 count = get qnids 0 348 for j ulong get sreq 0 349 id = get sst 0 350 if or eq id 248044 eq id 248046 351 put sreq 7 1 352 break 353 end 354 put sreq 6 add j 1 355 stoken id 356 if eq get sreq 1 1 357 r = sevent cs io ev 358 if lt r 0 359 ret r 360 end 361 else 362 if not sgather ev txt 363 ret HEBIG 364 end 365 end 366 if lt add j 1 get sreq 0 367 put sst 0 qtoken id add count j true 368 end 369 end 370 put sreq 5 ulong monons 371 ret long get sst 1 372 end 373 374 fn sfinish cs edit ulong 320 io edit uchar 131072 ev edit uchar 8192 to long 375 q = scat ev 0 sf0 376 q = sreason ev q 377 q = scat ev q sf1 378 q = susage ev q 379 r = hssedat cs io view part ev 0 q 380 r = hfe r hsseend cs io 381 r = hfe r hssedat cs io sdone 382 r = hfe r hsseend cs io 383 ret hfe r hwfin cs io 384 end 385 386 fn swhole1 cs edit ulong 320 io edit uchar 131072 ev edit uchar 8192 txt edit uchar `1048576` n ulong to long 387 q = scat ev 0 sm1 388 q = sreason ev q 389 q = scat ev q sf1 390 q = susage ev q 391 r = hrspst cs io 200 392 r = hfe r hhname cs io sctype 393 r = hfe r hhval cs io sjson 394 r = hfe r hhend cs io 0 add add len sm0 n q 395 r = hfe r hput cs io sm0 396 r = hfe r hput cs io view part txt 0 n 397 r = hfe r hput cs io view part ev 0 q 398 ret hfe r hwfin cs io 399 end 400 401 fn sauthorized cs edit ulong 320 io edit uchar 131072 to bool 402 i = hhfind cs io sauth 403 if lt i 0 404 ret false 405 end 406 o = hhvoff cs ulong i 407 n = hhvlen cs ulong i 408 kn = get sreq 8 409 if ne n add len sbearer kn 410 ret false 411 end 412 diff = ulong 0 413 for j ulong len sbearer 414 diff = or diff ulong xor get io add o j get sbearer j 415 end 416 for j ulong kn 417 diff = or diff ulong xor get io add add o len sbearer j get skey j 418 end 419 ret eq diff 0 420 end 421 422 fn schat cs edit ulong 320 io edit uchar 131072 body edit uchar `1048576` tape edit uint 262160 to long 423 txt = new uchar `1048576` 0 424 ev = new uchar 8192 0 425 if not sauthorized cs io 426 ret hrspempty cs io 401 427 end 428 put cs HBCAP `1048576` 429 nb = hbodyall cs io body 430 if lt nb 0 431 if eq nb HEBIG 432 ret hrsperr cs io 413 433 end 434 ret hrsperr cs io 400 435 end 436 if not srender body tape ulong nb txt 437 ret hrsperr cs io 400 438 end 439 put qnids 0 0 440 encodeinput get sreq 2 441 count = get qnids 0 442 if or eq count 0 gt add count sub get sreq 0 1 2048 443 ret hrsperr cs io 413 444 end 445 qdiskrecheck 446 if eq get sreq 1 1 447 r = hrspsse cs io 448 if lt r 0 449 ret r 450 end 451 r = sdecode cs io ev txt 452 if lt r 0 453 ret r 454 end 455 r = sevent cs io ev 456 ret sfinish cs io ev 457 end 458 r = sdecode cs io ev txt 459 if lt r 0 460 ret hrsperr cs io 500 461 end 462 ret swhole1 cs io ev txt ulong r 463 end 464 465 fn shealthy cs edit ulong 320 io edit uchar 131072 to long 466 r = hrspst cs io 200 467 r = hfe r hhname cs io sctype 468 r = hfe r hhval cs io sjson 469 r = hfe r hhend cs io 0 len shealth 470 r = hfe r hput cs io shealth 471 ret hfe r hwfin cs io 472 end 473 474 fn sroute cs edit ulong 320 io edit uchar 131072 body edit uchar `1048576` tape edit uint 262160 to long 475 if hispath cs io snhealth 476 if hismeth cs io sget 477 ret shealthy cs io 478 end 479 ret hrspempty cs io 405 480 end 481 if hispath cs io snchat 482 if hismeth cs io spost 483 ret schat cs io body tape 484 end 485 ret hrspempty cs io 405 486 end 487 ret hrspempty cs io 404 488 end 489 490 fn sserve lf int cs edit ulong 320 io edit uchar 131072 body edit uchar `1048576` tape edit uint 262160 to void 491 c = haccept lf `30000000000` 492 if lt c 0 493 ret 494 end 495 r = hinit cs io int c 1 496 if lt r 0 497 x = hclose cs 498 ret 499 end 500 hsettmo cs `10000000000` `10000000000` 501 hsetbdl cs `3600000000000` 502 for k ulong 100 503 r = hreqrd cs io 504 if le r 0 505 if lt r 0 506 x = hreject cs io r 507 end 508 break 509 end 510 r = sroute cs io body tape 511 if lt r 0 512 break 513 end 514 if eq hnext cs io 0 515 break 516 end 517 end 518 r = hclose cs 519 end 520 521 fn skeyload to bool 522 path = new uchar 4096 0 523 n = argread 4 path 0 4095 524 if le n 0 525 ret false 526 end 527 fd = fopen path 0 ulong n 0 0 528 if lt fd 0 529 ret false 530 end 531 r = read int fd skey 0 160 532 x = fclose int fd 533 if lt r 16 534 ret false 535 end 536 k = ulong r 537 while gt k 0 bound 160 538 c = get skey sub k 1 539 if and ne c 10 ne c 13 540 break 541 end 542 k = sub k 1 543 end 544 if or lt k 16 ge k 160 545 ret false 546 end 547 put sreq 8 k 548 ret true 549 end 550 551 fn main to int 552 cs = new ulong 320 0 553 io = new uchar 131072 0 554 body = new uchar `1048576` 0 555 tape = new uint 262160 0 556 a = new uchar 16 0 557 if or lt argcnt 5 gt argcnt 6 558 ret 2 559 end 560 if ne qnumericarg 1 38 561 ret 2 562 end 563 port = qnumericarg 3 564 if or lt port 1024 gt port 65535 565 ret 2 566 end 567 slots = ulong 8320 568 if eq argcnt 6 569 slots = qnumericarg 5 570 if or lt slots 16 gt slots 26624 571 ret 2 572 end 573 end 574 if not skeyload 575 ret 2 576 end 577 put qecfg 1 slots 578 loadmodel 579 if ne get qtidentity 0 27 580 qwfail 59 581 end 582 unicodeinit 583 qwload 2 48 584 qprofile 48 585 put qrun 0 38 586 qgpuopen 587 r = haddr4 a `2130706433` port 588 lf = hlisten a 16 589 if lt lf 0 590 ret 3 591 end 592 w = write 2 sport 0 len sport 593 fstatnum hport a 594 nl = new uchar 1 10 595 w = write 2 nl 0 1 596 while true 597 sserve int lf cs io body tape 598 end 599 ret 0 600 end