AI Logic

Tokenization

BPE-dropout

Updated:

Comparison of the steps for splitting the word lowest, proceeding from bottom to top. On the left, ordinary BPE applies the highest-ranked applicable merge each time and splits the word into two pieces, low and est</w>. On the right, BPE-dropout drops each applicable merge with probability p (dotted strikethrough) and applies the highest-ranked remaining merge; in round 5 nothing remains and the process stops, splitting the word into three pieces: low, est, and </w>

What Is BPE-dropout?

BPE-dropout is a method that splits the same word in various ways by making each merge rule unavailable with probability pp every time a word is split with BPE. It is used only while training the model; at inference, words are split with ordinary BPE (p=0p = 0). It was proposed in 2019 by Provilkov, Emelianenko, and Voita and presented at ACL 2020.

Ordinary BPE always splits the same word in the same single way. For example, "lowest" always becomes "low" and "est</w>". The model then never gets the chance to learn that "lowest" can also be built from smaller parts such as "lo," "w," and "est." The paper points out that this makes it hard to learn the composition of words (which parts they are made of) and makes the model vulnerable to segmentation errors and spelling errors.

An existing method for the same problem was subword regularization with a Unigram language model (Kudo 2018). However, it requires a segmentation mechanism separate from BPE and training of that mechanism. BPE-dropout achieves the same effect while using the BPE vocabulary and merge table as is, and adding just one line to the segmentation procedure. In the paper, the machine translation BLEU score improved by up to 2.3 over BPE and by up to 0.9 over Unigram subword regularization.

The figure at the top of the article shows the same word "lowest" split by ordinary BPE on the left and by BPE-dropout on the right. On the right, some merge candidates are dropped with probability pp (dotted), leading to a different segmentation.

Segmentation Procedure

BPE-dropout leaves BPE training (the procedure that builds the merge table) unchanged and modifies only the procedure for splitting words (Algorithm 1 in the paper). The merge table contains which two symbols to merge and their rank (the order in which the table was built).

  1. Split the word into characters.
  2. Collect every pair of adjacent symbols in the current segmentation that appears in the merge table (the applicable merges).
  3. Remove each collected merge with probability pp. This is the only difference from ordinary BPE. If the same merge occurs at two places in the word, whether to remove each is decided independently.
  4. If any merges remain, apply the highest-ranked one among them and return to step 2.
  5. If no merges remain, stop. Even if applicable merges still exist in the table, the segmentation is fixed once all of them are removed in that round.

pp determines how fine the segmentation is.

  • p=0p = 0: nothing is removed, so this is the same as ordinary BPE.
  • p=1p = 1: everything is removed, so the word is split into characters.
  • Between 0 and 1: the granularity lies in between, and the segmentation differs every time training sees the word. The paper usually used p=0.1p = 0.1.

The right side of the figure at the top of the article is one example of removals with pp, as follows.

RoundApplicable merges (rank)Merges removedMerge appliedSegmentation
1e+s (1), l+o (4)e+sl+olo w e s t </w>
2e+s (1), lo+w (5)Nonee+slo w es t </w>
3es+t (2), lo+w (5)es+tlo+wlow es t </w>
4es+t (2)Nonees+tlow est </w>
5est+</w> (3)est+</w>None (stop)low est </w>

The e+s removed in round 1 is back among the candidates in round 2, because whether to remove a merge is decided afresh each time. At the end, nothing remained, so the process stopped without merging "est" and "</w>". Ordinary BPE would give two pieces, "low" and "est</w>", but here there are three.

In training, as with subword regularization, sentences are re-segmented every time a minibatch is built. In equation form, segmentations xx (source) and yy (target) of a sentence are sampled from the segmentation distribution, and the expected log-likelihood over them is maximized (Eq. (1) in the paper). L=∑(X,Y)∈DEx∼P(x∣X)y∼P(y∣Y)log⁡P(y∣x,θ)\mathcal{L} = \sum_{(X, Y) \in D} \mathbb{E}_{\substack{x \sim P(x \mid X) \\ y \sim P(y \mid Y)}} \log P(y \mid x, \theta) In practice, a single segmentation is sampled for each update.

Why It Works

Comparing models trained with BPE-dropout against ordinary BPE models, the paper found the following differences (Section 6).

  • Frequent character sequences start appearing as single symbols. In ordinary BPE, a frequent sequence (e.g., "est") usually appears as part of a larger symbol and rarely as a symbol on its own, because only rare words get split. With BPE-dropout, keeping the same vocabulary, the proportion of frequent sequences appearing as standalone symbols increased markedly.
  • Embeddings of rare symbols improve. In ordinary BPE models, the embeddings of rare symbols clustered in a region apart from the other symbols. No such separation was observed with BPE-dropout. In addition, the proportion of the 10 nearest symbols in embedding space that share character sequences with the original symbol (character 4-gram overlap) was 0.18 for BPE versus 0.29 for BPE-dropout.
  • Robustness to spelling errors improves. Although no misspellings were shown during training, translation quality on inputs containing errors improved substantially (next section).

