A problem with pre-tokenizer regex

#2
by sszymczyk - opened

In tokenizer.json we have:

    "pretokenizers": [
      {
        "type": "Split",
        "pattern": {
          "Regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|s+(?!\\S)|\\s+"
        },
        "behavior": "Isolated",
        "invert": false
      },

Note the '|s' near the end, shouldn't it be '|\\s' like in the original llama pre-tokenizer regex below?

      {
        "type": "Split",
        "pattern": {
          "Regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+"
        },
        "behavior": "Isolated",
        "invert": false
      },
Fraunhofer-Institut für Integrierte Schaltungen IIS org

Hey, thanks for noticing! I don't think the double escape will make any difference while for AutoTokenizer, but we can test it.

Fraunhofer-Institut für Integrierte Schaltungen IIS org
edited 8 days ago

Alright I have been checking this out. The Tokenizer causes a different behavior for this scenario:
"Hello World" (so anything with more than one whitespace char between two words.
current version: ['Hello', ' ', 'World']
version with "\s"-fix: ['Hello', ' ', ' World']

I think ordinary, the model should benefit from the fix, since it learns to connect the convention space-prefixed version of other tokens to situations, where there are more spaces involved. Right now it learned, that multi-spaces are connected to tokens, which are ordinary used as first tokens in sentences.

However, I did some perplexity check with a longer text using multi spaces instead of white spaces and the model was far off, using the fix. I therefore suggest to stay with this "broken" version, since we'd need to perform a retraining to really make use of it.

(Maybe there are some other situations affected by this bug, but I did not find them so far.)

Edit: remark, the formatting broke in this comment. Imagine multiple spaces in the above mentioned example

I compared perplexity in llama.cpp on CPP code. Used ggml/src/ggml-opencl/ggml-opencl.cpp from llama.cpp since it's very long.

With the original "broken" regex I have: Final estimate: PPL = 3.7883 +/- 0.02268
With the "corrected" regex I have: Final estimate: PPL = 4.3889 +/- 0.02808

So I guess it's a good decision to keep the original form used for training the model.

Sign up or log in to comment