Kwker

Kwker Runtime C API reference

Every function and type of kwker_rt.h, the Kwker Runtime's header (Kwker 0.1.0). The guides walk through whole programs: Run a language model from C and Embeddings from C.

Overview

Kwker Runtime C API: run language models, text encoders and image models from C, without Python or PyTorch. Load a Hugging Face model folder; text goes in through the tokenizer and comes back as generated text (decoders) or embedding vectors (encoders), with the same results as KwkDecoder and KwkEncoder in Python. Link with -lkwker_rt.

Calls return 0 on success, or -1 with the reason in the matching _error() call: kwker_rt_tokenizer_error(), kwker_rt_decoder_error() or kwker_rt_last_error() (encoders). Handles are released with their _free() call.

Models: decoders of the Llama, Mistral, Qwen2 / Qwen3, SmolLM2 / SmolLM3, Phi-3 / Phi-4-mini, Granite, Helium, Gemma 2 / 3, Mixtral, Qwen3-MoE, Granite MoE, GPT-2, GPT-NeoX, Phi-2, OPT, StableLM, StarCoder2, OLMo and GPTBigCode families; encoders of the BERT, RoBERTa, XLM-RoBERTa, CamemBERT, DistilBERT, MPNet, ModernBERT, NomicBERT and EuroBERT families (MiniLM, BGE, E5, GTE, nomic-embed and the sentence-transformers models built on them); image models saved by kwker.cnn.save() (ResNet, MobileNet, EfficientNet, RegNet, ConvNeXt and the other torchvision families KwkCNN runs). A model that cannot run is refused with the reason.

Functions

kwker_rt_tokenizer Page

A tokenizer: text to token ids and back, read from a model's tokenizer.json (the Hugging Face tokenizers format), with the same ids and text as the tokenizers library. It reads BPE tokenizers (GPT-2, Llama 2 / 3, Mistral, Qwen2, SmolLM, Phi, ModernBERT and others), WordPiece tokenizers (BERT-family encoders) and Unigram tokenizers (XLM-R, multilingual-e5, bge-m3, T5). One tokenizer may be used from several threads at once.

CInclude kwker.h; link with -lkwker_c.
typedef struct kwker_rt_tokenizer kwker_rt_tokenizer;

kwker_rt_tokenizer_load Page

Text
Load a tokenizer.
  path: a model folder with tokenizer.json, or the tokenizer.json file itself
  out:  receives the tokenizer; release it with kwker_rt_tokenizer_free
Returns 0, or -1 with the reason in kwker_rt_tokenizer_error() (for example a tokenizer kind it does not read).
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_tokenizer_load(const char* path, kwker_rt_tokenizer** out);

Examples: Run a language model from C: The program, Sentence embeddings from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_tokenizer_chat Page

Text
Build a chat prompt from messages with the model's chat template: the same text as transformers'
apply_chat_template(messages, tokenize=False). The template comes from tokenizer_config.json (chat_template) or
chat_template.jinja beside tokenizer.json.
  messages:              a JSON array of messages, such as [{"role": "user", "content": "Hi"}]
  add_generation_prompt: 1 ends the text with the start of the assistant's turn, so the model answers next
  options:               NULL, or a JSON object whose members become template variables, such as
                         {"enable_thinking": false} or {"tools": [...]}
  text, cap:             room for cap bytes: the text's first cap - 1 bytes and a terminating 0 are written
  len:                   receives the text's length in bytes. Call with cap 0 to size text.
Returns 0, or -1 with the reason in kwker_rt_tokenizer_error(): no chat template, or a message list the template
refuses (its own error message, as Python would raise it).
Notes: tokenize the result with add_special 0 - the template already writes the special tokens it wants.
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_tokenizer_chat(const kwker_rt_tokenizer* t, const char* messages, int add_generation_prompt, const char* options, char* text,
                            int64_t cap, int64_t* len);

Examples: Run a language model from C: The program, Sentence embeddings from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_tokenizer_encode Page

Text
Turn UTF-8 text into token ids.
  text, len:   the text; len its length in bytes, or -1 for a 0-terminated string
  add_special: 1 adds the model's special tokens around the text (such as Llama's start token), 0 leaves them out
  ids, cap:    room for cap token ids
  n:           receives how many ids the text has; only the first cap are written. Call with cap 0 to size ids.
Returns 0, or -1 for text that is not valid UTF-8.
Notes: tokenizer.json's truncation length applies; its padding does not.
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_tokenizer_encode(const kwker_rt_tokenizer* t, const char* text, int64_t len, int add_special, int64_t* ids, int64_t cap,
                              int64_t* n);