As an analogy, ordinary BPE teaches words with a fixed set of parts every time, while BPE-dropout sometimes shows the parts broken down into smaller pieces. Having seen the broken-down forms as well, the model can more easily handle the meaning of the parts and unfamiliar combinations. Just as dropout randomly switches off neurons, this is a regularization that randomly perturbs the input side.

Experimental Results

The paper compares BPE, Unigram subword regularization (Kudo 2018, l=64l = 64, α=0.1\alpha = 0.1), and BPE-dropout on machine translation with a Transformer (base, 6 layers, 8 heads, dmodel=512d_{\text{model}} = 512). At inference, all three use a single deterministic segmentation (ordinary BPE for BPE-dropout).

DatasetDirectionBPEKudo (2018)BPE-dropout
IWSLT15En→Vi31.7832.4333.27
IWSLT15Vi→En30.8332.3632.99
IWSLT15En→Zh20.4823.0122.84
IWSLT15Zh→En19.7221.1021.45
IWSLT17En→Fr39.3739.4540.02
IWSLT17Fr→En38.1838.8839.39
IWSLT17En→Ar13.8914.4315.05
IWSLT17Ar→En31.9032.8033.72
WMT14En→De27.4127.8228.01
WMT14De→En32.6933.6534.19
ASPECEn→Ja54.5155.4655.00
ASPECJa→En30.7731.2331.29

Values are BLEU (Table 2 in the paper). BPE-dropout outperforms BPE on all 12, and compared with Kudo (2018) it is better on 8, not significantly different on 3, and worse only on En→Ja. The paper suggests that Japanese and Chinese may benefit from language-specific segmentation.

Robustness to misspellings: when each word of the test input was corrupted with 10% probability by deleting, inserting, or substituting one character, the gain from BPE-dropout grew larger (Table 5 in the paper).

DataBPE (original → with errors)BPE-dropout (original → with errors)Difference with errors
WMT14 En→De27.41 → 24.4528.01 → 26.03+1.58
WMT14 De→En32.69 → 29.7134.19 → 32.03+2.32
WMT14 En→Fr (4M sentences)33.38 → 30.3033.85 → 32.13+1.83
WMT14 En→Fr (16M sentences)34.37 → 31.2334.82 → 32.94+1.71

Inference speed: with BPE-dropout, training sentences are on average about 1.25 times longer than with BPE (p=0.1p = 0.1). Even so, the length of translations generated by beam search is almost the same as for the BPE model, and inference time barely changes: 1.03 times BPE with a 32k vocabulary, and 1.46 times with 4k (1.44 times for BPE) (Table 4 in the paper; values relative to BPE with a 32k vocabulary = 1).

Practical Caveats

If pp is too large, the model can no longer translate. The training segmentation approaches character level and diverges greatly from the ordinary BPE segmentation used at inference. On WMT14 En→Fr (500k sentences) in the paper, p=0.1p = 0.1 gave the best result. For Chinese and Japanese, which have no word boundaries, p=0.6p = 0.6 is used so that sentences lengthen by the same amount as with p=0.1p = 0.1 in other languages.

For large datasets, apply it to the source side only. Based on experiments with subsets of WMT14 En→Fr (Table 3), the paper recommends applying it to both source and target for small and medium datasets, and only to the source side for large datasets (from around 4M sentences). With 16M sentences, applying it to both sides fell below BPE (33.66 vs. 34.37), while applying it to the source side only gave 34.82.

Also keep the following points in mind.

  • Use ordinary BPE at inference. BPE-dropout is used only during training; set p=0p = 0 at inference.
  • The larger the data, the smaller the effect. As with other regularization, the gain shrinks in data-rich settings. However, on inputs containing noise such as misspellings, a difference of 1.6–2.3 BLEU appeared even with large data.
  • The ordinary BPE vocabulary is fine. Skipping merges occasionally produces symbols not in the vocabulary (treated as unknown). The paper also compared adding the symbols that BPE-dropout can produce to the vocabulary, found no difference in preliminary experiments, and kept the BPE vocabulary. On English IWSLT15, unknown tokens were 0.6% of all tokens (32k vocabulary) and 0.09% (4k vocabulary).
  • It becomes less sensitive to vocabulary size. BPE-dropout models show a smaller gap between 4k and 32k vocabularies than ordinary BPE, so the vocabulary size need not be tuned finely for each dataset.

Comparison with Other Tokenization Methods

