← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
ck_session_v8.h
Go to the documentation of this file.
1#ifndef CK_SESSION_V8_H
2#define CK_SESSION_V8_H
3
4#include <stddef.h>
5#include <stdint.h>
6
7#include "ck_model_abi_v8.h"
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13#define CK_SESSION_ABI_V8_VERSION UINT32_C(1)
14
15typedef struct CKSessionV8 CKSessionV8;
16
17/* v1 ownership and concurrency contract:
18 * - The caller owns all request strings, output buffers, and callback user_data.
19 * Keep them alive until the synchronous call returns; the session retains none.
20 * - A token callback runs synchronously inside generate. Its utf8_text pointer is
21 * borrowed only for that callback invocation. Copy it before returning if needed.
22 * - The host must serialize encode, decode, format_chat, generate, and reset with
23 * one another. Current generated-model state is not certified for concurrent
24 * generation by separate sessions in one process.
25 * - cancel may be called from another thread while generate runs. It requests a
26 * cooperative stop; only generate returning acknowledges that native work has
27 * finished. A slow kernel can delay that return. Do not release request buffers,
28 * callback user_data, or session storage on the strength of cancel returning.
29 * - Quiesce all other callers before close; close is not a concurrent free API.
30 * The pointer returned by last_error is session-owned and becomes invalid after
31 * another session call or close.
32 */
33
46
55
60 /* Requests reset generated recurrent/KV state by default. Set this only
61 * when the caller deliberately continues the preceding request. */
63 /* Fail the request instead of silently truncating a prompt that does not
64 * fit the remaining context budget. Serving layers set this so their
65 * admission policy can never be bypassed by the native truncation
66 * fallback. */
68};
69
70typedef struct CKSessionConfigV8 {
71 uint32_t struct_size;
72 uint32_t abi_version;
73 const char *model_library_path;
74 const char *weights_path;
75 const char *manifest_path;
77 int32_t num_threads;
79 uint64_t reserved[8];
81
83 uint32_t struct_size;
84 uint32_t abi_version;
85 const char *system_text;
86 const char *user_text;
87 int32_t max_tokens;
88 /* Sampling is a host policy applied after the generated model's mandatory
89 * generation policy (for example Whisper timestamp masking). */
91 float top_p;
92 uint32_t flags;
93 uint32_t reserved0;
94 uint64_t reserved[8];
96
108
109/* Return zero to continue generation. A non-zero result requests a clean stop. */
111 void *user_data,
112 int32_t token_id,
113 const char *utf8_text,
114 size_t utf8_length,
115 int32_t sequence_index);
116
118
121 CKSessionV8 **session_out);
122
124
126 const CKSessionV8 *session,
127 CKModelRuntimeDescriptorV8 *descriptor,
128 size_t descriptor_size);
129
130/* Size-query convention: pass output=NULL/capacity=0 to receive the required
131 * element or byte count. The returned count excludes a text NUL terminator. */
133 CKSessionV8 *session,
134 const char *text,
135 int32_t *output,
136 int32_t capacity);
137
139 CKSessionV8 *session,
140 const int32_t *tokens,
141 int32_t token_count,
142 char *output,
143 int32_t capacity);
144
145/* Stop IDs from the actually loaded generated model. NULL output queries the
146 * count; a caller buffer smaller than that count is rejected. */
148 CKSessionV8 *session, int32_t *output, int32_t capacity);
149
151 CKSessionV8 *session,
152 const char *system_text,
153 const char *user_text,
154 char *output,
155 int32_t capacity);
156
158 CKSessionV8 *session,
159 const CKSessionGenerateRequestV8 *request,
161 void *user_data,
163
166const char *ck_session_v8_last_error(const CKSessionV8 *session);
167
168/* Optional bounded two-slot decode mode. This mode owns the generated model
169 * until close and is mutually exclusive with generate/reset. Both prompts are
170 * prefilled independently using token IDs supplied by the caller. A step
171 * advances each ready, uncancelled slot at most once; when both are eligible
172 * it calls the generated two-row entry, otherwise ordinary single-row decode.
173 * Cancellation is sampled at the next step boundary: a request made during a
174 * native kernel can still allow that in-flight step to finish. On success,
175 * advanced_mask identifies exactly which slots advanced. On a native error,
176 * advancement is unknown and both slots are poisoned until individually reset.
177 * All functions except request_cancel must
178 * be serialized by the host; cancellation is thread-safe. This is a native
179 * scheduling primitive, not an HTTP admission queue.
180 * The legacy whole-prompt prefill is synchronous and has no ticket until it
181 * returns. The staged decode-replay prefill below has a ticket before work.
182 */
183/* max_extra_bytes is a mandatory admission budget for two state arenas plus
184 * the batch workspace. needed_bytes reports that requirement even when the
185 * budget is too small; alignment padding is handled by the allocator. */
186int ck_session_v8_batch2_enable(CKSessionV8 *session, size_t max_extra_bytes,
187 size_t *needed_bytes);
188int ck_session_v8_batch2_prefill(CKSessionV8 *session, uint32_t slot,
189 const int32_t *tokens, int32_t count,
190 float *logits, uint64_t *ticket_out);
191/* Staged prefill reserves a slot and publishes its ticket before model work.
192 * Append replays at most count tokens through the generated single-token
193 * decoder. The caller chooses a bounded count and may schedule another ready
194 * slot between calls. Logits are scratch until the full prompt is consumed;
195 * the slot becomes decode-eligible only after exactly total_tokens tokens.
196 * Cancellation is observed between token calls, not inside a kernel. A
197 * cancelled or failed partial prefill must be reset before slot reuse.
198 * scratch_logits must point to vocab_size writable floats, remain valid for
199 * the duration of the call, and not alias native model or sequence memory.
200 * consumed_out is updated after each completed token; it is unchanged when
201 * validation or a previously requested cancellation rejects the call.
202 * Decode replay can differ numerically from the generated batched prefill
203 * path; callers must certify their selected artifact before using this mode.
204 * This ABI does not perform mixed prefill/decode projection batching. */
206 int32_t total_tokens,
207 uint64_t *ticket_out);
209 uint64_t ticket, const int32_t *tokens,
210 int32_t count, float *scratch_logits,
211 int32_t *consumed_out);
212int ck_session_v8_batch2_step(CKSessionV8 *session, const int32_t tokens[2],
213 float *const logits[2], uint32_t *advanced_mask);
214/* Opt-in one-row mixed step: one ready decode slot and one staged prefill slot
215 * enter the same generated batch2 call. prefill_slot identifies which token
216 * comes from the staged prompt; tickets[slot] must match each live generation.
217 * Both rows use their own sequence handle and current position. On success,
218 * advanced_mask is 3 and consumed_out is the staged prompt's cumulative token
219 * count. A pre-execution cancellation returns CANCELLED with no advancement.
220 * A runtime error may leave advancement unknown and poisons both slots; reset
221 * both before reuse. Cancellation during generated execution is acknowledged
222 * only after that call returns, so successful work is reported before reset.
223 * This is one prefill token plus one decode token, not a multi-row chunk. */
225 const int32_t tokens[2],
226 const uint64_t tickets[2],
227 uint32_t prefill_slot,
228 float *const logits[2],
229 uint32_t *advanced_mask,
230 int32_t *consumed_out);
231int ck_session_v8_batch2_reset_slot(CKSessionV8 *session, uint32_t slot);
232int ck_session_v8_batch2_position(CKSessionV8 *session, uint32_t slot,
233 int32_t *position_out);
234void ck_session_v8_batch2_request_cancel(CKSessionV8 *session, uint64_t ticket);
235
236#ifdef __cplusplus
237}
238#endif
239
240#endif
const char * ck_session_v8_last_error(const CKSessionV8 *session)
int ck_session_v8_batch2_position(CKSessionV8 *session, uint32_t slot, int32_t *position_out)
int ck_session_v8_open(const CKSessionConfigV8 *config, CKSessionV8 **session_out)
void ck_session_v8_close(CKSessionV8 *session)
int(* ck_session_token_callback_v8)(void *user_data, int32_t token_id, const char *utf8_text, size_t utf8_length, int32_t sequence_index)
int ck_session_v8_decode(CKSessionV8 *session, const int32_t *tokens, int32_t token_count, char *output, int32_t capacity)
int ck_session_v8_batch2_mixed_step(CKSessionV8 *session, const int32_t tokens[2], const uint64_t tickets[2], uint32_t prefill_slot, float *const logits[2], uint32_t *advanced_mask, int32_t *consumed_out)
int ck_session_v8_batch2_reset_slot(CKSessionV8 *session, uint32_t slot)
int ck_session_v8_batch2_prefill_append(CKSessionV8 *session, uint32_t slot, uint64_t ticket, const int32_t *tokens, int32_t count, float *scratch_logits, int32_t *consumed_out)
int ck_session_v8_batch2_prefill_begin(CKSessionV8 *session, uint32_t slot, int32_t total_tokens, uint64_t *ticket_out)
int ck_session_v8_batch2_enable(CKSessionV8 *session, size_t max_extra_bytes, size_t *needed_bytes)
void ck_session_v8_batch2_request_cancel(CKSessionV8 *session, uint64_t ticket)
struct CKSessionV8 CKSessionV8
int ck_session_v8_get_model_descriptor(const CKSessionV8 *session, CKModelRuntimeDescriptorV8 *descriptor, size_t descriptor_size)
void ck_session_v8_cancel(CKSessionV8 *session)
int ck_session_v8_format_chat(CKSessionV8 *session, const char *system_text, const char *user_text, char *output, int32_t capacity)
CKSessionStatusV8
@ CK_SESSION_V8_ERROR_LOAD
@ CK_SESSION_V8_OK
@ CK_SESSION_V8_ERROR_BUFFER_TOO_SMALL
@ CK_SESSION_V8_ERROR_CANCELLED
@ CK_SESSION_V8_ERROR_INIT
@ CK_SESSION_V8_ERROR_CAPABILITY
@ CK_SESSION_V8_ERROR_ABI
@ CK_SESSION_V8_ERROR_RUNTIME
@ CK_SESSION_V8_ERROR_INVALID_ARGUMENT
@ CK_SESSION_V8_ERROR_BUSY
uint32_t ck_session_v8_get_abi_version(void)
int ck_session_v8_reset(CKSessionV8 *session)
int ck_session_v8_batch2_prefill(CKSessionV8 *session, uint32_t slot, const int32_t *tokens, int32_t count, float *logits, uint64_t *ticket_out)
int ck_session_v8_generate(CKSessionV8 *session, const CKSessionGenerateRequestV8 *request, ck_session_token_callback_v8 callback, void *user_data, CKSessionGenerateResultV8 *result)
CKSessionStopReasonV8
@ CK_SESSION_STOP_CANCELLED
@ CK_SESSION_STOP_RUNTIME_ERROR
@ CK_SESSION_STOP_CALLBACK
@ CK_SESSION_STOP_EOS
@ CK_SESSION_STOP_NONE
@ CK_SESSION_STOP_TOKEN_LIMIT
int ck_session_v8_encode(CKSessionV8 *session, const char *text, int32_t *output, int32_t capacity)
CKSessionRequestFlagsV8
@ CK_SESSION_REQUEST_CONTINUE_STATE
@ CK_SESSION_REQUEST_TIMESTAMPS
@ CK_SESSION_REQUEST_REJECT_PROMPT_TRUNCATION
@ CK_SESSION_REQUEST_RAW_PROMPT
@ CK_SESSION_REQUEST_IGNORE_EOS
int ck_session_v8_batch2_step(CKSessionV8 *session, const int32_t tokens[2], float *const logits[2], uint32_t *advanced_mask)
int ck_session_v8_get_stop_token_ids(CKSessionV8 *session, int32_t *output, int32_t capacity)
const char * weights_path
uint64_t reserved[8]
const char * model_library_path
uint64_t required_capabilities
const char * manifest_path
const char * text
Definition tokenizer.h:564
const CKBPEConfig * config
Definition true_bpe.h:179
const uint8_t size_t count
Definition true_bpe.h:209