Examples: Run a language model from C: The program, Sentence embeddings from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_tokenizer_decode Page

Text
Turn token ids back into UTF-8 text.
  ids, n:       the token ids
  skip_special: 1 leaves special tokens (such as an end-of-sequence token) out of the text
  text, cap:    room for cap bytes: the text's first cap - 1 bytes and a terminating 0 are written
  len:          receives the text's full length in bytes. Call with cap 0 to size text.
Returns 0, or -1 for a NULL argument.
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_tokenizer_decode(const kwker_rt_tokenizer* t, const int64_t* ids, int64_t n, int skip_special, char* text, int64_t cap,
                              int64_t* len);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_tokenizer_token_id Page

The id of one token, such as "<|im_start|>", or -1 if the tokenizer has no such token.

CInclude kwker.h; link with -lkwker_c.
int64_t kwker_rt_tokenizer_token_id(const kwker_rt_tokenizer* t, const char* token);

kwker_rt_tokenizer_vocab Page

The number of token ids the tokenizer knows.

CInclude kwker.h; link with -lkwker_c.
int64_t kwker_rt_tokenizer_vocab(const kwker_rt_tokenizer* t);

kwker_rt_tokenizer_free Page

Release a tokenizer.

CInclude kwker.h; link with -lkwker_c.
void kwker_rt_tokenizer_free(kwker_rt_tokenizer* t);

Examples: Run a language model from C: The program, Sentence embeddings from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_tokenizer_error Page

The reason the last tokenizer call on this thread returned -1.

CInclude kwker.h; link with -lkwker_c.
const char* kwker_rt_tokenizer_error(void);

Examples: Run a language model from C: The program, Sentence embeddings from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_decoder Page

A decoder: a language model that generates tokens, with KwkDecoder's results.

CInclude kwker.h; link with -lkwker_c.
typedef struct kwker_rt_decoder kwker_rt_decoder;

kwker_rt_decoder_load Page

Text
Load a language model from a Hugging Face model folder (config.json and model.safetensors or its shards).
  dir:           the folder
  precision:     "int4", "int8", "bf16" or "float32"; mixture-of-experts models load in "int4" or "int8"
  max_cache_len: the most token positions one sequence holds (prompt and new tokens)
  max_batch:     the number of sequences kwker_rt_decoder_step_batch runs at once (1 for one conversation)
  out:           receives the decoder; release it with kwker_rt_decoder_free
Returns 0, or -1 with the reason in kwker_rt_decoder_error().
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_decoder_load(const char* dir, const char* precision, int64_t max_cache_len, int64_t max_batch, kwker_rt_decoder** out);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_decoder_load_calibrated Page

