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 TypeMethodDescriptionint[]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.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|>).voidrequireChatSpecialsInVocab(int vocabSize) Ensures chat control tokens lie in[0, vocabSize).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.
-
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.
-