Different results compared with python BertWordPieceTokenizer
Hi Team,
Thanks for your great work! Recently I tested BertWordPieceTokenizer in this repo on my Multi-Lingual Dataset, compared with python BertWordPieceTokenizer, the results are different on some cases, I found two types:
-
Punctuation Type:
- Sentence: 台北‧08月20日
- Python Results: '台', '北', '‧', '08', '月', '20', '日'
- Rust Results: '台', '北', '‧', '##0', '##8', '月', '20', '日'
-
[UNK] Type:
- Sentence: application remained denied“
- Python Results: 'application', 'remained', 'denied', '[UNK]'
- Rust Results: 'application', 'remained', '[UNK]'
Are these differences expected?
Best Wishes!
Add two cases:
-
Punctuation Type:
- Sentence: Del Mundo ... ¿Quién seleccionó las siete maravillas del mundo antiguo y cuándo lo hizo?
- Python Results: 'Del', 'Mundo', '.', '.', '.', '¿', 'Qui', '##én', 'sel', '##ec', '##cionó', 'las', 'siete', 'mara', '##villa', '##s', 'del', 'mundo', 'antiguo', 'y', 'cu', '##ánd', '##o', 'lo', 'hizo', '?'
- Rust Results: 'Del', 'Mundo', '.', '.', '.', '¿', '##Q', '##ui', '##én', 'sel', '##ec', '##cionó', 'las', 'siete', 'mara', '##villa', '##s', 'del', 'mundo', 'antiguo', 'y', 'cu', '##ánd', '##o', 'lo', 'hizo', '?'
-
[UNK] Type:
- Sentence: Explore iPhone, the world\u2019s most powerful personal device.
- Python Results: 'Ex', '##plo', '##re', 'iPhone', ',', 'the', 'world', '[UNK]', 's', 'most', 'powerful', 'personal', 'device', '.'
- Rust Results: 'Ex', '##plo', '##re', 'iPhone', ',', 'the', '[UNK]', 'most', 'powerful', 'personal', 'device', '.'
Hey @Mansterteddy, could you share your code to reproduce these results?
Firstly, I use multi-lingual vocab from BERT as vocabulary.
For Rust Results, you can refer following code:
from tokenizers import BertWordPieceTokenizer
tokenizer = BertWordPieceTokenizer("vocab.txt", lowercase=False, add_special_tokens=False, strip_accents=False)
test_str = "application remained denied“"
print(test_str)
output = tokenizer.encode(test_str)
print(output.ids)
print(output.tokens)
print(output.offsets)
test_str = "\u53f0\u5317\u202708\u670820\u65e5"
print(test_str)
output = tokenizer.encode(test_str)
print(output.ids)
print(output.tokens)
print(output.offsets)
test_str = "Explore iPhone, the world\u2019s most powerful personal device."
print(test_str)
output = tokenizer.encode(test_str)
print(output.ids)
print(output.tokens)
print(output.offsets)
test_str = "Del Mundo ... \u00bfQui\u00e9n seleccion\u00f3 las siete maravillas del mundo antiguo y cu\u00e1ndo lo hizo?"
print(test_str)
output = tokenizer.encode(test_str)
print(output.ids)
print(output.tokens)
print(output.offsets)
For Python Results, I used tokenizer from huggingface transformer, you can refer following code:
import tokenization
vocab_file = "./vocab.txt"
tokenizer = tokenization.FullTokenizer(vocab_file=vocab_file, do_lower_case=False)
query = "application remained denied“"
query = tokenizer.tokenize(query)
print(query)
Looks like the differences are due to how we do pre-tokenization vs how Google's FullTokenizer does pre-tokenization. We only look for ASCII punctuation currently, so "“" for example is not considered punctuation.
@n1t0 maybe this needs improvement. I think there are several crates for looking up Unicode categories of characters that we could use to find non-ASCII punctuation.
Was just going to create a new issue but then found this one which seems the same as our problem. In case my examples help anyone I'll add them here.
In my case a sentence like a’b was tokenizing as ['a', '’', 'b'] but is now tokenizing the latter characters as suffixes like ['a', '##’', '##b']. This appears to come down to a difference of opinion between what is punctuation (in this case with the right single quote char)
In the transformers repo it is considered punctuation.
def _is_punctuation(char):
"""Checks whether `chars` is a punctuation character."""
cp = ord(char)
# We treat all non-letter/number ASCII as punctuation.
# Characters such as "^", "$", and "`" are not in the Unicode
# Punctuation class but we treat them as punctuation anyways, for
# consistency.
if (cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126):
return True
cat = unicodedata.category(char)
if cat.startswith("P"):
return True
return False
assert _is_punctuation('’') is True
While in the rust implementation is_ascii_punctuation returns false.
fn main() {
assert!('’'.is_ascii_punctuation());
}
thread 'main' panicked at 'assertion failed: '’'.is_ascii_punctuation()', main.rs:2:3
From a quick check - we are seeing this with at least the following characters:
['§', '’', '/', '‡', '.', '=', '<', '』', ',', '"', '>', '`', '。', '?', "'", '}', '„', '‚', '”', '、', '·', '¶', '+', '«', '|', '•', ';', '^', '-', '{', '(', '_', ')', '『', '@', '—', '[', '\\', '#', '»', '†', '「', '~', '–', '$', ':', '&', '…', '‰', '¿', '?', '!', ']', '!', '‘', '」', '.', '*', '¡', '“', '%']
I can't say which punctuation set is correct 🤷♂but we'd rather not retrain our model if we could make them consistent.
@epwalsh Found a crate: https://github.com/swgillespie/unicode-categories.
Thanks @Mansterteddy! I definitely think it makes sense to stay consistent with transformers and other implementations which look for unicode punctuation. @n1t0 @julien-c @thomwolf what do you think?
Sure! I didn’t get a chance to have a look yet but we definitely want to be consistent with other implementations.
Sounds good! @Mansterteddy, @kkaehler, anyone willing to make a PR for this? If not I'll have some time early next week.
@epwalsh Currently I am working on a version for solving this punctuation issue, I have solved above cases, I am testing it on other multi-lingual datasets. It is not hard to implement, but maybe it will affect other tokenizers.
I agree with @kkaehler and @n1t0 , at least we need to be consistent with HuggingFace's Transformer, otherwise, users need to retrain their model using this tokenizer, it will cost time and resources.
Furthermore, I have another question, I can build python binding on Ubuntu, however, when I want to build python binding on Windows, I always met this error: fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'x86', do you have any idea?
I have solved this issue by replacing char::is_ascii_punctuation with following function:
use unicode_categories::UnicodeCategories;
fn is_punc(x: &char) -> bool{
char::is_ascii_punctuation(x) || x.is_punctuation()
}
This function is equal to python code posted by @kkaehler . You can refer following links: char::is_ascii_punctuation, is_punctuation(), and Unicode Categories.
I have tested on my multi-lingual datasets, the results are consistent compared with Python Tokenizer of HuggingFace Transformers. However, I still cannot build python binding on Windows.
@Mansterteddy I'm sorry I probably can't help much with Windows. Can you open a separate issue for that? And can you make a PR with your punctuation patch?
@epwalsh Have sent the PR.
Have merged the PR, close this issue.