interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / native / omegallm / serve.om

2788 lines · 61344 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm serve.om (aien-train DRA-119): `ollm serve PORT CK MERGES [MAXCONN] [tplmax=N]
3note [test=1]`, an OpenAI-compatible HTTP/1.1 server for a v1 BPE checkpoint (V > 256) on
4note 127.0.0.1:PORT only. aarch64 only: build.sh composes it under one `om2` line after the
5note omega-mv e5b32567 units net/http.om, net/json.om and net/jsonw.om (vendored unmodified); the
6note other targets take noserve.om. It prints `PORT n`, then serves one request at a time, one
7note request per connection (Connection: close), until MAXCONN connections (default 1000000) have
8note been served. A connection is served once its first bytes arrive; a silent one waits in one
9note of 8 pending slots and is closed unanswered 10 s after its accept (svwait). tplmax=N
10note (256 .. 983040, default 65536) caps the chat template in bytes; test=1 enables the test-only
11note members ollm_test_output and ollm_test_delay_ms (refused otherwise).
12note Routes: GET /health {"status":"ok","in_flight":0,"busy":false}; GET /v1/models;
13note POST /v1/chat/completions with JSON or SSE (stream true) responses; other paths 404, other
14note methods 405. in_flight counts the generating chat completions (0 or 1, one slot); busy is
15note in_flight 1. While a completion generates, the listener and pending connections are served
16note between tokens: GET /health answers {"status":"ok","in_flight":1,"busy":true}, GET
17note /v1/models the list, and every other request 503 server_busy with Retry-After: 1. A client
18note that leaves (svgone) stops its generation: nothing more is sent, logged as status 499.
19note Request: messages (non-empty array of objects with a string role of at most 32 bytes and a
20note content that is a string, null, or an array of {"type":"text","text":string} parts joined
21note with no separator; other part types are 400; content may be absent beside tool_calls),
22note optional max_tokens or max_completion_tokens (1 .. 999999), temperature (0 .. 2 without
23note exponent, used to 0.001; default 1), seed (unsigned, default 1), top_k (integer -1 ..
24note 999999; -1, 0 and K >= V are off), top_p (0 <= p <= 1 without exponent, used to 0.000001;
25note 0 keeps only the most probable token, 1 is off), stop (null, a string or an array of at most
26note 8 strings of 1 .. 128 bytes), tools (array of at most 128
27note {"type":"function","function":{"name":1 .. 64 bytes, ...}}), tool_choice ("none", "auto",
28note "required" or a {"type":"function"} object), stream,
29note stream_options.include_usage and n (only 1). Other members (model, ...) are ignored.
30note Interim chat template (until DRA-123 fixes one shared template): when tools is non-empty,
31note `tools: ` and the request's tools JSON text as sent, then `\n`; then `role: content\n` for
32note each message in order, an assistant's tool_calls appended to its content as
33note `<tool_call>{"name":NAME,"arguments":ARGS}</tool_call>` (NAME the JSON string as sent, ARGS
34note the decoded string or the object's JSON text as sent, {} when absent); then `assistant: `.
35note At most tplmax bytes, else 400. The context is token 0 then the template's encoding (CT0
36note tokens, reported as prompt_tokens); CT0 >= T is refused with 400 context_length_exceeded
37note stating CT0 and T. Output runs at most min(max_tokens, T - CT0) tokens, so the T-token window
38note never slides (cmdsamp's step osstep, splitmix64 seed stream; with top_k or top_p on, the
39note kept tokens are the most probable in order, ties to the lower id, up to K and until their
40note mass reaches top_p, drawn by the same uniform scaled to the kept mass); a generated token 0
41note ends it with finish_reason stop, the limit with length, the earliest stop string with stop
42note (the content cut before it; a streamed chunk never sends a byte a stop could still claim).
43note Tool calls (tools non-empty, tool_choice not "none"): when the content from the first
44note `<tool_call>` on is 1 .. 8 blocks `<tool_call>{"name":N,"arguments":A}</tool_call>` with only
45note whitespace between and after, N a declared tool name and A an object, a string or absent,
46note the answer carries tool_calls (id call_<request>_<i>, arguments the object's JSON text or the
47note string) with finish_reason tool_calls and content the text before, trailing whitespace
48note dropped, null when empty; otherwise the text is plain content. Streaming holds back a
49note possible marker and trailing whitespace, then sends the tool_calls in one delta chunk.
50note Decoded bytes are sent as UTF-8: a sequence cut by a token boundary waits for the next token,
51note an ill-formed one (or one cut by the end) becomes U+FFFD. One line per routed response (and
52note per 413 at the head, and per request answered during a generation) goes to stdout: `C status
53note prompt_tokens completion_tokens finish(0 none, 1 stop, 2 length, 3 tool_calls, 4 client left)
54note ms`. A body over 65532 JSON tokens is 413 request_too_large.
55note Exit codes: 2 usage; 27 merges (bpe.om); 71 the port cannot be bound.
56note wb layout: raw token bytes [0, 8192), content [8192, 73728), stop strings [73728, 74752) (8 x
57note 128), markers [74752, 74784), message [81920, 82176), response id [82176, 82240), call id
58note [82240, 82432), tool names [82432, 90624) (128 x 64), template [90624, 1073664). bd holds the
59note request, then the tool calls' names and arguments [0, 131072). sv slots: 0 stream, 1 max
60note tokens, 2 TEMPMILLI, 3 seed state, 4 include_usage, 5 template bytes, 6 CT0, 7 generated, 8
61note finish, 9 request count, 10 created, 11 start ns, 12 raw bytes, 13 content bytes, 14 content
62note sent, 15 id length, 16 error code, 17 chunk end, 19 T, 21 last status, 22 test mode, 23
63note tplmax, 24 forced tokens, 25 forced on, 26 test output bytes, 27 delay ms, 28 top_k, 29 top_p
64note millionths, 30 tools, 31 tool detection, 32 tool calls, 33 marker position, 34 bd fill,
65note 35 stop strings, 36 listen fd.
66note Model id (svmid): read from the loaded checkpoint's header, so it names this model and this
67note checkpoint: `ollm-ck0M-vV-tT-dD-hH-lL-stepS-fnvX`, M the checkpoint magic (1 OLLMCK01,
68note 2 OLLMCK02), V T D H L its shapes, S its step field and X the FNV-1a 64 of its payload as the
69note header records it (ockload has already refused a payload that does not hash to it, exit 14).
70note The same id is `/v1/models` data[0].id (also while a completion generates) and the `model`
71note of every completion and chunk; it is kept in the global svmi, its length in svml, and
72note printed as `MODEL id` before `PORT n`.
73note
74note Mutants (tests/omegallm/build.sh --mutant NAME sets one constant to 0; tests/omegallm/
75note serve-mutants.sh requires the serve gate to fail on each): OMUTMETH answers a known path's
76note other methods instead of 405, OMUTPATH answers other paths as /health instead of 404, OMUTBIG
77note drops the 413 (no head bound, an over-long body is a plain 400), OMUTJSON serves malformed JSON
78note instead of 400, OMUTLOOP binds 0.0.0.0 instead of 127.0.0.1, OMUTSEED shifts the request seed
79note by one (parity with `sample`), OMUTMID reports the old fixed id `ollm`.
80const OMUTMETH ulong 1
81const OMUTPATH ulong 1
82const OMUTBIG ulong 1
83const OMUTJSON ulong 1
84const OMUTLOOP ulong 1
85const OMUTSEED ulong 1
86const OMUTMID ulong 1
87
88text svtjson "application/json"
89text svtctyp "Content-Type"
90text svphlth "/health"
91text svpmods "/v1/models"
92text svpchat "/v1/chat/completions"
93text svpcomp "/v1/completions"
94text skprompt "prompt"
95text svtcomp "text_completion"
96text svcidp "cmpl-ollm-"
97text svmget "GET"
98text svmpost "POST"
99text svthlth "{\"status\":\"ok\",\"in_flight\":0,\"busy\":false}"
100text svthbsy "{\"status\":\"ok\",\"in_flight\":1,\"busy\":true}"
101text svtlist "list"
102text skdata "data"
103text skowned "owned_by"
104text svaien "aien"
105text svt503 "{\"error\":{\"message\":\"a chat completion is in flight and this server runs one at a time: retry after 1 s\",\"type\":\"server_error\",\"param\":null,\"code\":\"server_busy\"}}"
106text svtra "Retry-After"
107text svone "1"
108text svport "PORT "
109text svmodl "MODEL "
110text svmid1 "ollm-ck0"
111text svmidv "-v"
112text svmidt "-t"
113text svmidd "-d"
114text svmidh "-h"
115text svmidl "-l"
116text svmids "-step"
117text svmidf "-fnv"
118text skmsgs "messages"
119text skrole "role"
120text skcont "content"
121text skstrm "stream"
122text skmaxt "max_tokens"
123text skmaxc "max_completion_tokens"
124text sktemp "temperature"
125text skseed "seed"
126text skopts "stream_options"
127text skincl "include_usage"
128text skn "n"
129text skid "id"
130text skobj "object"
131text skcrt "created"
132text skmodel "model"
133text skchoi "choices"
134text skidx "index"
135text skmsg "message"
136text skdelta "delta"
137text skfin "finish_reason"
138text skusage "usage"
139text skpt "prompt_tokens"
140text skct "completion_tokens"
141text sktt "total_tokens"
142text skerr "error"
143text sktype "type"
144text skcode "code"
145text skparam "param"
146text sktext "text"
147text sktopk "top_k"
148text sktopp "top_p"
149text sktools "tools"
150text sktchoi "tool_choice"
151text sktcall "tool_calls"
152text skfunc "function"
153text sknam "name"
154text skargs "arguments"
155text sktout "ollm_test_output"
156text sktdel "ollm_test_delay_ms"
157text sknone "none"
158text svauto "auto"
159text svreq "required"
160text svollm "ollm"
161text svasst "assistant"
162text svcc "chat.completion"
163text svccc "chat.completion.chunk"
164text svstop "stop"
165text svlen "length"
166text svinv "invalid_request_error"
167text svsrv "server_error"
168text svdone "[DONE]"
169text svidp "chatcmpl-ollm-"
170text svcallp "call_"
171text svus "_"
172text svsep ": "
173text svnl "\n"
174text svaprm "assistant: "
175text svtlp "tools: "
176text svtco "<tool_call>"
177text svtcc "</tool_call>"
178text svtcn "{\"name\":"
179text svtca ",\"arguments\":"
180text svtcz "}</tool_call>"
181text svempt "{}"
182text svoptt "tplmax="
183text svopte "test=1"
184text secjson "invalid_json"
185text secreq "invalid_request"
186text secctx "context_length_exceeded"
187text secbig "request_too_large"
188text secnf "not_found"
189text secmeth "method_not_allowed"
190text secint "internal_error"
191text sem1 "request body is not valid JSON (RFC 8259)"
192text sem2 "the request must be a JSON object"
193text sem3 "messages must be a non-empty array of objects"
194text sem4 "message content must be a string, null or an array of text parts (absent only beside tool_calls)"
195text sem5 "message role must be a string of at most 32 bytes"
196text sem6a "the chat template is over "
197text sem6b " bytes (the serve option tplmax=N sets this cap)"
198text sem7 "temperature must be a number from 0 to 2 without exponent"
199text sem8 "max_tokens must be an integer from 1 to 999999"
200text sem9 "seed must be an unsigned integer"
201text sem10 "stream must be true or false"
202text sem12 "only n = 1 is supported"
203text sem13 "the request body is over 983040 bytes"
204text sem14 "no such path; routes: GET /health, GET /v1/models, POST /v1/chat/completions, POST /v1/completions"
205text sem15 "method not allowed on this path"
206text sem16 "response construction failed"
207text sem17 "message content parts must be {\"type\":\"text\",\"text\":string}; image_url, input_audio, file and other part types are not supported"
208text sem18 "stop must be null, a string or an array of at most 8 strings of 1 to 128 bytes"
209text sem19 "top_k must be an integer from -1 to 999999 (-1 and 0 turn it off)"
210text sem20 "top_p must be a number from 0 to 1 without exponent (resolution 0.000001; 0 keeps only the most probable token)"
211text sem21 "tools must be an array of at most 128 {\"type\":\"function\",\"function\":{\"name\":string of 1 to 64 bytes}} objects"
212text sem22 "tool_choice must be \"none\", \"auto\", \"required\" or a {\"type\":\"function\"} object"
213text sem23 "tool_calls must be an array of {\"function\":{\"name\":string,\"arguments\":string or object}} objects"
214text sem24 "ollm_test_output (a string of at most 16384 bytes) and ollm_test_delay_ms (0 to 10000) need a server started with test=1"
215text sem25 "the request body has over 65532 JSON tokens (values, keys and closers), the JSON parser's limit"
216text sem26 "prompt must be a non-empty string"
217text sem27 "stream is not supported on /v1/completions: send stream false or leave it out"
218text sem28 "messages, tools and tool_choice belong to /v1/chat/completions; /v1/completions takes prompt"
219text sem11a "prompt is "
220text sem11b " tokens including the start token; the context window is "
221text sem11c " tokens, so no room is left for output"
222
223note forced tokens (test=1), top_k/top_p marks, stop lengths, tool name lengths, tool call spans
224global svft ulong 4096
225global svmk uchar 4096
226global svstl ulong 8
227global svtnl ulong 128
228global svtcx ulong 32
229note pending connections (svwait): descriptor + 1 (0 free), accept time, record slot; pollfd records;
230note the svgone peek byte
231global svpfd ulong 8
232global svpat ulong 8
233global svpix ulong 8
234global svpl uchar 72
235global svpk uchar 1
236global svmi uchar 192
237global svml ulong 1
238
239note ======== small byte helpers over wb
240
241note svmcat: copy text t into wb at p, at most up to e; the next p
242fn svmcat wb edit uchar 1073664 p ulong e ulong t view uchar * to ulong
243q = p
244for i ulong len t
245if ge q e
246ret q
247end
248put wb q get t i
249q = add q 1
250end
251ret q
252end
253
254note svmdec: v in decimal into wb at p (at most 20 digits, up to e); the next p
255fn svmdec wb edit uchar 1073664 p ulong e ulong v ulong to ulong
256d = new uchar 20 0
257i = ulong 20
258x = v
259for it ulong 20
260i = sub i 1
261put d i uchar add rem x 10 48
262x = div x 10
263if eq x 0
264break
265end
266end
267q = p
268for k ulong sub 20 i
269if ge q e
270ret q
271end
272put wb q get d add i k
273q = add q 1
274end
275ret q
276end
277
278note svat: whether the content at p starts with the pattern wb[po, po + pl) (p + pl <= content)
279fn svat wb edit uchar 1073664 p ulong po ulong pl ulong to bool
280for i ulong pl
281if ne get wb add 8192 add p i get wb add po i
282ret false
283end
284end
285ret true
286end
287
288note svfind: the first position >= s where the pattern wb[po, po + pl) lies wholly in the content
289note [0, n), or jsnone
290fn svfind wb edit uchar 1073664 s ulong n ulong po ulong pl ulong to ulong
291p = s
292while le add p pl n bound 65537
293if svat wb p po pl
294ret p
295end
296p = add p 1
297end
298ret jsnone
299end
300
301note svpsuf: the longest k < pl, k <= n - lo, with content [n - k, n) a prefix of the pattern
302fn svpsuf wb edit uchar 1073664 lo ulong n ulong po ulong pl ulong to ulong
303k = sub pl 1
304if lt sub n lo k
305k = sub n lo
306end
307m = k
308for it ulong m
309if svat wb sub n k po k
310ret k
311end
312k = sub k 1
313end
314ret 0
315end
316
317fn svisws c ulong to bool
318ret or or eq c 32 eq c 9 or eq c 10 eq c 13
319end
320
321note svtrim: h moved back over trailing whitespace of the content, not below lo
322fn svtrim wb edit uchar 1073664 lo ulong h ulong to ulong
323if le h lo
324ret h
325end
326q = h
327for it ulong sub h lo
328if not svisws ulong get wb add 8191 q
329break
330end
331q = sub q 1
332end
333ret q
334end
335
336note svskws: p moved over whitespace of the content, not past n
337fn svskws wb edit uchar 1073664 p ulong n ulong to ulong
338q = p
339for it ulong sub n p
340if not svisws ulong get wb add 8192 q
341break
342end
343q = add q 1
344end
345ret q
346end
347
348note ======== UTF-8 (RFC 3629 section 4)
349
350note svu8n: the sequence at a[p], with a[0 .. n) the bytes so far: 1 .. 4 its length when well
351note formed; 0 when a[p .. n) is a proper prefix of a well-formed sequence; 8 + k when its maximal
352note ill-formed subpart is k bytes (replaced by one U+FFFD)
353fn svu8n a view uchar * p ulong n ulong to ulong
354c = ulong get a p
355if lt c 128
356ret 1
357end
358k = svu8k c
359if eq k 0
360ret 9
361end
362for i ulong sub k 1
363q = add p add i 1
364if ge q n
365ret 0
366end
367if not svu8ok c i ulong get a q
368ret add 9 i
369end
370end
371ret k
372end
373
374note svu8k: the sequence length a lead byte c >= 128 starts, 0 when it starts none
375fn svu8k c ulong to ulong
376if and ge c 194 le c 223
377ret 2
378end
379if and ge c 224 le c 239
380ret 3
381end
382if and ge c 240 le c 244
383ret 4
384end
385ret 0
386end
387
388note svu8ok: whether b may be continuation byte i (0 the first) after lead byte c (RFC 3629 table)
389fn svu8ok c ulong i ulong b ulong to bool
390lo = ulong 128
391hi = ulong 191
392if eq i 0
393if eq c 224
394lo = 160
395end
396if eq c 237
397hi = 159
398end
399if eq c 240
400lo = 144
401end
402if eq c 244
403hi = 143
404end
405end
406ret and ge b lo le b hi
407end
408
409note svrepl: U+FFFD (EF BF BD) into the content at its end
410fn svrepl wb edit uchar 1073664 sv edit ulong 64 to void
411g = add 8192 get sv 13
412put wb g 239
413put wb add g 1 191
414put wb add g 2 189
415put sv 13 add get sv 13 3
416end
417
418note svclean: move the complete sequences of the raw bytes wb[0, sv12) to the content; fin 1 also
419note replaces a cut tail. The rest moves to the front of the raw bytes.
420fn svclean wb edit uchar 1073664 sv edit ulong 64 fin ulong to void
421rn = get sv 12
422p = ulong 0
423for it ulong add rn 1
424if ge p rn
425break
426end
427r = svu8n wb p rn
428if eq r 0
429if eq fin 0
430break
431end
432svrepl wb sv
433p = rn
434else
435if gt r 8
436svrepl wb sv
437p = add p sub r 8
438else
439svmove wb add 8192 get sv 13 p r
440put sv 13 add get sv 13 r
441p = add p r
442end
443end
444end
445svmove wb 0 p sub rn p
446put sv 12 sub rn p
447end
448
449note svmove: wb[d, d + n) = wb[s, s + n), copying forward (d <= s or the ranges apart)
450fn svmove wb edit uchar 1073664 d ulong s ulong n ulong to void
451for i ulong n
452put wb add d i get wb add s i
453end
454end
455
456note ======== responses
457
458fn svstat cs edit ulong 320 io edit uchar 131072 code ulong t view uchar * to long
459r = hrspst cs io code
460r = hfe r hhname cs io svtctyp
461r = hfe r hhval cs io svtjson
462r = hfe r hhend cs io 0 len t
463r = hfe r hput cs io t
464ret hfe r hwfin cs io
465end
466
467note svs503: the fixed server_busy error with Retry-After: 1
468fn svs503 cs edit ulong 320 io edit uchar 131072 to long
469r = hrspst cs io 503
470r = hfe r hhname cs io svtctyp
471r = hfe r hhval cs io svtjson
472r = hfe r hhname cs io svtra
473r = hfe r hhval cs io svone
474r = hfe r hhend cs io 0 len svt503
475r = hfe r hput cs io svt503
476ret hfe r hwfin cs io
477end
478
479note svsend: the writer's text ob[1040, 1040 + n) as a JSON response with status code
480fn svsend cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 code ulong n ulong to long
481r = hrspst cs io code
482r = hfe r hhname cs io svtctyp
483r = hfe r hhval cs io svtjson
484r = hfe r hhend cs io 0 n
485r = hfe r hput cs io view part ob 1040 n
486ret hfe r hwfin cs io
487end
488
489fn svwt ob edit uchar 409600 t view uchar * to void
490js_wsbuf ob t 0 len t
491end
492
493note svemsg: the message of error code sv16 into wb[81920 ..); its length
494fn svemsg wb edit uchar 1073664 sv edit ulong 64 to ulong
495e = get sv 16
496b = ulong 81920
497z = ulong 82176
498p = b
499if eq e 1
500p = svmcat wb p z sem1
501end
502if eq e 2
503p = svmcat wb p z sem2
504end
505if eq e 3
506p = svmcat wb p z sem3
507end
508if eq e 4
509p = svmcat wb p z sem4
510end
511if eq e 5
512p = svmcat wb p z sem5
513end
514if eq e 6
515p = svmcat wb p z sem6a
516p = svmdec wb p z get sv 23
517p = svmcat wb p z sem6b
518end
519if eq e 7
520p = svmcat wb p z sem7
521end
522if eq e 8
523p = svmcat wb p z sem8
524end
525if eq e 9
526p = svmcat wb p z sem9
527end
528if eq e 10
529p = svmcat wb p z sem10
530end
531if eq e 11
532p = svmcat wb p z sem11a
533p = svmdec wb p z get sv 6
534p = svmcat wb p z sem11b
535p = svmdec wb p z get sv 19
536p = svmcat wb p z sem11c
537end
538if eq e 12
539p = svmcat wb p z sem12
540end
541if eq e 13
542p = svmcat wb p z sem13
543end
544if eq e 14
545p = svmcat wb p z sem14
546end
547if eq e 15
548p = svmcat wb p z sem15
549end
550if eq e 16
551p = svmcat wb p z sem16
552end
553ret sub svemsg2 wb e p z b
554end
555
556note svemsg2: the messages of error codes 17 .. 25 at p; the end
557fn svemsg2 wb edit uchar 1073664 e ulong p ulong z ulong to ulong
558if eq e 17
559ret svmcat wb p z sem17
560end
561if eq e 18
562ret svmcat wb p z sem18
563end
564if eq e 19
565ret svmcat wb p z sem19
566end
567if eq e 20
568ret svmcat wb p z sem20
569end
570if eq e 21
571ret svmcat wb p z sem21
572end
573if eq e 22
574ret svmcat wb p z sem22
575end
576if eq e 23
577ret svmcat wb p z sem23
578end
579if eq e 24
580ret svmcat wb p z sem24
581end
582if eq e 25
583ret svmcat wb p z sem25
584end
585if eq e 26
586ret svmcat wb p z sem26
587end
588if eq e 27
589ret svmcat wb p z sem27
590end
591if eq e 28
592ret svmcat wb p z sem28
593end
594ret p
595end
596
597fn svecode ob edit uchar 409600 e ulong to void
598if eq e 1
599svwt ob secjson
600ret
601end
602if eq e 11
603svwt ob secctx
604ret
605end
606if or eq e 13 eq e 25
607svwt ob secbig
608ret
609end
610if eq e 14
611svwt ob secnf
612ret
613end
614if eq e 15
615svwt ob secmeth
616ret
617end
618if eq e 16
619svwt ob secint
620ret
621end
622svwt ob secreq
623end
624
625note svfail: {"error":{"message","type","param":null,"code"}} for error code sv16 with status st
626fn svfail cs edit ulong 320 io edit uchar 131072 wb edit uchar 1073664 sv edit ulong 64 st ulong to long
627ob = new uchar 409600 0
628put sv 21 st
629m = svemsg wb sv
630x = js_winit ob
631js_wobj ob
632svwt ob skerr
633js_wobj ob
634svwt ob skmsg
635js_wsbuf ob wb 81920 m
636svwt ob sktype
637if eq st 500
638svwt ob svsrv
639else
640svwt ob svinv
641end
642svwt ob skparam
643js_wnull ob
644svwt ob skcode
645svecode ob get sv 16
646js_wobje ob
647js_wobje ob
648n = js_wdone ob
649if lt n 0
650put cs HKEEP 0
651ret hrsperr cs io 500
652end
653ret svsend cs io ob st ulong n
654end
655
656note svhead: {"id", "object" t, "created", "model" opened; the caller adds the rest
657fn svhead ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 t view uchar * to void
658x = js_winit ob
659js_wobj ob
660svwt ob skid
661js_wsbuf ob wb 82176 get sv 15
662svwt ob skobj
663svwt ob t
664svwt ob skcrt
665js_wulong ob get sv 10
666svwt ob skmodel
667svmidw ob
668end
669
670note svmidw: the model id as a JSON string value (the checkpoint id; OMUTMID 0: the fixed `ollm`)
671fn svmidw ob edit uchar 409600 to void
672if eq OMUTMID 0
673svwt ob svollm
674else
675js_wsbeg ob
676for i ulong get svml 0
677js_wsb ob get svmi i
678end
679js_wsend ob
680end
681end
682
683note svicat: copy text t into svmi at p, at most up to e; the next p
684fn svicat p ulong e ulong t view uchar * to ulong
685q = p
686for i ulong len t
687if ge q e
688ret q
689end
690put svmi q get t i
691q = add q 1
692end
693ret q
694end
695
696note svidec: v in decimal into svmi at p (at most 20 digits, up to e); the next p
697fn svidec p ulong e ulong v ulong to ulong
698d = new uchar 20 0
699i = ulong 20
700x = v
701for it ulong 20
702i = sub i 1
703put d i uchar add rem x 10 48
704x = div x 10
705if eq x 0
706break
707end
708end
709q = p
710for it ulong sub 20 i
711if ge q e
712ret q
713end
714put svmi q get d add i it
715q = add q 1
716end
717ret q
718end
719
720note svihex: v as 16 lowercase hex digits into svmi at p, at most up to e; the next p
721fn svihex p ulong e ulong v ulong to ulong
722q = p
723for i ulong 16
724if ge q e
725ret q
726end
727d = and shr v mul sub 15 i 4 15
728c = add d 48
729if gt d 9
730c = add d 87
731end
732put svmi q uchar c
733q = add q 1
734end
735ret q
736end
737
738note svmid: the model id of the checkpoint loaded from argv[3] (ockload has run and checked it)
739note into svmi, its length into svml, then `MODEL id` on stdout. The header is read again for
740note its payload FNV-1a field, which ockload keeps only while it loads.
741fn svmid to void
742hl = ulong 112
743if eq get og 46 2
744hl = 120
745end
746hn = oprd 3 0 0 120
747if lt hn hl
748exit 10
749end
750e = ulong 192
751p = svicat 0 e svmid1
752p = svidec p e get og 46
753p = svicat p e svmidv
754p = svidec p e get og 0
755p = svicat p e svmidt
756p = svidec p e get og 1
757p = svicat p e svmidd
758p = svidec p e get og 2
759p = svicat p e svmidh
760p = svidec p e get og 3
761p = svicat p e svmidl
762p = svidec p e get og 5
763p = svicat p e svmids
764p = svidec p e get og 41
765p = svicat p e svmidf
766p = svihex p e opw8 sub hl 8
767put svml 0 p
768for i ulong len svmodl
769oputc ulong get svmodl i
770end
771for i ulong p
772oputc ulong get svmi i
773end
774oputc 10
775end
776
777note svmodw: the /v1/models answer, the one model this server answers with (not logged)
778fn svmodw cs edit ulong 320 io edit uchar 131072 to long
779ob = new uchar 409600 0
780x = js_winit ob
781js_wobj ob
782svwt ob skobj
783svwt ob svtlist
784svwt ob skdata
785js_warr ob
786js_wobj ob
787svwt ob skid
788svmidw ob
789svwt ob skobj
790svwt ob skmodel
791svwt ob skcrt
792js_wulong ob 0
793svwt ob skowned
794svwt ob svaien
795js_wobje ob
796js_warre ob
797js_wobje ob
798m = js_wdone ob
799if lt m 0
800put cs HKEEP 0
801ret hrsperr cs io 500
802end
803ret svsend cs io ob 200 ulong m
804end
805
806note svmods: GET /v1/models, logged
807fn svmods cs edit ulong 320 io edit uchar 131072 sv edit ulong 64 to long
808put sv 21 200
809r = svmodw cs io
810svlog sv
811ret r
812end
813
814note svbig: the status for a body over the bound, 413 (OMUTBIG 0: 400, the 413 refusal removed)
815fn svbig to ulong
816if eq OMUTBIG 0
817ret 400
818end
819ret 413
820end
821
822fn svfr ob edit uchar 409600 sv edit ulong 64 to void
823f = get sv 8
824if eq f 1
825svwt ob svstop
826ret
827end
828if eq f 2
829svwt ob svlen
830ret
831end
832if eq f 3
833svwt ob sktcall
834ret
835end
836js_wnull ob
837end
838
839fn svusage ob edit uchar 409600 sv edit ulong 64 to void
840svwt ob skusage
841js_wobj ob
842svwt ob skpt
843js_wulong ob get sv 6
844svwt ob skct
845js_wulong ob get sv 7
846svwt ob sktt
847js_wulong ob add get sv 6 get sv 7
848js_wobje ob
849end
850
851note svwcalls: the tool_calls array (idx 1 adds each call's index, as a stream delta does)
852fn svwcalls ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 idx ulong to void
853js_warr ob
854for i ulong get sv 32
855b = mul i 4
856js_wobj ob
857if eq idx 1
858svwt ob skidx
859js_wulong ob i
860end
861svwt ob skid
862p = svmcat wb 82240 82432 svcallp
863p = svmdec wb p 82432 get sv 9
864p = svmcat wb p 82432 svus
865p = svmdec wb p 82432 i
866js_wsbuf ob wb 82240 sub p 82240
867svwt ob sktype
868svwt ob skfunc
869svwt ob skfunc
870js_wobj ob
871svwt ob sknam
872js_wsbuf ob bd get svtcx b get svtcx add b 1
873svwt ob skargs
874js_wsbuf ob bd get svtcx add b 2 get svtcx add b 3
875js_wobje ob
876js_wobje ob
877end
878js_warre ob
879end
880
881note svcfull: the text_completion object of /v1/completions
882fn svcfull ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 to long
883svhead ob wb sv svtcomp
884svwt ob skchoi
885js_warr ob
886js_wobj ob
887svwt ob skidx
888js_wulong ob 0
889svwt ob sktext
890js_wsbuf ob wb 8192 get sv 13
891svwt ob skfin
892svfr ob sv
893js_wobje ob
894js_warre ob
895svusage ob sv
896js_wobje ob
897ret js_wdone ob
898end
899
900note svfull: the chat.completion object (stream false), or svcfull on /v1/completions
901fn svfull ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 to long
902if eq get sv 37 1
903ret svcfull ob wb sv
904end
905svhead ob wb sv svcc
906svwt ob skchoi
907js_warr ob
908js_wobj ob
909svwt ob skidx
910js_wulong ob 0
911svwt ob skmsg
912js_wobj ob
913svwt ob skrole
914svwt ob svasst
915svwt ob skcont
916if gt get sv 32 0
917h = svtrim wb 0 get sv 33
918if eq h 0
919js_wnull ob
920else
921js_wsbuf ob wb 8192 h
922end
923svwt ob sktcall
924svwcalls ob wb sv bd 0
925else
926js_wsbuf ob wb 8192 get sv 13
927end
928js_wobje ob
929svwt ob skfin
930svfr ob sv
931js_wobje ob
932js_warre ob
933svusage ob sv
934js_wobje ob
935ret js_wdone ob
936end
937
938note svchunk: one chat.completion.chunk; kind 0 the role, 1 the content wb[8192 + sv14, 8192 + sv17),
939note 2 the finish reason, 3 usage (empty choices). Sends it as one SSE event.
940fn svchunk cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 kind ulong to long
941svhead ob wb sv svccc
942svwt ob skchoi
943js_warr ob
944if ne kind 3
945js_wobj ob
946svwt ob skidx
947js_wulong ob 0
948svwt ob skdelta
949js_wobj ob
950if eq kind 0
951svwt ob skrole
952svwt ob svasst
953svwt ob skcont
954note "" as zero bytes of a non-empty text: under omc e5b32567 an empty text passed as a view has a
955note wrong length (omega-mv#775, fixed after this pin), and js_wsbuf then refuses it (jswearg)
956js_wsbuf ob svasst 0 0
957end
958if eq kind 1
959svwt ob skcont
960g0 = get sv 14
961js_wsbuf ob wb add 8192 g0 sub get sv 17 g0
962end
963js_wobje ob
964svwt ob skfin
965if eq kind 2
966svfr ob sv
967else
968js_wnull ob
969end
970js_wobje ob
971end
972js_warre ob
973if eq kind 3
974svusage ob sv
975end
976js_wobje ob
977ret svsse cs io ob js_wdone ob
978end
979
980note svsse: the writer's text ob[1040, 1040 + n) as one SSE event; -5 when n < 0
981fn svsse cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 n long to long
982if lt n 0
983ret -5
984end
985r = hssedat cs io view part ob 1040 ulong n
986ret hfe r hsseend cs io
987end
988
989note svtchunk: the tool_calls delta chunk (stream true)
990fn svtchunk cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 to long
991svhead ob wb sv svccc
992svwt ob skchoi
993js_warr ob
994js_wobj ob
995svwt ob skidx
996js_wulong ob 0
997svwt ob skdelta
998js_wobj ob
999svwt ob sktcall
1000svwcalls ob wb sv bd 1
1001js_wobje ob
1002svwt ob skfin
1003js_wnull ob
1004js_wobje ob
1005js_warre ob
1006js_wobje ob
1007ret svsse cs io ob js_wdone ob
1008end
1009
1010note ======== request parsing
1011
1012note svtemp: the temperature token's lexeme at bd[o, o + n) as TEMPMILLI (digits, optional
1013note fraction, used to 0.001), or 999999 when it is not a number from 0 to 2
1014fn svtemp bd edit uchar 983040 o ulong n ulong to ulong
1015ip = ulong 0
1016fr = ulong 0
1017fd = ulong 0
1018dot = false
1019for i ulong n
1020c = ulong get bd add o i
1021if eq c 46
1022if dot
1023ret 999999
1024end
1025dot = true
1026else
1027if or lt c 48 gt c 57
1028ret 999999
1029end
1030if dot
1031if lt fd 3
1032fr = add mul fr 10 sub c 48
1033fd = add fd 1
1034end
1035else
1036if ge ip 100
1037ret 999999
1038end
1039ip = add mul ip 10 sub c 48
1040end
1041end
1042end
1043for k ulong 3
1044if lt fd 3
1045fr = mul fr 10
1046fd = add fd 1
1047end
1048end
1049m = add mul ip 1000 fr
1050if gt m 2000
1051ret 999999
1052end
1053ret m
1054end
1055
1056note svtopp: the top_p token's lexeme at bd[o, o + n) in millionths (used to 0.000001), or jsnone
1057note when it is not a number from 0 to 1
1058fn svtopp bd edit uchar 983040 o ulong n ulong to ulong
1059ip = ulong 0
1060fr = ulong 0
1061fd = ulong 0
1062dot = false
1063for i ulong n
1064c = ulong get bd add o i
1065if eq c 46
1066if dot
1067ret jsnone
1068end
1069dot = true
1070else
1071if or lt c 48 gt c 57
1072ret jsnone
1073end
1074if dot
1075if lt fd 6
1076fr = add mul fr 10 sub c 48
1077fd = add fd 1
1078end
1079else
1080if ge ip 10
1081ret jsnone
1082end
1083ip = add mul ip 10 sub c 48
1084end
1085end
1086end
1087for k ulong 6
1088if lt fd 6
1089fr = mul fr 10
1090fd = add fd 1
1091end
1092end
1093m = add mul ip 1000000 fr
1094if gt m 1000000
1095ret jsnone
1096end
1097ret m
1098end
1099
1100note svmem: the value token of member key in object o, or jsnone (the first when repeated)
1101fn svmem src view uchar * tp view uint 262144 o ulong key view uchar * to ulong
1102if ne js_type tp o 1
1103ret jsnone
1104end
1105k = js_first tp o
1106for it ulong js_nkids tp o
1107if eq k jsnone
1108break
1109end
1110if js_streq src js_off tp k key
1111ret add k 1
1112end
1113k = js_mnext tp k
1114end
1115ret jsnone
1116end
1117
1118note svpcat: append text t to the template; false when it would pass tplmax (sv23)
1119fn svpcat wb edit uchar 1073664 sv edit ulong 64 t view uchar * to bool
1120pl = get sv 5
1121if gt add pl len t get sv 23
1122ret false
1123end
1124for i ulong len t
1125put wb add 90624 add pl i get t i
1126end
1127put sv 5 add pl len t
1128ret true
1129end
1130
1131note svpstr: append the decoded string token t to the template
1132fn svpstr bd edit uchar 983040 tp edit uint 262144 t ulong wb edit uchar 1073664 sv edit ulong 64 to bool
1133pl = get sv 5
1134cap = get sv 23
1135if ge pl cap
1136ret false
1137end
1138n = js_sget bd js_off tp t part wb add 90624 pl sub cap pl
1139if lt n 0
1140ret false
1141end
1142put sv 5 add pl ulong n
1143ret true
1144end
1145
1146note svpraw: append token t's JSON text as sent (a container's whole span) to the template
1147fn svpraw bd edit uchar 983040 tp edit uint 262144 t ulong wb edit uchar 1073664 sv edit ulong 64 to bool
1148ret svpcopy bd js_off tp t js_span tp t wb sv
1149end
1150
1151note svpcopy: append bd[o, o + n) to the template; false when it would pass tplmax (sv23)
1152fn svpcopy bd edit uchar 983040 o ulong n ulong wb edit uchar 1073664 sv edit ulong 64 to bool
1153d = add 90624 get sv 5
1154if gt add get sv 5 n get sv 23
1155ret false
1156end
1157for i ulong n
1158put wb add d i get bd add o i
1159end
1160put sv 5 add get sv 5 n
1161ret true
1162end
1163
1164note svparts: a content array of text parts into the template, joined with no separator
1165fn svparts bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1166e = js_first tp a
1167for it ulong js_nkids tp a
1168if eq e jsnone
1169break
1170end
1171ty = svmem bd tp e sktype
1172tx = svmem bd tp e sktext
1173if or eq ty jsnone eq tx jsnone
1174ret 17
1175end
1176if or ne js_type tp ty 6 ne js_type tp tx 6
1177ret 17
1178end
1179if not js_streq bd js_off tp ty sktext
1180ret 17
1181end
1182if not svpstr bd tp tx wb sv
1183ret 6
1184end
1185e = js_enext tp e
1186end
1187ret 0
1188end
1189
1190note svtc1r: one assistant tool call e as <tool_call>{"name":N,"arguments":A}</tool_call>
1191fn svtc1r bd edit uchar 983040 tp edit uint 262144 e ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1192f = svmem bd tp e skfunc
1193if eq f jsnone
1194ret 23
1195end
1196nm = svmem bd tp f sknam
1197if eq nm jsnone
1198ret 23
1199end
1200if ne js_type tp nm 6
1201ret 23
1202end
1203ar = svmem bd tp f skargs
1204aty = ulong 0
1205if ne ar jsnone
1206aty = js_type tp ar
1207end
1208if and and ne aty 0 ne aty 6 ne aty 1
1209ret 23
1210end
1211if not svpcat wb sv svtcn
1212ret 6
1213end
1214if not svpraw bd tp nm wb sv
1215ret 6
1216end
1217if not svpcat wb sv svtca
1218ret 6
1219end
1220ok = true
1221if eq aty 6
1222ok = svpstr bd tp ar wb sv
1223end
1224if eq aty 1
1225ok = svpraw bd tp ar wb sv
1226end
1227if eq aty 0
1228ok = svpcat wb sv svempt
1229end
1230if not ok
1231ret 6
1232end
1233if not svpcat wb sv svtcz
1234ret 6
1235end
1236ret 0
1237end
1238
1239note svtcs: an assistant message's tool_calls array a into the template
1240fn svtcs bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1241if ne js_type tp a 3
1242ret 23
1243end
1244e = js_first tp a
1245for it ulong js_nkids tp a
1246if eq e jsnone
1247break
1248end
1249if not svpcat wb sv svtco
1250ret 6
1251end
1252r = svtc1r bd tp e wb sv
1253if ne r 0
1254ret r
1255end
1256e = js_enext tp e
1257end
1258ret 0
1259end
1260
1261note svmsg1: one message object e into the template; 0 or an error code
1262fn svmsg1 bd edit uchar 983040 tp edit uint 262144 e ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1263if ne js_type tp e 1
1264ret 3
1265end
1266ro = svmem bd tp e skrole
1267if eq ro jsnone
1268ret 5
1269end
1270if or ne js_type tp ro 6 gt js_slen tp ro 32
1271ret 5
1272end
1273co = svmem bd tp e skcont
1274tc = svmem bd tp e sktcall
1275if ne tc jsnone
1276if eq js_type tp tc 10
1277tc = jsnone
1278end
1279end
1280ct = ulong 10
1281if ne co jsnone
1282ct = js_type tp co
1283end
1284if and eq co jsnone eq tc jsnone
1285ret 4
1286end
1287if and and ne ct 6 ne ct 10 ne ct 3
1288ret 4
1289end
1290if not svpstr bd tp ro wb sv
1291ret 6
1292end
1293if not svpcat wb sv svsep
1294ret 6
1295end
1296if eq ct 6
1297if not svpstr bd tp co wb sv
1298ret 6
1299end
1300end
1301if eq ct 3
1302r = svparts bd tp co wb sv
1303if ne r 0
1304ret r
1305end
1306end
1307if ne tc jsnone
1308r = svtcs bd tp tc wb sv
1309if ne r 0
1310ret r
1311end
1312end
1313if not svpcat wb sv svnl
1314ret 6
1315end
1316ret 0
1317end
1318
1319fn svmsgs bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1320if ne js_type tp a 3
1321ret 3
1322end
1323n = js_nkids tp a
1324if eq n 0
1325ret 3
1326end
1327e = js_first tp a
1328for it ulong n
1329if eq e jsnone
1330break
1331end
1332r = svmsg1 bd tp e wb sv
1333if ne r 0
1334ret r
1335end
1336e = js_enext tp e
1337end
1338if not svpcat wb sv svaprm
1339ret 6
1340end
1341ret 0
1342end
1343
1344note svtool1: one tools entry e; its name into wb[82432 + 64 i ..)
1345fn svtool1 bd edit uchar 983040 tp edit uint 262144 e ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1346ty = svmem bd tp e sktype
1347f = svmem bd tp e skfunc
1348if or eq ty jsnone eq f jsnone
1349ret 21
1350end
1351if ne js_type tp ty 6
1352ret 21
1353end
1354if not js_streq bd js_off tp ty skfunc
1355ret 21
1356end
1357nm = svmem bd tp f sknam
1358if eq nm jsnone
1359ret 21
1360end
1361if ne js_type tp nm 6
1362ret 21
1363end
1364ret svtname bd js_off tp nm wb sv
1365end
1366
1367note svtname: the tool name string at bd[o ..) into wb[82432 + 64 i ..), i = sv30
1368fn svtname bd edit uchar 983040 o ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1369i = get sv 30
1370if ge i 128
1371ret 21
1372end
1373m = js_sget bd o part wb add 82432 mul i 64 64
1374if lt m 1
1375ret 21
1376end
1377put svtnl i ulong m
1378put sv 30 add i 1
1379ret 0
1380end
1381
1382note svtools: the tools value a (jsnone when absent): checked, its names kept, its JSON text
1383note rendered as the template's first line
1384fn svtools bd edit uchar 983040 tp edit uint 262144 a ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1385if eq a jsnone
1386ret 0
1387end
1388ty = js_type tp a
1389if eq ty 10
1390ret 0
1391end
1392if ne ty 3
1393ret 21
1394end
1395n = js_nkids tp a
1396if gt n 128
1397ret 21
1398end
1399if eq n 0
1400ret 0
1401end
1402e = js_first tp a
1403for it ulong n
1404if eq e jsnone
1405break
1406end
1407r = svtool1 bd tp e wb sv
1408if ne r 0
1409ret r
1410end
1411e = js_enext tp e
1412end
1413if not svpcat wb sv svtlp
1414ret 6
1415end
1416if not svpraw bd tp a wb sv
1417ret 6
1418end
1419if not svpcat wb sv svnl
1420ret 6
1421end
1422ret 0
1423end
1424
1425note svopts: stream_options value v: include_usage true sets sv4
1426fn svopts bd edit uchar 983040 tp edit uint 262144 v ulong sv edit ulong 64 to void
1427if ne js_type tp v 1
1428ret
1429end
1430k = js_first tp v
1431for it ulong js_nkids tp v
1432if eq k jsnone
1433break
1434end
1435if js_streq bd js_off tp k skincl
1436if eq js_type tp add k 1 8
1437put sv 4 1
1438end
1439end
1440k = js_mnext tp k
1441end
1442end
1443
1444note svscal: one scalar member (key token k, value k + 1); 0 or an error code
1445fn svscal bd edit uchar 983040 tp edit uint 262144 k ulong sv edit ulong 64 to ulong
1446o = js_off tp k
1447v = add k 1
1448ty = js_type tp v
1449vo = js_off tp v
1450if or js_streq bd o skmaxt js_streq bd o skmaxc
1451if eq ty 10
1452ret 0
1453end
1454if or ne ty 7 not js_isu bd vo
1455ret 8
1456end
1457x = js_tou bd vo
1458if or eq x 0 gt x 999999
1459ret 8
1460end
1461put sv 1 x
1462ret 0
1463end
1464if js_streq bd o sktemp
1465if eq ty 10
1466ret 0
1467end
1468if ne ty 7
1469ret 7
1470end
1471if ne and js_nflags tp v 5 0
1472ret 7
1473end
1474m = svtemp bd vo js_span tp v
1475if gt m 2000
1476ret 7
1477end
1478put sv 2 m
1479ret 0
1480end
1481if js_streq bd o skseed
1482if eq ty 10
1483ret 0
1484end
1485if or ne ty 7 not js_isu bd vo
1486ret 9
1487end
1488put sv 3 add js_tou bd vo sub 1 OMUTSEED
1489ret 0
1490end
1491if js_streq bd o skstrm
1492if eq ty 8
1493put sv 0 1
1494ret 0
1495end
1496if or eq ty 9 eq ty 10
1497ret 0
1498end
1499ret 10
1500end
1501if js_streq bd o skn
1502if eq ty 10
1503ret 0
1504end
1505if or ne ty 7 not js_isu bd vo
1506ret 12
1507end
1508if ne js_tou bd vo 1
1509ret 12
1510end
1511ret 0
1512end
1513if js_streq bd o skopts
1514svopts bd tp v sv
1515end
1516ret 0
1517end
1518
1519note svstop1: one stop string token t into wb[73728 + 128 i ..)
1520fn svstop1 bd edit uchar 983040 tp edit uint 262144 t ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1521if ne js_type tp t 6
1522ret 18
1523end
1524ret svstopn bd js_off tp t wb sv
1525end
1526
1527note svstopn: the stop string at bd[o ..) into wb[73728 + 128 i ..), i = sv35
1528fn svstopn bd edit uchar 983040 o ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1529i = get sv 35
1530if ge i 8
1531ret 18
1532end
1533m = js_sget bd o part wb add 73728 mul i 128 128
1534if lt m 1
1535ret 18
1536end
1537put svstl i ulong m
1538put sv 35 add i 1
1539ret 0
1540end
1541
1542note svstopp: the stop value v (null, a string or an array of strings)
1543fn svstopp bd edit uchar 983040 tp edit uint 262144 v ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1544ty = js_type tp v
1545if eq ty 10
1546ret 0
1547end
1548if eq ty 6
1549ret svstop1 bd tp v wb sv
1550end
1551if ne ty 3
1552ret 18
1553end
1554n = js_nkids tp v
1555if gt n 8
1556ret 18
1557end
1558e = js_first tp v
1559for it ulong n
1560if eq e jsnone
1561break
1562end
1563r = svstop1 bd tp e wb sv
1564if ne r 0
1565ret r
1566end
1567e = js_enext tp e
1568end
1569ret 0
1570end
1571
1572note svtopk: the top_k value v; -1, 0 and null leave it off
1573fn svtopk bd edit uchar 983040 tp edit uint 262144 v ulong sv edit ulong 64 to ulong
1574ty = js_type tp v
1575if eq ty 10
1576ret 0
1577end
1578if ne ty 7
1579ret 19
1580end
1581vo = js_off tp v
1582if js_isu bd vo
1583x = js_tou bd vo
1584if gt x 999999
1585ret 19
1586end
1587put sv 28 x
1588ret 0
1589end
1590if js_isl bd vo
1591if eq js_tol bd vo neg long 1
1592ret 0
1593end
1594end
1595ret 19
1596end
1597
1598note svtchoi: the tool_choice value v; "none" turns tool-call detection off
1599fn svtchoi bd edit uchar 983040 tp edit uint 262144 v ulong sv edit ulong 64 to ulong
1600ty = js_type tp v
1601if eq ty 10
1602ret 0
1603end
1604if eq ty 6
1605o = js_off tp v
1606if js_streq bd o sknone
1607put sv 31 0
1608ret 0
1609end
1610if or js_streq bd o svauto js_streq bd o svreq
1611ret 0
1612end
1613ret 22
1614end
1615if eq ty 1
1616t = svmem bd tp v sktype
1617if ne t jsnone
1618if eq js_type tp t 6
1619if js_streq bd js_off tp t skfunc
1620ret 0
1621end
1622end
1623end
1624end
1625ret 22
1626end
1627
1628note svtest: a test-only member (key k), refused unless the server runs with test=1
1629fn svtest bd edit uchar 983040 tp edit uint 262144 k ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1630if eq get sv 22 0
1631ret 24
1632end
1633v = add k 1
1634ty = js_type tp v
1635vo = js_off tp v
1636if js_streq bd js_off tp k sktdel
1637if ne ty 7
1638ret 24
1639end
1640if not js_isu bd vo
1641ret 24
1642end
1643x = js_tou bd vo
1644if gt x 10000
1645ret 24
1646end
1647put sv 27 x
1648ret 0
1649end
1650if ne ty 6
1651ret 24
1652end
1653m = js_sget bd vo part wb 8192 16384
1654if lt m 0
1655ret 24
1656end
1657put sv 25 1
1658put sv 26 ulong m
1659ret 0
1660end
1661
1662note svscal2: the members stop, top_k, top_p, tool_choice and the test-only ones; 0 or a code
1663fn svscal2 bd edit uchar 983040 tp edit uint 262144 k ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1664o = js_off tp k
1665v = add k 1
1666if js_streq bd o svstop
1667ret svstopp bd tp v wb sv
1668end
1669if js_streq bd o sktopk
1670ret svtopk bd tp v sv
1671end
1672if js_streq bd o sktopp
1673ty = js_type tp v
1674if eq ty 10
1675ret 0
1676end
1677if ne ty 7
1678ret 20
1679end
1680if ne and js_nflags tp v 5 0
1681ret 20
1682end
1683m = svtopp bd js_off tp v js_span tp v
1684if eq m jsnone
1685ret 20
1686end
1687put sv 29 m
1688ret 0
1689end
1690if js_streq bd o sktchoi
1691ret svtchoi bd tp v sv
1692end
1693if or js_streq bd o sktout js_streq bd o sktdel
1694ret svtest bd tp k wb sv
1695end
1696ret 0
1697end
1698
1699note svreset: a request's options to their defaults
1700fn svreset sv edit ulong 64 to void
1701put sv 0 0
1702put sv 1 999999
1703put sv 2 1000
1704put sv 3 1
1705put sv 4 0
1706put sv 5 0
1707for i ulong 12
1708put sv add 24 i 0
1709end
1710put sv 29 1000000
1711put sv 31 1
1712end
1713
1714note svparse: the request body bd[0, n) into sv and the template; 0 or an error code
1715fn svparse bd edit uchar 983040 n ulong wb edit uchar 1073664 sv edit ulong 64 to ulong
1716tp = new uint 262144 0
1717x = js_init tp 983040 64
1718r = js_parse bd tp n
1719if lt r 0
1720if eq r neg long jsetok
1721ret 25
1722end
1723if eq OMUTJSON 0
1724svreset sv
1725put sv 1 1
1726ret 0
1727end
1728ret 1
1729end
1730if ne js_type tp 0 1
1731ret 2
1732end
1733svreset sv
1734k = js_first tp 0
1735for it ulong js_nkids tp 0
1736if eq k jsnone
1737break
1738end
1739e = svscal bd tp k sv
1740if eq e 0
1741e = svscal2 bd tp k wb sv
1742end
1743if ne e 0
1744ret e
1745end
1746k = js_mnext tp k
1747end
1748if eq get sv 37 1
1749ret svcprm bd tp wb sv
1750end
1751ma = svmem bd tp 0 skmsgs
1752if eq ma jsnone
1753ret 3
1754end
1755e = svtools bd tp svmem bd tp 0 sktools wb sv
1756if ne e 0
1757ret e
1758end
1759if eq get sv 30 0
1760put sv 31 0
1761end
1762ret svmsgs bd tp ma wb sv
1763end
1764
1765note svcprm: a /v1/completions body after its scalars: prompt (a non-empty string) is the whole
1766note template, with no role lines and no assistant prefix; stream true, messages, tools and
1767note tool_choice are refused; 0 or an error code
1768fn svcprm bd edit uchar 983040 tp edit uint 262144 wb edit uchar 1073664 sv edit ulong 64 to ulong
1769if eq get sv 0 1
1770ret 27
1771end
1772if ne svmem bd tp 0 skmsgs jsnone
1773ret 28
1774end
1775if ne svmem bd tp 0 sktools jsnone
1776ret 28
1777end
1778if ne svmem bd tp 0 sktchoi jsnone
1779ret 28
1780end
1781pr = svmem bd tp 0 skprompt
1782if eq pr jsnone
1783ret 26
1784end
1785if or ne js_type tp pr 6 eq js_slen tp pr 0
1786ret 26
1787end
1788put sv 30 0
1789put sv 31 0
1790if not svpstr bd tp pr wb sv
1791ret 6
1792end
1793ret 0
1794end
1795
1796note ======== encoding
1797
1798note svemit: token t, the m-th of an encoding: md 0 onto the context, md 1 into the forced tokens
1799fn svemit md ulong m ulong t ulong to void
1800if eq md 0
1801if lt get og 9 get og 1
1802ocpush t
1803end
1804ret
1805end
1806if lt m 4096
1807put svft m t
1808end
1809end
1810
1811note svenc1: the pre-token oarg[i, i + l) encoded and emitted after m tokens; the new count
1812fn svenc1 i ulong l ulong md ulong m ulong to ulong
1813for k ulong l
1814put bqtm k ushort get oarg add i k
1815end
1816c = bpenct l
1817for k ulong c
1818svemit md add m k ulong get bqtm k
1819end
1820ret add m c
1821end
1822
1823note svenc: encode src[0, n) of any length as obpenc does, in windows of at most 4000 bytes copied
1824note to oarg; a window ends at a pre-token boundary at least 64 bytes before its end (a pre-token
1825note is at most 64 bytes, so the boundaries agree with one pass), the last one at n. Returns the
1826note token count.
1827fn svenc src view uchar * n ulong md ulong to ulong
1828s = ulong 0
1829m = ulong 0
1830while lt s n bound 999999
1831w = sub n s
1832if gt w 4000
1833w = 4000
1834end
1835for k ulong w
1836put oarg k get src add s k
1837end
1838la = eq add s w n
1839i = ulong 0
1840while lt i w bound 4097
1841if and not la gt add i 64 w
1842break
1843end
1844l = bpclena i w
1845m = svenc1 i l md m
1846i = add i l
1847end
1848s = add s i
1849end
1850ret m
1851end
1852
1853note ======== generation
1854
1855note svgen: sample up to min(sv1, T - CT0) tokens; streams chunks when sv0 is 1. 0, or negative
1856note when a chunk could not be sent or the client left (svgone: finish 4, status 499). Between
1857note tokens the listener and pending connections are served (svside), waiting up to
1858note ollm_test_delay_ms.
1859fn svgen cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 to long
1860lim = sub get sv 19 get sv 6
1861if lt get sv 1 lim
1862lim = get sv 1
1863end
1864put sv 7 0
1865put sv 8 0
1866put sv 12 0
1867put sv 13 0
1868put sv 14 0
1869put sv 17 0
1870tmo = long mul get sv 27 1000000
1871for g ulong lim
1872svside bd sv tmo
1873if svgone cs
1874put sv 8 4
1875put sv 21 499
1876ret -104
1877end
1878if not svtake wb sv
1879break
1880end
1881r = svpend cs io ob wb sv 0
1882if lt r 0
1883ret r
1884end
1885end
1886if eq get sv 8 0
1887put sv 8 2
1888end
1889p = get sv 13
1890svclean wb sv 1
1891x = svstopc wb sv p
1892svtcall wb sv bd
1893r = svpend cs io ob wb sv 1
1894if lt r 0
1895ret r
1896end
1897if and eq get sv 0 1 gt get sv 32 0
1898ret svtchunk cs io ob wb sv bd
1899end
1900ret r
1901end
1902
1903note svtake: one sampled token into the context and the content; false when it ends the output
1904note (sv8: 1 token 0 or a stop string, 2 the content would pass its 65536 bytes)
1905fn svtake wb edit uchar 1073664 sv edit ulong 64 to bool
1906c = svdraw sv
1907if eq c 0
1908put sv 8 1
1909ret false
1910end
1911if not svfits sv c
1912put sv 8 2
1913ret false
1914end
1915svcap wb sv c
1916p = get sv 13
1917svclean wb sv 0
1918if svstopc wb sv p
1919ret false
1920end
1921ret true
1922end
1923
1924note svstopc: the earliest stop string match that ends past the content's old length p cuts the
1925note content there (finish stop); whether one did
1926fn svstopc wb edit uchar 1073664 sv edit ulong 64 p ulong to bool
1927n = get sv 13
1928b = jsnone
1929for i ulong get sv 35
1930l = get svstl i
1931s = ulong 0
1932if gt add p 1 l
1933s = sub add p 1 l
1934end
1935q = svfind wb s n add 73728 mul i 128 l
1936if lt q b
1937b = q
1938end
1939end
1940if eq b jsnone
1941ret false
1942end
1943put sv 13 b
1944put sv 12 0
1945put sv 8 1
1946ret true
1947end
1948
1949note svhold: how far a streamed chunk may send: not into the tail a stop string or the tool-call
1950note marker could still claim, nor (detection on) past the marker or into trailing whitespace
1951fn svhold wb edit uchar 1073664 sv edit ulong 64 to ulong
1952n = get sv 13
1953lo = get sv 14
1954h = n
1955for i ulong get sv 35
1956k = svpsuf wb lo n add 73728 mul i 128 get svstl i
1957if lt sub n k h
1958h = sub n k
1959end
1960end
1961if eq get sv 31 1
1962q = svfind wb lo n 74752 11
1963if lt q h
1964h = q
1965end
1966k2 = svpsuf wb lo n 74752 11
1967if lt sub n k2 h
1968h = sub n k2
1969end
1970h = svtrim wb lo h
1971end
1972ret h
1973end
1974
1975note svdraw: the next token (cmdsamp's step osstep, or svdrawf with top_k/top_p on, or the forced
1976note tokens of ollm_test_output then 0), advancing the seed stream when sampling
1977fn svdraw sv edit ulong 64 to ulong
1978if eq get sv 25 1
1979g = get sv 7
1980if lt g get sv 24
1981ret get svft g
1982end
1983ret 0
1984end
1985st = get sv 3
1986tm = get sv 2
1987c = ulong 0
1988if or eq tm 0 not svfon sv
1989c = osstep st tm
1990else
1991c = svdrawf st div f64 tm 1000.0 get sv 28 get sv 29
1992end
1993if ne tm 0
1994put sv 3 add st 11400714819323198485
1995end
1996ret c
1997end
1998
1999note svfon: whether top_k (0 < K < V) or top_p (< 1) filters
2000fn svfon sv edit ulong 64 to bool
2001k = get sv 28
2002if and gt k 0 lt k get og 0
2003ret true
2004end
2005ret lt get sv 29 1000000
2006end
2007
2008note svdrawf: odraw over the kept tokens (svkeep), the uniform scaled to their mass
2009fn svdrawf st ulong tau f64 k ulong p6 ulong to ulong
2010svlogit tau
2011o = get og 27
2012v = get og 0
2013cm = svkeep o v k p6
2014u = mul f64 shr smx st 11 1.1102230246251565e-16
2015r = svpick o v mul u cm
2016for j ulong v
2017put svmk j 0
2018end
2019ret r
2020end
2021
2022note svlogit: the next-token logits divided by tau, then made probabilities (as odraw does)
2023fn svlogit tau f64 to void
2024olastf
2025o = get og 27
2026v = get og 0
2027m = bitcast f64 ulong 18442240474082181120
2028for c ulong v
2029x = div get oa add o c tau
2030put oa add o c x
2031m = fmax m x
2032end
2033sm = osoft o v m
2034end
2035
2036note svkeep: mark the most probable tokens (ties to the lower id), at most K (0 < K < V), until
2037note their mass reaches top_p (p6 < 1000000); their mass
2038fn svkeep o ulong v ulong k ulong p6 ulong to f64
2039lim = v
2040if and gt k 0 lt k v
2041lim = k
2042end
2043pf = div f64 p6 1000000.0
2044cm = 0.0f64
2045for j ulong lim
2046b = svmax o v
2047put svmk b 1
2048cm = add cm get oa add o b
2049if and lt p6 1000000 ge cm pf
2050break
2051end
2052end
2053ret cm
2054end
2055
2056note svmax: the unmarked token of highest probability, the lower id on ties
2057fn svmax o ulong v ulong to ulong
2058b = ulong 0
2059bv = 0.0f64
2060fd = false
2061for c ulong v
2062if eq get svmk c 0
2063x = get oa add o c
2064if or not fd gt x bv
2065b = c
2066bv = x
2067fd = true
2068end
2069end
2070end
2071ret b
2072end
2073
2074note svpick: the first kept token (id order) whose running kept mass passes t; else the last kept
2075fn svpick o ulong v ulong t f64 to ulong
2076acc = 0.0f64
2077la = ulong 0
2078for c ulong v
2079if eq get svmk c 1
2080acc = add acc get oa add o c
2081la = c
2082if lt t acc
2083ret c
2084end
2085end
2086end
2087ret la
2088end
2089
2090note svfits: whether token c's bytes fit the raw buffer (8192) and, as up to three content bytes
2091note each, the content (65536)
2092fn svfits sv edit ulong 64 c ulong to bool
2093tl = obtlen c
2094rn = add get sv 12 tl
2095gn = add get sv 13 mul rn 3
2096ret not or or gt rn 8192 gt tl 4096 gt gn 65536
2097end
2098
2099note svcap: token c's bytes onto the raw buffer, c onto the context
2100fn svcap wb edit uchar 1073664 sv edit ulong 64 c ulong to void
2101obtcap c
2102rn = get sv 12
2103for i ulong get obon 0
2104put wb add rn i get obo i
2105end
2106put sv 12 add rn get obon 0
2107ocpush c
2108put sv 7 add get sv 7 1
2109end
2110
2111note svpend: when streaming, the content not yet sent (up to svhold; fin 1 all of it, or the text
2112note before the tool calls) as one chunk; 0 or the send error
2113fn svpend cs edit ulong 320 io edit uchar 131072 ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 fin ulong to long
2114if ne get sv 0 1
2115ret 0
2116end
2117h = get sv 13
2118if eq fin 0
2119h = svhold wb sv
2120else
2121if gt get sv 32 0
2122h = svtrim wb get sv 14 get sv 33
2123end
2124end
2125if le h get sv 14
2126ret 0
2127end
2128put sv 17 h
2129r = svchunk cs io ob wb sv 1
2130put sv 14 h
2131ret r
2132end
2133
2134note ======== tool calls in the output
2135
2136note svtdecl: whether bd[f, f + q) is a declared tool name
2137fn svtdecl wb edit uchar 1073664 bd edit uchar 983040 f ulong q ulong sv edit ulong 64 to bool
2138for i ulong get sv 30
2139if eq get svtnl i q
2140if svteq wb bd f q add 82432 mul i 64
2141ret true
2142end
2143end
2144end
2145ret false
2146end
2147
2148note svteq: whether bd[f, f + q) equals wb[b, b + q)
2149fn svteq wb edit uchar 1073664 bd edit uchar 983040 f ulong q ulong b ulong to bool
2150for j ulong q
2151if ne get bd add f j get wb add b j
2152ret false
2153end
2154end
2155ret true
2156end
2157
2158note svtargs: the arguments token ar of a block at wb[b ..) into bd at f (object: its JSON text;
2159note string: decoded; absent: {}); its length, or negative
2160fn svtargs wb edit uchar 1073664 bd edit uchar 983040 tp view uint 262144 ar ulong f ulong b ulong to long
2161if eq ar jsnone
2162put bd f 123
2163put bd add f 1 125
2164ret 2
2165end
2166ty = js_type tp ar
2167if eq ty 6
2168ret js_sget view part wb b 65536 js_off tp ar part bd f 65536
2169end
2170if ne ty 1
2171ret -1
2172end
2173o = add b js_off tp ar
2174n = js_span tp ar
2175for i ulong n
2176put bd add f i get wb add o i
2177end
2178ret long n
2179end
2180
2181note svtc1: the block JSON at content [s, s + l) as tool call sv32; false when it is not one
2182fn svtc1 wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 s ulong l ulong to bool
2183if or eq l 0 gt l 65536
2184ret false
2185end
2186tp = new uint 262144 0
2187x = js_init tp 65536 64
2188b = add 8192 s
2189r = js_parse view part wb b l tp l
2190if lt r 0
2191ret false
2192end
2193if ne js_type tp 0 1
2194ret false
2195end
2196ret svtc2 wb sv bd tp b
2197end
2198
2199note svtc2: the parsed block at wb[b ..) (tape tp) recorded as tool call sv32; whether it is one
2200fn svtc2 wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 tp view uint 262144 b ulong to bool
2201nm = svmem view part wb b 65536 tp 0 sknam
2202if eq nm jsnone
2203ret false
2204end
2205if ne js_type tp nm 6
2206ret false
2207end
2208q = svtnam wb sv bd js_off tp nm b
2209if lt q 1
2210ret false
2211end
2212f = get sv 34
2213g = add f ulong q
2214a = svtargs wb bd tp svmem view part wb b 65536 tp 0 skargs g b
2215if lt a 0
2216ret false
2217end
2218svtrec sv f ulong q g ulong a
2219ret true
2220end
2221
2222note svtnam: the name string at wb[b + o ..) decoded to bd[sv34 ..); its length when it is a
2223note declared tool name, else -1
2224fn svtnam wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 o ulong b ulong to long
2225f = get sv 34
2226q = js_sget view part wb b 65536 o part bd f 64
2227if lt q 1
2228ret -1
2229end
2230if not svtdecl wb bd f ulong q sv
2231ret -1
2232end
2233ret q
2234end
2235
2236note svtrec: record tool call sv32's name bd[f, f + q) and arguments bd[g, g + a)
2237fn svtrec sv edit ulong 64 f ulong q ulong g ulong a ulong to void
2238m = mul get sv 32 4
2239put svtcx m f
2240put svtcx add m 1 q
2241put svtcx add m 2 g
2242put svtcx add m 3 a
2243put sv 34 add g a
2244put sv 32 add get sv 32 1
2245end
2246
2247note svtblk: the block <tool_call>JSON</tool_call> at content p; the position after it, or jsnone
2248fn svtblk wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 p ulong n ulong to ulong
2249if gt add p 11 n
2250ret jsnone
2251end
2252if not svat wb p 74752 11
2253ret jsnone
2254end
2255s = add p 11
2256z = svfind wb s n 74763 12
2257if eq z jsnone
2258ret jsnone
2259end
2260if not svtc1 wb sv bd s sub z s
2261ret jsnone
2262end
2263ret add z 12
2264end
2265
2266note svtcall: with detection on, whether the content from the first marker on is 1 .. 8 tool-call
2267note blocks with only whitespace around: sv32 calls, sv33 the marker, finish tool_calls
2268fn svtcall wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 to void
2269put sv 32 0
2270put sv 34 0
2271if eq get sv 31 0
2272ret
2273end
2274n = get sv 13
2275q = svfind wb 0 n 74752 11
2276if eq q jsnone
2277ret
2278end
2279p = q
2280for it ulong 9
2281p = svskws wb p n
2282if ge p n
2283break
2284end
2285z = jsnone
2286if lt get sv 32 8
2287z = svtblk wb sv bd p n
2288end
2289if eq z jsnone
2290put sv 32 0
2291ret
2292end
2293p = z
2294end
2295if lt p n
2296put sv 32 0
2297ret
2298end
2299put sv 33 q
2300put sv 8 3
2301end
2302
2303note ======== requests during a generation
2304
2305note svwait: wait up to tmo ns for the listener or a pending connection (svpfd); the connection to
2306note serve now (bytes arrived, or it closed or failed), or -1. A new connection whose first byte
2307note does not arrive within 20 ms waits in a free pending slot (8; all taken: new connections stay
2308note in the listen backlog), so a connection that sends nothing costs the caller 20 ms once, not a
2309note head deadline. A pending connection is closed unanswered 10 s after its accept.
2310fn svwait sv edit ulong 64 tmo long to long
2311now = ulong monons
2312iv = tmo
2313n = ulong 0
2314for i ulong 8
2315f = get svpfd i
2316if ne f 0
2317age = sub now get svpat i
2318if ge age 10000000000
2319y = fclose int sub f 1
2320put svpfd i 0
2321else
2322rm = long sub 10000000000 age
2323if or lt iv 0 lt rm iv
2324iv = rm
2325end
2326svprec n sub f 1
2327put svpix n i
2328n = add n 1
2329end
2330end
2331end
2332fr = lt n 8
2333m = n
2334if fr
2335svprec n get sv 36
2336m = add n 1
2337end
2338r = pollfds svpl 0 mul m 8 iv
2339if le r 0
2340ret -1
2341end
2342for j ulong n
2343if ne get svpl add mul j 8 6 0
2344s = get svpix j
2345cp = long sub get svpfd s 1
2346put svpfd s 0
2347ret cp
2348end
2349end
2350if not fr
2351ret -1
2352end
2353c = haccept int get sv 36 0
2354if lt c 0
2355ret -1
2356end
2357w = waitio int c 1 long 20000000
2358if ne w 0
2359ret c
2360end
2361for k ulong 8
2362if eq get svpfd k 0
2363put svpfd k ulong add c 1
2364put svpat k ulong monons
2365ret -1
2366end
2367end
2368y2 = fclose int c
2369ret -1
2370end
2371
2372note svprec: pollfd record j for descriptor fd, events POLLIN
2373fn svprec j ulong fd ulong to void
2374b = mul j 8
2375put svpl b uchar and fd 255
2376put svpl add b 1 uchar and shr fd 8 255
2377put svpl add b 2 uchar and shr fd 16 255
2378put svpl add b 3 uchar and shr fd 24 255
2379put svpl add b 4 1
2380put svpl add b 5 0
2381put svpl add b 6 0
2382put svpl add b 7 0
2383end
2384
2385note svgone: whether the client of the generating request has left: a non-blocking peek (MSG_PEEK |
2386note MSG_DONTWAIT) of its socket reads end of stream or an error. Request bytes stay unread. A
2387note client that half-closes its sending side after the request counts as gone.
2388fn svgone cs edit ulong 320 to bool
2389r = recv int get cs HFD svpk 0 1 66
2390if eq r 0
2391ret true
2392end
2393ret and lt r 0 and ne r -11 ne r -4
2394end
2395
2396note svside: serve the connections svwait yields (the first wait up to tmo ns, then without waiting;
2397note at most 9 per call) with svside2
2398fn svside bd edit uchar 983040 sv edit ulong 64 tmo long to void
2399c = svwait sv tmo
2400for k ulong 9
2401if lt c 0
2402ret
2403end
2404svside2 c bd sv
2405c = svwait sv 0
2406end
2407end
2408
2409note svside2: one request while a completion generates: health busy, models, else 503 (logged)
2410fn svside2 c long bd edit uchar 983040 sv edit ulong 64 to void
2411cs = new ulong 320 0
2412io = new uchar 131072 0
2413r = hinit cs io int c 1
2414if lt r 0
2415x = hclose cs
2416ret
2417end
2418note bytes have arrived (svwait), so a head or body that stalls costs at most 250 ms or 1 s
2419hsettmo cs 250000000 250000000
2420hsetbdl cs 1000000000
2421put cs HBCAP 983040
2422r = hreqrd cs io
2423if gt r 0
2424put cs HKEEP 0
2425st = svbusy cs io bd
2426svlogs st
2427else
2428if lt r 0
2429x = hreject cs io r
2430end
2431end
2432x = hclose cs
2433end
2434
2435fn svbusy cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 to ulong
2436if and hispath cs io svphlth hismeth cs io svmget
2437x = svstat cs io 200 svthbsy
2438ret 200
2439end
2440if and hispath cs io svpmods hismeth cs io svmget
2441x = svmodw cs io
2442ret 200
2443end
2444note read the body before answering, so the client sees the 503 rather than a reset
2445n = hbodyall cs io bd
2446x = svs503 cs io
2447ret 503
2448end
2449
2450fn svlogs st ulong to void
2451oputc 67
2452oputc 32
2453oputu st
2454for i ulong 4
2455oputc 32
2456oputc 48
2457end
2458oputc 10
2459oflush
2460end
2461
2462note ======== serving
2463
2464note svid: response id chatcmpl-ollm-<request count> (cmpl-ollm- on /v1/completions) into
2465note wb[82176 ..), its length into sv15
2466fn svid wb edit uchar 1073664 sv edit ulong 64 to void
2467put sv 9 add get sv 9 1
2468p = ulong 82176
2469if eq get sv 37 1
2470p = svmcat wb p 82240 svcidp
2471else
2472p = svmcat wb p 82240 svidp
2473end
2474p = svmdec wb p 82240 get sv 9
2475put sv 15 sub p 82176
2476end
2477
2478fn svlog sv edit ulong 64 to void
2479oputc 67
2480oputc 32
2481oputu get sv 21
2482oputc 32
2483oputu get sv 6
2484oputc 32
2485oputu get sv 7
2486oputc 32
2487oputu get sv 8
2488oputc 32
2489oputu div sub ulong monons get sv 11 1000000
2490oputc 10
2491oflush
2492end
2493
2494note svprep: the context (token 0, then the template's encoding) and the forced tokens; 0 or 11
2495fn svprep wb edit uchar 1073664 sv edit ulong 64 to ulong
2496put og 9 0
2497ocpush 0
2498np = svenc view part wb 90624 get sv 5 get sv 5 0
2499put sv 6 add np 1
2500if ge get sv 6 get sv 19
2501ret 11
2502end
2503if eq get sv 25 1
2504c = ulong 0
2505if gt get sv 26 0
2506c = svenc view part wb 8192 get sv 26 get sv 26 1
2507end
2508if gt c 4096
2509c = 4096
2510end
2511put sv 24 c
2512end
2513ret 0
2514end
2515
2516note svchat: POST /v1/chat/completions
2517fn svchat cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit uchar 1073664 sv edit ulong 64 to long
2518note the request's clock and log fields come from svroute (svbeg)
2519n = hbodyall cs io bd
2520if lt n 0
2521put cs HKEEP 0
2522if eq n HEBIG
2523put sv 16 13
2524r = svfail cs io wb sv svbig
2525svlog sv
2526ret r
2527end
2528put sv 21 400
2529svlog sv
2530ret hrsperr cs io 400
2531end
2532note generation outlasts the 60 s body deadline: only the idle limit applies from here
2533hsetdl cs 0
2534e = svparse bd ulong n wb sv
2535if eq e 0
2536e = svprep wb sv
2537end
2538if ne e 0
2539put sv 16 e
2540st = ulong 400
2541if eq e 25
2542st = ulong 413
2543end
2544r = svfail cs io wb sv st
2545svlog sv
2546ret r
2547end
2548svid wb sv
2549put sv 10 ulong div realns 1000000000
2550ob = new uchar 409600 0
2551put sv 21 200
2552if eq get sv 0 1
2553r = hrspsse cs io
2554r = hfe r svchunk cs io ob wb sv 0
2555if lt r 0
2556svlog sv
2557ret r
2558end
2559r = svgen cs io ob wb sv bd
2560if lt r 0
2561svlog sv
2562ret r
2563end
2564r = svchunk cs io ob wb sv 2
2565if eq get sv 4 1
2566r = hfe r svchunk cs io ob wb sv 3
2567end
2568r = hfe r hssedat cs io svdone
2569r = hfe r hsseend cs io
2570r = hfe r hwfin cs io
2571svlog sv
2572ret r
2573end
2574r = svgen cs io ob wb sv bd
2575if lt r 0
2576svlog sv
2577ret r
2578end
2579m = svfull ob wb sv bd
2580if lt m 0
2581put sv 16 16
2582r = svfail cs io wb sv 500
2583svlog sv
2584ret r
2585end
2586r = svsend cs io ob 200 ulong m
2587svlog sv
2588ret r
2589end
2590
2591fn svroute cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit uchar 1073664 sv edit ulong 64 to long
2592svbeg sv
2593if hispath cs io svpchat
2594if or hismeth cs io svmpost eq OMUTMETH 0
2595put sv 37 0
2596ret svchat cs io bd wb sv
2597end
2598ret svend cs io wb sv 15 405
2599end
2600if hispath cs io svpcomp
2601if or hismeth cs io svmpost eq OMUTMETH 0
2602put sv 37 1
2603ret svchat cs io bd wb sv
2604end
2605ret svend cs io wb sv 15 405
2606end
2607if hispath cs io svphlth
2608if or hismeth cs io svmget eq OMUTMETH 0
2609ret svok cs io sv svthlth
2610end
2611ret svend cs io wb sv 15 405
2612end
2613if hispath cs io svpmods
2614if or hismeth cs io svmget eq OMUTMETH 0
2615ret svmods cs io sv
2616end
2617ret svend cs io wb sv 15 405
2618end
2619if eq OMUTPATH 0
2620ret svok cs io sv svthlth
2621end
2622ret svend cs io wb sv 14 404
2623end
2624
2625note svbeg: a request's log fields cleared, its clock started
2626fn svbeg sv edit ulong 64 to void
2627put sv 11 ulong monons
2628put sv 6 0
2629put sv 7 0
2630put sv 8 0
2631end
2632
2633note svend: error code e with status st, logged
2634fn svend cs edit ulong 320 io edit uchar 131072 wb edit uchar 1073664 sv edit ulong 64 e ulong st ulong to long
2635put sv 16 e
2636r = svfail cs io wb sv st
2637svlog sv
2638ret r
2639end
2640
2641note svok: the fixed JSON text t with status 200, logged
2642fn svok cs edit ulong 320 io edit uchar 131072 sv edit ulong 64 t view uchar * to long
2643put sv 21 200
2644r = svstat cs io 200 t
2645svlog sv
2646ret r
2647end
2648
2649note svconn: one accepted connection c carries one request, answered with Connection: close.
2650note Connections are served one at a time, so a kept-alive idle connection would hold the listener:
2651note allen-chat pools its upstream connection, and its next request queued 30 s behind the pooled
2652note one (the old idle limit) until this change. svwait hands over a connection only once its first
2653note bytes arrived; the rest of the head must arrive within 10 s.
2654fn svconn c long cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit uchar 1073664 sv edit ulong 64 to void
2655r = hinit cs io int c 1
2656if lt r 0
2657x = hclose cs
2658ret
2659end
2660hsettmo cs 10000000000 10000000000
2661hsetbdl cs 60000000000
2662note the JSON parser takes at most 983040 bytes (jsmaxn); larger Content-Length is 413 at the head
2663put cs HBCAP 983040
2664r = hreqrd cs io
2665if eq r HEBIG
2666put cs HKEEP 0
2667svbeg sv
2668x = svend cs io wb sv 13 svbig
2669end
2670if and lt r 0 ne r HEBIG
2671x = hreject cs io r
2672end
2673if gt r 0
2674put cs HKEEP 0
2675x = svroute cs io bd wb sv
2676end
2677x = hclose cs
2678end
2679
2680note svpre: whether the argument in oarg (length n) starts with t
2681fn svpre n ulong t view uchar * to bool
2682if lt n len t
2683ret false
2684end
2685for i ulong len t
2686if ne get oarg i get t i
2687ret false
2688end
2689end
2690ret true
2691end
2692
2693note svdig: the decimal oarg[a, n) (1 .. 7 digits), or 0
2694fn svdig a ulong n ulong to ulong
2695if or le n a gt sub n a 7
2696ret 0
2697end
2698v = ulong 0
2699for i ulong sub n a
2700c = ulong get oarg add a i
2701if or lt c 48 gt c 57
2702ret 0
2703end
2704v = add mul v 10 sub c 48
2705end
2706ret v
2707end
2708
2709note svcli: the options after MERGES: [MAXCONN] (first only), tplmax=N, test=1; MAXCONN
2710fn svcli sv edit ulong 64 na ulong to ulong
2711mx = ulong 1000000
2712for j ulong sub na 5
2713i = add j 5
2714n = oargp i
2715if svpre n svoptt
2716v = svdig 7 n
2717if or lt v 256 gt v 983040
2718exit 2
2719end
2720put sv 23 v
2721else
2722if and eq n len svopte svpre n svopte
2723put sv 22 1
2724else
2725if ne i 5
2726exit 2
2727end
2728mx = oargu 5
2729if eq mx 0
2730exit 2
2731end
2732end
2733end
2734end
2735ret mx
2736end
2737
2738note serve PORT CK MERGES [MAXCONN] [tplmax=N] [test=1]
2739fn cmdserve to void
2740na = oargc
2741if or lt na 5 gt na 8
2742exit 2
2743end
2744port = oargu 2
2745if or eq port 0 gt port 65535
2746exit 2
2747end
2748sv = new ulong 64 0
2749put sv 23 65536
2750mx = svcli sv na
2751ockload 3
2752v = get og 0
2753obload 4 v
2754ochipup
2755cs = new ulong 320 0
2756io = new uchar 131072 0
2757bd = new uchar 983040 0
2758wb = new uchar 1073664 0
2759a = new uchar 16 0
2760put sv 19 get og 1
2761x = svmcat wb 74752 74763 svtco
2762x = svmcat wb 74763 74775 svtcc
2763svmid
2764r = haddr4 a uint mul 2130706433 OMUTLOOP port
2765lf = hlisten a 16
2766if lt lf 0
2767exit 71
2768end
2769put sv 36 ulong lf
2770for i ulong len svport
2771oputc ulong get svport i
2772end
2773oputu port
2774oputc 10
2775oflush
2776nc = ulong 0
2777for j ulong 999999999
2778c = svwait sv 60000000000
2779if ge c 0
2780svconn c cs io bd wb sv
2781nc = add nc 1
2782if ge nc mx
2783break
2784end
2785end
2786end
2787y = fclose int lf
2788end