High-level tokenizer wrapper for DNN usage.
More...
High-level tokenizer wrapper for DNN usage.
Provides a simple API to encode and decode tokens for LLMs. Models are loaded via Tokenizer.load().
using namespace cv::dnn;
std::vector<int> ids = tok.
encode(
"hello world");
std::string text = tok.
decode(ids);
High-level tokenizer wrapper for DNN usage.
Definition Tokenizer.cs:26
MatOfInt encode(string text)
Encode UTF-8 text to token ids.
Definition Tokenizer.cs:113
static Tokenizer load(string model_config)
Load a tokenizer from a model directory.
Definition Tokenizer.cs:88
string decode(MatOfInt tokens)
Definition Tokenizer.cs:158
◆ __fromPtr__()
| static Tokenizer OpenCVForUnity.DnnModule.Tokenizer.__fromPtr__ |
( |
IntPtr | addr | ) |
|
|
static |
◆ decode()
| string OpenCVForUnity.DnnModule.Tokenizer.decode |
( |
MatOfInt | tokens | ) |
|
◆ Dispose()
| override void OpenCVForUnity.DnnModule.Tokenizer.Dispose |
( |
bool | disposing | ) |
|
|
protectedvirtual |
◆ encode() [1/2]
| MatOfInt OpenCVForUnity.DnnModule.Tokenizer.encode |
( |
List< string > | textChunks | ) |
|
Encode several text chunks as one sequence.
The chunks are laid out the way the model's own post_processor declares a pair of sequences, repeated for as many chunks as are given. For a BERT-family tokenizer that is [CLS] textChunks[0] [SEP] textChunks[1] [SEP] ... textChunks[N-1] [SEP]; for T5 it is textChunks[0] </s> textChunks[1] </s> .... Whatever the template puts between its two sequences is repeated between every neighbouring pair.
A single chunk is encoded exactly like encode(const std::string&). Two or more require a tokenizer whose post_processor declares a pair template: BERT and ALBERT, T5 and Gemma do; byte-level BPE models such as GPT-2 do not, and throw.
- Parameters
-
| textChunks | UTF-8 input chunks; must not be empty. |
- Returns
- Vector of token ids. Throws cv::Exception if
textChunks is empty, or if it holds more than one chunk and the loaded tokenizer has no pair template.
◆ encode() [2/2]
| MatOfInt OpenCVForUnity.DnnModule.Tokenizer.encode |
( |
string | text | ) |
|
Encode UTF-8 text to token ids.
Special-token text in text is recognized and mapped to its own id, and any wrapper the model's post_processor declares (e.g. BERT's [CLS]/[SEP]) is applied.
- Parameters
-
- Returns
- Vector of token ids (32-bit ids narrowed to int for convenience).
◆ getNativeObjAddr()
| IntPtr OpenCVForUnity.DnnModule.Tokenizer.getNativeObjAddr |
( |
| ) |
|
◆ load()
| static Tokenizer OpenCVForUnity.DnnModule.Tokenizer.load |
( |
string | model_config | ) |
|
|
static |
Load a tokenizer from a model directory.
Accepts either the model directory or the path to its config.json. The directory must contain:
config.json. An optional method field (one of: BPE, Gemma, SentencePiece, Unigram, WordPiece) selects the family; without it the family is detected from tokenizer.json, so a stock Hugging Face config works unmodified.
tokenizer.json produced by the corresponding model family.
- Parameters
-
| model_config | Model directory, or the path to its config.json. |
- Returns
- A Tokenizer ready for use. Throws cv::Exception if files are missing or
method is unsupported.
The documentation for this class was generated from the following file:
- OpenCVForUnity/Assets/OpenCVForUnity/Scripts/Core/org/opencv/dnn/Tokenizer.cs