Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • Getting Started with AI for VLSI Engineers5
  • Just Enough AI: Models and Context15
    • What a Language Model Does
    • Tokens and Tokenization
      • Tokens, Words and Characters
      • Subword Tokens and Byte-Pair Encoding
      • Why Some Text Costs More Tokens
      • Approximate Rules of Thumb
      • Estimating Tokens Before Sending
    • Context Window and Attention for Users
  • Prompting13
  • Trust and Verification14
  • Using AI for Engineering Work17
  • Engineering Walkthroughs7
  • Practical Foundations for AI Workflows4
  • Engineering Data5
  • Retrieval-Augmented Generation6
  • Agents and Tool Use18
  • Agent Design8
  • MCP and Skills6
  • Multi-Agent Design6
  • Practical Capstone6
  • Tokens, Cost and Latency12
  • Agentic Workflows for Chip Teams18
  • Interview and Judgment10
  • AI Skills: Full Course Index
  • Working with AI Glossary
  • Working with AI Cheatsheet
Loading lesson

‹Tokens and Tokenization
  1. Topics
  2. AI Skills for VLSI Engineers
  3. Just Enough AI: Models and Context
  4. Tokens and Tokenization
  5. Subword Tokens and Byte-Pair Encoding

Just Enough AI · Beginner to Intermediate

Subword Tokens and Byte-Pair Encoding

Subword tokens and byte-pair encoding, with a small runnable merge trainer.

On this page

    The subword idea

    Frequent letter groups keep long pieces, while rare strings fall back to short pieces. That trade keeps common text compact without a huge table.

    The table of pieces is fixed before use. The same text then splits the same way every time on that splitter.

    Piece length

    When it appears

    Example shape

    Long piece

    A frequent group in the training corpus

    ing, tion, assign

    Short piece

    A rare group or a single character fallback

    x, qz, 7

    Single character

    Anything unseen: the guaranteed fallback

    One character per piece

    The merge loop

    Training starts from single characters and repeats one step: count adjacent pairs, join the most frequent pair into a new piece. Each round adds exactly one piece.

    Ties need a stated rule, such as first in sorted order. Without one, two runs can learn different tables.

    BPE merge loop.
    One BPE round counts pairs, joins the top pair and repeats (schematic).

    A worked merge

    The calc block joins pairs on a tiny corpus by hand. It is separate from the program corpus below.

    plaintext
    1
    2
    3
    4
    5
    6
    7
    8
    corpus: "ab" x 3, "ac" x 1
    start pieces: [a, b] x 3 and [a, c] x 1
    pair counts: (a,b) = 3, (a,c) = 1; total pairs = 4
    merge 1: join (a,b) -> "ab" since 3 > 1
    after merge 1: [ab] x 3 and [a, c] x 1
    remaining pairs: (a,c) = 1
    merge 2: join (a,c) -> "ac"
    result: table gains "ab" then "ac"; "ab" splits as [ab], "ac" as [ac]

    Toy trainer

    The program below is a real deterministic trainer on a small corpus. It learns 5 merges, tokenizes seen and unseen words, and checks its own invariants.

    Unseen words reuse the learned merges in order. Joining the pieces always rebuilds the input word.

    example.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    import sys
    
    CORPUS = {"low": 3, "lowest": 2, "newer": 2, "wider": 1}
    ROUNDS = 5
    
    splits = {}
    for word in CORPUS:
        splits[word] = list(word)
    
    merges = []
    for _ in range(ROUNDS):
        counts = {}
        for word, freq in CORPUS.items():
            parts = splits[word]
            for i in range(len(parts) - 1):
                pair = (parts[i], parts[i + 1])
                counts[pair] = counts.get(pair, 0) + freq
        if not counts:
            break
        best = sorted(counts.items(), key=lambda kv: (-kv[1], kv[0]))[0][0]
        new_tok = best[0] + best[1]
        for word in splits:
            parts = splits[word]
            out = []
            i = 0
            while i < len(parts):
                if i + 1 < len(parts) and parts[i] == best[0] and parts[i + 1] == best[1]:
                    out.append(new_tok)
                    i += 2
                else:
                    out.append(parts[i])
                    i += 1
            splits[word] = out
        merges.append((best[0], best[1], new_tok))
    
    def tokenize(raw):
        parts = list(raw)
        for a, b, new_tok in merges:
            out = []
            i = 0
            while i < len(parts):
                if i + 1 < len(parts) and parts[i] == a and parts[i + 1] == b:
                    out.append(new_tok)
                    i += 2
                else:
                    out.append(parts[i])
                    i += 1
            parts = out
        return parts
    
    bad = 0
    if len(merges) != ROUNDS:
        bad += 1
    for a, b, new_tok in merges:
        if new_tok != a + b:
            bad += 1
    for w in ["low", "lowest", "newer", "wider", "lower", "newest"]:
        tok = tokenize(w)
        if "".join(tok) != w:
            bad += 1
        if len(tok) > len(w):
            bad += 1
        if any(t == "" for t in tok):
            bad += 1
    if bad != 0:
        print("FAIL: " + str(bad) + " checks wrong")
        sys.exit(1)
    print("PASS: " + str(len(merges)) + " merges " + str(merges) + " newest " + str(tokenize("newest")))

    Common mistakes

    Common pitfall

    Merges depend on the training corpus, not on the text being split. The same word splits differently under two tables, so never quote splits without naming the splitter.

    Interview

    Likely probes: the loop and the fallback.

    • Each round joins the most frequent adjacent pair and adds one piece.

    • Ties need a stated deterministic rule to keep tables stable.

    • Single characters are the fallback, so any string still splits.

    Check yourself The hand example merges (a,b) before (a,c). What arithmetic decides that order?

    Pair counts 3 against 1 over 4 total pairs. The larger count joins first.

    Check yourself newest is not in the program corpus. How does the program still split it, and what stays true?

    It reuses the 5 learned merges in order on its characters. Joining the pieces still rebuilds newest, with no more pieces than characters.

    Related topics

    • Tokens, words and characters

    • Why some text costs more tokens

    Previous

    12 / 173

    Next: Why Some Text Costs More Tokens

    Up next

    Up nextWhy Some Text Costs More Tokens

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Approximate Rules of Thumb
    • Estimating Tokens Before Sending