Loading...
Searching...
No Matches
tokenizer_operator.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Artificial Intelligence Techniques, S.L. and Roberto López González
3
4#pragma once
5
6#include <array>
7#include <memory>
8#include <unordered_set>
9
12
13namespace opennn
14{
15
16vector<string> make_vocabulary(const unordered_map<string_view, size_t>&,
17 span<const string> reserved_tokens,
18 Index maximum_size,
19 Index minimum_frequency);
20
21inline constexpr size_t utf8_sequence_length(unsigned char lead) noexcept
22{
23 if ((lead & 0xE0) == 0xC0) return 2;
24 if ((lead & 0xF0) == 0xE0) return 3;
25 if ((lead & 0xF8) == 0xF0) return 4;
26 return 1;
27}
28
29inline constexpr bool is_utf8_continuation(unsigned char byte) noexcept
30{
31 return (byte & 0xC0) == 0x80;
32}
33
35{
36public:
38
39 virtual vector<string> tokenize(string_view text) const = 0;
40
41 virtual void build_vocabulary(const vector<vector<string>>& documents,
42 Index maximum_vocabulary_size,
43 Index minimum_token_frequency);
44
45 virtual void set_vocabulary(const vector<string>&);
46 const vector<string>& get_vocabulary() const noexcept { return vocabulary; }
47 const vector<string>& get_reserved_tokens() const noexcept { return reserved_tokens; }
48 const VocabularyMap& get_vocabulary_map() const noexcept { return vocabulary_map; }
49 Index get_vocabulary_size() const noexcept { return Index(vocabulary.size()); }
50
51 Index token_to_id(string_view token) const;
52 const string& id_to_token(Index id) const;
53
54 static constexpr Index UNK_INDEX = 1;
55 static constexpr Index START_INDEX = 2;
56 static constexpr Index END_INDEX = 3;
57
58 virtual vector<Index> encode(string_view text) const;
59 vector<Index> encode_sequence(const vector<string>& tokens, Index sequence_length) const;
60 vector<Index> encode_sequence(string_view text, Index sequence_length) const;
61 virtual string decode(const vector<Index>& ids) const;
62 virtual string decode_token(Index id) const;
63
64 virtual bool supports_incremental_decode() const noexcept { return false; }
65
66 Index get_unk_id() const noexcept { return unk_id; }
67
68 virtual unique_ptr<TokenizerOperator> clone() const = 0;
69 virtual string_view get_kind() const = 0;
70 virtual uint64_t fingerprint() const;
71
72 void to_JSON(JsonWriter&) const override;
73 void from_JSON(const Json*) override;
74
75protected:
76
77 vector<string> reserved_tokens;
78 Index unk_id = 0;
79 Index start_id = -1;
80 Index end_id = -1;
81
82 vector<string> vocabulary;
84
86 vector<Index> frame_sequence(span<const Index>, Index sequence_length) const;
87};
88
89unique_ptr<TokenizerOperator> make_tokenizer_operator(string_view kind);
90
92{
93public:
94
96 explicit WordLevelTokenizer(vector<string> reserved_tokens);
97
98 vector<string> tokenize(string_view text) const override;
99 vector<Index> encode(string_view text) const override;
100
101 unique_ptr<TokenizerOperator> clone() const override { return make_unique<WordLevelTokenizer>(*this); }
102 string_view get_kind() const override { return "WordLevel"; }
103 uint64_t fingerprint() const override;
104
105 void to_JSON(JsonWriter&) const override;
106 void from_JSON(const Json*) override;
107};
108
110{
111public:
112
114 explicit WordPieceTokenizer(const vector<string>& vocabulary);
115
116 void load_vocabulary(const filesystem::path& vocabulary_file);
117
118 void set_vocabulary(const vector<string>&) override;
119
120 vector<string> tokenize(string_view text) const override;
121 vector<Index> encode(string_view text) const override;
122
123 void build_vocabulary(const vector<vector<string>>&, Index, Index) override {}
124
125 void set_lower_case(bool value) noexcept { do_lower_case = value; }
126
127 unique_ptr<TokenizerOperator> clone() const override { return make_unique<WordPieceTokenizer>(*this); }
128 string_view get_kind() const override { return "WordPiece"; }
129 uint64_t fingerprint() const override;
130
131 void to_JSON(JsonWriter&) const override;
132 void from_JSON(const Json*) override;
133
134private:
135
136 vector<string> basic_tokenize(string_view text) const;
137 void wordpiece(const string&, vector<string>*, vector<Index>*) const;
138
139 string unk_token = "[UNK]";
140 string continuation_prefix = "##";
141 Index max_input_chars_per_word = 100;
142 bool do_lower_case = true;
143};
144
146{
147public:
148
150 BytePairTokenizer(const filesystem::path& vocabulary_json,
151 const filesystem::path& merges_txt);
152
153 void load(const filesystem::path& vocabulary_json,
154 const filesystem::path& merges_txt);
155
156 void set_vocabulary(const vector<string>&) override;
157
158 vector<string> tokenize(string_view text) const override;
159 vector<Index> encode(string_view text) const override;
160 string decode(const vector<Index>& ids) const override;
161 string decode_token(Index id) const override;
162 bool supports_incremental_decode() const noexcept override { return true; }
163
164 void build_vocabulary(const vector<vector<string>>&, Index, Index) override {}
165
166 vector<string> get_merges() const;
167 void set_merges(const vector<string>&);
168 void set_special_tokens(const vector<string>&);
169 Index get_special_token_id(string_view) const;
170
171 unique_ptr<TokenizerOperator> clone() const override { return make_unique<BytePairTokenizer>(*this); }
172 string_view get_kind() const override { return "BytePair"; }
173 uint64_t fingerprint() const override;
174
175 void to_JSON(JsonWriter&) const override;
176 void from_JSON(const Json*) override;
177
178 static constexpr string_view PAD_TOKEN = "[PAD]";
179
180protected:
181
182 struct Cache;
183
184 virtual vector<string> pre_tokenize(string_view text) const;
185 vector<string> bpe(const string& token) const;
186 void tokenize_into(string_view, vector<string>*, vector<Index>*) const;
187
189 unordered_map<uint32_t, unsigned char> byte_decoder;
191
192 shared_ptr<Cache> bpe_cache;
193 vector<string> special_strings;
194 unordered_set<Index> special_ids;
195};
196
198{
199public:
200
201 Qwen3Tokenizer() = default;
202 explicit Qwen3Tokenizer(const filesystem::path& directory);
203
204 void load(const filesystem::path& vocabulary_json,
205 const filesystem::path& merges_txt,
206 const filesystem::path& special_tokens_tsv);
207
208 Index get_im_start_id() const { return get_special_token_id("<|im_start|>"); }
209 Index get_im_end_id() const { return get_special_token_id("<|im_end|>"); }
210 Index get_endoftext_id() const { return get_special_token_id("<|endoftext|>"); }
211
212 unique_ptr<TokenizerOperator> clone() const override { return make_unique<Qwen3Tokenizer>(*this); }
213 string_view get_kind() const override { return "Qwen3"; }
214
215protected:
216
217 vector<string> pre_tokenize(string_view text) const override;
218};
219
220}
void load(const filesystem::path &vocabulary_json, const filesystem::path &merges_txt)
unique_ptr< TokenizerOperator > clone() const override
Definition tokenizer_operator.h:171
void tokenize_into(string_view, vector< string > *, vector< Index > *) const
void build_vocabulary(const vector< vector< string > > &, Index, Index) override
Definition tokenizer_operator.h:164
BytePairTokenizer(const filesystem::path &vocabulary_json, const filesystem::path &merges_txt)
virtual vector< string > pre_tokenize(string_view text) const
string_view get_kind() const override
Definition tokenizer_operator.h:172
void set_merges(const vector< string > &)
bool supports_incremental_decode() const noexcept override
Definition tokenizer_operator.h:162
void from_JSON(const Json *) override
static constexpr string_view PAD_TOKEN
Definition tokenizer_operator.h:178
StringMap< int > merge_ranks
Definition tokenizer_operator.h:190
vector< string > get_merges() const
unordered_map< uint32_t, unsigned char > byte_decoder
Definition tokenizer_operator.h:189
vector< string > bpe(const string &token) const
vector< Index > encode(string_view text) const override
string decode(const vector< Index > &ids) const override
unordered_set< Index > special_ids
Definition tokenizer_operator.h:194
string decode_token(Index id) const override
array< uint32_t, 256 > byte_encoder
Definition tokenizer_operator.h:188
vector< string > tokenize(string_view text) const override
Index get_special_token_id(string_view) const
void set_vocabulary(const vector< string > &) override
void set_special_tokens(const vector< string > &)
shared_ptr< Cache > bpe_cache
Definition tokenizer_operator.h:192
uint64_t fingerprint() const override
vector< string > special_strings
Definition tokenizer_operator.h:193
void to_JSON(JsonWriter &) const override
Definition json.h:92
Definition json.h:24
void load(const filesystem::path &vocabulary_json, const filesystem::path &merges_txt, const filesystem::path &special_tokens_tsv)
Index get_im_start_id() const
Definition tokenizer_operator.h:208
unique_ptr< TokenizerOperator > clone() const override
Definition tokenizer_operator.h:212
string_view get_kind() const override
Definition tokenizer_operator.h:213
vector< string > pre_tokenize(string_view text) const override
Index get_im_end_id() const
Definition tokenizer_operator.h:209
Qwen3Tokenizer(const filesystem::path &directory)
Index get_endoftext_id() const
Definition tokenizer_operator.h:210
Definition tokenizer_operator.h:35
Index unk_id
Definition tokenizer_operator.h:78
VocabularyMap vocabulary_map
Definition tokenizer_operator.h:83
virtual vector< Index > encode(string_view text) const
vector< Index > frame_sequence(span< const Index >, Index sequence_length) const
vector< string > vocabulary
Definition tokenizer_operator.h:82
void from_JSON(const Json *) override
virtual string decode(const vector< Index > &ids) const
Index token_to_id(string_view token) const
virtual string_view get_kind() const =0
virtual void build_vocabulary(const vector< vector< string > > &documents, Index maximum_vocabulary_size, Index minimum_token_frequency)
Index start_id
Definition tokenizer_operator.h:79
void to_JSON(JsonWriter &) const override
vector< Index > encode_sequence(const vector< string > &tokens, Index sequence_length) const
Index end_id
Definition tokenizer_operator.h:80
const string & id_to_token(Index id) const
vector< string > reserved_tokens
Definition tokenizer_operator.h:77
virtual string decode_token(Index id) const
const VocabularyMap & get_vocabulary_map() const noexcept
Definition tokenizer_operator.h:48
virtual void set_vocabulary(const vector< string > &)
static constexpr Index UNK_INDEX
Definition tokenizer_operator.h:54
const vector< string > & get_vocabulary() const noexcept
Definition tokenizer_operator.h:46
virtual uint64_t fingerprint() const
StringMap< Index > VocabularyMap
Definition tokenizer_operator.h:37
Index get_vocabulary_size() const noexcept
Definition tokenizer_operator.h:49
virtual vector< string > tokenize(string_view text) const =0
virtual bool supports_incremental_decode() const noexcept
Definition tokenizer_operator.h:64
const vector< string > & get_reserved_tokens() const noexcept
Definition tokenizer_operator.h:47
virtual unique_ptr< TokenizerOperator > clone() const =0
static constexpr Index START_INDEX
Definition tokenizer_operator.h:55
vector< Index > encode_sequence(string_view text, Index sequence_length) const
static constexpr Index END_INDEX
Definition tokenizer_operator.h:56
Index get_unk_id() const noexcept
Definition tokenizer_operator.h:66
void to_JSON(JsonWriter &) const override
void from_JSON(const Json *) override
uint64_t fingerprint() const override
WordLevelTokenizer(vector< string > reserved_tokens)
vector< string > tokenize(string_view text) const override
unique_ptr< TokenizerOperator > clone() const override
Definition tokenizer_operator.h:101
vector< Index > encode(string_view text) const override
string_view get_kind() const override
Definition tokenizer_operator.h:102
unique_ptr< TokenizerOperator > clone() const override
Definition tokenizer_operator.h:127
void from_JSON(const Json *) override
vector< Index > encode(string_view text) const override
void build_vocabulary(const vector< vector< string > > &, Index, Index) override
Definition tokenizer_operator.h:123
void set_vocabulary(const vector< string > &) override
void set_lower_case(bool value) noexcept
Definition tokenizer_operator.h:125
void to_JSON(JsonWriter &) const override
void load_vocabulary(const filesystem::path &vocabulary_file)
string_view get_kind() const override
Definition tokenizer_operator.h:128
uint64_t fingerprint() const override
WordPieceTokenizer(const vector< string > &vocabulary)
vector< string > tokenize(string_view text) const override
Definition configuration.h:10
constexpr size_t utf8_sequence_length(unsigned char lead) noexcept
Definition tokenizer_operator.h:21
Eigen::array< T, N > array
Definition tensor_types.h:736
unique_ptr< TokenizerOperator > make_tokenizer_operator(string_view kind)
constexpr bool is_utf8_continuation(unsigned char byte) noexcept
Definition tokenizer_operator.h:29
unordered_map< string, Value, TransparentStringHash, equal_to<> > StringMap
Definition string_utilities.h:72
vector< string > make_vocabulary(const unordered_map< string_view, size_t > &, span< const string > reserved_tokens, Index maximum_size, Index minimum_frequency)
Definition operator.h:28