FixTheRobot Explore Robotics / LLM / LM2 / LM2-L2

This lab works on phones, but writing code is much easier on a laptop.

LLM · Text Data and Tokenization · LM2-L2 · Intermediate

Implement Byte-Pair Encoding

A tokenizer with one token per word fails on every new word. One token per character makes every sentence long. Byte-pair encoding finds the middle: it builds frequent pieces out of characters, and unseen words fall back to the pieces.

You will learn the merge rules from a tiny corpus and use them to encode words the corpus never contained.

Your task

    Simulation
    solution.pyLoading Python…
    Ctrl+Enter runs
    What Python can I use?

    Plain Python with math (and slamkit for small matrix helpers: matmul, transpose, solve, inverse, identity). Images, point clouds and detections arrive as lists inside the dictionary D; an image is a list of rows, each row a list of pixels. There is no NumPy or OpenCV: you write the algorithm yourself, which is the point of the lab. Print with print().

    Hints

    What this lab teaches

    Stage 1 shows the trade-off between vocabulary size and sequence length. Stage 2 implements the merge-learning algorithm. Stage 3 encodes words by replaying the merges in order. The corpus is a handful of invented word counts, and BPE works on characters with an end-of-word marker; it is a faithful miniature of the algorithm, not a production tokenizer. Your code is graded on hidden corpora and words.

    Theory for this lab

    Text Data and Tokenization labs

    1. LM2-L1: Build a Simple Tokenizer
    2. LM2-L2: Implement Byte-Pair Encoding: this lab.
    3. LM2-L3: Diagnose Sliding Windows and Batching

    Educational simplification: a handful of invented words, characters instead of bytes, an explicit end-of-word marker, and deterministic tie-breaking. Production tokenizers (such as the one used by GPT-2) operate on bytes of very large corpora, pre-split text with rules, and have tens of thousands of merges. All data in this lab are invented.

    Get the next lab

    Get notified when new robotics labs are released. One email per new lab, nothing else.