[KongchangAI]
· 3 min read· 1,544 words

Bilibili Hit Animation Breaks Down Transformer: Is AI Writing Just Drawing Lots?

Bilibili Hit Animation Breaks Down Transformer: Is AI Writing Just Drawing Lots?

Bilibili animation tutorial uses a 'drawing lots' metaphor to break down the entire Transformer inference pipeline.

Based on a Bilibili animation tutorial, this article uses a 'drawing lots' metaphor to explain how large language models perform inference. The model computes only the probability table for the next token each time, knowing over 150,000 candidates, but doesn't directly output the whole sentence. To avoid repetitive loops from greedy decoding, the model uses probability-based sampling (drawing lots), controlled by temperature and TOP P knobs. Temperature scales score gaps before softmax, and TOP P truncates the long-tail candidates. The article further breaks down the Transformer's four-station internal computation: token-to-vector conversion, multi-layer attention and feed-forward networks, final vector comparison with candidates, and softmax probability conversion. It concludes by noting that the model doesn't directly see the text users input—it sees tokens that have been split, leading into the topic of tokenization.

You press Enter, and the AI starts spitting out one character at a time. What exactly happens in between? How is each character chosen? A Bilibili animation tutorial uses the metaphor of "drawing lots" to break down the entire Transformer inference process from start to finish. This article combines the video content and screenshots to map out the complete chain from input to output.

1. The First Step of AI Writing: Computing a Probability Table

The model doesn't directly spit out a sentence. Each time it computes, it does only one thing: predict the candidate words for the next position and assign a probability to each candidate.

According to the video demonstration, a small open-source model with tens of millions of parameters knows over 150,000 characters and words, each of which is a candidate. Given the input "I want to drink a cup of," the top-ranked candidates are exactly water, coffee, and milk tea, but the top ten together account for only about a quarter, with a 150,000-long tail trailing behind. These characters and words are collectively called tokens—the model reads them and writes them.

A hundred sticks in the lot tube

This table is the only thing the model does. The actual choice happens in the next step.

2. Drawing Lots Rather Than Picking the Highest Score: Why AI Writes This Way

Now that we have a probability table, why not just pick the one with the highest probability? The experiment in the video provides the answer: turn off lot-drawing and pick the top candidate at every step, ask the same question twenty times, and all twenty responses are identical—regenerating becomes meaningless.

What's worse, ask it to continue "The weather is nice today," and it writes "The sun is shining, the breeze is gentle, perfect for a walk," then loops back to "The weather is nice today," going around and around. The video author tried four openings, and two of them did this. The reason is that the sentence already appears in the context, so writing it again becomes the most confident choice. This approach of always picking the top candidate at each step is called greedy decoding.

So the model switched to drawing lots according to probability: the higher the probability, the more lots. Ask the same question twice, and after "Autumn is the season of harvest," the first draw yields "and also," the second yields "all things."

One step can take the whole sentence elsewhere

The drawn word is appended to the context, the whole passage is fed back in to compute a new table, and another draw is made. This writing method is called autoregressive. The one-character-at-a-time output you see is not a typewriter effect—it really computes one and outputs one. In the video, the same question was asked twenty times; the first word was always the same, diverging more and more afterward, ultimately yielding seventeen different responses.

Greedy decoding is the simplest strategy in sequence generation: at each step, choose the token with the highest current probability. It is deterministic and computationally simple, but prone to repetitive loops, because once the model generates a phrase, that phrase enters the context, making its regeneration a high-probability event. The "The weather is nice today" loop in the video is a typical manifestation of this phenomenon.

Opposite to greedy is sampling decoding, which randomly draws the next token according to the probability distribution. Sampling introduces diversity, but pure random sampling may draw absurd low-probability words, so in practice it is often combined with temperature scaling and TOP P truncation to constrain the sampling space.

3. Two Knobs: Temperature and TOP P

All 150,000 candidates have probabilities—what if an absurd one gets drawn? The video points out that two knobs control this.

The model first scores the candidates, then converts the scores into probabilities that sum to one hundred percent—this step is called softmax. The first knob is installed before the conversion and is called temperature: lowering it widens the score gaps, like someone who always orders the same three things—water, coffee, milk tea; raising it flattens the gaps, like pointing randomly at the menu with eyes closed. Turn temperature to the max, and the model starts talking nonsense, followed by incomprehensible gibberish that won't stop.

