#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <string.h>#include <stdbool.h>#include <errno.h>#include <signal.h>#include <dlfcn.h>#include <unistd.h>#include <time.h>#include <math.h>#include <dirent.h>#include <sys/stat.h>#include "tokenizer/true_bpe.h"#include "ck_features.h"Go to the source code of this file.
Macros | |
| #define | _GNU_SOURCE |
| #define | CK_CLI_DEFAULT_MAX_TOKENS 256 |
| #define | CK_CLI_EOS_MAX 8 |
| #define | CK_CLI_HISTORY_FILE ".ck_cli_history" |
| #define | CK_CLI_MAX_CONTEXT 32768 |
| #define | CK_CLI_OUTPUT_BUF_SIZE 4096 |
| #define | CK_CLI_VERSION "6.5.0" |
| #define | EOS_PATTERN_BUF_SIZE 64 |
| #define | EOS_PENDING_MAX 8 |
Typedefs | |
| typedef int(* | decode_t) (int32_t token, float *logits_out) |
| typedef int(* | embed_t) (const int32_t *tokens, int num_tokens) |
| typedef int(* | forward_t) (float *logits_out) |
| typedef void(* | free_t) (void) |
| typedef int(* | get_int_t) (void) |
| typedef float *(* | get_logits_t) (void) |
| typedef void *(* | get_ptr_t) (void) |
| typedef int(* | init_t) (const char *weights_path) |
| typedef int(* | kv_enable_t) (int capacity) |
| typedef void(* | kv_reset_t) (void) |
| typedef int(* | sample_argmax_t) (void) |
Enumerations | |
| enum | ChatTemplateType { CHAT_TEMPLATE_NONE = 0 , CHAT_TEMPLATE_QWEN , CHAT_TEMPLATE_LLAMA , CHAT_TEMPLATE_CHATML , CHAT_TEMPLATE_MISTRAL , CHAT_TEMPLATE_NONE = 0 , CHAT_TEMPLATE_QWEN , CHAT_TEMPLATE_LLAMA , CHAT_TEMPLATE_CHATML , CHAT_TEMPLATE_MISTRAL } |
Functions | |
| static char * | apply_chat_template (const ChatTemplate *tmpl, const char *system, const char *user) |
| static int | decode_bpe_token (const char *token, char *out, int max) |
| static ChatTemplateType | detect_chat_template (const char *model_name) |
| static bool | eos_is_potential_prefix (const char *token) |
| static void | eos_pattern_init (ChatTemplateType tmpl) |
| static bool | eos_pattern_process (const char *token_text, char *out_buf, size_t *out_len, void(*output_fn)(char *, size_t *, const char *), ChatTemplateType tmpl) |
| static void | eos_pattern_reset (void) |
| static bool | find_model_in_cache (const char *model_name, char *lib_out, char *weights_out, size_t out_size) |
| static const char * | get_cache_dir (void) |
| static void | handle_sigint (int sig) |
| static bool | is_eos_token (const CLIOptions *opt, int token) |
| static void | list_available_models (void) |
| static bool | load_eos_from_vocab_json (const char *weights_path, CLIOptions *opt) |
| static bool | load_model_api (const char *lib_path, ModelAPI *api) |
| int | main (int argc, char **argv) |
| static void | output_append (char *buf, size_t *len, const char *text) |
| static void | output_flush (char *buf, size_t *len) |
| static void | output_token (char *buf, size_t *len, const char *token) |
| static bool | parse_args (int argc, char **argv, CLIOptions *opt) |
| static bool | parse_eos_ids (const char *arg, CLIOptions *opt) |
| static void | print_banner (void) |
| static void | print_help (const char *prog) |
| static bool | process_repl_command (const char *line, CLIOptions *opt, ModelAPI *api) |
| static bool | resolve_symbol (void *handle, const char *name, void **out_ptr, bool required) |
| static int | run_prompt (ModelAPI *api, CKTrueBPE *tokenizer, CLIOptions *opt, const char *input) |
| static int | sample_top_p (float *logits, int vocab_size, float temperature, float top_p) |
Variables | |
| static int | g_decode_count = 0 |
| static double | g_decode_time_ms = 0.0 |
| static EOSPatternState | g_eos_state = {0} |
| static volatile sig_atomic_t | g_exit_requested = 0 |
| static volatile sig_atomic_t | g_generation_active = 0 |
| static double | g_prefill_time_ms = 0.0 |
| static int | g_prompt_tokens = 0 |
| static const ChatTemplate | g_templates [] |
| #define _GNU_SOURCE |
Definition at line 17 of file ck_cli_v6.5.c.
| #define CK_CLI_DEFAULT_MAX_TOKENS 256 |
Definition at line 41 of file ck_cli_v6.5.c.
| #define CK_CLI_EOS_MAX 8 |
Definition at line 42 of file ck_cli_v6.5.c.
| #define CK_CLI_HISTORY_FILE ".ck_cli_history" |
Definition at line 45 of file ck_cli_v6.5.c.
| #define CK_CLI_MAX_CONTEXT 32768 |
Definition at line 44 of file ck_cli_v6.5.c.
| #define CK_CLI_OUTPUT_BUF_SIZE 4096 |
Definition at line 43 of file ck_cli_v6.5.c.
| #define CK_CLI_VERSION "6.5.0" |
Definition at line 40 of file ck_cli_v6.5.c.
| #define EOS_PATTERN_BUF_SIZE 64 |
Text-based EOS pattern detection with pending output buffering.
When special tokens like <|im_end|> are tokenized as regular text (e.g., !, im, _end, !), we need to detect the pattern in the output and avoid outputting the partial pattern tokens.
This is a workaround for tokenizers that don't properly encode special tokens.
Definition at line 635 of file ck_cli_v6.5.c.
| #define EOS_PENDING_MAX 8 |
Definition at line 636 of file ck_cli_v6.5.c.
| typedef int(* decode_t) (int32_t token, float *logits_out) |
Definition at line 74 of file ck_cli_v6.5.c.
| typedef int(* embed_t) (const int32_t *tokens, int num_tokens) |
Definition at line 70 of file ck_cli_v6.5.c.
| typedef int(* forward_t) (float *logits_out) |
Definition at line 71 of file ck_cli_v6.5.c.
| typedef void(* free_t) (void) |
Definition at line 79 of file ck_cli_v6.5.c.
| typedef int(* get_int_t) (void) |
Definition at line 77 of file ck_cli_v6.5.c.
| typedef float*(* get_logits_t) (void) |
Definition at line 76 of file ck_cli_v6.5.c.
| typedef void*(* get_ptr_t) (void) |
Definition at line 78 of file ck_cli_v6.5.c.
| typedef int(* init_t) (const char *weights_path) |
Definition at line 69 of file ck_cli_v6.5.c.
| typedef int(* kv_enable_t) (int capacity) |
Definition at line 72 of file ck_cli_v6.5.c.
| typedef void(* kv_reset_t) (void) |
Definition at line 73 of file ck_cli_v6.5.c.
| typedef int(* sample_argmax_t) (void) |
Definition at line 75 of file ck_cli_v6.5.c.
| enum ChatTemplateType |
Definition at line 106 of file ck_cli_v6.5.c.
|
static |
|
static |
Decode GPT-2 byte-level BPE representation back to actual bytes.
GPT-2's tokenizer maps certain bytes to Unicode code points:
This function reverses that mapping.
| token | Input BPE token string (UTF-8) |
| out | Output buffer for decoded bytes |
| max | Size of output buffer |
Definition at line 427 of file ck_cli_v6.5.c.
References out_len, and token.
Referenced by output_token().
|
static |
Definition at line 573 of file ck_cli_v6.5.c.
References CHAT_TEMPLATE_CHATML, CHAT_TEMPLATE_LLAMA, CHAT_TEMPLATE_MISTRAL, and CHAT_TEMPLATE_QWEN.
Referenced by parse_args().
|
static |
Check if token might be start of EOS pattern.
Definition at line 682 of file ck_cli_v6.5.c.
References EOS_PATTERN_BUF_SIZE, g_eos_state, and token.
Referenced by eos_pattern_process().
|
static |
Definition at line 661 of file ck_cli_v6.5.c.
References CHAT_TEMPLATE_CHATML, CHAT_TEMPLATE_LLAMA, CHAT_TEMPLATE_MISTRAL, CHAT_TEMPLATE_QWEN, eos_pattern_reset(), and g_eos_state.
Referenced by run_prompt().
|
static |
Process a token for EOS pattern detection.
| token_text | The token text to process |
| out_buf | Output buffer for safe-to-output text |
| out_len | Current length of output buffer |
| tmpl | Chat template type |
Definition at line 725 of file ck_cli_v6.5.c.
References eos_is_potential_prefix(), EOS_PATTERN_BUF_SIZE, eos_pattern_reset(), EOS_PENDING_MAX, g_eos_state, and out_len.
Referenced by run_prompt().
|
static |
Definition at line 649 of file ck_cli_v6.5.c.
References g_eos_state.
Referenced by eos_pattern_init(), and eos_pattern_process().
|
static |
Definition at line 205 of file ck_cli_v6.5.c.
References get_cache_dir().
Referenced by parse_args().
|
static |
Definition at line 197 of file ck_cli_v6.5.c.
Referenced by find_model_in_cache(), and list_available_models().
|
static |
Definition at line 56 of file ck_cli_v6.5.c.
References g_exit_requested, and g_generation_active.
Referenced by main().
|
static |
|
static |
Definition at line 297 of file ck_cli_v6.5.c.
References get_cache_dir().
Referenced by parse_args().
|
static |
|
static |
Definition at line 535 of file ck_cli_v6.5.c.
References resolve_symbol().
Referenced by main().
| int main | ( | int | argc, |
| char ** | argv | ||
| ) |
Definition at line 1196 of file ck_cli_v6.5.c.
References ck_capability_t::best_kernel, CHAT_TEMPLATE_LLAMA, CHAT_TEMPLATE_MISTRAL, CHAT_TEMPLATE_QWEN, CK_CLI_HISTORY_FILE, ck_get_capabilities(), ck_true_bpe_create(), ck_true_bpe_free(), ck_true_bpe_load_binary(), g_exit_requested, handle_sigint(), ck_capability_t::has_ai_accel, ck_capability_t::has_fma, load_model_api(), merges, ck_capability_t::name, num_merges, offsets, parse_args(), print_banner(), process_repl_command(), run_prompt(), strings, vocab_size, and ck_capability_t::width.
|
static |
Definition at line 496 of file ck_cli_v6.5.c.
References CK_CLI_OUTPUT_BUF_SIZE, output_flush(), and text.
Referenced by output_token().
|
static |
Definition at line 490 of file ck_cli_v6.5.c.
Referenced by output_append(), and run_prompt().
|
static |
Definition at line 510 of file ck_cli_v6.5.c.
References decode_bpe_token(), output_append(), and token.
Referenced by run_prompt().
|
static |
Definition at line 1021 of file ck_cli_v6.5.c.
References CK_CLI_DEFAULT_MAX_TOKENS, detect_chat_template(), find_model_in_cache(), list_available_models(), load_eos_from_vocab_json(), parse_eos_ids(), and print_help().
Referenced by main().
|
static |
Definition at line 773 of file ck_cli_v6.5.c.
References CK_CLI_EOS_MAX, and end.
Referenced by parse_args().
|
static |
Definition at line 981 of file ck_cli_v6.5.c.
References CK_CLI_VERSION.
Referenced by main(), and print_help().
|
static |
Definition at line 988 of file ck_cli_v6.5.c.
References CK_CLI_DEFAULT_MAX_TOKENS, and print_banner().
Referenced by parse_args().
|
static |
|
static |
|
static |
Definition at line 792 of file ck_cli_v6.5.c.
References apply_chat_template(), CHAT_TEMPLATE_NONE, CK_CLI_DEFAULT_MAX_TOKENS, CK_CLI_MAX_CONTEXT, CK_CLI_OUTPUT_BUF_SIZE, ck_true_bpe_encode(), ck_true_bpe_id_to_token(), eos_pattern_init(), eos_pattern_process(), g_decode_count, g_decode_time_ms, g_exit_requested, g_generation_active, g_prefill_time_ms, g_prompt_tokens, g_templates, ids, is_eos_token(), out_len, output_flush(), output_token(), sample_top_p(), and vocab_size.
Referenced by main().
|
static |
|
static |
Definition at line 53 of file ck_cli_v6.5.c.
Referenced by run_prompt().
|
static |
Definition at line 52 of file ck_cli_v6.5.c.
Referenced by run_prompt().
|
static |
Definition at line 647 of file ck_cli_v6.5.c.
Referenced by eos_is_potential_prefix(), eos_pattern_init(), eos_pattern_process(), and eos_pattern_reset().
|
static |
Definition at line 47 of file ck_cli_v6.5.c.
Referenced by handle_sigint(), main(), process_repl_command(), and run_prompt().
|
static |
Definition at line 48 of file ck_cli_v6.5.c.
Referenced by handle_sigint(), and run_prompt().
|
static |
Definition at line 51 of file ck_cli_v6.5.c.
Referenced by run_prompt().
|
static |
Definition at line 54 of file ck_cli_v6.5.c.
Referenced by run_prompt().
|
static |
Definition at line 124 of file ck_cli_v6.5.c.
Referenced by run_prompt().