Skip to content
INDIE / MACHINE
// FREE COURSE /courses

The N-Grammer Course

N-Grammer layers (Roy et al., 2022) give a transformer a cheap lookup table keyed by latent bigrams: pairs of cluster ids computed from the model's own representations. This course takes the layer apart in six short lessons - two conceptual, two on the math, one on code and one on training - with interactive diagrams, quick quizzes, and a real layer that runs in your browser and matches PyTorch.
START LESSON 1 FOLLOW VIA RSS6 LESSONS~39 MINBEGINNER TO INTERMEDIATE
0 / 6 LESSONS DONE
// STEP BY STEP

Lessons

  1. 01
    CONCEPT5 MIN5 SLIDES2 QUESTIONS
    What is an n-gram?

    Bigrams, why counting them predicts the next word, and why N-Grammer uses latent bigrams instead of token bigrams.

  2. 02
    CONCEPT6 MIN5 SLIDES2 QUESTIONS
    The N-Grammer layer at a glance

    Where an N-Grammer layer sits in a transformer, the six steps of its forward pass, and what is learned versus computed.

  3. 03
    MATH7 MIN7 SLIDES3 QUESTIONS
    Snapping to centroids: product quantization

    Split a vector into heads, RMS-normalize each slice, and pick the nearest centroid with the |c|² − 2h·c distance trick.

  4. 04
    MATH6 MIN6 SLIDES3 QUESTIONS
    Bigram ids and the embedding table

    How (previous id, id) pairs become exact rows in one embedding table, the start-of-sequence row, and how many parameters the table costs.

  5. 05
    CODE8 MIN7 SLIDES2 QUESTIONS
    Coding it: PyTorch to Hyperfield

    The N-Grammer forward pass in PyTorch and in Hyperfield (TypeScript, WebGPU/CPU) side by side, an op-by-op mapping, and a live layer that matches PyTorch.

  6. 06
    CONCEPT7 MIN8 SLIDES3 QUESTIONS
    Training it: centroid loss and zero init

    Why the ids get no gradient, how the auxiliary centroid loss trains the codebook, what dead centroids are, and why a fresh layer is a no-op.