Text
Load a language model in int4 calibrated on sample text: the int4 weights are fitted to the inputs they see on these tokens,
as KwkDecoder(..., calib=ids) does - usually a lower perplexity than kwker_rt_decoder_load's int4 at the same speed.
  dir, precision, max_cache_len, max_batch, out: as kwker_rt_decoder_load; precision "int4"
  calib:      the calibration token ids (a few thousand tokens of text like the model's use)
  n_calib:    how many
  calib_ctx:  the chunk length they are run in (512 as KwkDecoder's default)
Returns 0, or -1 with the reason in kwker_rt_decoder_error(). The first load runs the model in float32 over the tokens; the
pack cache keeps the result, so a later load with the same tokens skips that. Mixture-of-experts models are refused.
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_decoder_load_calibrated(const char* dir, const char* precision, int64_t max_cache_len, int64_t max_batch, const int64_t* calib,
                                     int64_t n_calib, int64_t calib_ctx, kwker_rt_decoder** out);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_decoder_generate Page

Text
Generate tokens after a prompt, greedily or by sampling, until an end-of-sequence token or max_new tokens.
  prompt, n_prompt:   the prompt's token ids
  max_new:            the most tokens to generate
  eos, neos:          the end-of-sequence ids (kwker_rt_decoder_eos gives the model's); the one that ends the run is
                      written last
  min_new:            generate at least this many tokens before an end-of-sequence id may come
  temperature:        0 for greedy decoding; above 0 samples with the settings below
  top_k:              keep the k most likely tokens (0: off)
  top_p:              keep the most likely tokens that together reach probability top_p (1: off)
  min_p:              drop tokens less likely than min_p times the most likely one (0: off)
  repetition_penalty: make tokens of the prompt and the text so far less likely (1: off)
  seed:               the random draws: the same seed gives the same tokens
  out, nout:          room for max_new token ids; nout receives how many were generated
Returns 0, or -1 with the reason in kwker_rt_decoder_error() (for example a prompt longer than max_cache_len allows).
Notes: the settings apply in Hugging Face generate's order. seed is the value Python's KwkDecoder.generate(
do_sample=True, generator=g) draws first from g with torch.randint(0, 2**62, (1,), generator=g): with it, both return
the same tokens.
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_decoder_generate(kwker_rt_decoder* d, const int64_t* prompt, int64_t n_prompt, int64_t max_new, const int64_t* eos,
                              int64_t neos, int64_t min_new, double temperature, int64_t top_k, double top_p, double min_p,
                              double repetition_penalty, uint64_t seed, int64_t* out, int64_t* nout);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_decoder_eos Page

Text
The model's end-of-sequence token ids, from generation_config.json or else config.json.
  ids, cap: room for cap ids
Returns how many there are (0 for a decoder made by kwker_rt_decoder_new, -1 for NULL); only the first cap are written.
CInclude kwker.h; link with -lkwker_c.
int64_t kwker_rt_decoder_eos(const kwker_rt_decoder* d, int64_t* ids, int64_t cap);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_decoder_step Page

Text
Run tokens through the model and get the next-token scores (logits): your own decoding loop.
  ids, n: n token ids of sequence seq, at cache positions pos to pos + n - 1
  nlog:   how many of the last rows to return logits for (-1: all n)
  logits: receives nlog rows of kwker_rt_decoder_vocab floats
Returns 0, or -1 with the reason in kwker_rt_decoder_error().
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_decoder_step(kwker_rt_decoder* d, const int64_t* ids, int64_t n, int64_t pos, int64_t nlog, int64_t seq, float* logits);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_decoder_step_batch Page

kwker_rt_decoder_step for several sequences at once: row i is token ids[i] of sequence seq[i] at position pos[i]; logits receives the last nlog rows' scores. Returns 0, or -1 with the reason in kwker_rt_decoder_error().

CInclude kwker.h; link with -lkwker_c.
int kwker_rt_decoder_step_batch(kwker_rt_decoder* d, const int64_t* ids, int64_t n, const int64_t* pos, const int64_t* seq, int64_t nlog,
                                float* logits);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_decoder_greedy Page

Greedy decoding from token tok at position pos of sequence 0: up to n tokens into out, nout receives how many. It stops after a token of eos; eos tokens are not chosen in the first min_new steps. Returns 0, or -1 with the reason in kwker_rt_decoder_error(). kwker_rt_decoder_generate runs the prompt too and samples as well.

CInclude kwker.h; link with -lkwker_c.
int kwker_rt_decoder_greedy(kwker_rt_decoder* d, int64_t tok, int64_t pos, int64_t n, const int64_t* eos, int64_t neos, int64_t min_new,
                            int64_t* out, int64_t* nout);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_decoder_vocab Page

The vocabulary size: the length of one logits row.

CInclude kwker.h; link with -lkwker_c.
int64_t kwker_rt_decoder_vocab(const kwker_rt_decoder* d);

kwker_rt_decoder_free Page

Release a decoder.

CInclude kwker.h; link with -lkwker_c.
void kwker_rt_decoder_free(kwker_rt_decoder* d);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_decoder_error Page

The reason the last decoder call on this thread returned -1.

CInclude kwker.h; link with -lkwker_c.
const char* kwker_rt_decoder_error(void);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

kwker_rt_encoder Page

An encoder: a text encoder that turns token ids into vectors (hidden states or sentence embeddings), with KwkEncoder's results.

CInclude kwker.h; link with -lkwker_c.
typedef struct kwker_rt_encoder kwker_rt_encoder;

kwker_rt_encoder_load Page

Text
Load a text encoder from a Hugging Face model folder (config.json and model.safetensors or its shards).
  dir:       the folder, such as a downloaded sentence-transformers model
  precision: "float32" (the checkpoint's weights exactly), "bf16" (faster) or "int8" (faster still; not ModernBERT, NomicBERT or EuroBERT)
  out:       receives the encoder; release it with kwker_rt_encoder_free
Returns 0, or -1 with the reason in kwker_rt_last_error().
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_encoder_load(const char* dir, const char* precision, kwker_rt_encoder** out);

Examples: Sentence embeddings from C: The program, Image models from C: The program

kwker_rt_encoder_embed Page

Text
Sentence embeddings: one vector per text, pooled and normalized as the folder's sentence-transformers settings say.
  B, S:    B texts as rows of S token ids (ids[b * S + i])
  lens:    row b holds lens[b] real tokens, the rest is ignored (NULL: all S)
  out:     receives B rows of kwker_rt_encoder_hidden floats
  threads: the most threads to use (0: every core)
Returns 0, or -1 with the reason in kwker_rt_last_error().
Notes: the pooling (mean, first token, max or last token) and the normalization come from modules.json and
1_Pooling/config.json; a folder without modules.json gets mean pooling. Other steps, such as a Dense layer, are refused.
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_encoder_embed(kwker_rt_encoder* e, int64_t B, int64_t S, const int64_t* ids, const int64_t* lens, float* out, int threads);

Examples: Sentence embeddings from C: The program, Image models from C: The program

kwker_rt_encoder_forward Page

Text
The model's last hidden state: one vector per token.
  B, S, lens: as for kwker_rt_encoder_embed
  types:      the token type ids, B rows of S (NULL: all 0)
  out:        receives B x S rows of kwker_rt_encoder_hidden floats; rows past a text's length are 0
  threads:    the most threads to use (0: every core)
Returns 0, or -1 with the reason in kwker_rt_last_error().
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_encoder_forward(kwker_rt_encoder* e, int64_t B, int64_t S, const int64_t* ids, const int64_t* lens, const int64_t* types,
                             float* out, int threads);

Examples: Sentence embeddings from C: The program, Image models from C: The program

kwker_rt_encoder_hidden Page

The hidden size: the length of one output vector.

CInclude kwker.h; link with -lkwker_c.
int64_t kwker_rt_encoder_hidden(const kwker_rt_encoder* e);

Examples: Sentence embeddings from C: The program

kwker_rt_encoder_free Page

Release an encoder.

CInclude kwker.h; link with -lkwker_c.
void kwker_rt_encoder_free(kwker_rt_encoder* e);

Examples: Sentence embeddings from C: The program

kwker_rt_last_error Page

The reason the last encoder call on this thread returned -1.

CInclude kwker.h; link with -lkwker_c.
const char* kwker_rt_last_error(void);

Examples: Sentence embeddings from C: The program, Image models from C: The program

kwker_rt_cnn Page

An image model (a CNN classifier or backbone): a model file that kwker.cnn.save() wrote in Python from a torchvision-style model - its operators, weights and, for int8, its calibrated scales - run with the same results as KwkCNN.

CInclude kwker.h; link with -lkwker_c.
typedef struct kwker_rt_cnn kwker_rt_cnn;

kwker_rt_cnn_load Page

Text
Load an image model file.
  path: the file kwker.cnn.save() wrote
  out:  receives the model
Returns 0, or -1 with the reason in kwker_rt_last_error().
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_cnn_load(const char* path, kwker_rt_cnn** out);

Examples: Sentence embeddings from C: The program, Image models from C: The program

kwker_rt_cnn_input_shape Page

The input image shape the model was saved for: shape receives [channels, height, width].

CInclude kwker.h; link with -lkwker_c.
void kwker_rt_cnn_input_shape(const kwker_rt_cnn* m, int64_t shape[3]);

Examples: Image models from C: The program

kwker_rt_cnn_output_size Page

The number of floats one image's output holds (1000 for an ImageNet classifier).

CInclude kwker.h; link with -lkwker_c.
int64_t kwker_rt_cnn_output_size(const kwker_rt_cnn* m);

Examples: Image models from C: The program

kwker_rt_cnn_run Page

Text
Run the model on a batch of images.
  B:       the number of images
  x:       B images as float32, [B][channels][height][width] (the layout PyTorch uses), preprocessed as for the model
  out:     receives B x kwker_rt_cnn_output_size() floats
  threads: the most threads to use (0: every core)
Returns 0, or -1 with the reason in kwker_rt_last_error(). One call at a time per model.
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_cnn_run(kwker_rt_cnn* m, int64_t B, const float* x, float* out, int threads);

Examples: Image models from C: The program, Sentence embeddings from C: The program

kwker_rt_cnn_free Page

Release an image model.

CInclude kwker.h; link with -lkwker_c.
void kwker_rt_cnn_free(kwker_rt_cnn* m);

Examples: Image models from C: The program

KWKER_RT_F32, KWKER_RT_BF16, KWKER_RT_F16, ... Page

Building from your own weights: kwker_rt_decoder_new and kwker_rt_encoder_new take a model's configuration and weight list directly (the lists KwkDecoder and KwkEncoder build in Python), for hosts that keep weights in their own format. Most programs load a folder instead. A kwker_rt_view is one tensor of a weight list: row-major data, its type (one of the KWKER_RT_F32 / BF16 / F16 / I8 codes) and its shape; data NULL stands for a tensor the model lacks, such as a bias.

CInclude kwker.h; link with -lkwker_c.
#define KWKER_RT_F32 0
#define KWKER_RT_BF16 1
#define KWKER_RT_F16 2
#define KWKER_RT_I8 3
typedef struct kwker_rt_view {
    const void* data;
    int32_t dtype, ndim;
    int64_t shape[4];
} kwker_rt_view;

kwker_rt_decoder_new Page

Text
A decoder from a configuration and weight list.
  c, nc / f, nf: the configuration integers and floats (KwkDecoder's DecodeRunner configuration)
  ts, nts:       the weight list
  flags:         KWKER_RT_BORROW to use the views in place (they must then outlive the decoder); 0 copies them
Returns 0, or -1 with the reason in kwker_rt_decoder_error().
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_decoder_new(const int64_t* c, int nc, const double* f, int nf, const kwker_rt_view* ts, int nts, int flags,
                         kwker_rt_decoder** out);

Examples: Run a language model from C: The program, Run a language model from C: Chat with an instruct model

KWKER_RT_SQ, KWKER_RT_BORROW, KWKER_RT_RELB, ... Page

Text
Flags of kwker_rt_encoder_new (and KWKER_RT_BORROW for kwker_rt_decoder_new):
  KWKER_RT_SQ      SmoothQuant factors follow the weights (int8, mode 18)
  KWKER_RT_BORROW  the embedding tables (decoders: every view) used in place; the caller keeps them alive
  KWKER_RT_RELB    MPNet's relative position bias: one more weight, [buckets][heads]; c[9] buckets, c[10] max distance
  KWKER_RT_MODERN  ModernBERT's layers and weight list; c[9] every how many layers one is global, c[10] half the
                   local window; eps[2] / eps[3] the global / local rotary thetas
  KWKER_RT_POSTLN  with KWKER_RT_MODERN, NomicBERT: post-LayerNorm layers, token type embeddings (c[8] types, their
                   table the last weight), no final LayerNorm
  KWKER_RT_RMS     with KWKER_RT_MODERN, EuroBERT: RMSNorms (weights only) throughout, the embedding norm optional
CInclude kwker.h; link with -lkwker_c.
#define KWKER_RT_SQ 1
#define KWKER_RT_BORROW 2
#define KWKER_RT_RELB 4
#define KWKER_RT_MODERN 8
#define KWKER_RT_POSTLN 16
#define KWKER_RT_RMS 32

kwker_rt_encoder_new Page

Text
An encoder from a configuration and weight list.
  c:     [layers, hidden, heads, intermediate, activation, mode (16 bf16, 18 int8, 32 float32; on CPUs with AMX also 1 bf16
         and 8 int8 on AMX tiles), vocabulary, positions, token types], then the flags' extra entries
  eps:   [layer norm epsilon, embedding norm epsilon], then the flags' extra entries
  w:     the weight list as float32 arrays (KwkEncoder's order; NULL for a tensor the model lacks)
  flags: the KWKER_RT_ flags above, or 0
Returns 0, or -1 with the reason in kwker_rt_last_error().
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_encoder_new(const int64_t* c, const double* eps, const float* const* w, int flags, kwker_rt_encoder** out);

Examples: Sentence embeddings from C: The program, Image models from C: The program

kwker_rt_encoder_run Page

Text
Run an encoder made by kwker_rt_encoder_new on token ids with your own positions and mask.
  ids, pos: B rows of S token ids and their position ids
  types:    the token type ids, or NULL (all 0)
  mask:     B rows of S floats, 0 for padding, or NULL (no padding)
  out:      receives B x S rows of hidden floats; padded rows are 0
  threads:  the most threads to use (0: every core)
Returns 0, or -1 with the reason in kwker_rt_last_error(). One call at a time per encoder.
CInclude kwker.h; link with -lkwker_c.
int kwker_rt_encoder_run(kwker_rt_encoder* e, int64_t B, int64_t S, const int64_t* ids, const int64_t* pos, const int64_t* types, const float* mask,
                         float* out, int threads);

Examples: Sentence embeddings from C: The program, Image models from C: The program

Applies to Kwker 0.1 · C
Last updated
Was this page helpful?
Kwker 0.1.x: the engines each platform chooses from at run time (details)
PlatformEngines
Linux x86-64AVX-512, AVX2, SSE4.2, portable
Linux ARM64SVE / SVE2 (64-bit keys), NEON, portable
Windows x64AVX-512, AVX2, SSE4.2, portable
Windows ARM64NEON, portable
macOS ARM64NEON, portable
macOS x86-64AVX2, SSE4.2, portable
Other CPUs (RISC-V, POWER, x86 without SSE4.2, ...)portable