gigatoken 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/gigatoken/tokenizer.rb +2 -1
- data/lib/gigatoken/version.rb +1 -1
- data/src/bpe/tiktoken.rs +1 -0
- data/src/pretokenize/fast/kimi.rs +5 -5
- data/src/pretokenize/fast/mask.rs +17 -0
- data/src/pretokenize/fast/mod.rs +1 -1
- data/src/pretokenize/fast/nemotron.rs +110 -19
- data/src/pretokenize/fast/o200k.rs +5 -5
- data/src/pretokenize/fast/o200k_family.rs +131 -34
- data/src/pretokenize/options.rs +17 -3
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2df6a7d15b5efe06273213ea12ef7e41ae219492f5c1d8f899169fa6afe5367f
|
|
4
|
+
data.tar.gz: 69dd3fdb4f7bd3dea7d93bff3cad1ac6ab9d6197212db92a94a89261ebeb01f5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a87fe8df1582a1bc16d1b77fdd842371a284a3cb7ac4b2f2f678292c5531e47a5e88b0570bac869931546b46e0aa8e13969aeac02fd921e5f88c7b396bda590c
|
|
7
|
+
data.tar.gz: e4e801206186a9b508218a302ee2fc8d3c94281bfbf1e055f11891992fc0f2fb5d6cdae17eb25dba4ee85069b58a1afabb769109e88ace738c3583a36a328df6
|
data/lib/gigatoken/tokenizer.rb
CHANGED
|
@@ -46,7 +46,8 @@ module Gigatoken
|
|
|
46
46
|
# pretokenization scheme nor special tokens — nothing is guessed here,
|
|
47
47
|
# so `pretokenizer:` is required: one of the schemes gigatoken ships
|
|
48
48
|
# (see Native.pretokenizer_names, e.g. "gpt2"/"r50k", "gpt4"/"cl100k",
|
|
49
|
-
# "o200k", "qwen2", "qwen35", "olmo3", "deepseek_v3", "nemotron",
|
|
49
|
+
# "o200k", "qwen2", "qwen35", "olmo3", "deepseek_v3", "nemotron",
|
|
50
|
+
# "nemotron_right_aligned" or "kimi").
|
|
50
51
|
# `special_tokens:` maps token content to id (none by default).
|
|
51
52
|
def self.from_tiktoken(path, pretokenizer:, special_tokens: {})
|
|
52
53
|
native = Native::BPETokenizer.from_tiktoken(path.to_s, pretokenizer, special_tokens)
|
data/lib/gigatoken/version.rb
CHANGED
data/src/bpe/tiktoken.rs
CHANGED
|
@@ -1973,6 +1973,7 @@ mod tests {
|
|
|
1973
1973
|
PretokenizerType::DeepSeekV3,
|
|
1974
1974
|
PretokenizerType::O200k,
|
|
1975
1975
|
PretokenizerType::Nemotron,
|
|
1976
|
+
PretokenizerType::NemotronRightAligned,
|
|
1976
1977
|
PretokenizerType::Kimi,
|
|
1977
1978
|
];
|
|
1978
1979
|
let input = "Hello, 世界! café 12345\r\ncan't stop".as_bytes();
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
//!
|
|
5
5
|
//! The o200k scheme with a leading `[\p{Han}]+` alternative, Han excluded
|
|
6
6
|
//! from the letter brackets, and no `/` in the absorbed punct tail. See
|
|
7
|
-
//! `o200k_family` (`CONTRACTIONS = true`, `
|
|
7
|
+
//! `o200k_family` (`CONTRACTIONS = true`, `DIGITS = DIGITS_3`,
|
|
8
8
|
//! `SLASH = false`, `HAN = true`).
|
|
9
9
|
|
|
10
10
|
use super::mask::{MaskScheme, MaskState};
|
|
11
|
-
use super::o200k_family;
|
|
11
|
+
use super::o200k_family::{self, DIGITS_3};
|
|
12
12
|
use crate::pretokenize::Pretoken;
|
|
13
13
|
|
|
14
14
|
pub(crate) struct KimiScheme;
|
|
@@ -16,20 +16,20 @@ pub(crate) struct KimiScheme;
|
|
|
16
16
|
impl MaskScheme for KimiScheme {
|
|
17
17
|
#[inline(always)]
|
|
18
18
|
fn advance(bytes: &[u8], pos: usize) -> usize {
|
|
19
|
-
o200k_family::advance_pos::<true,
|
|
19
|
+
o200k_family::advance_pos::<true, DIGITS_3, false, true>(bytes, pos)
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
#[cfg(target_arch = "aarch64")]
|
|
23
23
|
#[inline(always)]
|
|
24
24
|
fn batch_masks(bytes: &[u8], scan: usize) -> (u64, u64) {
|
|
25
|
-
o200k_family::batch_masks::<true,
|
|
25
|
+
o200k_family::batch_masks::<true, DIGITS_3, false, true>(bytes, scan)
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
#[cfg(target_arch = "x86_64")]
|
|
29
29
|
#[inline(always)]
|
|
30
30
|
unsafe fn batch_masks_x86<const AVX512: bool>(bytes: &[u8], scan: usize) -> (u64, u64) {
|
|
31
31
|
// SAFETY: the caller detected the tier (trait contract).
|
|
32
|
-
unsafe { o200k_family::batch_masks_x86::<AVX512, true,
|
|
32
|
+
unsafe { o200k_family::batch_masks_x86::<AVX512, true, DIGITS_3, false, true>(bytes, scan) }
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
@@ -566,6 +566,23 @@ pub(crate) fn digit_run_splits3(d: u64) -> u64 {
|
|
|
566
566
|
b
|
|
567
567
|
}
|
|
568
568
|
|
|
569
|
+
/// [`digit_run_splits3`] mirrored for right-aligned groups of three: each
|
|
570
|
+
/// run's last digit marks a group end, re-armed 3 back while the run
|
|
571
|
+
/// continues; a group starts after every group end but the run's last,
|
|
572
|
+
/// and at the run start. Exact for runs that end inside the batch.
|
|
573
|
+
#[inline(always)]
|
|
574
|
+
pub(crate) fn digit_run_splits3_right(d: u64) -> u64 {
|
|
575
|
+
let mut e = d & !(d >> 1); // run ends (last digits)
|
|
576
|
+
let mut c = d & (d << 1) & (d << 2) & (d << 3); // p-3..p all digits
|
|
577
|
+
let mut sh = 3u32;
|
|
578
|
+
while sh < 64 {
|
|
579
|
+
e |= (e & c) >> sh;
|
|
580
|
+
c &= c << sh;
|
|
581
|
+
sh <<= 1;
|
|
582
|
+
}
|
|
583
|
+
((e & (d >> 1)) << 1) | (d & !(d << 1))
|
|
584
|
+
}
|
|
585
|
+
|
|
569
586
|
// -----------------------------------------------------------------------
|
|
570
587
|
// The batch walker
|
|
571
588
|
// -----------------------------------------------------------------------
|
data/src/pretokenize/fast/mod.rs
CHANGED
|
@@ -23,7 +23,7 @@ pub mod r50k;
|
|
|
23
23
|
pub use cl100k::FastCl100kPretokenizer;
|
|
24
24
|
pub use deepseek_v3::FastDeepSeekV3Pretokenizer;
|
|
25
25
|
pub use kimi::FastKimiPretokenizer;
|
|
26
|
-
pub use nemotron::FastNemotronPretokenizer;
|
|
26
|
+
pub use nemotron::{FastNemotronPretokenizer, FastNemotronRightAlignedPretokenizer};
|
|
27
27
|
pub use o200k::FastO200kPretokenizer;
|
|
28
28
|
pub use olmo3::FastOlmo3Pretokenizer;
|
|
29
29
|
pub use qwen2::FastQwen2Pretokenizer;
|
|
@@ -1,46 +1,53 @@
|
|
|
1
1
|
//! Fast pretokenizer for the Nemotron-3 regex (nvidia Nemotron-3 family):
|
|
2
2
|
//! `[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`
|
|
3
|
+
//! and its right-aligned digit variant, which replaces `\p{N}` with
|
|
4
|
+
//! `\p{N}{1,3}(?=(?:\p{N}{3})*(?!\p{N}))|\p{N}{1,2}`.
|
|
3
5
|
//!
|
|
4
|
-
//! The o200k scheme without contraction suffixes
|
|
5
|
-
//!
|
|
6
|
-
//! `DIGITS3 = false`).
|
|
6
|
+
//! The o200k scheme without contraction suffixes. See `o200k_family`
|
|
7
|
+
//! (`CONTRACTIONS = false`, `DIGITS` = `DIGITS_1` or `DIGITS_3_RIGHT`).
|
|
7
8
|
|
|
8
9
|
use super::mask::{MaskScheme, MaskState};
|
|
9
|
-
use super::o200k_family;
|
|
10
|
+
use super::o200k_family::{self, DIGITS_1, DIGITS_3_RIGHT};
|
|
10
11
|
use crate::pretokenize::Pretoken;
|
|
11
12
|
|
|
12
|
-
pub(crate) struct NemotronScheme
|
|
13
|
+
pub(crate) struct NemotronScheme<const DIGITS: u8>;
|
|
13
14
|
|
|
14
|
-
impl MaskScheme for NemotronScheme {
|
|
15
|
+
impl<const DIGITS: u8> MaskScheme for NemotronScheme<DIGITS> {
|
|
15
16
|
#[inline(always)]
|
|
16
17
|
fn advance(bytes: &[u8], pos: usize) -> usize {
|
|
17
|
-
o200k_family::advance_pos::<false,
|
|
18
|
+
o200k_family::advance_pos::<false, DIGITS, true, false>(bytes, pos)
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
#[cfg(target_arch = "aarch64")]
|
|
21
22
|
#[inline(always)]
|
|
22
23
|
fn batch_masks(bytes: &[u8], scan: usize) -> (u64, u64) {
|
|
23
|
-
o200k_family::batch_masks::<false,
|
|
24
|
+
o200k_family::batch_masks::<false, DIGITS, true, false>(bytes, scan)
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
#[cfg(target_arch = "x86_64")]
|
|
27
28
|
#[inline(always)]
|
|
28
29
|
unsafe fn batch_masks_x86<const AVX512: bool>(bytes: &[u8], scan: usize) -> (u64, u64) {
|
|
29
30
|
// SAFETY: the caller detected the tier (trait contract).
|
|
30
|
-
unsafe { o200k_family::batch_masks_x86::<AVX512, false,
|
|
31
|
+
unsafe { o200k_family::batch_masks_x86::<AVX512, false, DIGITS, true, false>(bytes, scan) }
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
/// With SIMD support (aarch64 NEON, or x86_64 AVX-512/AVX2 detected at
|
|
35
36
|
/// runtime), iteration runs the shared o200k-family mask scanner (see
|
|
36
37
|
/// `o200k_family::batch_masks`); elsewhere every token takes the scalar
|
|
37
|
-
/// `advance_pos`.
|
|
38
|
-
pub struct
|
|
38
|
+
/// `advance_pos`. Used through its two aliases.
|
|
39
|
+
pub struct NemotronPretokenizer<'a, const DIGITS: u8> {
|
|
39
40
|
bytes: &'a [u8],
|
|
40
41
|
state: MaskState,
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
/// Nemotron-3: every `\p{N}` char is its own token.
|
|
45
|
+
pub type FastNemotronPretokenizer<'a> = NemotronPretokenizer<'a, DIGITS_1>;
|
|
46
|
+
|
|
47
|
+
/// Nemotron with right-aligned digit groups: "1234567" is `1|234|567`.
|
|
48
|
+
pub type FastNemotronRightAlignedPretokenizer<'a> = NemotronPretokenizer<'a, DIGITS_3_RIGHT>;
|
|
49
|
+
|
|
50
|
+
impl<'a, const DIGITS: u8> NemotronPretokenizer<'a, DIGITS> {
|
|
44
51
|
#[inline]
|
|
45
52
|
pub fn new(bytes: &'a [u8]) -> Self {
|
|
46
53
|
Self::with_pos(bytes, 0)
|
|
@@ -59,28 +66,44 @@ impl<'a> FastNemotronPretokenizer<'a> {
|
|
|
59
66
|
}
|
|
60
67
|
}
|
|
61
68
|
|
|
62
|
-
impl<'a> Iterator for
|
|
69
|
+
impl<'a, const DIGITS: u8> Iterator for NemotronPretokenizer<'a, DIGITS> {
|
|
63
70
|
type Item = Pretoken<'a>;
|
|
64
71
|
|
|
65
72
|
#[inline]
|
|
66
73
|
fn next(&mut self) -> Option<Pretoken<'a>> {
|
|
67
|
-
let (start, end) = self.state.next_span::<NemotronScheme
|
|
74
|
+
let (start, end) = self.state.next_span::<NemotronScheme<DIGITS>>(self.bytes)?;
|
|
68
75
|
Some(Pretoken(&self.bytes[start..end]))
|
|
69
76
|
}
|
|
70
77
|
}
|
|
71
78
|
|
|
72
|
-
super::impl_mask_pretoken_spans!(FastNemotronPretokenizer, NemotronScheme);
|
|
79
|
+
super::impl_mask_pretoken_spans!(FastNemotronPretokenizer, NemotronScheme<DIGITS_1>);
|
|
80
|
+
super::impl_mask_pretoken_spans!(
|
|
81
|
+
FastNemotronRightAlignedPretokenizer,
|
|
82
|
+
NemotronScheme<DIGITS_3_RIGHT>
|
|
83
|
+
);
|
|
73
84
|
|
|
74
85
|
#[cfg(test)]
|
|
75
86
|
mod tests {
|
|
76
87
|
use super::*;
|
|
88
|
+
use crate::pretokenize::{PretokenSpans, SpanBatch};
|
|
89
|
+
use std::sync::OnceLock;
|
|
77
90
|
|
|
78
91
|
/// The Nemotron pattern verbatim — no possessive quantifiers, so it
|
|
79
92
|
/// runs directly under fancy-regex.
|
|
80
93
|
const NEMOTRON_REF_REGEX: &str = r"[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+";
|
|
81
94
|
|
|
82
|
-
|
|
83
|
-
|
|
95
|
+
/// Tokens under the reference regex: Nemotron's, or with `right_aligned`
|
|
96
|
+
/// its right-aligned digit variant.
|
|
97
|
+
fn regex_tokens(s: &str, right_aligned: bool) -> Vec<String> {
|
|
98
|
+
static RES: [OnceLock<fancy_regex::Regex>; 2] = [const { OnceLock::new() }; 2];
|
|
99
|
+
let re = RES[right_aligned as usize].get_or_init(|| {
|
|
100
|
+
let digits = if right_aligned {
|
|
101
|
+
r"|\p{N}{1,3}(?=(?:\p{N}{3})*(?!\p{N}))|\p{N}{1,2}|"
|
|
102
|
+
} else {
|
|
103
|
+
r"|\p{N}|"
|
|
104
|
+
};
|
|
105
|
+
fancy_regex::Regex::new(&NEMOTRON_REF_REGEX.replace(r"|\p{N}|", digits)).unwrap()
|
|
106
|
+
});
|
|
84
107
|
re.find_iter(s)
|
|
85
108
|
.map(|m| m.unwrap().as_str().to_string())
|
|
86
109
|
.collect()
|
|
@@ -92,6 +115,29 @@ mod tests {
|
|
|
92
115
|
.collect()
|
|
93
116
|
}
|
|
94
117
|
|
|
118
|
+
/// Right-aligned tokens from the iterator, checked against the chunked
|
|
119
|
+
/// span fill (the encode path).
|
|
120
|
+
#[track_caller]
|
|
121
|
+
fn right_aligned_tokens(s: &str) -> Vec<String> {
|
|
122
|
+
let lossy = |b: &[u8]| String::from_utf8_lossy(b).into_owned();
|
|
123
|
+
let mut scanner = FastNemotronRightAlignedPretokenizer::new(s.as_bytes());
|
|
124
|
+
let mut batch = SpanBatch::new();
|
|
125
|
+
let mut filled = Vec::new();
|
|
126
|
+
loop {
|
|
127
|
+
let n = scanner.fill_spans_keyed(&mut batch, &|_| {});
|
|
128
|
+
if n == 0 {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
// SAFETY: entries 0..n were written by the fill just above.
|
|
132
|
+
filled.extend((0..n).map(|i| lossy(unsafe { batch.span(i) })));
|
|
133
|
+
}
|
|
134
|
+
let iterated: Vec<String> = FastNemotronRightAlignedPretokenizer::new(s.as_bytes())
|
|
135
|
+
.map(|t| lossy(t.0))
|
|
136
|
+
.collect();
|
|
137
|
+
assert_eq!(filled, iterated, "span fill vs iterator on {s:?}");
|
|
138
|
+
iterated
|
|
139
|
+
}
|
|
140
|
+
|
|
95
141
|
/// The o200k small-case list applies verbatim (contraction cases just
|
|
96
142
|
/// tokenize differently, which the reference regex reflects).
|
|
97
143
|
#[test]
|
|
@@ -99,9 +145,14 @@ mod tests {
|
|
|
99
145
|
for case in crate::pretokenize::fast::o200k::tests::SMALL_CASES {
|
|
100
146
|
assert_eq!(
|
|
101
147
|
fast_tokens(case),
|
|
102
|
-
regex_tokens(case),
|
|
148
|
+
regex_tokens(case, false),
|
|
103
149
|
"Mismatch on case {case:?}"
|
|
104
150
|
);
|
|
151
|
+
assert_eq!(
|
|
152
|
+
right_aligned_tokens(case),
|
|
153
|
+
regex_tokens(case, true),
|
|
154
|
+
"Right-aligned mismatch on case {case:?}"
|
|
155
|
+
);
|
|
105
156
|
}
|
|
106
157
|
}
|
|
107
158
|
|
|
@@ -130,9 +181,49 @@ mod tests {
|
|
|
130
181
|
.collect();
|
|
131
182
|
assert_eq!(
|
|
132
183
|
fast_tokens(&s),
|
|
133
|
-
regex_tokens(&s),
|
|
184
|
+
regex_tokens(&s, false),
|
|
134
185
|
"Mismatch on round {round}, case {s:?}"
|
|
135
186
|
);
|
|
187
|
+
assert_eq!(
|
|
188
|
+
right_aligned_tokens(&s),
|
|
189
|
+
regex_tokens(&s, true),
|
|
190
|
+
"Right-aligned mismatch on round {round}, case {s:?}"
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/// Right-aligned digit runs of every length at every offset against the
|
|
196
|
+
/// 64-byte batch grid, and non-ASCII runs inside and across batches.
|
|
197
|
+
#[test]
|
|
198
|
+
fn right_aligned_digit_runs() {
|
|
199
|
+
let mut cases = vec![];
|
|
200
|
+
for n in 1..130 {
|
|
201
|
+
for prefix in 0..70 {
|
|
202
|
+
cases.push(format!("{} {} suffix", "a".repeat(prefix), "1".repeat(n)));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
let non_ascii = ["١٢٣٤٥٦٧", "12345", "ⅧⅡⅣⅦⅤ", "½¼¾⅓⅔", "1٢3Ⅷ½4٥6"];
|
|
206
|
+
for text in non_ascii {
|
|
207
|
+
cases.push(text.to_string());
|
|
208
|
+
cases.push(format!("{}{text}{}", "x".repeat(60), "y".repeat(70)));
|
|
209
|
+
}
|
|
210
|
+
for case in &cases {
|
|
211
|
+
assert_eq!(
|
|
212
|
+
right_aligned_tokens(case),
|
|
213
|
+
regex_tokens(case, true),
|
|
214
|
+
"Mismatch on case {case:?}"
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/// A 100k-digit run splits in linear time: only a run's first token
|
|
220
|
+
/// counts the run.
|
|
221
|
+
#[test]
|
|
222
|
+
fn right_aligned_long_run() {
|
|
223
|
+
for digit in ["1", "٢"] {
|
|
224
|
+
let tokens = right_aligned_tokens(&digit.repeat(100_001));
|
|
225
|
+
assert_eq!(tokens[0], digit.repeat(2));
|
|
226
|
+
assert!(tokens[1..].iter().all(|t| *t == digit.repeat(3)));
|
|
136
227
|
}
|
|
137
228
|
}
|
|
138
229
|
}
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
//! `[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+(?i:'s|'t|'re|'ve|'m|'ll|'d)?|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*(?i:'s|'t|'re|'ve|'m|'ll|'d)?|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`
|
|
3
3
|
//!
|
|
4
4
|
//! See `o200k_family` for the shared scalar walker and mask-scanner
|
|
5
|
-
//! boundary algebra (`CONTRACTIONS = true`, `
|
|
5
|
+
//! boundary algebra (`CONTRACTIONS = true`, `DIGITS = DIGITS_3`).
|
|
6
6
|
|
|
7
7
|
use super::mask::{MaskScheme, MaskState};
|
|
8
|
-
use super::o200k_family;
|
|
8
|
+
use super::o200k_family::{self, DIGITS_3};
|
|
9
9
|
use crate::pretokenize::Pretoken;
|
|
10
10
|
|
|
11
11
|
pub(crate) struct O200kScheme;
|
|
@@ -13,20 +13,20 @@ pub(crate) struct O200kScheme;
|
|
|
13
13
|
impl MaskScheme for O200kScheme {
|
|
14
14
|
#[inline(always)]
|
|
15
15
|
fn advance(bytes: &[u8], pos: usize) -> usize {
|
|
16
|
-
o200k_family::advance_pos::<true,
|
|
16
|
+
o200k_family::advance_pos::<true, DIGITS_3, true, false>(bytes, pos)
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
#[cfg(target_arch = "aarch64")]
|
|
20
20
|
#[inline(always)]
|
|
21
21
|
fn batch_masks(bytes: &[u8], scan: usize) -> (u64, u64) {
|
|
22
|
-
o200k_family::batch_masks::<true,
|
|
22
|
+
o200k_family::batch_masks::<true, DIGITS_3, true, false>(bytes, scan)
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
#[cfg(target_arch = "x86_64")]
|
|
26
26
|
#[inline(always)]
|
|
27
27
|
unsafe fn batch_masks_x86<const AVX512: bool>(bytes: &[u8], scan: usize) -> (u64, u64) {
|
|
28
28
|
// SAFETY: the caller detected the tier (trait contract).
|
|
29
|
-
unsafe { o200k_family::batch_masks_x86::<AVX512, true,
|
|
29
|
+
unsafe { o200k_family::batch_masks_x86::<AVX512, true, DIGITS_3, true, false>(bytes, scan) }
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
//! Shared implementation for the o200k regex family: o200k_base (GPT-4o,
|
|
2
|
-
//! gpt-oss), the Nemotron-3
|
|
2
|
+
//! gpt-oss), the Nemotron-3 variants, and the Kimi (moonshotai K2) variant.
|
|
3
3
|
//! Their patterns share the shape
|
|
4
4
|
//!
|
|
5
5
|
//! `HAN-RUN?|
|
|
6
6
|
//! [^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+SUF?|
|
|
7
7
|
//! [^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*SUF?|
|
|
8
|
-
//!
|
|
8
|
+
//! DIGITS| ?[^\s\p{L}\p{N}]+TAIL|\s*[\r\n]+|\s+(?!\S)|\s+`
|
|
9
9
|
//!
|
|
10
10
|
//! where `SUF = (?i:'s|'t|'re|'ve|'m|'ll|'d)` exists in o200k and Kimi
|
|
11
|
-
//! (`CONTRACTIONS`), the digit group is
|
|
12
|
-
//!
|
|
11
|
+
//! (`CONTRACTIONS`), the digit group `DIGITS` is one of [`DIGITS_1`],
|
|
12
|
+
//! [`DIGITS_3`] or [`DIGITS_3_RIGHT`], the absorbed punct tail `TAIL` is
|
|
13
13
|
//! `[\r\n/]*` for o200k/Nemotron vs `[\r\n]*` for Kimi (`SLASH`), and Kimi
|
|
14
14
|
//! alone (`HAN`) prepends a `[\p{Han}]+` alternative while intersecting
|
|
15
15
|
//! both letter brackets with `[^\p{Han}]` — Han chars form their own runs
|
|
@@ -53,6 +53,17 @@ use super::mask::{self, AsciiMasks};
|
|
|
53
53
|
use super::{decode_cp, is_ascii_ws, is_digit, is_letter, scan_numbers_max3};
|
|
54
54
|
use crate::pretokenize::unicode::{O200kCharClass, kimi_class_of, o200k_class_of};
|
|
55
55
|
|
|
56
|
+
/// `DIGITS` = `\p{N}`: every number char is its own token (Nemotron-3).
|
|
57
|
+
pub(crate) const DIGITS_1: u8 = 0;
|
|
58
|
+
/// `DIGITS` = `\p{N}{1,3}`: runs split into groups of three from the left
|
|
59
|
+
/// (o200k, Kimi).
|
|
60
|
+
pub(crate) const DIGITS_3: u8 = 1;
|
|
61
|
+
/// `DIGITS` = `\p{N}{1,3}(?=(?:\p{N}{3})*(?!\p{N}))|\p{N}{1,2}`: groups of
|
|
62
|
+
/// three counted back from the run's end, so "1234567" is `1|234|567`
|
|
63
|
+
/// (the right-aligned Nemotron variant). The first alternative matches
|
|
64
|
+
/// at every position of a run, so `\p{N}{1,2}` never applies.
|
|
65
|
+
pub(crate) const DIGITS_3_RIGHT: u8 = 2;
|
|
66
|
+
|
|
56
67
|
#[inline(always)]
|
|
57
68
|
fn is_upper_ascii(b: u8) -> bool {
|
|
58
69
|
b.wrapping_sub(b'A') < 26
|
|
@@ -308,14 +319,54 @@ fn ws_token_end(bytes: &[u8], start: usize) -> usize {
|
|
|
308
319
|
p // `\s+`: single whitespace char before content
|
|
309
320
|
}
|
|
310
321
|
|
|
311
|
-
///
|
|
322
|
+
/// The `DIGITS` token starting at `pos`, a number char ending at
|
|
323
|
+
/// `first_end`.
|
|
312
324
|
#[inline(always)]
|
|
313
|
-
fn digit_token_end<const
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
first_end
|
|
325
|
+
fn digit_token_end<const DIGITS: u8>(bytes: &[u8], pos: usize, first_end: usize) -> usize {
|
|
326
|
+
match DIGITS {
|
|
327
|
+
DIGITS_1 => first_end,
|
|
328
|
+
DIGITS_3 => scan_numbers_max3(bytes, first_end, 1),
|
|
329
|
+
_ => right_aligned_digit_end(bytes, pos, first_end),
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/// End of the number char at `pos`, if one starts there.
|
|
334
|
+
#[inline(always)]
|
|
335
|
+
fn number_end(bytes: &[u8], pos: usize) -> Option<usize> {
|
|
336
|
+
let &b = bytes.get(pos)?;
|
|
337
|
+
if b < 0x80 {
|
|
338
|
+
return is_digit(b).then_some(pos + 1);
|
|
339
|
+
}
|
|
340
|
+
// SAFETY: pos < len and bytes[pos] >= 0x80.
|
|
341
|
+
let (cp, l) = unsafe { decode_cp(bytes, pos) };
|
|
342
|
+
(o200k_class_of(cp) == O200kCharClass::Number).then_some(pos + l)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/// [`DIGITS_3_RIGHT`]: at a run start, count the run and take its length
|
|
346
|
+
/// mod 3 (3 when divisible). Every later token of the run starts with a
|
|
347
|
+
/// whole number of groups left, so mid-run (`pos` right after a number
|
|
348
|
+
/// char) it takes 3 without counting, keeping long runs linear.
|
|
349
|
+
fn right_aligned_digit_end(bytes: &[u8], pos: usize, first_end: usize) -> usize {
|
|
350
|
+
let mid_run = pos > 0 && {
|
|
351
|
+
// Back up over at most 3 continuation bytes to the char before pos.
|
|
352
|
+
let mut j = pos - 1;
|
|
353
|
+
while j > 0 && pos - j < 4 && bytes[j] & 0xC0 == 0x80 {
|
|
354
|
+
j -= 1;
|
|
355
|
+
}
|
|
356
|
+
number_end(bytes, j) == Some(pos)
|
|
357
|
+
};
|
|
358
|
+
let limit = if mid_run { 3 } else { usize::MAX };
|
|
359
|
+
let mut ends = [first_end; 3]; // the end after 1, 2 and 3 chars
|
|
360
|
+
let (mut n, mut p) = (1, first_end);
|
|
361
|
+
while n < limit
|
|
362
|
+
&& let Some(q) = number_end(bytes, p)
|
|
363
|
+
{
|
|
364
|
+
if n < 3 {
|
|
365
|
+
ends[n] = q;
|
|
366
|
+
}
|
|
367
|
+
(n, p) = (n + 1, q);
|
|
318
368
|
}
|
|
369
|
+
ends[(n - 1) % 3]
|
|
319
370
|
}
|
|
320
371
|
|
|
321
372
|
/// Advance past one token starting at `pos`. Returns the new position.
|
|
@@ -323,7 +374,7 @@ fn digit_token_end<const DIGITS3: bool>(bytes: &[u8], first_end: usize) -> usize
|
|
|
323
374
|
#[inline(always)]
|
|
324
375
|
pub(crate) fn advance_pos<
|
|
325
376
|
const CONTRACTIONS: bool,
|
|
326
|
-
const
|
|
377
|
+
const DIGITS: u8,
|
|
327
378
|
const SLASH: bool,
|
|
328
379
|
const HAN: bool,
|
|
329
380
|
>(
|
|
@@ -408,7 +459,7 @@ pub(crate) fn advance_pos<
|
|
|
408
459
|
bytes,
|
|
409
460
|
scan_case_run::<HAN>(bytes, p0, CaseState::U { last_cl_end: p0 }),
|
|
410
461
|
),
|
|
411
|
-
O200kCharClass::Number => digit_token_end::<
|
|
462
|
+
O200kCharClass::Number => digit_token_end::<DIGITS>(bytes, pos, p0),
|
|
412
463
|
// Any non-letter/number char except \r\n may prefix a run
|
|
413
464
|
class => {
|
|
414
465
|
if let Some((e, st)) = letter_run_first::<HAN>(bytes, p0) {
|
|
@@ -425,7 +476,7 @@ pub(crate) fn advance_pos<
|
|
|
425
476
|
|
|
426
477
|
// ASCII digit
|
|
427
478
|
if is_digit(b0) {
|
|
428
|
-
return digit_token_end::<
|
|
479
|
+
return digit_token_end::<DIGITS>(bytes, pos, pos + 1);
|
|
429
480
|
}
|
|
430
481
|
|
|
431
482
|
// \r and \n are excluded from the letter-run prefix
|
|
@@ -470,6 +521,21 @@ fn smear_up(seed: u64, within: u64) -> u64 {
|
|
|
470
521
|
a
|
|
471
522
|
}
|
|
472
523
|
|
|
524
|
+
/// [`smear_up`] toward lower bits.
|
|
525
|
+
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
|
|
526
|
+
#[inline(always)]
|
|
527
|
+
fn smear_down(seed: u64, within: u64) -> u64 {
|
|
528
|
+
let mut a = seed;
|
|
529
|
+
let mut m = within;
|
|
530
|
+
let mut sh = 1u32;
|
|
531
|
+
while sh < 64 {
|
|
532
|
+
a |= (a >> sh) & m;
|
|
533
|
+
m &= m >> sh;
|
|
534
|
+
sh <<= 1;
|
|
535
|
+
}
|
|
536
|
+
a
|
|
537
|
+
}
|
|
538
|
+
|
|
473
539
|
/// Per-byte class masks for a batch's unicode chars under the o200k
|
|
474
540
|
/// classifier — the o200k analogue of [`mask::UniClasses`], with the
|
|
475
541
|
/// case-split letter masks the scheme needs. Every byte of a classified
|
|
@@ -828,7 +894,7 @@ struct OAsciiExtra {
|
|
|
828
894
|
#[inline(never)]
|
|
829
895
|
fn o200k_extended_masks<
|
|
830
896
|
const CONTRACTIONS: bool,
|
|
831
|
-
const
|
|
897
|
+
const DIGITS: u8,
|
|
832
898
|
const SLASH: bool,
|
|
833
899
|
const HAN: bool,
|
|
834
900
|
>(
|
|
@@ -1002,7 +1068,7 @@ fn o200k_extended_masks<
|
|
|
1002
1068
|
uni.mk |= cl.mk;
|
|
1003
1069
|
uni.han |= cl.han;
|
|
1004
1070
|
|
|
1005
|
-
o200k_algebra::<CONTRACTIONS,
|
|
1071
|
+
o200k_algebra::<CONTRACTIONS, DIGITS, SLASH, HAN>(bytes, scan, am, ax, cr, uni)
|
|
1006
1072
|
}
|
|
1007
1073
|
|
|
1008
1074
|
/// The o200k family's shared u64 boundary algebra over per-byte class
|
|
@@ -1013,7 +1079,7 @@ fn o200k_extended_masks<
|
|
|
1013
1079
|
#[inline(always)]
|
|
1014
1080
|
fn o200k_algebra<
|
|
1015
1081
|
const CONTRACTIONS: bool,
|
|
1016
|
-
const
|
|
1082
|
+
const DIGITS: u8,
|
|
1017
1083
|
const SLASH: bool,
|
|
1018
1084
|
const HAN: bool,
|
|
1019
1085
|
>(
|
|
@@ -1084,9 +1150,13 @@ fn o200k_algebra<
|
|
|
1084
1150
|
let b_letters =
|
|
1085
1151
|
(lb & !contm & !p_l & !p_s & !p_wt & !absorb) | b_su;
|
|
1086
1152
|
|
|
1087
|
-
// --- Digits
|
|
1088
|
-
let b_digits = if
|
|
1089
|
-
|
|
1153
|
+
// --- Digits (`DIGITS`) ---------------------------------------------------
|
|
1154
|
+
let b_digits = if DIGITS != DIGITS_1 && am.d & (am.d >> 1) != 0 {
|
|
1155
|
+
if DIGITS == DIGITS_3_RIGHT {
|
|
1156
|
+
mask::digit_run_splits3_right(am.d)
|
|
1157
|
+
} else {
|
|
1158
|
+
mask::digit_run_splits3(am.d)
|
|
1159
|
+
}
|
|
1090
1160
|
} else {
|
|
1091
1161
|
am.d
|
|
1092
1162
|
};
|
|
@@ -1142,13 +1212,21 @@ fn o200k_algebra<
|
|
|
1142
1212
|
}
|
|
1143
1213
|
|
|
1144
1214
|
// A digit run whose `\p{N}{1,3}` phase did not start inside this batch
|
|
1145
|
-
// (continuation from before it, or after a bad zone) defers
|
|
1146
|
-
|
|
1215
|
+
// (continuation from before it, or after a bad zone) defers; so does
|
|
1216
|
+
// a right-aligned run whose end the batch can't see (touching the
|
|
1217
|
+
// batch end or a bad zone, e.g. a non-ASCII digit).
|
|
1218
|
+
if DIGITS != DIGITS_1 {
|
|
1147
1219
|
let seed = (am.d & (bad << 1)) | (am.d & pd);
|
|
1148
1220
|
if seed != 0 {
|
|
1149
1221
|
bad |= smear_up(seed, am.d);
|
|
1150
1222
|
}
|
|
1151
1223
|
}
|
|
1224
|
+
if DIGITS == DIGITS_3_RIGHT {
|
|
1225
|
+
let seed = am.d & ((bad >> 1) | 1 << 63);
|
|
1226
|
+
if seed != 0 {
|
|
1227
|
+
bad |= smear_down(seed, am.d);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1152
1230
|
|
|
1153
1231
|
// Base rule (NL-free runs; NL runs are overridden below).
|
|
1154
1232
|
let ws_leads1 = (am.s | am.wt | am.n) & ws_eff;
|
|
@@ -1291,7 +1369,7 @@ fn ascii_batch_carries<const SLASH: bool, const HAN: bool>(bytes: &[u8], scan: u
|
|
|
1291
1369
|
#[inline]
|
|
1292
1370
|
pub(crate) fn batch_masks<
|
|
1293
1371
|
const CONTRACTIONS: bool,
|
|
1294
|
-
const
|
|
1372
|
+
const DIGITS: u8,
|
|
1295
1373
|
const SLASH: bool,
|
|
1296
1374
|
const HAN: bool,
|
|
1297
1375
|
>(
|
|
@@ -1383,11 +1461,11 @@ pub(crate) fn batch_masks<
|
|
|
1383
1461
|
{
|
|
1384
1462
|
let mut am = am;
|
|
1385
1463
|
am.hi = mask::movemask64(hiv[0], hiv[1], hiv[2], hiv[3]);
|
|
1386
|
-
return o200k_extended_masks::<CONTRACTIONS,
|
|
1464
|
+
return o200k_extended_masks::<CONTRACTIONS, DIGITS, SLASH, HAN>(bytes, scan, am, ax);
|
|
1387
1465
|
}
|
|
1388
1466
|
|
|
1389
1467
|
let cr = ascii_batch_carries::<SLASH, HAN>(bytes, scan);
|
|
1390
|
-
o200k_algebra::<CONTRACTIONS,
|
|
1468
|
+
o200k_algebra::<CONTRACTIONS, DIGITS, SLASH, HAN>(bytes, scan, am, ax, cr, OUni::default())
|
|
1391
1469
|
}
|
|
1392
1470
|
}
|
|
1393
1471
|
|
|
@@ -1409,7 +1487,7 @@ pub(crate) fn batch_masks<
|
|
|
1409
1487
|
pub(crate) unsafe fn batch_masks_x86<
|
|
1410
1488
|
const AVX512: bool,
|
|
1411
1489
|
const CONTRACTIONS: bool,
|
|
1412
|
-
const
|
|
1490
|
+
const DIGITS: u8,
|
|
1413
1491
|
const SLASH: bool,
|
|
1414
1492
|
const HAN: bool,
|
|
1415
1493
|
>(
|
|
@@ -1434,11 +1512,11 @@ pub(crate) unsafe fn batch_masks_x86<
|
|
|
1434
1512
|
// SAFETY: both detected tiers include the BMI1/BMI2/LZCNT/POPCNT
|
|
1435
1513
|
// bit features `o200k_extended_masks` re-declares (fn contract).
|
|
1436
1514
|
return unsafe {
|
|
1437
|
-
o200k_extended_masks::<CONTRACTIONS,
|
|
1515
|
+
o200k_extended_masks::<CONTRACTIONS, DIGITS, SLASH, HAN>(bytes, scan, am, ax)
|
|
1438
1516
|
};
|
|
1439
1517
|
}
|
|
1440
1518
|
let cr = ascii_batch_carries::<SLASH, HAN>(bytes, scan);
|
|
1441
|
-
o200k_algebra::<CONTRACTIONS,
|
|
1519
|
+
o200k_algebra::<CONTRACTIONS, DIGITS, SLASH, HAN>(bytes, scan, am, ax, cr, OUni::default())
|
|
1442
1520
|
}
|
|
1443
1521
|
|
|
1444
1522
|
/// The strict-uppercase and slash masks for `bytes[scan..scan+64]`,
|
|
@@ -1494,9 +1572,13 @@ fn ascii_extra_avx2(bytes: &[u8], scan: usize) -> OAsciiExtra {
|
|
|
1494
1572
|
mod tests {
|
|
1495
1573
|
use crate::pretokenize::fast::kimi::KimiScheme;
|
|
1496
1574
|
use crate::pretokenize::fast::mask::{MaskScheme, MaskState};
|
|
1575
|
+
use super::{DIGITS_1, DIGITS_3_RIGHT};
|
|
1497
1576
|
use crate::pretokenize::fast::nemotron::NemotronScheme;
|
|
1498
1577
|
use crate::pretokenize::fast::o200k::O200kScheme;
|
|
1499
1578
|
|
|
1579
|
+
type Nemotron = NemotronScheme<DIGITS_1>;
|
|
1580
|
+
type NemotronRight = NemotronScheme<DIGITS_3_RIGHT>;
|
|
1581
|
+
|
|
1500
1582
|
fn scalar_tokens<S: MaskScheme>(bytes: &[u8]) -> Vec<Vec<u8>> {
|
|
1501
1583
|
let mut pos = 0;
|
|
1502
1584
|
let mut out = vec![];
|
|
@@ -1522,7 +1604,12 @@ mod tests {
|
|
|
1522
1604
|
fn check_all(buf: &[u8]) {
|
|
1523
1605
|
for (name, a, b) in [
|
|
1524
1606
|
("o200k", scalar_tokens::<O200kScheme>(buf), mask_tokens::<O200kScheme>(buf)),
|
|
1525
|
-
("nemotron", scalar_tokens::<
|
|
1607
|
+
("nemotron", scalar_tokens::<Nemotron>(buf), mask_tokens::<Nemotron>(buf)),
|
|
1608
|
+
(
|
|
1609
|
+
"nemotron_right_aligned",
|
|
1610
|
+
scalar_tokens::<NemotronRight>(buf),
|
|
1611
|
+
mask_tokens::<NemotronRight>(buf),
|
|
1612
|
+
),
|
|
1526
1613
|
("kimi", scalar_tokens::<KimiScheme>(buf), mask_tokens::<KimiScheme>(buf)),
|
|
1527
1614
|
] {
|
|
1528
1615
|
if a != b {
|
|
@@ -1574,10 +1661,11 @@ mod tests {
|
|
|
1574
1661
|
let mut report = format!("(len {}) {:?}", buf.len(), String::from_utf8_lossy(&buf));
|
|
1575
1662
|
for (name, a, b) in [
|
|
1576
1663
|
("o200k", scalar_tokens::<O200kScheme>(&buf), mask_tokens::<O200kScheme>(&buf)),
|
|
1664
|
+
("nemotron", scalar_tokens::<Nemotron>(&buf), mask_tokens::<Nemotron>(&buf)),
|
|
1577
1665
|
(
|
|
1578
|
-
"
|
|
1579
|
-
scalar_tokens::<
|
|
1580
|
-
mask_tokens::<
|
|
1666
|
+
"nemotron_right_aligned",
|
|
1667
|
+
scalar_tokens::<NemotronRight>(&buf),
|
|
1668
|
+
mask_tokens::<NemotronRight>(&buf),
|
|
1581
1669
|
),
|
|
1582
1670
|
("kimi", scalar_tokens::<KimiScheme>(&buf), mask_tokens::<KimiScheme>(&buf)),
|
|
1583
1671
|
] {
|
|
@@ -1611,6 +1699,8 @@ mod tests {
|
|
|
1611
1699
|
"price: $5.99! 100,000.00 3.14159 2nd 3rd 4th",
|
|
1612
1700
|
"a\u{2028}b a\u{2028}\n \n\n\t x \n\n ",
|
|
1613
1701
|
"mixed 1\u{662}3x \u{661}\u{662}\u{663} arabic",
|
|
1702
|
+
"1234567890123456789012345678901234567890123456789012345678901234567890 12",
|
|
1703
|
+
"x 12345678\u{661}12345678 \u{ff11}\u{ff12}\u{ff13}\u{ff14} 1\u{301}234",
|
|
1614
1704
|
"\u{65e5}\u{672c}\u{8a9e}ABC abc\u{65e5}\u{672c}Def \u{416}\u{416}dz",
|
|
1615
1705
|
"e\u{301}f !!\u{301}x '\u{301}s x\u{301}'s A\u{301}B",
|
|
1616
1706
|
// A mark's contextual class reaching a boundary past the ±4
|
|
@@ -1629,7 +1719,7 @@ mod tests {
|
|
|
1629
1719
|
}
|
|
1630
1720
|
}
|
|
1631
1721
|
|
|
1632
|
-
/// Differential fuzz across
|
|
1722
|
+
/// Differential fuzz across all family schemes.
|
|
1633
1723
|
#[test]
|
|
1634
1724
|
fn o200k_family_mask_matches_scalar_fuzz() {
|
|
1635
1725
|
let pieces: &[&str] = &[
|
|
@@ -1644,6 +1734,8 @@ mod tests {
|
|
|
1644
1734
|
// Han classes (Kimi): letters, numerals (Nl), symbols (So/Mc)
|
|
1645
1735
|
"中", "中文", "々", "〆", "𠀀", "〇", "〡", "㆒", "⼀", "⺀",
|
|
1646
1736
|
"\u{16FF0}", "中's", "1〇", "中\n", "!⼀", "中⼀",
|
|
1737
|
+
// Digit runs of every length mod 3, mixed-script runs
|
|
1738
|
+
"123", "12345", "1234567890", "٣٤٥", "1٢3", "12",
|
|
1647
1739
|
];
|
|
1648
1740
|
let mut state = 0x243F6A8885A308D3u64;
|
|
1649
1741
|
let mut rng = move || {
|
|
@@ -1674,9 +1766,13 @@ mod tests {
|
|
|
1674
1766
|
mod owt_tests {
|
|
1675
1767
|
use crate::pretokenize::fast::kimi::KimiScheme;
|
|
1676
1768
|
use crate::pretokenize::fast::mask::{MaskScheme, MaskState};
|
|
1769
|
+
use super::{DIGITS_1, DIGITS_3_RIGHT};
|
|
1677
1770
|
use crate::pretokenize::fast::nemotron::NemotronScheme;
|
|
1678
1771
|
use crate::pretokenize::fast::o200k::O200kScheme;
|
|
1679
1772
|
|
|
1773
|
+
type Nemotron = NemotronScheme<DIGITS_1>;
|
|
1774
|
+
type NemotronRight = NemotronScheme<DIGITS_3_RIGHT>;
|
|
1775
|
+
|
|
1680
1776
|
fn check_streaming<S: MaskScheme>(bytes: &[u8], scheme: &str) {
|
|
1681
1777
|
let mut st = MaskState::new(0);
|
|
1682
1778
|
let mut pos = 0usize;
|
|
@@ -1702,7 +1798,8 @@ mod owt_tests {
|
|
|
1702
1798
|
|
|
1703
1799
|
fn check_streaming_all(bytes: &[u8]) {
|
|
1704
1800
|
check_streaming::<O200kScheme>(bytes, "o200k");
|
|
1705
|
-
check_streaming::<
|
|
1801
|
+
check_streaming::<Nemotron>(bytes, "nemotron");
|
|
1802
|
+
check_streaming::<NemotronRight>(bytes, "nemotron_right_aligned");
|
|
1706
1803
|
check_streaming::<KimiScheme>(bytes, "kimi");
|
|
1707
1804
|
}
|
|
1708
1805
|
|
|
@@ -1721,7 +1818,7 @@ mod owt_tests {
|
|
|
1721
1818
|
check_streaming_all(&input);
|
|
1722
1819
|
}
|
|
1723
1820
|
|
|
1724
|
-
/// Full-OWT (~12 GB) mask-vs-scalar differential for
|
|
1821
|
+
/// Full-OWT (~12 GB) mask-vs-scalar differential for all schemes.
|
|
1725
1822
|
#[test]
|
|
1726
1823
|
#[ignore = "reads the full ~12 GB OWT file"]
|
|
1727
1824
|
fn o200k_family_mask_matches_scalar_owt_full() {
|
data/src/pretokenize/options.rs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
use crate::pretokenize::Pretoken;
|
|
2
2
|
use crate::pretokenize::fast::{
|
|
3
3
|
FastCl100kPretokenizer, FastDeepSeekV3Pretokenizer, FastKimiPretokenizer,
|
|
4
|
-
FastNemotronPretokenizer,
|
|
5
|
-
FastQwen2Pretokenizer, FastQwen35Pretokenizer, FastR50kPretokenizer,
|
|
4
|
+
FastNemotronPretokenizer, FastNemotronRightAlignedPretokenizer, FastO200kPretokenizer,
|
|
5
|
+
FastOlmo3Pretokenizer, FastQwen2Pretokenizer, FastQwen35Pretokenizer, FastR50kPretokenizer,
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
/// Which pretokenization scheme (regex) a tokenizer uses.
|
|
@@ -16,6 +16,7 @@ pub enum PretokenizerType {
|
|
|
16
16
|
DeepSeekV3, // Sequence of three Splits (digits, CJK, main); used by DeepSeek V3/V3.1/V4
|
|
17
17
|
O200k, // o200k_base: case-structured letter runs; GPT-4o, gpt-oss
|
|
18
18
|
Nemotron, // o200k without contractions, single-digit \p{N}; nvidia Nemotron-3
|
|
19
|
+
NemotronRightAligned, // Nemotron with right-aligned \p{N} groups of three ("1|234|567")
|
|
19
20
|
Kimi, // o200k with [\p{Han}]+ runs and no `/` tail absorption; moonshotai Kimi-K2 line
|
|
20
21
|
}
|
|
21
22
|
|
|
@@ -60,6 +61,9 @@ impl PretokenizerType {
|
|
|
60
61
|
PretokenizerType::Nemotron => {
|
|
61
62
|
FastPretokenizerDispatch::Nemotron(FastNemotronPretokenizer::new(bytes))
|
|
62
63
|
}
|
|
64
|
+
PretokenizerType::NemotronRightAligned => FastPretokenizerDispatch::NemotronRightAligned(
|
|
65
|
+
FastNemotronRightAlignedPretokenizer::new(bytes),
|
|
66
|
+
),
|
|
63
67
|
PretokenizerType::Kimi => {
|
|
64
68
|
FastPretokenizerDispatch::Kimi(FastKimiPretokenizer::new(bytes))
|
|
65
69
|
}
|
|
@@ -69,7 +73,7 @@ impl PretokenizerType {
|
|
|
69
73
|
/// The canonical name of each variant, in variant order — the
|
|
70
74
|
/// identifiers `from_name` accepts (plus the aliases listed there).
|
|
71
75
|
/// Error messages naming the valid schemes derive from this list.
|
|
72
|
-
pub const NAMES: [&'static str;
|
|
76
|
+
pub const NAMES: [&'static str; 10] = [
|
|
73
77
|
"gpt2",
|
|
74
78
|
"gpt4",
|
|
75
79
|
"qwen2",
|
|
@@ -78,6 +82,7 @@ impl PretokenizerType {
|
|
|
78
82
|
"deepseek_v3",
|
|
79
83
|
"o200k",
|
|
80
84
|
"nemotron",
|
|
85
|
+
"nemotron_right_aligned",
|
|
81
86
|
"kimi",
|
|
82
87
|
];
|
|
83
88
|
|
|
@@ -95,6 +100,7 @@ impl PretokenizerType {
|
|
|
95
100
|
"deepseek_v3" => PretokenizerType::DeepSeekV3,
|
|
96
101
|
"o200k" => PretokenizerType::O200k,
|
|
97
102
|
"nemotron" => PretokenizerType::Nemotron,
|
|
103
|
+
"nemotron_right_aligned" => PretokenizerType::NemotronRightAligned,
|
|
98
104
|
"kimi" => PretokenizerType::Kimi,
|
|
99
105
|
_ => return None,
|
|
100
106
|
})
|
|
@@ -137,6 +143,9 @@ impl PretokenizerType {
|
|
|
137
143
|
r"[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+" => {
|
|
138
144
|
Some(PretokenizerType::Nemotron)
|
|
139
145
|
}
|
|
146
|
+
r"[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*|\p{N}{1,3}(?=(?:\p{N}{3})*(?!\p{N}))|\p{N}{1,2}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+" => {
|
|
147
|
+
Some(PretokenizerType::NemotronRightAligned)
|
|
148
|
+
}
|
|
140
149
|
_ => None,
|
|
141
150
|
}
|
|
142
151
|
}
|
|
@@ -153,6 +162,7 @@ pub enum FastPretokenizerDispatch<'a> {
|
|
|
153
162
|
DeepSeekV3(FastDeepSeekV3Pretokenizer<'a>),
|
|
154
163
|
O200k(FastO200kPretokenizer<'a>),
|
|
155
164
|
Nemotron(FastNemotronPretokenizer<'a>),
|
|
165
|
+
NemotronRightAligned(FastNemotronRightAlignedPretokenizer<'a>),
|
|
156
166
|
Kimi(FastKimiPretokenizer<'a>),
|
|
157
167
|
}
|
|
158
168
|
|
|
@@ -170,6 +180,7 @@ impl<'a> Iterator for FastPretokenizerDispatch<'a> {
|
|
|
170
180
|
FastPretokenizerDispatch::DeepSeekV3(it) => it.next(),
|
|
171
181
|
FastPretokenizerDispatch::O200k(it) => it.next(),
|
|
172
182
|
FastPretokenizerDispatch::Nemotron(it) => it.next(),
|
|
183
|
+
FastPretokenizerDispatch::NemotronRightAligned(it) => it.next(),
|
|
173
184
|
FastPretokenizerDispatch::Kimi(it) => it.next(),
|
|
174
185
|
}
|
|
175
186
|
}
|
|
@@ -196,6 +207,9 @@ unsafe impl<'a> crate::pretokenize::PretokenSpans<'a> for FastPretokenizerDispat
|
|
|
196
207
|
FastPretokenizerDispatch::DeepSeekV3(it) => it.fill_spans_keyed(batch, prefetch),
|
|
197
208
|
FastPretokenizerDispatch::O200k(it) => it.fill_spans_keyed(batch, prefetch),
|
|
198
209
|
FastPretokenizerDispatch::Nemotron(it) => it.fill_spans_keyed(batch, prefetch),
|
|
210
|
+
FastPretokenizerDispatch::NemotronRightAligned(it) => {
|
|
211
|
+
it.fill_spans_keyed(batch, prefetch)
|
|
212
|
+
}
|
|
199
213
|
FastPretokenizerDispatch::Kimi(it) => it.fill_spans_keyed(batch, prefetch),
|
|
200
214
|
}
|
|
201
215
|
}
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: gigatoken
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Eric Jacobs
|
|
@@ -183,7 +183,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
183
183
|
requirements:
|
|
184
184
|
- - ">="
|
|
185
185
|
- !ruby/object:Gem::Version
|
|
186
|
-
version: 3.
|
|
186
|
+
version: 3.4.0
|
|
187
187
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
188
188
|
requirements:
|
|
189
189
|
- - ">="
|