Repository navigation
fix: lowercase text for bge-small-zh-v1.5 (capitalized words became [UNK]) - #940
chaitanya0305 wants to merge 1 commit into
Conversation
The vocabulary of BAAI/bge-small-zh-v1.5 has no uppercase latin letters, but its tokenizer doesn't lowercase (do_lower_case false in its tokenizer_config.json, unlike bge-base-zh and bge-large-zh), so every capitalized latin word became [UNK]. sentence-transformers lowercases the text first (do_lower_case in sentence_bert_config.json). "Apple iPhone 售价" had cosine 0.80 with the sentence-transformers embedding. Add a lowercase flag to DenseModelDescription, set it for bge-small-zh-v1.5 and lowercase the text before tokenization and token counting, as mxbai-edge-colbert does.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
What is wrong
BAAI/bge-small-zh-v1.5turns every capitalized Latin word into[UNK]. Its WordPiece vocabulary has no uppercase Latin letters, but its tokenizer does not lowercase:do_lower_caseisfalsein itstokenizer_config.json, unlikebge-base-zh-v1.5andbge-large-zh-v1.5, and theBertNormalizerintokenizer.jsonhaslowercase: false. This holds for both theQdrant/bge-small-zh-v1.5export and theXenovaCoreML copy. sentence-transformers lowercases the text before tokenizing ("do_lower_case": truein the model'ssentence_bert_config.json), so the reference embedding is computed on lowercased text.Apple iPhone 售价[CLS] [UNK] [UNK] 售 价 [SEP]The quick brown fox jumps over the lazy dog.[CLS] [UNK] quick ...apple iphone 售价/今天天气很好Any brand, product name or sentence-initial word in mixed Chinese/English text is affected, and distinct names collapse to the same vector (also reported upstream for the plain transformers path in FlagOpen/FlagEmbedding#1617). The canonical vector in the tests did not catch it because it is computed on lowercase text.
I checked every text, late-interaction and cross-encoder model fastembed lists: this is the only one whose
sentence_bert_config.jsonlowercases while the tokenizer fastembed downloads does not.mxbai-edge-colbert-v0-17mis the other one, and it already lowercases in Python.Fix
DenseModelDescription.lowercase(defaultFalse), set forBAAI/bge-small-zh-v1.5.OnnxTextEmbedding.tokenizeand_token_countlowercase the text in Python when the flag is set, likeMxbaiEdgeColbert, and not through a tokenizer normalizer, for the reasons given there. Token counts and length-sorted batching therefore see the same tokens the model does.parallel) and the CoreML path go through the sametokenize, so they are covered too. I checkedparallel=2andCoreMLExecutionProviderby hand: both now match the lowercase text.Tests
test_lowercases_text_for_bge_small_zhembeds"Apple iPhone 售价"and checks it against the sentence-transformers values (SentenceTransformer("BAAI/bge-small-zh-v1.5").encode(..., normalize_embeddings=True)[:5]), against the lowercase text, and that the token counts match. It fails on main ([-0.0137, 0.0196, ...]vs[-0.0038, 0.0409, ...]) and passes with the fix.tests/test_text_onnx_embeddings.pyandtests/test_custom_models.pypass (37 passed). ruff, ruff format and mypy are clean on the changed files.Canonical values snippet:
All Submissions:
New Feature Submissions:
pre-commitwithpip3 install pre-commitand set up hooks withpre-commit install? (ran the same hooks,ruff checkandruff format, directly)