morpheme 0.4.0

Fast, pure-Rust subword tokenization (BPE, WordPiece, Unigram) compatible with Hugging Face tokenizers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Normalizers: rewrite the input text (lowercasing, Unicode
//! normalization, accent stripping, …) while keeping alignments to the
//! original so offsets stay exact.
//!
//! Every normalizer here matches the Hugging Face `tokenizers`
//! implementation of the same name, both in behavior and in its
//! `tokenizer.json` representation.

mod bert;
pub(crate) mod byte_level;
mod precompiled;
mod prepend;
mod replace;
mod strip;
mod unicode;
mod utils;

use serde::de::{self, DeserializeOwned};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};

use crate::error::Result;
use crate::normalized_string::NormalizedString;
use crate::traits::Normalizer;

pub use bert::BertNormalizer;
pub use byte_level::ByteLevel;
pub use precompiled::Precompiled;
pub use prepend::Prepend;
pub use replace::Replace;
pub use strip::{Strip, StripAccents};
pub use unicode::{Nfc, Nfd, Nfkc, Nfkd, Nmt};
pub use utils::{Lowercase, Sequence};

/// Any built-in normalizer. This is what `tokenizer.json`'s
/// `"normalizer"` field (de)serializes to; the `"type"` key selects the
/// variant.
///
/// Loading also accepts the legacy untagged forms (no `"type"` key) that
/// Hugging Face `tokenizers` still reads: the variant is inferred from the
/// fields, exactly as HF's untagged fallback does. Saving always writes
/// the tagged form. Unknown keys are ignored, as everywhere else.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum NormalizerWrapper {
    /// BERT's normalizer.
    BertNormalizer(BertNormalizer),
    /// Strip whitespace on the left and/or right.
    Strip(Strip),
    /// Remove combining marks.
    StripAccents(StripAccents),
    /// Unicode NFC.
    #[serde(rename = "NFC")]
    Nfc(Nfc),
    /// Unicode NFD.
    #[serde(rename = "NFD")]
    Nfd(Nfd),
    /// Unicode NFKC.
    #[serde(rename = "NFKC")]
    Nfkc(Nfkc),
    /// Unicode NFKD.
    #[serde(rename = "NFKD")]
    Nfkd(Nfkd),
    /// Apply several normalizers in order.
    Sequence(Sequence),
    /// Unicode lowercase.
    Lowercase(Lowercase),
    /// NMT-style control character cleanup.
    Nmt(Nmt),
    /// SentencePiece precompiled character map.
    Precompiled(Precompiled),
    /// Replace a pattern with a string.
    Replace(Replace),
    /// Prepend a string.
    Prepend(Prepend),
    /// Map each byte to its GPT-2 byte-level char.
    ByteLevel(ByteLevel),
}

/// The `"type"` names, in the order HF lists them.
const TYPES: &[&str] = &[
    "BertNormalizer",
    "Strip",
    "StripAccents",
    "NFC",
    "NFD",
    "NFKC",
    "NFKD",
    "Sequence",
    "Lowercase",
    "Nmt",
    "Precompiled",
    "Replace",
    "Prepend",
    "ByteLevel",
];

/// Deserialize a component from the remaining keys of its JSON object.
pub(crate) fn from_fields<T: DeserializeOwned, E: de::Error>(
    fields: Map<String, Value>,
) -> std::result::Result<T, E> {
    serde_json::from_value(Value::Object(fields)).map_err(E::custom)
}

/// Deserialize a JSON object, split off its `"type"` key, and hand the
/// rest to `tagged` (type present) or `legacy` (type absent).
pub(crate) fn deserialize_component<'de, D, T>(
    deserializer: D,
    what: &'static str,
    tagged: impl FnOnce(&str, Map<String, Value>) -> std::result::Result<T, D::Error>,
    legacy: impl FnOnce(Map<String, Value>) -> std::result::Result<T, D::Error>,
) -> std::result::Result<T, D::Error>
where
    D: Deserializer<'de>,
{
    let value = Value::deserialize(deserializer)?;
    let Value::Object(mut fields) = value else {
        return Err(de::Error::custom(format!(
            "invalid type: {}, expected a {what} object",
            json_type_name(&value)
        )));
    };
    match fields.remove("type") {
        Some(Value::String(ty)) => tagged(&ty, fields),
        Some(other) => Err(de::Error::custom(format!(
            "invalid type: {}, expected the {what} \"type\" to be a string",
            json_type_name(&other)
        ))),
        None => legacy(fields),
    }
}

fn json_type_name(v: &Value) -> &'static str {
    match v {
        Value::Null => "null",
        Value::Bool(_) => "a boolean",
        Value::Number(_) => "a number",
        Value::String(_) => "a string",
        Value::Array(_) => "a sequence",
        Value::Object(_) => "a map",
    }
}