The second knob is TOP P: before drawing, cut off the tail. Sort candidates by probability from high to low, add from the top until reaching eighty percent (the P value), then stop and draw only from what remains.

Cut off the tail first

Often after cutting, only one candidate remains—for example, after "harvest" only "season" is left, and there's no choice at all. The video mentions that about half the steps in twenty responses are like this. So although the model writes by drawing lots, it isn't chaotic: where it's unsure, it draws among several plausible words; where it's sure, there's actually no choice.

Three practical tips: for stability—writing code, organizing materials—lower the temperature; for more ideas, raise it a bit; if unsatisfied, click regenerate once to let it draw another round. TOP P generally doesn't need adjustment.

Softmax is the standard function for converting arbitrary real-valued scores into a probability distribution: first take the exponent of each score (ensuring positivity), then divide by the sum of all exponents (ensuring the total is 1). The temperature parameter actually scales the scores before softmax: scores are divided by temperature T. When T<1, score gaps are amplified and the distribution becomes sharper; when T>1, gaps are reduced and the distribution becomes flatter; as T→0, it degenerates to greedy.

TOP P (nucleus sampling) truncates the probability distribution after softmax: sort candidates by descending probability, accumulate probabilities until reaching threshold P, keep only those candidates and renormalize. Unlike TOP K (which keeps a fixed top K), TOP P's candidate count varies dynamically with the distribution shape—fewer candidates when the distribution is sharp, more when flat—making it more adaptive.

4. Inside the Model: Four-Station Computation

The outer loop is clear, but how is the innermost table computed? The video breaks it into four stations.

Station 1: Convert to arrows. Text is first split into tokens, and each token is looked up in a large dictionary to produce a long string of numbers, which can be drawn as an arrow in space—direction represents meaning, and similar meanings have similar directions.

Station 2: Push layer by layer. Each layer does two things. First, look at others—each arrow looks back at preceding arrows and moves useful information over; this is called attention. For example, "a cup of" looks back at the preceding "drink" to know that what follows should be something drinkable. Then process it yourself—digest the moved information; this is called the feed-forward network. The small model in the video has 28 layers to push through.

Each arrow looks back at preceding arrows

Station 3: Compare using the last arrow. After pushing to the end, it no longer points to the last word but to what should appear next. Compare its direction with the arrows of 150,000 candidates one by one; the more aligned, the higher the score—so water, coffee, and milk tea score high, while stone scores low.

Station 4: Convert scores to probabilities. This is softmax, and the result is the table from the beginning.

Which way to push and how much is determined by a large number of numbers in the model called parameters. The small model in the video has about 600 million parameters, initially all random, trained by having the model repeatedly guess the next word on massive text and adjusting slightly when it guesses wrong. This four-station machine is called a Transformer—the final letter T in GPT refers to it.

The attention mechanism is the core of the Transformer. Each token's vector generates three vectors: Query, Key, and Value. In attention computation, the current token's Query is dot-producted with the Keys of all preceding tokens to obtain relevance scores, which are normalized by softmax as weights, then used to compute a weighted sum of Values, yielding the token's new representation. This allows the model to dynamically decide "where to look."

The feed-forward network consists of two linear transformations plus a nonlinear activation, applied independently to each token for feature transformation and memory storage. Attention handles cross-token information aggregation, while the feed-forward network handles single-token deep processing; the two alternate and stack to form Transformer layers.

5. An Easily Overlooked Twist

The video ends with a twist: the phrase "reads every word you say" isn't quite right. It doesn't see a single character you type into it—what it sees are tokens that have been split. Using "Autumn is one whole piece, but withering is split into two" as an example, the video introduces the next episode's topic: tokenization.

Looking back at the whole path: you press Enter, the sentence is split into tokens, converted to arrows, pushed layer by layer, turned into a probability table, a token is drawn from the table and appended to the context, and the process repeats until an end token is drawn and it stops. Every character is drawn, but which lots are in the tube and how many of each are placed there are calculated one by one by this machine after reading every word you say.

#Transformer#AI inference#sampling decoding#temperature#TOP P#tokenization
Share:

Related articles