Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix CLIP tokenizer vocab validation and whitespace parity coverage

laurenspriem8b82992ddc49
diff --git a/mobile/apps/photos/integration_test/clip_text_tokenizer_parity_test.dart b/mobile/apps/photos/integration_test/clip_text_tokenizer_parity_test.dart
index 850452216a..e7d7489e35 100644
--- a/mobile/apps/photos/integration_test/clip_text_tokenizer_parity_test.dart
+++ b/mobile/apps/photos/integration_test/clip_text_tokenizer_parity_test.dart
@@ -35,7 +35,7 @@ const _kExpectedMobileClipPythonTokenPrefixes = <String, List<int>>{
324,
49407,
],
- "multiple spaces\\tand\\nlines": [
+ "multiple spaces\tand\nlines": [
49406,
76,
84,
@@ -51,13 +51,9 @@ const _kExpectedMobileClipPythonTokenPrefixes = <String, List<int>>{
66,
68,
338,
- 315,
- 83,
64,
77,
323,
- 315,
- 77,
75,
72,
77,
@@ -279,6 +275,20 @@ void main() {
);
expect(rustTokens.length, _kClipTotalTokens);
}
+
+ final whitespaceVariant = await rust_ml.tokenizeClipTextRust(
+ text: "multiple spaces\tand\nlines",
+ vocabPath: vocabPath,
+ );
+ final normalizedWhitespace = await rust_ml.tokenizeClipTextRust(
+ text: "multiple spaces and lines",
+ vocabPath: vocabPath,
+ );
+ expect(
+ whitespaceVariant.toList(growable: false),
+ normalizedWhitespace.toList(growable: false),
+ reason: "Expected tab/newline runs to normalize to single spaces",
+ );
});
}
diff --git a/mobile/apps/photos/rust/src/ml/clip/tokenizer.rs b/mobile/apps/photos/rust/src/ml/clip/tokenizer.rs
index f9b63fba91..49c3cf65f1 100644
--- a/mobile/apps/photos/rust/src/ml/clip/tokenizer.rs
+++ b/mobile/apps/photos/rust/src/ml/clip/tokenizer.rs
@@ -82,12 +82,13 @@ impl ClipTextTokenizer {
let (byte_encoder, byte_encoder_values) = bytes_to_unicode()?;
let split = vocabulary.split('\n').collect::<Vec<_>>();
- let merges_end = split.len().min(BPE_MERGES_END_EXCLUSIVE);
- let merges_slice = if split.len() > 1 {
- &split[1..merges_end]
- } else {
- &[][..]
- };
+ if split.len() < BPE_MERGES_END_EXCLUSIVE {
+ return Err(MlError::Runtime(format!(
+ "invalid clip vocab: expected at least {BPE_MERGES_END_EXCLUSIVE} lines, got {}",
+ split.len()
+ )));
+ }
+ let merges_slice = &split[1..BPE_MERGES_END_EXCLUSIVE];
let mut merges = Vec::<(String, String)>::new();
for merge in merges_slice {
@@ -327,7 +328,7 @@ fn bytes_to_unicode() -> MlResult<(HashMap<u8, String>, Vec<String>)> {
#[cfg(test)]
mod tests {
- use super::{TOKEN_PATTERN, basic_clean, whitespace_clean};
+ use super::{ClipTextTokenizer, TOKEN_PATTERN, basic_clean, whitespace_clean};
fn cleaned_tokens(text: &str) -> Vec<String> {
let clean_text = whitespace_clean(&basic_clean(text)).to_lowercase();
@@ -365,4 +366,17 @@ mod tests {
],
);
}
+
+ #[test]
+ fn rejects_incomplete_bpe_vocabulary_file() {
+ let truncated_vocab = "#version: 0.2\na b";
+ let err = match ClipTextTokenizer::from_vocabulary(truncated_vocab) {
+ Ok(_) => panic!("expected incomplete vocab to fail"),
+ Err(err) => err,
+ };
+ assert!(
+ err.to_string().contains("invalid clip vocab"),
+ "unexpected error: {err}",
+ );
+ }
}