/// Whether every key in `required` is present.
pub(crate) fn has_fields(fields: &Map<String, Value>, required: &[&str]) -> bool {
    required.iter().all(|k| fields.contains_key(*k))
}

impl<'de> Deserialize<'de> for NormalizerWrapper {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
        deserialize_component(
            deserializer,
            "normalizer",
            |ty, fields| {
                Ok(match ty {
                    "BertNormalizer" => Self::BertNormalizer(from_fields(fields)?),
                    "Strip" => Self::Strip(from_fields(fields)?),
                    "StripAccents" => Self::StripAccents(from_fields(fields)?),
                    "NFC" => Self::Nfc(from_fields(fields)?),
                    "NFD" => Self::Nfd(from_fields(fields)?),
                    "NFKC" => Self::Nfkc(from_fields(fields)?),
                    "NFKD" => Self::Nfkd(from_fields(fields)?),
                    "Sequence" => Self::Sequence(from_fields(fields)?),
                    "Lowercase" => Self::Lowercase(from_fields(fields)?),
                    "Nmt" => Self::Nmt(from_fields(fields)?),
                    "Precompiled" => Self::Precompiled(from_fields(fields)?),
                    "Replace" => Self::Replace(from_fields(fields)?),
                    "Prepend" => Self::Prepend(from_fields(fields)?),
                    "ByteLevel" => Self::ByteLevel(from_fields(fields)?),
                    other => return Err(de::Error::unknown_variant(other, TYPES)),
                })
            },
            |fields| {
                // The legacy untagged forms, tried in the order of HF's
                // untagged fallback enum. Only normalizers with fields can
                // be told apart; the parameterless ones (`{}`) cannot, and
                // HF rejects them too.
                Ok(
                    if has_fields(
                        &fields,
                        &["clean_text", "handle_chinese_chars", "lowercase"],
                    ) {
                        Self::BertNormalizer(from_fields(fields)?)
                    } else if has_fields(&fields, &["strip_left", "strip_right"]) {
                        Self::Strip(from_fields(fields)?)
                    } else if has_fields(&fields, &["normalizers"]) {
                        Self::Sequence(from_fields(fields)?)
                    } else if has_fields(&fields, &["precompiled_charsmap"]) {
                        Self::Precompiled(from_fields(fields)?)
                    } else if has_fields(&fields, &["pattern", "content"]) {
                        Self::Replace(from_fields(fields)?)
                    } else if has_fields(&fields, &["prepend"]) {
                        Self::Prepend(from_fields(fields)?)
                    } else {
                        return Err(de::Error::custom(
                            "missing field `type` and the data does not match any legacy \
                             untagged normalizer form",
                        ));
                    },
                )
            },
        )
    }
}

impl Normalizer for NormalizerWrapper {
    fn normalize(&self, normalized: &mut NormalizedString) -> Result<()> {
        match self {
            NormalizerWrapper::BertNormalizer(n) => n.normalize(normalized),
            NormalizerWrapper::Strip(n) => n.normalize(normalized),
            NormalizerWrapper::StripAccents(n) => n.normalize(normalized),
            NormalizerWrapper::Nfc(n) => n.normalize(normalized),
            NormalizerWrapper::Nfd(n) => n.normalize(normalized),
            NormalizerWrapper::Nfkc(n) => n.normalize(normalized),
            NormalizerWrapper::Nfkd(n) => n.normalize(normalized),
            NormalizerWrapper::Sequence(n) => n.normalize(normalized),
            NormalizerWrapper::Lowercase(n) => n.normalize(normalized),
            NormalizerWrapper::Nmt(n) => n.normalize(normalized),
            NormalizerWrapper::Precompiled(n) => n.normalize(normalized),
            NormalizerWrapper::Replace(n) => n.normalize(normalized),
            NormalizerWrapper::Prepend(n) => n.normalize(normalized),
            NormalizerWrapper::ByteLevel(n) => n.normalize(normalized),
        }
    }
}

macro_rules! impl_from {
    ($($ty:ident => $variant:ident),* $(,)?) => {
        $(
            impl From<$ty> for NormalizerWrapper {
                fn from(n: $ty) -> Self {
                    NormalizerWrapper::$variant(n)
                }
            }
        )*
    };
}

