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/serve_run.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 / serve_run.om` 1146 lines ยท 22325 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note 100% Pure Seeded-Omega Zero-CUDA Real-Weight Qwen 3.6 35B-A3B HTTP Server + Live Arboris (q36-serve). 3 note ZERO libcuda.so.1, ZERO libc, ZERO Python, ZERO C, ZERO Rust. 4 note Defaults to Full-W4 mode (67.76 tok/s, ~19.5 GB VRAM); pass --fp8 for Exact FP8 (48.01 tok/s). 5 note Endpoints: 6 note GET /health (status, model, mode, live Arboris transitions/hits/hit_pct) 7 note GET /v1/models (OpenAI model list: qwen3.6-35b-a3b-fp8) 8 note POST /v1/chat/completions (OpenAI chat completion with live Arboris tracking) 9 note 10 note Usage: 11 note q36-serve [--model-dir PATH] [--port PORT] [--fp8 | --w4] [--self-test] 12 13 global qsReqBuf uchar 524288 14 global qsRespBuf uchar 262144 15 global qsRespLen ulong 1 16 global qsOutTxt uchar 131072 17 global qsOutLen ulong 1 18 global qsPort ulong 1 19 global qsSelfTest ulong 1 20 global qsPendPreArb ulong 1 21 global qsStreamFd int 1 22 23 text qsDefDir "/home/drakestapleton/models/qwen3.6-35b-a3b-fp8-`95a723d0`" 24 text qsSufTok "/tokenizer.json" 25 text qsBanner "=== QWEN 3.6 35B-A3B 100% PURE SEEDED-OMEGA ZERO-CUDA SERVER + LIVE ARBORIS (q36-serve) ===\n" 26 text qsModeW4 "Mode: Full-W4 (67.76 tok/s class, ~19.5 GB VRAM) + Live Arboris\n" 27 text qsModeFp8 "Mode: Exact FP8 (48.01 tok/s class, ~34.7 GB VRAM) + Live Arboris\n" 28 text qsTokOk "Loaded Qwen 3.6 ByteLevel BPE tokenizer (248,044 vocab)\n" 29 text qsTokFail "FAIL: could not load tokenizer.json\n" 30 text qsListenMsg "Listening on HTTP port " 31 text qsListenEnd " (GET /health, GET /v1/models, POST /v1/chat/completions)\n" 32 text qsSelfHdr "Running live real-weight q36-serve + Arboris self-test...\n" 33 text qsP0 "What is the capital of France?" 34 text qsP1 "What is 2 + 2?" 35 text qsL0 "Prompt 0: " 36 text qsL1 "Prompt 1: " 37 text qsArbLbl "Live Arboris: hits=" 38 text qsArbMid " / tot=" 39 text qsArbPct " hit_pct=" 40 text qsSelfOk "PASS: q36-serve real-weight 35B + live Arboris self-test\n" 41 text qsSelfBad "FAIL: q36-serve self-test\n" 42 43 text qsH200 "HTTP/1.1 200 OK\nContent-Type: application/json\nConnection: close\n\n" 44 text qsH200Sse "HTTP/1.1 200 OK\nContent-Type: text/event-stream\nCache-Control: no-cache\nConnection: close\n\n" 45 text qsH404 "HTTP/1.1 404 Not Found\nContent-Type: application/json\nConnection: close\n\n{\"error\":{\"type\":\"not_found\"}}\n" 46 text qsHealthA "{\"status\":\"ok\",\"model\":\"qwen3.6-35b-a3b-fp8\",\"engine\":\"omega-zero-cuda\",\"context_window\":131072,\"precision\":\"" 47 text qsPrecW4 "w4" 48 text qsPrecFp8 "fp8" 49 text qsHealthB "\",\"arboris_enabled\":true,\"arboris_hits\":" 50 text qsHealthC ",\"arboris_total\":" 51 text qsHealthD ",\"arboris_hit_pct\":" 52 text qsHealthE "}\n" 53 text qsModels "{\"object\":\"list\",\"data\":[{\"id\":\"qwen3.6-35b-a3b-fp8\",\"object\":\"model\",\"owned_by\":\"omega-zero-cuda\",\"context_window\":131072}]}\n" 54 text qsChatA "{\"id\":\"cmpl-q36\",\"object\":\"chat.completion\",\"model\":\"qwen3.6-35b-a3b-fp8\",\"choices\":[{\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\"" 55 text qsChatB "\"},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":" 56 text qsChatC ",\"completion_tokens\":" 57 text qsChatD ",\"total_tokens\":" 58 text qsChatE "},\"arboris\":{\"hits\":" 59 text qsChatF ",\"total\":" 60 text qsChatG ",\"hit_pct\":" 61 text qsChatH "},\"timings\":{\"prefill_ms\":" 62 text qsChatI ",\"decode_ms\":" 63 text qsChatJ "}}\n" 64 text qsSseData "data: " 65 text qsSseDeltaA "{\"id\":\"cmpl-q36\",\"object\":\"chat.completion.chunk\",\"model\":\"qwen3.6-35b-a3b-fp8\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"" 66 text qsSseDeltaB "\"},\"finish_reason\":null}]}\n\n" 67 text qsSseFinalA "{\"id\":\"cmpl-q36\",\"object\":\"chat.completion.chunk\",\"model\":\"qwen3.6-35b-a3b-fp8\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":" 68 text qsSseDone "data: [DONE]\n\n" 69 text qsKeyContent "\"content\"" 70 text qsKeyMaxTok "\"max_tokens\"" 71 text qsKeyStream "\"stream\"" 72 text qsKeyClen "content-length:" 73 74 fn qsRespReset to void 75 put qsRespLen 0 0 76 end 77 78 fn qsRespAppendHdr s view uchar * n ulong to void 79 p = get qsRespLen 0 80 for i ulong n bound 512 81 c = get s i 82 if and eq c 10 lt p 65520 83 put qsRespBuf p 13 84 p = add p 1 85 end 86 if lt p 262100 87 put qsRespBuf p c 88 p = add p 1 89 end 90 end 91 put qsRespLen 0 p 92 end 93 94 fn qsRespAppend s view uchar * n ulong to void 95 p = get qsRespLen 0 96 for i ulong n bound 16384 97 if lt p 262100 98 put qsRespBuf p get s i 99 p = add p 1 100 end 101 end 102 put qsRespLen 0 p 103 end 104 105 fn qsRespAppendU v ulong to void 106 tmp = new uchar 24 0 107 p = get qsRespLen 0 108 if eq v 0 109 if lt p 262100 110 put qsRespBuf p 48 111 put qsRespLen 0 add p 1 112 end 113 ret 114 end 115 n = ulong 0 116 x = v 117 while gt x 0 bound 24 118 put tmp n uchar add 48 rem x 10 119 x = div x 10 120 n = add n 1 121 end 122 for i ulong n bound 24 123 if lt p 262100 124 put qsRespBuf p get tmp sub sub n 1 i 125 p = add p 1 126 end 127 end 128 put qsRespLen 0 p 129 end 130 131 fn qsRespAppendEscByte c uchar to void 132 p = get qsRespLen 0 133 if ge p 262096 134 ret 135 end 136 if or eq c 34 eq c 92 137 put qsRespBuf p 92 138 put qsRespBuf add p 1 c 139 put qsRespLen 0 add p 2 140 ret 141 end 142 if eq c 10 143 put qsRespBuf p 92 144 put qsRespBuf add p 1 110 145 put qsRespLen 0 add p 2 146 ret 147 end 148 if eq c 13 149 put qsRespBuf p 92 150 put qsRespBuf add p 1 114 151 put qsRespLen 0 add p 2 152 ret 153 end 154 if eq c 9 155 put qsRespBuf p 92 156 put qsRespBuf add p 1 116 157 put qsRespLen 0 add p 2 158 ret 159 end 160 if eq c 8 161 put qsRespBuf p 92 162 put qsRespBuf add p 1 98 163 put qsRespLen 0 add p 2 164 ret 165 end 166 if eq c 12 167 put qsRespBuf p 92 168 put qsRespBuf add p 1 102 169 put qsRespLen 0 add p 2 170 ret 171 end 172 if ge c 32 173 put qsRespBuf p c 174 put qsRespLen 0 add p 1 175 end 176 end 177 178 fn qsRespAppendJsonEsc n ulong to void 179 for i ulong n bound 131072 180 qsRespAppendEscByte get qsOutTxt i 181 end 182 end 183 184 fn qsSendSseDelta fd int tid ulong to void 185 if ge tid Q36_VOCAB_MAX 186 ret 187 end 188 l = ulong get q36TokLen tid 189 if eq l 0 190 ret 191 end 192 off = ulong get q36TokOff tid 193 qsRespReset 194 qsRespAppend qsSseData len qsSseData 195 qsRespAppend qsSseDeltaA len qsSseDeltaA 196 for i ulong l bound 64 197 qsRespAppendEscByte get q36TokPool add off i 198 end 199 qsRespAppend qsSseDeltaB len qsSseDeltaB 200 w = write fd qsRespBuf 0 get qsRespLen 0 201 end 202 203 fn qsAppendTok tid ulong to void 204 if ge tid Q36_VOCAB_MAX 205 ret 206 end 207 l = ulong get q36TokLen tid 208 if eq l 0 209 ret 210 end 211 off = ulong get q36TokOff tid 212 cur = get qsOutLen 0 213 for i ulong l bound 64 214 if lt cur 131000 215 put qsOutTxt cur get q36TokPool add off i 216 cur = add cur 1 217 end 218 end 219 put qsOutLen 0 cur 220 end 221 222 fn qsArbHitPct to ulong 223 tot = get qdArbTot 0 224 if eq tot 0 225 ret 0 226 end 227 ret div mul get qdArbHits 0 100 tot 228 end 229 230 fn qsPrefill nTok ulong to ulong 231 if and eq get qdUseW4 0 1 ge nTok 2 232 pos = ulong 0 233 prevChunk = ulong 0 234 hasPrev = false 235 while lt pos nTok bound 1024 236 chunk = sub nTok pos 237 if gt chunk QP_MAX_B 238 chunk = QP_MAX_B 239 end 240 isFinal = eq add pos chunk nTok 241 if hasPrev 242 if not qdWaitPre 243 ret 999999 244 end 245 okSnap = qdPreSnapTrace prevChunk 246 for j ulong chunk bound 512 247 put qdPreTokIds j get q36TokOutIds add pos j 248 end 249 if not qdPreKickChunk chunk pos isFinal 250 ret 999999 251 end 252 if okSnap 253 qdPreUpdateArborisSnap prevChunk 254 end 255 else 256 for j ulong chunk bound 512 257 put qdPreTokIds j get q36TokOutIds add pos j 258 end 259 if not qdPreKickChunk chunk pos isFinal 260 ret 999999 261 end 262 end 263 prevChunk = chunk 264 hasPrev = true 265 pos = add pos chunk 266 end 267 if not qdWaitPre 268 ret 999999 269 end 270 put qsPendPreArb 0 0 271 if qdPreSnapTrace prevChunk 272 put qsPendPreArb 0 prevChunk 273 end 274 ret qdPrePickBest prevChunk false 275 end 276 put qsPendPreArb 0 0 277 nextTok = ulong 0 278 for p ulong nTok bound 131072 279 isLast = eq add p 1 nTok 280 nextTok = qdForwardToken ulong get q36TokOutIds p p isLast false 281 if eq nextTok 999999 282 ret 999999 283 end 284 end 285 ret nextTok 286 end 287 288 fn qsFlushPendPreArb to void 289 n = get qsPendPreArb 0 290 if gt n 0 291 put qsPendPreArb 0 0 292 qdPreUpdateArborisSnap n 293 end 294 end 295 296 fn qsEmitTokStep tid ulong to void 297 qsAppendTok tid 298 sfd = get qsStreamFd 0 299 if ge sfd 0 300 qsSendSseDelta sfd tid 301 end 302 end 303 304 fn qsIsStopTok tid ulong to bool 305 if eq tid 248046 306 ret true 307 end 308 ret eq tid 248044 309 end 310 311 global qsSpecCnt ulong 2 312 313 fn qsSpecEmitAcc acc ulong cap ulong to bool 314 g = get qsSpecCnt 0 315 for k ulong acc bound 16 316 tokK = get qdSpecPred k 317 if qsIsStopTok tokK 318 put qsSpecCnt 0 g 319 ret true 320 end 321 if lt g cap 322 qsEmitTokStep tokK 323 g = add g 1 324 h2 = get qdHistLen 0 325 if lt h2 131070 326 put qdHistToks h2 tokK 327 put qdHistLen 0 add h2 1 328 end 329 end 330 end 331 put qsSpecCnt 0 g 332 ret false 333 end 334 335 fn qsDecodeLoopSpec firstTok ulong nTok ulong cap ulong to ulong 336 M = get qdSpecM 0 337 qsFlushPendPreArb 338 hl = nTok 339 if gt hl 131000 340 hl = ulong 131000 341 end 342 for p ulong hl bound 131072 343 put qdHistToks p ulong get q36TokOutIds p 344 end 345 put qdHistToks hl firstTok 346 put qdHistLen 0 add hl 1 347 qsEmitTokStep firstTok 348 put qsSpecCnt 0 1 349 curTok = firstTok 350 pos = nTok 351 while lt get qsSpecCnt 0 cap bound 4096 352 if eq get qdMtpReady 0 1 353 if not qdMtpDraftChain curTok sub pos 1 M 354 qdSpecDraftNgram curTok M 355 end 356 else 357 qdSpecDraftNgram curTok M 358 end 359 acc = qdSpecVerifyStep M pos 360 if eq acc 0 361 break 362 end 363 if qsSpecEmitAcc acc cap 364 break 365 end 366 if eq get qdMtpReady 0 1 367 okH = qdMtpCopyMainHid acc 368 end 369 curTok = get qdSpecPred sub acc 1 370 pos = add pos acc 371 end 372 ret get qsSpecCnt 0 373 end 374 375 fn qsDecodeLoop firstTok ulong nTok ulong maxGen ulong to ulong 376 cap = maxGen 377 if gt add nTok cap QD_MAX_T 378 cap = sub QD_MAX_T nTok 379 end 380 if or eq cap 0 qsIsStopTok firstTok 381 qsFlushPendPreArb 382 ret 0 383 end 384 if and eq get qdUseW4 0 1 gt get qdSpecM 0 0 385 ret qsDecodeLoopSpec firstTok nTok cap 386 end 387 qsEmitTokStep firstTok 388 genCnt = ulong 1 389 if ge genCnt cap 390 qsFlushPendPreArb 391 ret genCnt 392 end 393 if not qdPrepAsyncDecode firstTok nTok 394 qsFlushPendPreArb 395 ret genCnt 396 end 397 if not qdKickPre 2 398 qsFlushPendPreArb 399 ret genCnt 400 end 401 note Overlap final prefill chunk Live Arboris update behind decode step 1 on GPU! 402 qsFlushPendPreArb 403 for g ulong sub cap 1 bound 16384 404 if not qdWaitPre 405 break 406 end 407 nextTok = qdReadOutTok 408 if eq nextTok 999999 409 break 410 end 411 okSnap = qdSnapTrace 412 if qsIsStopTok nextTok 413 if okSnap 414 qdUpdateArborisSnap 415 end 416 break 417 end 418 if lt add g 2 cap 419 if not qdKickPre 2 420 if okSnap 421 qdUpdateArborisSnap 422 end 423 qsEmitTokStep nextTok 424 genCnt = add genCnt 1 425 break 426 end 427 end 428 if okSnap 429 qdUpdateArborisSnap 430 end 431 qsEmitTokStep nextTok 432 genCnt = add genCnt 1 433 end 434 ret genCnt 435 end 436 437 fn qsGenerate tlen ulong maxGen ulong outCounts edit ulong 4 to bool 438 qdResetState 439 nTok = q36TokEncodeChatGlobal tlen 440 if eq nTok 0 441 ret false 442 end 443 put outCounts 0 nTok 444 put qsOutLen 0 0 445 t0 = monons 446 firstTok = qsPrefill nTok 447 if eq firstTok 999999 448 ret false 449 end 450 t1 = monons 451 genCnt = qsDecodeLoop firstTok nTok maxGen 452 t2 = monons 453 put outCounts 1 genCnt 454 put outCounts 2 div ulong sub t1 t0 `1000000` 455 put outCounts 3 div ulong sub t2 t1 `1000000` 456 ret true 457 end 458 459 fn qsMatchKey kbuf view uchar * pos ulong klen ulong to bool 460 for j ulong klen bound 16 461 if ne get qsReqBuf add pos j get kbuf j 462 ret false 463 end 464 end 465 ret true 466 end 467 468 fn qsScanQuoted p0 ulong n ulong outBounds edit ulong 2 to bool 469 p = p0 470 while and lt p n ne get qsReqBuf p 34 bound 64 471 p = add p 1 472 end 473 if ge p n 474 ret false 475 end 476 st = add p 1 477 q = st 478 while and lt q n ne get qsReqBuf q 34 bound 524288 479 if eq get qsReqBuf q 92 480 q = add q 2 481 else 482 q = add q 1 483 end 484 end 485 if gt q st 486 put outBounds 0 st 487 put outBounds 1 sub q st 488 ret true 489 end 490 ret false 491 end 492 493 fn qsFindLastContent n ulong outBounds edit ulong 2 to bool 494 put outBounds 0 0 495 put outBounds 1 0 496 found = false 497 if lt n 14 498 ret false 499 end 500 lim = sub n 10 501 for i ulong lim bound 524288 502 if qsMatchKey qsKeyContent i 9 503 if qsScanQuoted add i 9 n outBounds 504 found = true 505 end 506 end 507 end 508 ret found 509 end 510 511 fn qsParseReqU p0 ulong n ulong to ulong 512 p = p0 513 while and lt p n or eq get qsReqBuf p 32 eq get qsReqBuf p 58 bound 32 514 p = add p 1 515 end 516 v = ulong 0 517 while lt p n bound 16 518 c = get qsReqBuf p 519 if or lt c 48 gt c 57 520 break 521 end 522 v = add mul v 10 ulong sub c 48 523 p = add p 1 524 end 525 ret v 526 end 527 528 fn qsFindMaxTokens n ulong to ulong 529 if lt n 16 530 ret 512 531 end 532 lim = sub n 13 533 for i ulong lim bound 524288 534 if qsMatchKey qsKeyMaxTok i 12 535 v = qsParseReqU add i 12 n 536 if and gt v 0 le v 16384 537 ret v 538 end 539 end 540 end 541 ret 512 542 end 543 544 fn qsHexNib c uchar to ulong 545 if and ge c 48 le c 57 546 ret ulong sub c 48 547 end 548 if and ge c 97 le c 102 549 ret ulong add 10 sub c 97 550 end 551 if and ge c 65 le c 70 552 ret ulong add 10 sub c 65 553 end 554 ret 16 555 end 556 557 fn qsDecodeHexU4 p ulong to long 558 h0 = qsHexNib get qsReqBuf p 559 h1 = qsHexNib get qsReqBuf add p 1 560 h2 = qsHexNib get qsReqBuf add p 2 561 h3 = qsHexNib get qsReqBuf add p 3 562 if or or ge h0 16 ge h1 16 or ge h2 16 ge h3 16 563 ret -1 564 end 565 ret long or or shl h0 12 shl h1 8 or shl h2 4 h3 566 end 567 568 fn qsEmitUtf8Cp cp ulong opos ulong to ulong 569 if lt cp 128 570 put q36TokInBuf opos uchar cp 571 ret add opos 1 572 end 573 if lt cp 2048 574 if lt add opos 1 520000 575 put q36TokInBuf opos uchar or 192 shr cp 6 576 put q36TokInBuf add opos 1 uchar or 128 and cp 63 577 ret add opos 2 578 end 579 ret opos 580 end 581 if lt add opos 2 520000 582 put q36TokInBuf opos uchar or 224 shr cp 12 583 put q36TokInBuf add opos 1 uchar or 128 and shr cp 6 63 584 put q36TokInBuf add opos 2 uchar or 128 and cp 63 585 ret add opos 3 586 end 587 ret opos 588 end 589 590 fn qsCopyUnescapedContent st ulong clen ulong to ulong 591 opos = ulong 0 592 i = ulong 0 593 while lt i clen bound 520000 594 if ge opos 520000 595 break 596 end 597 c = get qsReqBuf add st i 598 if and eq c 92 lt add i 1 clen 599 nx = get qsReqBuf add st add i 1 600 i = add i 2 601 if eq nx 110 602 put q36TokInBuf opos 10 603 opos = add opos 1 604 continue 605 end 606 if eq nx 114 607 put q36TokInBuf opos 13 608 opos = add opos 1 609 continue 610 end 611 if eq nx 116 612 put q36TokInBuf opos 9 613 opos = add opos 1 614 continue 615 end 616 if eq nx 98 617 put q36TokInBuf opos 8 618 opos = add opos 1 619 continue 620 end 621 if eq nx 102 622 put q36TokInBuf opos 12 623 opos = add opos 1 624 continue 625 end 626 if and eq nx 117 le add i 4 clen 627 cp = qsDecodeHexU4 add st i 628 if ge cp 0 629 i = add i 4 630 opos = qsEmitUtf8Cp ulong cp opos 631 continue 632 end 633 end 634 put q36TokInBuf opos nx 635 opos = add opos 1 636 else 637 put q36TokInBuf opos c 638 opos = add opos 1 639 i = add i 1 640 end 641 end 642 ret opos 643 end 644 645 fn qsIsWsColon b uchar to bool 646 if or eq b 32 eq b 9 647 ret true 648 end 649 ret eq b 58 650 end 651 652 fn qsIsTrueAt p ulong n ulong to bool 653 if gt add p 4 n 654 ret false 655 end 656 if ne get qsReqBuf p 116 657 ret false 658 end 659 if ne get qsReqBuf add p 1 114 660 ret false 661 end 662 if ne get qsReqBuf add p 2 117 663 ret false 664 end 665 ret eq get qsReqBuf add p 3 101 666 end 667 668 fn qsFindStream n ulong to bool 669 if lt n 14 670 ret false 671 end 672 lim = sub n 9 673 for i ulong lim bound 524288 674 if qsMatchKey qsKeyStream i 8 675 p = add i 8 676 while lt p n bound 32 677 if not qsIsWsColon get qsReqBuf p 678 break 679 end 680 p = add p 1 681 end 682 if qsIsTrueAt p n 683 ret true 684 end 685 end 686 end 687 ret false 688 end 689 690 fn qsAppendUsageArbTimings cnts view ulong 4 to void 691 qsRespAppend qsChatC len qsChatC 692 qsRespAppendU get cnts 1 693 qsRespAppend qsChatD len qsChatD 694 qsRespAppendU add get cnts 0 get cnts 1 695 qsRespAppend qsChatE len qsChatE 696 qsRespAppendU get qdArbHits 0 697 qsRespAppend qsChatF len qsChatF 698 qsRespAppendU get qdArbTot 0 699 qsRespAppend qsChatG len qsChatG 700 qsRespAppendU qsArbHitPct 701 qsRespAppend qsChatH len qsChatH 702 qsRespAppendU get cnts 2 703 qsRespAppend qsChatI len qsChatI 704 qsRespAppendU get cnts 3 705 qsRespAppend qsChatJ len qsChatJ 706 end 707 708 fn qsHandleChat fd int n ulong to void 709 b = new ulong 2 0 710 pLen = ulong 0 711 if qsFindLastContent n b 712 clen = get b 1 713 if gt clen 520000 714 clen = ulong 520000 715 end 716 pLen = qsCopyUnescapedContent get b 0 clen 717 else 718 pLen = len qsP0 719 for i ulong pLen bound 64 720 put q36TokInBuf i get qsP0 i 721 end 722 end 723 maxGen = qsFindMaxTokens n 724 isStream = qsFindStream n 725 if isStream 726 put qsStreamFd 0 fd 727 qsRespReset 728 qsRespAppendHdr qsH200Sse len qsH200Sse 729 w = write fd qsRespBuf 0 get qsRespLen 0 730 else 731 put qsStreamFd 0 -1 732 end 733 cnts = new ulong 4 0 734 ok = qsGenerate pLen maxGen cnts 735 put qsStreamFd 0 -1 736 if isStream 737 qsRespReset 738 qsRespAppend qsSseData len qsSseData 739 qsRespAppend qsSseFinalA len qsSseFinalA 740 qsRespAppendU get cnts 0 741 qsAppendUsageArbTimings cnts 742 p = get qsRespLen 0 743 if lt p 262100 744 put qsRespBuf p 10 745 put qsRespLen 0 add p 1 746 end 747 qsRespAppend qsSseDone len qsSseDone 748 w = write fd qsRespBuf 0 get qsRespLen 0 749 ret 750 end 751 qsRespReset 752 qsRespAppendHdr qsH200 len qsH200 753 qsRespAppend qsChatA len qsChatA 754 qsRespAppendJsonEsc get qsOutLen 0 755 qsRespAppend qsChatB len qsChatB 756 qsRespAppendU get cnts 0 757 qsAppendUsageArbTimings cnts 758 w = write fd qsRespBuf 0 get qsRespLen 0 759 end 760 761 fn qsIsCrLf2 i ulong n ulong to bool 762 if gt add i 4 n 763 ret false 764 end 765 if ne get qsReqBuf i 13 766 ret false 767 end 768 if ne get qsReqBuf add i 1 10 769 ret false 770 end 771 if ne get qsReqBuf add i 2 13 772 ret false 773 end 774 ret eq get qsReqBuf add i 3 10 775 end 776 777 fn qsFindHdrEnd n ulong to ulong 778 if lt n 4 779 ret 0 780 end 781 i = ulong 0 782 while lt add i 1 n bound 32768 783 if qsIsCrLf2 i n 784 ret add i 4 785 end 786 if and eq get qsReqBuf i 10 eq get qsReqBuf add i 1 10 787 ret add i 2 788 end 789 i = add i 1 790 end 791 ret 0 792 end 793 794 fn qsMatchKeyLowerCI kbuf view uchar * pos ulong klen ulong to bool 795 for j ulong klen bound 32 796 c = get qsReqBuf add pos j 797 if and ge c 65 le c 90 798 c = add c 32 799 end 800 if ne c get kbuf j 801 ret false 802 end 803 end 804 ret true 805 end 806 807 fn qsFindContentLength hdrEnd ulong to ulong 808 if lt hdrEnd 16 809 ret 0 810 end 811 lim = sub hdrEnd 15 812 for i ulong lim bound 32768 813 if qsMatchKeyLowerCI qsKeyClen i 15 814 ret qsParseReqU add i 15 hdrEnd 815 end 816 end 817 ret 0 818 end 819 820 fn qsReadHttpRequest fd int to ulong 821 tot = ulong 0 822 hdrEnd = ulong 0 823 need = ulong 0 824 for it ulong 1024 825 if ge tot 524280 826 break 827 end 828 remCap = sub 524280 tot 829 nr = read fd qsReqBuf tot remCap 830 if eq nr -4 831 continue 832 end 833 if le nr 0 834 break 835 end 836 tot = add tot ulong nr 837 if eq hdrEnd 0 838 hdrEnd = qsFindHdrEnd tot 839 if gt hdrEnd 0 840 clen = qsFindContentLength hdrEnd 841 need = add hdrEnd clen 842 if gt need 524280 843 need = ulong 524280 844 end 845 end 846 end 847 if and gt hdrEnd 0 ge tot need 848 break 849 end 850 end 851 ret tot 852 end 853 854 text qsBatchResp "{\"status\":\"ok\",\"streams\":8,\"total_tokens\":128}\n" 855 856 fn qsHandleBatchHttp fd int to void 857 rc = qdRunBatchDecodeBench 16 8 858 qsRespReset 859 qsRespAppendHdr qsH200 len qsH200 860 qsRespAppend qsBatchResp len qsBatchResp 861 w = write fd qsRespBuf 0 get qsRespLen 0 862 end 863 864 fn qsHandleHttp fd int to void 865 n = qsReadHttpRequest fd 866 if le n 4 867 ret 868 end 869 if and eq get qsReqBuf 0 71 eq get qsReqBuf 4 47 870 if eq get qsReqBuf 5 104 871 qsRespReset 872 qsRespAppendHdr qsH200 len qsH200 873 qsRespAppend qsHealthA len qsHealthA 874 if eq get qdUseW4 0 1 875 qsRespAppend qsPrecW4 len qsPrecW4 876 else 877 qsRespAppend qsPrecFp8 len qsPrecFp8 878 end 879 qsRespAppend qsHealthB len qsHealthB 880 qsRespAppendU get qdArbHits 0 881 qsRespAppend qsHealthC len qsHealthC 882 qsRespAppendU get qdArbTot 0 883 qsRespAppend qsHealthD len qsHealthD 884 qsRespAppendU qsArbHitPct 885 qsRespAppend qsHealthE len qsHealthE 886 w = write fd qsRespBuf 0 get qsRespLen 0 887 ret 888 end 889 if and eq get qsReqBuf 5 118 eq get qsReqBuf 8 109 890 qsRespReset 891 qsRespAppendHdr qsH200 len qsH200 892 qsRespAppend qsModels len qsModels 893 w = write fd qsRespBuf 0 get qsRespLen 0 894 ret 895 end 896 end 897 if and eq get qsReqBuf 0 80 eq get qsReqBuf 5 47 898 if and ge n 14 and eq get qsReqBuf 9 98 eq get qsReqBuf 10 97 899 qsHandleBatchHttp fd 900 ret 901 end 902 qsHandleChat fd ulong n 903 ret 904 end 905 w = write fd qsH404 0 len qsH404 906 end 907 908 fn qsListen port ulong to long 909 addr = new uchar 16 0 910 one = new uchar 4 0 911 put addr 0 2 912 put addr 2 uchar shr port 8 913 put addr 3 uchar and port 255 914 put one 0 1 915 fd = socknew 2 524289 0 916 if lt fd 0 917 ret fd 918 end 919 r = sockopt int fd 1 2 one 0 4 920 if eq r 0 921 r = bind int fd addr 0 16 922 end 923 if eq r 0 924 r = listen int fd 16 925 end 926 if ne r 0 927 c = fclose int fd 928 ret r 929 end 930 ret fd 931 end 932 933 fn qsCopyTextToPrompt s view uchar * n ulong to ulong 934 for i ulong n bound 256 935 put q36TokInBuf i get s i 936 end 937 ret n 938 end 939 940 fn qsRunSelfTest to int 941 put qsStreamFd 0 -1 942 qdPrint qsSelfHdr 943 cnts = new ulong 4 0 944 n0 = qsCopyTextToPrompt qsP0 len qsP0 945 if not qsGenerate n0 32 cnts 946 qdPrint qsSelfBad 947 ret 30 948 end 949 qdPrint qsL0 950 w0 = write 1 qsOutTxt 0 get qsOutLen 0 951 qdPrint qdMsgNl 952 if eq get qsOutLen 0 0 953 qdPrint qsSelfBad 954 ret 31 955 end 956 if not qsGenerate qsCopyTextToPrompt qsP1 len qsP1 32 cnts 957 qdPrint qsSelfBad 958 ret 32 959 end 960 qdPrint qsL1 961 w1 = write 1 qsOutTxt 0 get qsOutLen 0 962 qdPrint qdMsgNl 963 qdPrint qsArbLbl 964 qdPrintU get qdArbHits 0 965 qdPrint qsArbMid 966 qdPrintU get qdArbTot 0 967 qdPrint qsArbPct 968 qdPrintU qsArbHitPct 969 qdPrint qdMsgNl 970 if or eq get qsOutLen 0 0 eq get qdArbTot 0 0 971 qdPrint qsSelfBad 972 ret 33 973 end 974 note Verify 128K context boundary (pos=131071 across all 10x512 MB KV slabs + 32 MB RoPE table) 975 edgeTok = qdForwardToken 271 131071 true false 976 if eq edgeTok 999999 977 qdPrint qsSelfBad 978 ret 34 979 end 980 if gt get qdBatchN 0 0 981 brc = qdRunBatchDecodeBench 16 get qdBatchN 0 982 if ne brc 0 983 qdPrint qsSelfBad 984 ret 35 985 end 986 end 987 qdPrint qsSelfOk 988 ret 0 989 end 990 991 fn qsParseArgs to void 992 put qdUseW4 0 1 993 put qdUseGdnBf16 0 0 994 put qdUseKvInt8 0 0 995 put qdUseWtbl 0 0 996 put qdRunP2 0 0 997 put qdUseMtp 0 0 998 put qdBatchN 0 0 999 put qdRunCryst 0 0 1000 put qdMtpReady 0 0 1001 put qdSpecMode 0 0 1002 put qdSpecM 0 0 1003 put qsPort 0 18091 1004 put qsSelfTest 0 0 1005 for i ulong len qsDefDir bound 128 1006 put qdDirBuf i get qsDefDir i 1007 end 1008 put qdDirLen 0 len qsDefDir 1009 a = new uchar 64 0 1010 argc = ulong argcnt 1011 ai = ulong 1 1012 while lt ai argc bound 32 1013 n = argread ai a 0 60 1014 if and ge n 5 eq get a 0 45 1015 if and eq get a 2 102 eq get a 3 112 1016 put qdUseW4 0 0 1017 end 1018 if and eq get a 2 119 eq get a 3 52 1019 put qdUseW4 0 1 1020 end 1021 if and eq get a 2 103 eq get a 3 100 1022 put qdUseGdnBf16 0 1 1023 end 1024 if and eq get a 2 107 eq get a 3 118 1025 put qdUseKvInt8 0 1 1026 end 1027 if and eq get a 2 115 eq get a 3 112 1028 put qdSpecM 0 8 1029 end 1030 if and eq get a 2 109 eq get a 3 116 1031 put qdUseMtp 0 1 1032 put qdSpecM 0 4 1033 end 1034 if and eq get a 2 98 eq get a 3 97 1035 put qdBatchN 0 8 1036 end 1037 if and eq get a 2 115 eq get a 3 101 1038 put qsSelfTest 0 1 1039 end 1040 if and eq get a 2 112 eq get a 3 111 1041 if lt add ai 1 argc 1042 ai = add ai 1 1043 pv = qdParseArgU ai 1044 if gt pv 0 1045 put qsPort 0 pv 1046 end 1047 end 1048 end 1049 if and eq get a 2 109 eq get a 3 111 1050 if lt add ai 1 argc 1051 ai = add ai 1 1052 dl = argread ai qdDirBuf 0 250 1053 if gt dl 0 1054 put qdDirLen 0 ulong dl 1055 end 1056 end 1057 end 1058 end 1059 ai = add ai 1 1060 end 1061 end 1062 1063 fn qsLoadTok to bool 1064 tlen = qdBuildPath qsSufTok -1 1065 pLocal = new uchar 512 0 1066 for i ulong tlen bound 512 1067 put pLocal i get qdPathBuf i 1068 end 1069 ret q36TokLoad pLocal tlen 1070 end 1071 1072 fn main to int 1073 qdPrint qsBanner 1074 qsParseArgs 1075 if eq get qdUseW4 0 1 1076 qdPrint qsModeW4 1077 else 1078 qdPrint qsModeFp8 1079 end 1080 if not qsLoadTok 1081 qdPrint qsTokFail 1082 ret 2 1083 end 1084 qdPrint qsTokOk 1085 qdInitRopeFreq 1086 qdInitLexRank 1087 q36ArbResetAll 1088 irc = qdGpuInit 1089 if ne irc 0 1090 qdPrint qdErr 1091 qdPrintU ulong irc 1092 qdPrint qdMsgNl 1093 ret irc 1094 end 1095 lrc = qdLoadAll false 1096 if ne lrc 0 1097 rc = nvstop 1098 ret lrc 1099 end 1100 if and eq get qdUseW4 0 1 gt get qdSpecM 0 0 1101 if not qdSpecEmitVerifyPkt get qdSpecM 0 1102 r = fclose get qdOutFd 0 1103 rc = nvstop 1104 ret 17 1105 end 1106 if eq get qdUseMtp 0 1 1107 if not qdLoadMtpW4 1108 r = fclose get qdOutFd 0 1109 rc = nvstop 1110 ret 18 1111 end 1112 if not qdMtpRecordPackets 1113 r = fclose get qdOutFd 0 1114 rc = nvstop 1115 ret 19 1116 end 1117 end 1118 end 1119 if eq get qsSelfTest 0 1 1120 prc = qsRunSelfTest 1121 r = fclose get qdOutFd 0 1122 rc = nvstop 1123 ret prc 1124 end 1125 port = get qsPort 0 1126 lfd = qsListen port 1127 if lt lfd 0 1128 r = fclose get qdOutFd 0 1129 rc = nvstop 1130 ret 40 1131 end 1132 qdPrint qsListenMsg 1133 qdPrintU port 1134 qdPrint qsListenEnd 1135 for conn ulong `1000000` 1136 cfd = accept int lfd 524288 1137 if ge cfd 0 1138 qsHandleHttp int cfd 1139 c0 = fclose int cfd 1140 end 1141 end 1142 c1 = fclose int lfd 1143 r = fclose get qdOutFd 0 1144 rc = nvstop 1145 ret 0 1146 end