The table below compares representative subword segmentation methods.

MethodHow the vocabulary is builtSegmentation during trainingSegmentation at inferenceExtra training
BPEStarting from characters, add the most frequent pairsSingleSingle—
BPE-dropout (this article)Same as BPEPerturbed by skipping merges with probability ppOrdinary BPENone
Unigram language model + subword regularizationStarting from a large vocabulary, remove symbols with little effect on the likelihoodSampled according to segmentation probabilitiesMost probable segmentationUnigram language model (EM and Viterbi)
WordPieceStarting from characters, add the pair that most increases the likelihoodSingleSingle—

BPE-dropout and subword regularization share the same goal (perturbing segmentation during training) but differ in how they perturb it. Subword regularization picks natural segmentations according to the Unigram language model's probabilities, while BPE-dropout skips BPE merges with a uniform probability. The advantage of BPE-dropout is that it can be introduced into a system that already uses BPE simply by swapping the segmentation function.

Rough guidelines for choosing:

  • Already using BPE, with little data or noisy input: BPE-dropout (start with p=0.1p = 0.1).
  • Mainly languages without word boundaries (Japanese, Chinese): also try the Unigram language model with subword regularization in SentencePiece. In the paper as well, Kudo (2018) gave better results for En→Ja.
  • Very large data: apply BPE-dropout to the source side only, or stick with ordinary BPE.

PyTorch example

Show code
python
import random
from collections import Counter


def bpe_dropout(word, merges, p=0.1, rng=random):
    """Algorithm 1 of the paper. merges is the merge table learned by BPE (in rank order). p = 0 gives ordinary BPE"""
    rank = {pair: i for i, pair in enumerate(merges)}
    symbols = list(word) + ["</w>"]                       # split into characters
    while True:
        # Collect every merge applicable to the current segmentation (the same merge at two positions is treated separately)
        cands = [(rank[(a, b)], i) for i, (a, b) in enumerate(zip(symbols, symbols[1:])) if (a, b) in rank]
        # The only difference from ordinary BPE: drop each one with probability p
        cands = [c for c in cands if rng.random() >= p]
        if not cands:                                     # stop if none remain
            return symbols
        _, i = min(cands)                                 # apply the highest-ranked one (leftmost on ties)
        symbols[i:i + 2] = [symbols[i] + symbols[i + 1]]


# Same merge table as in the BPE article (5 merges on the training data of Algorithm 1 in Sennrich et al. 2016)
merges = [("e", "s"), ("es", "t"), ("est", "</w>"), ("l", "o"), ("lo", "w")]

print(bpe_dropout("lowest", merges, p=0.0))   # ['low', 'est</w>'] (inference: ordinary BPE)

rng = random.Random(0)
samples = Counter(" ".join(bpe_dropout("lowest", merges, p=0.1, rng=rng)) for _ in range(1000))
for seg, n in samples.most_common(5):         # training: the same word is segmented differently
    print(f"{n:4d}  {seg}")


# In the training loop, re-segment every time a minibatch is built (example applied to the source side only)
def make_batch(sentences, merges, train=True, p=0.1):
    p = p if train else 0.0                   # p = 0 at inference
    return [[tok for w in s.split() for tok in bpe_dropout(w, merges, p)] for s in sentences]

FAQ

Q. Does BPE need to be retrained to use BPE-dropout?
A.

No. The BPE vocabulary and merge table are used as is. Only the procedure for splitting the training sentences changes: one line is added that removes each applicable merge with probability pp before merges are applied. At inference, words are split with ordinary BPE.

Q. What value should p take?
A.

The paper recommends p=0.1p = 0.1. Varying pp on WMT14 En→Fr (500k sentences), 0.1 was best; making it too large caused the training and inference segmentations to diverge, and the model could no longer translate. For Japanese and Chinese, which have no word boundaries, p=0.6p = 0.6 is used so that sentences lengthen as much as with p=0.1p = 0.1 in other languages.

Q. Is it related to Dropout in the model architecture?
A.

Only the name and idea are similar; they are different things. Dropout randomly switches off neurons in the network. BPE-dropout randomly switches off BPE merges at the stage of segmenting the input text. Both perturb randomly only during training and not at inference, so they can be used together (the Transformer in the paper also uses the same regularization as the original paper).

References

  1. I. Provilkov, D. Emelianenko, and E. Voita, "BPE-Dropout: Simple and Effective Subword Regularization," ACL, 2020.
  2. R. Sennrich, B. Haddow, and A. Birch, "Neural Machine Translation of Rare Words with Subword Units," ACL, 2016.
  3. T. Kudo, "Subword Regularization: Improving Neural Network Translation Models with Multiple Subword Candidates," ACL, 2018.