impl_from!(
    BertNormalizer => BertNormalizer,
    Strip => Strip,
    StripAccents => StripAccents,
    Nfc => Nfc,
    Nfd => Nfd,
    Nfkc => Nfkc,
    Nfkd => Nfkd,
    Sequence => Sequence,
    Lowercase => Lowercase,
    Nmt => Nmt,
    Precompiled => Precompiled,
    Replace => Replace,
    Prepend => Prepend,
    ByteLevel => ByteLevel,
);

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{Value, json};

    fn roundtrip(v: Value) {
        let n: NormalizerWrapper = serde_json::from_value(v.clone()).unwrap();
        assert_eq!(serde_json::to_value(&n).unwrap(), v);
    }

    #[test]
    fn unit_normalizers_roundtrip() {
        for ty in [
            "NFC",
            "NFD",
            "NFKC",
            "NFKD",
            "Lowercase",
            "StripAccents",
            "Nmt",
            "ByteLevel",
        ] {
            roundtrip(json!({ "type": ty }));
        }
    }

    #[test]
    fn configured_normalizers_roundtrip() {
        roundtrip(json!({
            "type": "BertNormalizer",
            "clean_text": true,
            "handle_chinese_chars": true,
            "strip_accents": null,
            "lowercase": false
        }));
        roundtrip(json!({"type": "Strip", "strip_left": true, "strip_right": false}));
        roundtrip(json!({"type": "Prepend", "prepend": "▁"}));
        roundtrip(json!({"type": "Replace", "pattern": {"String": " "}, "content": "▁"}));
        roundtrip(json!({"type": "Replace", "pattern": {"Regex": "\\s+"}, "content": " "}));
        roundtrip(json!({
            "type": "Sequence",
            "normalizers": [
                {"type": "Prepend", "prepend": "▁"},
                {"type": "Replace", "pattern": {"String": " "}, "content": "▁"}
            ]
        }));
    }

    #[test]
    fn serialization_field_order_matches_hf() {
        let n: NormalizerWrapper = BertNormalizer::default().into();
        assert_eq!(
            serde_json::to_string(&n).unwrap(),
            r#"{"type":"BertNormalizer","clean_text":true,"handle_chinese_chars":true,"strip_accents":null,"lowercase":true}"#
        );
        let n: NormalizerWrapper = Lowercase.into();
        assert_eq!(
            serde_json::to_string(&n).unwrap(),
            r#"{"type":"Lowercase"}"#
        );
    }

    #[test]
    fn unknown_type_is_an_error() {
        let err =
            serde_json::from_value::<NormalizerWrapper>(json!({"type": "Bogus"})).unwrap_err();
        assert!(err.to_string().contains("Bogus"), "{err}");
        let err = serde_json::from_value::<NormalizerWrapper>(json!({"clean_text": true}));
        assert!(err.is_err());
        let err = serde_json::from_value::<NormalizerWrapper>(json!({"type": 3}));
        assert!(err.is_err());
        let err = serde_json::from_value::<NormalizerWrapper>(json!("NFC"));
        assert!(err.is_err());
    }

    /// Extra keys are ignored for every normalizer, as for pre-tokenizers
    /// and decoders.
    #[test]
    fn unknown_fields_are_ignored() {
        for ty in TYPES {
            let v = match *ty {
                "BertNormalizer" => json!({
                    "type": ty, "clean_text": true, "handle_chinese_chars": true,
                    "strip_accents": null, "lowercase": true, "x": 1
                }),
                "Strip" => json!({"type": ty, "strip_left": true, "strip_right": true, "x": 1}),
                "Sequence" => json!({"type": ty, "normalizers": [], "x": 1}),
                "Precompiled" => continue,
                "Replace" => {
                    json!({"type": ty, "pattern": {"String": " "}, "content": "_", "x": 1})
                }
                "Prepend" => json!({"type": ty, "prepend": "_", "x": 1}),
                _ => json!({"type": ty, "x": 1}),
            };
            let n: NormalizerWrapper =
                serde_json::from_value(v.clone()).unwrap_or_else(|e| panic!("{ty}: {e}"));
            let mut expected = v.as_object().unwrap().clone();
            expected.remove("x");
            assert_eq!(serde_json::to_value(&n).unwrap(), Value::Object(expected));
        }
    }

    // Ported from Hugging Face `tokenizers` 0.23.2
    // (`normalizers/mod.rs`, `post_processor_deserialization_no_type`).
    #[test]
    fn legacy_untagged_forms_load() {
        let json = r#"{"strip_left":false, "strip_right":true}"#;
        let n: NormalizerWrapper = serde_json::from_str(json).unwrap();
        assert!(matches!(n, NormalizerWrapper::Strip(_)));
        // It is written back in the current tagged form.
        assert_eq!(
            serde_json::to_string(&n).unwrap(),
            r#"{"type":"Strip","strip_left":false,"strip_right":true}"#
        );

        // Fields of a pre-tokenizer: not a normalizer.
        let json = r#"{"trim_offsets":true, "add_prefix_space":true}"#;
        let err = serde_json::from_str::<NormalizerWrapper>(json).unwrap_err();
        assert!(err.to_string().contains("legacy untagged"), "{err}");

        let json = r#"{"prepend":"a"}"#;
        let n: NormalizerWrapper = serde_json::from_str(json).unwrap();
        assert!(matches!(n, NormalizerWrapper::Prepend(_)));

        let json = r#"{"clean_text":true,"handle_chinese_chars":true,"strip_accents":null,"lowercase":true}"#;
        let n: NormalizerWrapper = serde_json::from_str(json).unwrap();
        assert!(matches!(n, NormalizerWrapper::BertNormalizer(_)));
        // HF requires BertNormalizer's three bool fields in the untagged form.
        let json = r#"{"clean_text":true,"lowercase":true}"#;
        assert!(serde_json::from_str::<NormalizerWrapper>(json).is_err());

        let json = r#"{"pattern":{"String":" "},"content":"▁"}"#;
        let n: NormalizerWrapper = serde_json::from_str(json).unwrap();
        assert!(matches!(n, NormalizerWrapper::Replace(_)));

        let json = r#"{"normalizers":[{"prepend":"▁"},{"type":"NFKC"}]}"#;
        let n: NormalizerWrapper = serde_json::from_str(json).unwrap();
        assert_eq!(
            serde_json::to_string(&n).unwrap(),
            r#"{"type":"Sequence","normalizers":[{"type":"Prepend","prepend":"▁"},{"type":"NFKC"}]}"#
        );

        let t5 =
            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/hf/t5-small.json");
        if let Ok(raw) = std::fs::read_to_string(t5) {
            let file: Value = serde_json::from_str(&raw).unwrap();
            let mut legacy = file["normalizer"].as_object().unwrap().clone();
            assert_eq!(legacy.remove("type"), Some(json!("Precompiled")));
            let n: NormalizerWrapper = serde_json::from_value(Value::Object(legacy)).unwrap();
            assert!(matches!(n, NormalizerWrapper::Precompiled(_)));
            assert_eq!(serde_json::to_value(&n).unwrap(), file["normalizer"]);
        }
    }

    // Ported from Hugging Face `tokenizers` 0.23.2
    // (`normalizers/mod.rs`, `normalizer_serialization`).
    #[test]
    fn legacy_untagged_errors_match_hf() {
        let json = r#"{"type":"Sequence","normalizers":[]}"#;
        assert!(serde_json::from_str::<NormalizerWrapper>(json).is_ok());

        // `{}` matches nothing: the parameterless normalizers need a type.
        let json = r#"{"type":"Sequence","normalizers":[{}]}"#;
        let err = serde_json::from_str::<NormalizerWrapper>(json).unwrap_err();
        assert!(err.to_string().contains("legacy untagged"), "{err}");

        let json = r#"{"replacement":"▁","prepend_scheme":"always"}"#;
        let err = serde_json::from_str::<NormalizerWrapper>(json).unwrap_err();
        assert!(err.to_string().contains("legacy untagged"), "{err}");

        let json = r#"{"type":"Sequence","prepend_scheme":"always"}"#;
        let err = serde_json::from_str::<NormalizerWrapper>(json).unwrap_err();
        assert!(
            err.to_string().contains("missing field `normalizers`"),
            "{err}"
        );
    }

    #[test]
    fn invalid_regex_is_an_error_not_a_panic() {
        let err = serde_json::from_value::<NormalizerWrapper>(
            json!({"type": "Replace", "pattern": {"Regex": "("}, "content": ""}),
        );
        assert!(err.is_err());
    }

    /// Every normalizer config in the real fixture files must load and
    /// re-serialize identically.
    #[test]
    fn real_fixture_configs_roundtrip() {
        let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/hf");
        let Ok(entries) = std::fs::read_dir(&dir) else {
            eprintln!(
                "skipping: {} missing (scripts/fetch-hf-fixtures.sh)",
                dir.display()
            );
            return;
        };
        let mut checked = 0;
        for entry in entries {
            let path = entry.unwrap().path();
            let raw = std::fs::read_to_string(&path).unwrap();
            let file: Value = serde_json::from_str(&raw).unwrap();
            let n = &file["normalizer"];
            if n.is_null() {
                continue;
            }
            let parsed: NormalizerWrapper = serde_json::from_value(n.clone())
                .unwrap_or_else(|e| panic!("{}: {e}", path.display()));
            assert_eq!(
                &serde_json::to_value(&parsed).unwrap(),
                n,
                "{}",
                path.display()
            );
            checked += 1;
        }
        assert!(checked > 0);
    }
}