# A New Class of Glitch Tokens - BPE Subtoken Artifacts (BSA)

Introduction
------------

I've spent the last few days going through every glitch token listed in the [third SolidGoldMagikarp glitch token post](https://www.lesswrong.com/posts/8viQEp8KBg2QSW4Yc/solidgoldmagikarp-iii-glitch-token-archaeology), and was able to find the cause/source of almost every single one. This is the first in a series of posts in which I will explain the behaviors of these glitch tokens, the context in which they appear in the training data, and what they reveal about the internal dynamics of LLMs. If you're unfamiliar with glitch tokens, I highly suggest you read the [glitch](https://www.lesswrong.com/posts/aPeJE8bSo6rAFoLqg/solidgoldmagikarp-plus-prompt-generation) token [archaeology](https://www.lesswrong.com/posts/Ya9LzwEbfaAMY8ABo/solidgoldmagikarp-ii-technical-details-and-more-recent) posts first.

My process involved searching through [OpenWebText](https://skylion007.github.io/OpenWebTextCorpus/), a recreation of GPT2 training data, and prompting GPT2 to locate the context of the training data.

Previous Context
----------------

In their 2023 post, the authors made the following pronouncement in regard to the glitch token ' practition' (token_id: 17629). I took it as a personal challenge.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/4b9053e83c2bd38fd440527879f053afdf28af36f3170bd7.png)

The first thing I found was that ' practitioner' (32110) and ' practitioners' (24068) were both already tokens in the GPT2 tokenizer. Furthermore, all three tokens also present in the GPT3.5/4 and GPT4o tokenizers, meaning they weren't an artifact of GPT2's training data!

There were only 13 examples of " practition" in OpenWebText.

    ['urlsf_subset00-928_data.xz', '\nThe WCC is also a prominent supporter and practitioning body for Peace journalism: journalism practice that'], ['urlsf_subset01-45_data.xz', ' what is now the Czech Republic, became a noted practitioneer of lagers, and geology again']
    ['urlsf_subset06-66_data.xz', 'The site relaunched on Wordpress VIP, practitioned by ESPN and 10up, a company']
    ['urlsf_subset07-802_data.xz', ' one possible explanation for the positive association between BDSM practitioning and subjective well-being.”'], ['urlsf_subset09-305_data.xz' , ' that vital personal touch that feudalism asked of its practitioning monarchs. He made wild leaps of']
    ['urlsf_subset11-474_data.xz', ' of the Senators asked “…if a health practitionerr is advising a patient to go on a']
    ['urlsf_subset11-809_data.xz', ' the full range of Buddhist traditions and the diversity of practition-ers today. As Buddhism in the West']
    ['urlsf_subset12-398_data.xz', ' one possible explanation for the positive association between BDSM practitioning and subjective well‐being. Several limitations']
    ['urlsf_subset13-921_data.xz', ' availa ble.\n\nSecondly , LCA practitione rs need to comply with the ISO\n']
    ['urlsf_subset15-258_data.xz', 'en K, et al. Investigation of mindfulness meditation practitiones with voxel-based morphometry']
    ['urlsf_subset15-797_data.xz', ' size effects in statistical pattern recognition: Recommendations for practitioneers. - IEEE Transactions on Pattern Analysis and'
    ['urlsf_subset19-877_data.xz', '\n\nStarting at level 6 , as a mask practitionner , you dive deeper into your ways and']
    ['urlsf_subset20-165_data.xz', ' It’s tought in university, probably practitioned in most of the development shops out there']

They were mostly misspellings, as an element of " practitioning", or line breaks artifacts[^of1h648jf5a].

Experimentation
---------------

I examined some other low-frequency tokens in GPT2 and found a few which were substrings of a higher-frequency counterpart. 'ortunately' (4690) also behaved like a glitch token, while higher-frequency subtokens like ' volunte' (7105) didn't.

    tokenId | tokenString | tokenFrequency
    17629 ' practition' 13
    32110 ' practitioner' 9942
    24068 ' practitioners' 14646
    
    4690 'ortunately' 14
    6668 'fortunately' 4329
    39955 ' fortunately' 10768
    31276 'Fortunately' 15667
     
    7105 ' volunte' 34
    41434 ' volunteering' 10598
    32730 ' volunteered' 14176
    13904 ' volunteer' 20037
    11661 ' volunteers' 20284
    
    6598 ' behavi' 65
    46571 'behavior' 7295
    41672 ' behavioural' 7724
    38975 ' behaviours' 9416
    37722 ' behaving' 12645
    17211 ' behavioral' 16533
    14301 ' behaviors' 18709
    9172 ' behaviour' 20497
    4069 ' behavior' 20609

Helpful Contributions
---------------------

Others pointed out that this was a result of [Byte-Pair Encoding](https://huggingface.co/learn/nlp-course/en/chapter6/5), which builds tokens out of shorter, previously encounter, tokens.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/9a65ea8507c0aa76083c864be153e3a11353314778016152.png)

[Lao Mein's Shortform — LessWrong](https://www.lesswrong.com/posts/igEuJm3kdSgSFaTsG/lao-mein-s-shortform?commentId=JGcuDeiCZcGXumSR2)

I was very surprised by this, since glitch behavior implies a low frequency in the training data, and identifying and removing them from the tokenizer takes little effort. Gwern thinks the researchers are just that lazy. My overall impression is that glitch tokens are a useful tool to help prevent catastrophic forgetting, but that's for a future post. Even then, I'm doubtful that incorporating low-frequency BSA tokens can improve performance. Maybe they contribute to the [spelling miracle](https://www.lesswrong.com/posts/5sNLX2yY5FzkCp7Ju/the-spelling-miracle-gpt-3-spelling-abilities-and-glitch) in some poorly understood way?

Applications
------------

I'm not sure if this approach is useful for finding glitch tokens in other GPT models. Due to things like misspellings, line breaks and uncommon variants, even tokens rare enough to trigger glitch behavior in GPT2 are likely pushed over the glitchiness threshold in GPT4 and GPT4o. 

However, the differences in token ids of substrings can be used to help identify the potential glitchiness of a token with no other knowledge of the model or training data[^b9bff3wznc]. We know from the mechanisms of BPE encoding that smaller subtokens created before they are merged into larger tokens. However, if a subtoken is never/rarely encountered outside its parent token, we would expect it to have an index close to the parent token, and that's exactly what we observe for many glitch tokens!

For example:

    42066 Nitrome 8
    42089  TheNitrome 0
    42090  TheNitromeFan 0

Note how close the index of " TheNitrome" (42089) is to " TheNitromeFan"[^28kmvym2j2g] (42090). This indicates 2 things:

1.  The distribution of these tokens are highly uneven, a tell for glitch tokens[^ad5o2a0log].
2.  The subtokens were relatively rarely seen independently - they're infrequent outside the larger tokens[^ddjsjsgdfoa], meaning that they will rarely be encountered in the training data due to the greedy tokenizer[^jm409e6gahn].

Many of the most well-know glitch tokens exhibit this pattern:

    42202 GoldMagikarp 0
    43453  SolidGoldMagikarp 0
    
    36481 ertodd 125
    37444  petertodd 29
    
    40219 oreAnd 3
    40240 oreAndOnline 0
    40241 InstoreAndOnline 0
    40242 BuyableInstoreAndOnline 1

The token ids suggest "`oreAndOnline`" and "`InstoreAndOnline `" virtually never appears apart from "`BuyableInstoreAndOnline`". However, the small gap between "oreAnd" and the other tokens implies that "oreAnd" was occasionally present in the data as something else - and that's exactly what we observe! It appears as parts of various functions, generally in something like "IgnoreAnd..."[^c8q4zbkr1dh]. 

    ['urlsf_subset03-643_data.xz', "voltage__get_by_counts_value() whatsoever. That's what …_IgnoreAndReturn() functions are for (generated by CMock): void test_voltage_get ("
     '56. adc_handler__voltage__get_by_counts_value_IgnoreAndReturn ( 456 ) ; //-- actually call the function being tested, that should perform //'
      'ADC_CH_CNT ; channel ++ ) { adc_handler__ctor_IgnoreAndReturn ( ADC_HANDLER_RES__OK ) ; } //-- before each test']
    ['urlsf_subset05-264_data.xz', ' guts and gloryI\'m tryin to hear B. I. G. And some cuts from NoreAnd you keep talkin over the beat like Clue ("Do you remember?").. Go find']
    ['urlsf_subset11-699_data.xz', 'ax push eax call dword ptr[ebp + aShellExecuteA] // RestoreAndExit mov esp, [ebp + SaveESP] popfd popad jmp d']

Nearby (by token\_id) tokens can also be used to infer the context of hard-to-locate tokens. For example, "natureconservancy" (41380) and "assetsadobe" (41382) have close token\_ids. "assetadobe" also continues as something like

    "assetsadobe.com/is/image/content/dam/tnc/nature/en/photos"
    "assetsadobe.com/is/image/content/dam/tnc/nature/en/photos/tnc_92425982.jpg?crop=0,0,5120,3740&wid=800&hei=600"
    'assetsadobe.com/is/image/content/dam/tnc/nature/en/photos/tnc_91708743_4000x2200.jpg?crop=900,0,2200,2200&wid=150&hei'

~100% of the time. It looks like both tokens were part of nature.org scrapes. 

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/344f47938118e437444d21d3600aa3033ee6f627995c784d.png)

Similarly, "GoldMagikarp" (42202) and "TheNitromeFan" (42090) are around Reddit political discussion and numerical tokens like 

    ['42224', '411'],['42215', ' 412'], ['42277', ' fundamentalist'],  
    ['42318', ' Racial'], ['42315', 'Mario']

This suggests it's part of a Reddit scrape. When prompted with counting subreddit names, the continuation will assume it's a Reddit response. Note the "\\n\\n" (628), often times referred to as "Reddit spacing" due to its frequency on that site. 

     TheNitromeFan\n\nI am not an expert on the history of this subreddit but I have read the
     TheNitromeFan\n\nI'm a huge fan of the "I don't care" meme. I
     SolidGoldMagikarp\n\nThe first time I heard of the concept of an "alt-right" was
     SolidGoldMagikarp\n\nI have a few friends who love to watch porn, and I have a few

Interestingly, " SolidGoldMagikarp" (43453) occurs quite a bit away from "GoldMagikarp" (42202). My theory for this is that Reddit posts are often in the following format:

    u/redditusername
    redditusername
    •
    2d ago
    <actual post>

and "u/SolidGoldMagikarp" tokenizes as:

    [[84, 14, 46933, 42202], ['u', '/', 'Solid', 'GoldMagikarp']]

It's possible only enough of his post were included to get "GoldMagikarp" as a token for a while. This was corrected after more of his posts were read later, and the full " SolidGoldMagikarp" token was included in the final vocabulary[^rlcwe2brmb].

My next post will be about why an earlier hypothesis that many glitch tokens were excluded from training data is unlikely to be true for most glitch tokens, and the role that glitch tokens play in improving LLM performance.

If you're curious about a particular glitch token, feel free to comment.

Addendum
--------

There are two tokens I still don't have a source for. Unlike almost every other glitch token, they exhibit spontaneous glitch behavior, even when they are the only prompt.

    25658 '?????-' 3
    31666 '?????-?????-' 0
    

+++ Completions for "?????-":

    ?????-
    
    The first thing that struck me was how many people were there. It's a
    ?????-;. (2)
    
    A person is guilty of a crime, punishable by imprisonment
    ?????-;)<(;;<;;'<;;'<;;'<;;'<
    ?????-;
    
    -;-;-;-;-;-;-;-
    ?????-;)
    
    I'm not sure I'm going to be as successful with this as
    ?????-
    
    "I don't know if I should be proud or sad."
    
    "
    ?????-
    
    "What's wrong?" I asked him, as he was sitting on a bench
    ?????-
    
    -
    
    -
    
    -
    
    -
    
    -
    
    -
    ?????-;.  -. .  -  -. .  -
    ?????-
    
    -
    
    -
    
    -
    
    -
    
    -
    
    -
    ?????-;)
    
    I'm going to be very, very brief.
    
    I don
    ?????-
    
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    ?????-
    
    "You're not the one I was hoping to see!"




+++

[^of1h648jf5a]: " practitionerr" is tokenized into:[[17629, 8056], [' practition', 'err']]instead of [[32110, 81], [' practitioner', 'r']].There's something I don't understand here - I thought BPE tokenization was maximally greedy! Or does that only apply to tokenizer training, and something else happens when a tokenizer is ran on text? 

[^b9bff3wznc]: I can probably find glitch tokens in something the size of GPT2 just with access to the tokenizer. 

[^28kmvym2j2g]: "Nitrome" is also the name of a video game developer, so it appeared in other places in the dataset, and thus was tokenized slightly sooner. 

[^ad5o2a0log]: for example, literally all instances of "ÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂ" (token_id: 35496)(that's 64 characters) in OpenWebText are from archive.org. There are 9468 instances in total, not counting shorter variants with 1, 2, 4, 8, 16, and 32 characters. The following was the first example I found. This goes on for 200,000 characters on this page alone.@Rana Dexsin wrote the single best answer I've ever seen on the internet.Which links to this.There were no hidden characters in these sequences. 

[^ddjsjsgdfoa]: The second one isn't always true; many glitch tokens exhibiting such patterns may be from alternate tokenizations. For example, "EStream" almost always continues as "EStreamControl...", distinct from "EStreamFrame". Both "EStreamFrame" and "EStream" are part of many functions in the enums submodule of the Python Stream library. 

[^jm409e6gahn]: If "oreAndOnline" is only present as part of "BuyableInstoreAndOnline", the model will never see the "oreAndOnline" token. 

[^c8q4zbkr1dh]: For example, the CMock function "_IgnoreAndReturn()". CMock is used to measure the performance of C++ functions, and it makes sense that some CMock logs were included in the training data. 

[^rlcwe2brmb]: I do find it interesting that I haven't been able to induce counting behavior in GPT2. This may be related to so many multi-digit numbers having their own tokenizations - "411" (42224), "412"(42215), and so on, and that's just not a pattern GPT2 can figure out. It's possible that the r/Counting subreddit is a major contributor in why GPT2 is so bad at math!I then looked around at GPT4 and GPT4o tokenizers. To my shock and horror, they still include multi-digit numbers as tokens. "693" (48779) being an example in GPT4o.This is... what? Why? How can this possibly be a good idea in an LLM used for math and coding applications? It would certainly explain why GPT4/GPT4o is so bad at math.