Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test normalizers #1600

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
update
ArthurZucker committed Aug 6, 2024
commit bfb6222ba809c694f4303a4cc3fd5a20a8e9f875
7 changes: 4 additions & 3 deletions tokenizers/src/normalizers/byte_level.rs
Original file line number Diff line number Diff line change
@@ -2,10 +2,11 @@ use crate::processors::byte_level::bytes_char;
use crate::tokenizer::{NormalizedString, Normalizer, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use crate::utils::macro_rules_attribute;

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "type")]
pub struct ByteLevel {}
#[derive(Clone, Debug)]
#[macro_rules_attribute(impl_serde_type!)]
pub struct ByteLevel;

lazy_static! {
static ref BYTES_CHAR: HashMap<u8, char> = bytes_char();
33 changes: 33 additions & 0 deletions tokenizers/src/normalizers/mod.rs
Original file line number Diff line number Diff line change
@@ -73,3 +73,36 @@ impl_enum_from!(Precompiled, NormalizerWrapper, Precompiled);
impl_enum_from!(Replace, NormalizerWrapper, Replace);
impl_enum_from!(Prepend, NormalizerWrapper, Prepend);
impl_enum_from!(ByteLevel, NormalizerWrapper, ByteLevel);

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn post_processor_deserialization_no_type() {
let json = r#"{"strip_left":false, "strip_right":true}"#;
let reconstructed = serde_json::from_str::<NormalizerWrapper>(json);
assert!(matches!(
reconstructed.unwrap(),
NormalizerWrapper::StripNormalizer(_)
));

let json =
r#"{"sep":["</s>",2], "cls":["<s>",0], "trim_offsets":true, "add_prefix_space":true}"#;
let reconstructed = serde_json::from_str::<NormalizerWrapper>(json).unwrap();
println!("{:?}", reconstructed);
assert!(matches!(
reconstructed,
NormalizerWrapper::Sequence(_)
));

let json = r#"{"type":"RobertaProcessing", "sep":["</s>",2] }"#;
let reconstructed = serde_json::from_str::<NormalizerWrapper>(json);
match reconstructed {
Err(err) => assert_eq!(
err.to_string(),
"data did not match any variant of untagged enum NormalizerWrapper"
),
_ => panic!("Expected an error here"),
}
}
}