#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdint.h>#include <stdbool.h>#include "tokenizer/tokenizer.h"#include "tokenizer/hash_table.h"Go to the source code of this file.
Macros | |
| #define | GGUF_TOKEN_BYTE 6 |
| #define | GGUF_TOKEN_CONTROL 3 |
| #define | GGUF_TOKEN_NORMAL 1 |
| #define | GGUF_TOKEN_UNKNOWN 2 |
| #define | GGUF_TOKEN_UNUSED 5 |
| #define | GGUF_TOKEN_USER_DEFINED 4 |
Functions | |
| int | ck_tokenizer_encode_spm_dispatch (const CKTokenizer *tok, const char *text, int text_len, int32_t *ids, int max_ids) |
| static int | ck_tokenizer_encode_spm_impl (const CKTokenizer *tok, const char *text, int text_len, int32_t *ids, int max_ids) |
| static int | ck_tokenizer_encode_spm_llama_impl (const CKTokenizer *tok, const char *text, int text_len, int32_t *ids, int max_ids) |
| static int | ck_tokenizer_encode_spm_plain_segment (const CKTokenizer *tok, const char *text, int text_len, int32_t *ids, int max_ids) |
| int | ck_tokenizer_load_binary_with_scores (CKTokenizer *tok, int vocab_size, const int32_t *offsets, const char *strings, const float *scores, const uint8_t *types, int num_merges, const int32_t *merges) |
| static int32_t | ck_tokenizer_lookup_exact (const CKTokenizer *tok, const char *token) |
| static int32_t | ck_tokenizer_lookup_exact_n (const CKTokenizer *tok, const char *text, int text_len) |
| static int | preprocess_spm_llama_text (const char *text, int text_len, char *out, int out_max, bool add_space_prefix) |
| static int | preprocess_spm_text (const char *text, int text_len, char *out, int out_max, bool add_space_prefix) |
| static void | spm_build_byte_lookup (CKTokenizer *tok, const char *strings, const int32_t *offsets, int vocab_size) |
| static int | spm_count_unknown_run (const CKTokenizer *tok, const char *text, int text_len, size_t pos) |
| static int | spm_encode_byte_fallback (const CKTokenizer *tok, const char *text, int text_len, int32_t *ids, int max_ids) |
| static int | spm_find_candidates_at_pos (const CKTokenizer *tok, const char *text, int text_len, size_t pos, int32_t *candidates, int max_candidates) |
| static int32_t | spm_find_special_token_at_pos (const CKTokenizer *tok, const char *text, int text_len, size_t pos, size_t *match_len) |
| static int32_t | spm_get_byte_token (const CKTokenizer *tok, unsigned char byte_val) |
| static bool | spm_is_byte_token (const CKTokenizer *tok, int32_t token_id) |
| static int | spm_llama_resegment_node (const CKTokenizer *tok, const SpmLlamaNode *nodes, int node_id, int32_t *ids, int max_ids, int out_idx) |
| static bool | spm_token_allowed_in_dp (const CKTokenizer *tok, int32_t token_id) |
| static bool | spm_token_is_byte_format (const char *token) |
| static int | utf8_len (unsigned char c) |
| #define GGUF_TOKEN_BYTE 6 |
Definition at line 113 of file tokenizer_spm.c.
| #define GGUF_TOKEN_CONTROL 3 |
Definition at line 110 of file tokenizer_spm.c.
| #define GGUF_TOKEN_NORMAL 1 |
Definition at line 108 of file tokenizer_spm.c.
| #define GGUF_TOKEN_UNKNOWN 2 |
Definition at line 109 of file tokenizer_spm.c.
| #define GGUF_TOKEN_UNUSED 5 |
Definition at line 112 of file tokenizer_spm.c.
| #define GGUF_TOKEN_USER_DEFINED 4 |
Definition at line 111 of file tokenizer_spm.c.
| int ck_tokenizer_encode_spm_dispatch | ( | const CKTokenizer * | tok, |
| const char * | text, | ||
| int | text_len, | ||
| int32_t * | ids, | ||
| int | max_ids | ||
| ) |
Definition at line 792 of file tokenizer_spm.c.
References CKTokenizerConfig::add_bos, CKTokenizerConfig::add_eos, CKTokenizer::bos_id, ck_tokenizer_encode_spm_plain_segment(), CKTokenizer::config, CKTokenizer::eos_id, ids, max_ids, spm_find_special_token_at_pos(), text, and text_len.
Referenced by ck_tokenizer_encode().
|
static |
Definition at line 480 of file tokenizer_spm.c.
References CKTokenizerConfig::add_space_prefix, ck_tokenizer_id_to_token(), CKTokenizer::config, GGUF_TOKEN_USER_DEFINED, ids, max_ids, preprocess_spm_text(), CKTokenizer::scores, spm_count_unknown_run(), spm_encode_byte_fallback(), spm_find_candidates_at_pos(), spm_token_allowed_in_dp(), text, text_len, token, CKTokenizer::types, CKTokenizer::types_size, CKTokenizer::unk_id, and CKTokenizer::vocab_size.
Referenced by ck_tokenizer_encode_spm_plain_segment().
|
static |
Definition at line 290 of file tokenizer_spm.c.
References CKTokenizerConfig::add_space_prefix, ck_tokenizer_lookup_exact_n(), CKTokenizer::config, ids, left, max_ids, preprocess_spm_llama_text(), right, score, CKTokenizer::scores, CKTokenizer::scores_size, spm_llama_resegment_node(), text, text_len, utf8_len(), and CKTokenizer::vocab_size.
Referenced by ck_tokenizer_encode_spm_plain_segment().
|
static |
Definition at line 670 of file tokenizer_spm.c.
References CK_SPM_MODE_LLAMA, ck_tokenizer_encode_spm_impl(), ck_tokenizer_encode_spm_llama_impl(), CKTokenizer::config, ids, max_ids, CKTokenizerConfig::spm_mode, text, and text_len.
Referenced by ck_tokenizer_encode_spm_dispatch().
| int ck_tokenizer_load_binary_with_scores | ( | CKTokenizer * | tok, |
| int | vocab_size, | ||
| const int32_t * | offsets, | ||
| const char * | strings, | ||
| const float * | scores, | ||
| const uint8_t * | types, | ||
| int | num_merges, | ||
| const int32_t * | merges | ||
| ) |
Load vocabulary from memory-mapped binary data with scores and types.
This extended version supports SPM (SentencePiece) tokenizers which require token scores for Viterbi/DP encoding.
| tok | Tokenizer |
| vocab_size | Number of tokens |
| offsets | Array of offsets into strings pool |
| strings | String pool containing null-terminated tokens |
| scores | Array of token scores (float32), can be NULL |
| types | Array of token types (uint8), can be NULL |
| num_merges | Number of BPE merges |
| merges | Merge rules as (left, right, merged) triplets |
Definition at line 858 of file tokenizer_spm.c.
References ck_tokenizer_add_token(), ck_tokenizer_reset(), GGUF_TOKEN_BYTE, GGUF_TOKEN_CONTROL, GGUF_TOKEN_NORMAL, GGUF_TOKEN_UNKNOWN, merges, num_merges, offsets, score, CKTokenizer::scores, CKTokenizer::scores_size, spm_build_byte_lookup(), strings, token, CKTokenizer::types, CKTokenizer::types_size, and vocab_size.
Referenced by ck_tokenizer_load_binary().
|
static |
Definition at line 22 of file tokenizer_spm.c.
References ck_tokenizer_hash_table_lookup(), token, and CKTokenizer::vocab.
Referenced by ck_tokenizer_lookup_exact_n().
|
static |
Definition at line 29 of file tokenizer_spm.c.
References ck_tokenizer_lookup_exact(), id, text, and text_len.
Referenced by ck_tokenizer_encode_spm_llama_impl(), and spm_llama_resegment_node().
|
static |
Definition at line 202 of file tokenizer_spm.c.
References add_space_prefix, out_len, text, and text_len.
Referenced by ck_tokenizer_encode_spm_llama_impl().
|
static |
Definition at line 411 of file tokenizer_spm.c.
References add_space_prefix, out_len, text, and text_len.
Referenced by ck_tokenizer_encode_spm_impl().
|
static |
Definition at line 155 of file tokenizer_spm.c.
References CKTokenizer::byte_token_id, GGUF_TOKEN_BYTE, offsets, spm_token_is_byte_format(), strings, token, CKTokenizer::types, and vocab_size.
Referenced by ck_tokenizer_load_binary_with_scores().
|
static |
Definition at line 758 of file tokenizer_spm.c.
References ck_tokenizer_hash_table_lookup(), max_len, spm_token_allowed_in_dp(), text, text_len, CKTokenizer::unk_id, and CKTokenizer::vocab.
Referenced by ck_tokenizer_encode_spm_impl().
|
static |
Definition at line 684 of file tokenizer_spm.c.
References ids, max_ids, spm_get_byte_token(), text, text_len, and CKTokenizer::unk_id.
Referenced by ck_tokenizer_encode_spm_impl().
|
static |
Definition at line 707 of file tokenizer_spm.c.
References ck_tokenizer_hash_table_lookup(), max_len, spm_token_allowed_in_dp(), text, text_len, CKTokenizer::unk_id, and CKTokenizer::vocab.
Referenced by ck_tokenizer_encode_spm_impl().
|
static |
Definition at line 45 of file tokenizer_spm.c.
References CKTrieNode::children, ck_tokenizer_hash_table_lookup(), CK_TOKENIZER_MAX_TOKEN_LEN, CKTrieNode::is_special, max_len, CKTrie::root, text, text_len, CKTrieNode::token_id, CKTokenizer::vocab, and CKTokenizer::vocab_trie.
Referenced by ck_tokenizer_encode_spm_dispatch().
|
inlinestatic |
Definition at line 135 of file tokenizer_spm.c.
References CKTokenizer::byte_token_id, ck_tokenizer_lookup(), and CKTokenizer::unk_id.
Referenced by spm_encode_byte_fallback(), and spm_llama_resegment_node().
|
inlinestatic |
Definition at line 127 of file tokenizer_spm.c.
References GGUF_TOKEN_BYTE, CKTokenizer::types, and CKTokenizer::vocab_size.
|
static |
Definition at line 259 of file tokenizer_spm.c.
References ck_tokenizer_lookup_exact_n(), ids, max_ids, spm_get_byte_token(), spm_llama_resegment_node(), and CKTokenizer::unk_id.
Referenced by ck_tokenizer_encode_spm_llama_impl(), and spm_llama_resegment_node().
|
inlinestatic |
Definition at line 117 of file tokenizer_spm.c.
References GGUF_TOKEN_BYTE, GGUF_TOKEN_CONTROL, GGUF_TOKEN_UNUSED, CKTokenizer::types, and CKTokenizer::vocab_size.
Referenced by ck_tokenizer_encode_spm_impl(), spm_count_unknown_run(), and spm_find_candidates_at_pos().
|
inlinestatic |
|
inlinestatic |
Definition at line 189 of file tokenizer_spm.c.
Referenced by ck_tokenizer_encode(), and ck_tokenizer_encode_spm_llama_impl().