Class Tokenizer
java.lang.Object
smile.llm.tokenizer.Tiktoken
smile.llm.model.qwen.Tokenizer
- All Implemented Interfaces:
Tokenizer
-
Field Summary
Fields inherited from class Tiktoken
ranks, specialTokens -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the declared chat template behaviours.int[]encodeDialog(Message... dialog) Encodes a chat dialog in the Qwen chat-template format and leaves the assistant header open for completion.int[]encodeDialog(Message[] dialog, ChatOptions options) Encodes a chat dialog with optional tool-calling options.int[]encodeWithSpecials(String text) Encodes rendered chat text in which control tokens (<|im_start|>,<think>,<tool_call>, the vision placeholders, ...) stand for single tokens; everything between them is ordinary text.loadTokenizerJson(Path tokenizerJson) Extracts vocab from HuggingFacetokenizer.json(model.vocabplusadded_tokens).static TokenizerLoads a tokenizer from a HuggingFace checkpoint directory.intpad()Padding token id (<|endoftext|>).voidregisterAtomicTokens(Collection<String> contents) Registers added tokens (control tokens plus<tool_call>-style markers) that must stay a single token when they occur in text, as the reference tokenizer does.renderDialog(Message[] dialog, ChatOptions options) Renders a dialog with the checkpoint's chat template.voidrequireChatSpecialsInVocab(int vocabSize) Ensures chat control tokens lie in[0, vocabSize).voidsetChatTemplate(Qwen3ChatTemplate.Variant variant) Declares the chat template of the checkpoint, soencodeDialog(Message...)renders prompts like it.int[]Stop token ids used during generation.Methods inherited from class Tiktoken
allowSpecialTokens, decode, encode, encode, isSpecialTokenAllowed, load, size, specialToken, tokenize, tryDecode, tryDecode
-
Constructor Details
-
Tokenizer
-
Tokenizer
-
-
Method Details
-
pad
public int pad()Padding token id (<|endoftext|>).- Returns:
- pad token id.
-
stopTokens
public int[] stopTokens()Stop token ids used during generation.- Returns:
- stop token id array.
-
setChatTemplate
Declares the chat template of the checkpoint, soencodeDialog(Message...)renders prompts like it.- Parameters:
variant- the template's behaviours (Qwen3ChatTemplate.Variant.detect(String)), ornullfor the legacy hand-built prompt.
-
chatTemplate
Returns the declared chat template behaviours.- Returns:
- the variant, or
nullwhen none was declared.
-
registerAtomicTokens
Registers added tokens (control tokens plus<tool_call>-style markers) that must stay a single token when they occur in text, as the reference tokenizer does. Tokens missing from the vocabulary are ignored.- Parameters:
contents- the token strings.
-
encodeWithSpecials
Encodes rendered chat text in which control tokens (<|im_start|>,<think>,<tool_call>, the vision placeholders, ...) stand for single tokens; everything between them is ordinary text.- Parameters:
text- a rendered prompt.- Returns:
- token ids.
-
renderDialog
Renders a dialog with the checkpoint's chat template.- Parameters:
dialog- the messages.options- chat options (tools, thinking mode, reasoning effort); may benull.- Returns:
- the prompt text with the assistant turn open, or
nullwhen no template was declared.
-
encodeDialog
Encodes a chat dialog in the Qwen chat-template format and leaves the assistant header open for completion.- Parameters:
dialog- conversation turns.- Returns:
- token ids.
-
encodeDialog
Encodes a chat dialog with optional tool-calling options.- Parameters:
dialog- conversation turns.options- chat options; when tools are present (or the dialog already contains tool turns), usesQwen3ChatTemplate.- Returns:
- token ids.
-
of
Loads a tokenizer from a HuggingFace checkpoint directory.Order matters:
tokenizer.jsonis preferred because it carriesadded_tokenswith the true chat special ids. Loadingvocab.jsonalone and then appending specials atmaxId+1remaps<|im_start|>pastvocab_sizeand crashes embedding gather on GPU.- Parameters:
checkpointDir- model directory.- Returns:
- tokenizer instance.
- Throws:
IOException- if no supported tokenizer files are found.
-
loadTokenizerJson
Extracts vocab from HuggingFacetokenizer.json(model.vocabplusadded_tokens).- Parameters:
tokenizerJson- path totokenizer.json.- Returns:
- token byte sequence → id map (GPT-2 keys converted to raw bytes).
- Throws:
IOException- if the file is missing or not a valid tokenizer.json.
-
requireChatSpecialsInVocab
public void requireChatSpecialsInVocab(int vocabSize) Ensures chat control tokens lie in[0, vocabSize). Call after construction against the model config vocab size.- Parameters:
vocabSize- embedding / lm_head vocabulary size from model config.
-