4  From text to tokens to next-token probabilities

You will learn:

A language model never sees letters or digits. It sees a sequence of integers. The tokenizer splits text into pieces from a fixed vocabulary and replaces each piece by its position in that vocabulary. This chapter works with the model we fine-tune later, Qwen/Qwen3-4B-Instruct-2507.

4.1 From text to tokens

Code
from transformers import AutoTokenizer

from convfinqa import data
from convfinqa.book.viz import show_tokens, tokenize

tok = AutoTokenizer.from_pretrained("Qwen/Qwen3-4B-Instruct-2507")
print(f"vocabulary size: {len(tok):,}")
vocabulary size: 151,669

The helper show_tokens colours each token. Hover over a token to see its id. Examples come from the train split.

4.1.1 A question

Code
conv = {c["id"]: c for c in data.load_split("train")}[
    "Single_AAPL/2002/page_23.pdf-1"
]
question = conv["annotation"]["dialogue_break"][0]
show_tokens(tok, question)
what was the total of net sales in 2001?
14 tokens, 40 characters

Common words are single tokens, and the space in front of a word belongs to the word: ␣was, ␣the. The year 2001 is not one token. It is four.

4.1.2 A table row

Code
row = data.table_to_markdown(conv["table"]).splitlines()[2]
show_tokens(tok, row)
| net sales | $ 5742 | $ 5363 | $ 7983 |
25 tokens, 40 characters

The symbols and separators |, $ and the spaces around them cost tokens too. A table of a few dozen cells is already hundreds of tokens, which is why a whole report page makes a long prompt.

4.1.3 Numbers

Code
for text in ["206588", "0.14136", "1,234.5", "28%", "$ 5742"]:
    display(show_tokens(tok, text))
206588
6 tokens, 6 characters
0.14136
7 tokens, 7 characters
1,234.5
7 tokens, 7 characters
28%
3 tokens, 3 characters
$ 5742
6 tokens, 6 characters

Every digit is its own token. The same digits give the same tokens wherever they appear:

Code
display(show_tokens(
    tok,
    "net cash from operating activities was 181001 in 2008, so 181001 is the base"
))
net cash from operating activities was 181001 in 2008, so 181001 is the base
31 tokens, 76 characters

The model never sees 181001 as one unit, only the digit sequence 1 8 1 0 0 1. This follows from the tokenizer’s design (see below), and it is one reason why we do not ask the model to do arithmetic itself. It writes a program, and the executor from Chapter 3 computes the number.

4.1.4 A gold program

Code
show_tokens(tok, conv["annotation"]["turn_program"][-1])
subtract(5363, 7983), divide(#0, 7983)
23 tokens, 38 characters

The DSL was not designed for this tokenizer. Some pieces merge in surprising ways, such as ), and (#, and an operator takes its leading space with it (␣divide). Fine-tuning teaches the model to produce exactly this token sequence.

4.1.5 Another tokenizer, for contrast

GPT-2’s tokenizer has no rule that isolates digits. Its vocabulary merges digits that were frequent together in its training text, so the same number can be cut differently in different places.

Code
gpt2 = AutoTokenizer.from_pretrained("gpt2")
for text in ["206588", " 181001", "0.14136"]:
    print(f"GPT-2: {text!r}")
    display(show_tokens(gpt2, text))
    print(f"Qwen3: {text!r}")
    display(show_tokens(tok, text))
GPT-2: '206588'
206588
3 tokens, 6 characters
Qwen3: '206588'
206588
6 tokens, 6 characters
GPT-2: ' 181001'
181001
2 tokens, 7 characters
Qwen3: ' 181001'
181001
7 tokens, 7 characters
GPT-2: '0.14136'
0.14136
4 tokens, 7 characters
Qwen3: '0.14136'
0.14136
7 tokens, 7 characters

For arithmetic this is awkward either way, but Qwen’s digit-by-digit split is at least consistent: a digit is always one token, whatever its neighbours are.

NoteUnder the hood

Qwen’s tokenizer is a byte-level BPE tokenizer. Its vocabulary was built by starting from single bytes and repeatedly merging the most frequent adjacent pairs in a large text corpus. Before any merging applies, a pre-tokenizer first splits the text with a regular expression. Part of Qwen’s expression is \p{N}, which matches a single digit, so a digit can never merge with its neighbours.

Code
import json

pre = json.loads(tok.backend_tokenizer.to_str())\
    ["pre_tokenizer"]["pretokenizers"][0]["pattern"]["Regex"]
print(pre)
(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+

4.2 Tracing one prediction

Now we follow a single prediction through the model. The model’s only job is to read a sequence of tokens and say how likely each possible next token is. We take the first turn of the conversation from above, formatted as a chat, and stop right where the assistant’s answer begins.

Code
import torch
from transformers import AutoModelForCausalLM

from convfinqa.formatting import conversation_messages

# the system prompt and the first user turn
messages = conversation_messages(conv)[:2]

prompt = tok.apply_chat_template(
    messages, tokenize=False, add_generation_prompt=True
)

print("...", repr(prompt[-70:]))
gold = conv["annotation"]["turn_program"][0]
print("gold answer:", gold)
... 't was the total of net sales in 2001?<|im_end|>\n<|im_start|>assistant\n'
gold answer: 5363

The prompt ends with <|im_start|>assistant and a newline: the model is about to write its answer. The gold answer is 5363, so the right first token is 5. We load the model in bf16 on the GPU (the first run downloads about 8 GB).

Code
name = "Qwen/Qwen3-4B-Instruct-2507"
model = AutoModelForCausalLM.from_pretrained(name, dtype=torch.bfloat16)\
    .to("cuda").eval()
cfg = model.config
print(f"{cfg.num_hidden_layers} layers, hidden size {cfg.hidden_size}, vocabulary rows {cfg.vocab_size:,}")
36 layers, hidden size 2560, vocabulary rows 151,936

4.2.1 Token ids

Code
ids = tok(prompt, add_special_tokens=False, return_tensors="pt")\
    ["input_ids"].to("cuda")
print("shape:", tuple(ids.shape), "(batch, tokens)")
print(
    "last five ids:",
    ids[0, -5:].tolist(),
    [tok.decode([i]) for i in ids[0, -5:]]
)
shape: (1, 1125) (batch, tokens)
last five ids: [151645, 198, 151644, 77091, 198] ['<|im_end|>', '\n', '<|im_start|>', 'assistant', '\n']

4.2.2 Embeddings

Each id selects one row of a big table, the embedding matrix, which has one row per vocabulary entry. The row is a vector of 2,560 numbers that the model learned for that token.

Code
with torch.no_grad():
    emb = model.model.embed_tokens(ids)
print("embedding matrix:", tuple(model.model.embed_tokens.weight.shape))
print("after lookup:", tuple(emb.shape), emb.dtype, "(batch, tokens, hidden)")
embedding matrix: (151936, 2560)
after lookup: (1, 1125, 2560) torch.bfloat16 (batch, tokens, hidden)

4.2.3 The transformer

The vectors pass through 36 transformer layers. Each layer adds an update to every position’s vector, so the vector is a running sum that every layer nudges, and its shape never changes. What happens inside a layer, attention across positions and a small network at each position, is the subject of the next chapter. For now the stack is a black box that maps vectors to vectors. We feed the embeddings from the previous step straight in. output_hidden_states keeps the vectors at every stage: entry 0 is the embeddings we put in, entry k is what layer k produced, and the last entry is the transformer’s final output, which is what the next step uses.

Code
with torch.no_grad():
    out = model.model(inputs_embeds=emb, output_hidden_states=True)
hidden = out.last_hidden_state
print("hidden states kept:", len(out.hidden_states), "= embeddings + 36 layers")
print("final hidden state:", tuple(hidden.shape))

# the last entry is the final output, and feeding ids instead
# gives the same result: the model simply does the embedding lookup itself
assert torch.equal(out.hidden_states[-1], hidden)
with torch.no_grad():
    assert torch.equal(hidden, model.model(input_ids=ids).last_hidden_state)
hidden states kept: 37 = embeddings + 36 layers
final hidden state: (1, 1125, 2560)

The meaning of a position’s vector changes on the way through. It starts as the embedding of token i. By the end it encodes a prediction for the token at position i + 1.

4.2.4 Logits

The last step turns each position’s vector into one score per vocabulary entry. These scores are the logits. A matrix with 151,936 rows, one per vocabulary entry, the language-model head, does the multiplication.

Code
with torch.no_grad():
    logits = model.lm_head(hidden)
print("logits:", tuple(logits.shape), "(batch, tokens, vocabulary rows)")
logits: (1, 1125, 151936) (batch, tokens, vocabulary rows)

In this model the head’s weights are the embedding matrix itself (more on that at the end). So the score of a token is the dot product of the final vector with that token’s embedding: the final vector scores highest on the tokens it points toward.

Code
E = model.model.embed_tokens.weight                 # one embedding per vocabulary row
manual = hidden[0, -1].float() @ E.float().T        # dot product with every token's embedding
head = logits[0, -1].float()
diff = (manual - head).abs().max().item()
print(f"logits range from {head.min().item():.1f} to {head.max().item():.1f}")
print(f"largest difference between the two: {diff:.3f}")
assert diff < 0.07 and manual.argmax() == head.argmax()
logits range from -20.0 to 26.6
largest difference between the two: 0.062

The two agree up to rounding. The head stores its logits in bf16, whose spacing near 20 is 0.125, so a difference of up to half that, about 0.06, is just the rounding, and the top token is the same.

Every position gets its own row of logits: position i scores the token that should come at i + 1. For writing an answer only the last position matters, because the prompt is already given. (Training uses all of them; see the chapter on how fine-tuning works.) We check that our step-by-step path reproduces the model’s own forward pass exactly:

Code
with torch.no_grad():
    assert torch.equal(logits, model(input_ids=ids).logits)
last = logits[0, -1].float()
print("scores for the next token:", tuple(last.shape))
scores for the next token: (151936,)

4.2.5 Softmax and the prediction

Logits are arbitrary real numbers. Softmax turns them into probabilities: it exponentiates each score and divides by the total, so every value is positive and they sum to 1.

Code
probs = torch.softmax(last, dim=-1)
best = int(probs.argmax())
gold_id = tok(gold, add_special_tokens=False)["input_ids"][0]
rank = int((probs > probs[gold_id]).sum()) + 1

print(f"probabilities sum to {probs.sum().item():.4f}")
print(f"most likely next token: {tok.decode([best])!r} with probability {probs[best]:.3f}")
print(f"the gold first token {tok.decode([gold_id])!r}: probability {probs[gold_id]:.3f}, rank {rank}")
probabilities sum to 1.0000
most likely next token: '```' with probability 0.614
the gold first token '5': probability 0.024, rank 5

The untuned model does not know our DSL. It starts a Markdown code block and gives the correct first digit a few percent. Nothing in the pipeline was wrong; the model simply has not seen this task. Teaching it is what fine-tuning does, and we will measure this exact quantity on whole conversations in Chapter 7.

4.2.6 The whole path

Code
import pandas as pd

pd.DataFrame(
    [
        (
            "token ids",
            tuple(ids.shape),
            "integers from the tokenizer"
        ),
        (
            "embeddings",
            tuple(emb.shape),
            "one learned vector per token"
        ),
        (
            "after 36 layers",
            tuple(hidden.shape),
            "vectors that have read the whole prompt"
        ),
        (
            "logits",
            tuple(logits.shape),
            "one score per vocabulary row, at every position"
        ),
        (
            "last position",
            tuple(last.shape),
            "scores for the next token"
        ),
        (
            "softmax",
            tuple(probs.shape),
            "probabilities, summing to 1"
        ),
    ],
    columns=["stage", "shape", "meaning"],
)
stage shape meaning
0 token ids (1, 1125) integers from the tokenizer
1 embeddings (1, 1125, 2560) one learned vector per token
2 after 36 layers (1, 1125, 2560) vectors that have read the whole prompt
3 logits (1, 1125, 151936) one score per vocabulary row, at every position
4 last position (151936,) scores for the next token
5 softmax (151936,) probabilities, summing to 1
NoteUnder the hood

The vocabulary has 151,669 tokens but the matrices have 151,936 rows. The extra rows are padding that makes the sizes friendlier to the GPU. They correspond to no token:

Code
n_tokens = len(tok)
print(f"probability on the {cfg.vocab_size - n_tokens} padding rows: {probs[n_tokens:].sum().item():.2e}")
probability on the 267 padding rows: 3.88e-12

The embedding matrix and the language-model head are also the same tensor in this model (the config says tie_word_embeddings), so the table that turns a token into a vector is reused to turn a vector back into token scores:

Code
print(
    "tied:",
    cfg.tie_word_embeddings,
    "| same memory:",
    model.lm_head.weight.data_ptr() == model.model.embed_tokens.weight.data_ptr()
)
tied: True | same memory: True

4.3 The distribution over next tokens

The softmax output is a whole probability distribution over the vocabulary, not just a winner. Here are the ten most likely next tokens for the prompt above, and how they change with the temperature T: before the softmax, the logits are divided by T.

Code
from convfinqa.book.viz import next_token_stats, plot_next_token

plot_next_token(tok, last, temperatures=(0.3, 1.0, 2.0), k=10);

The ten most likely next tokens at three temperatures.
  • At T = 1 the logits are used as they are. This is the model’s own distribution.
  • A low T sharpens it: the leader takes almost everything. As T approaches 0, the result is just the argmax.
  • A high T flattens it: the tail of unlikely tokens gains probability.

The entropy in the panel titles summarises the spread: it is 0 when one token has all the probability, and grows as the distribution flattens. The same numbers as a table:

Code
pd.DataFrame(
    {f"T = {t}": next_token_stats(last, t, 10) for t in (0.3, 1.0, 2.0)}
).T.round(3)
top1 top10_mass entropy
T = 0.3 0.985 1.000 0.088
T = 1.0 0.614 0.998 1.251
T = 2.0 0.279 0.805 3.421
Code
stats = {t: next_token_stats(last, t, 10) for t in (0.3, 1.0, 2.0)}
gold_id = tok(gold, add_special_tokens=False).input_ids[0]
p_gold = {t: torch.softmax(last.float() / t, dim=-1)[gold_id].item() for t in (1.0, 2.0)}

# the paragraph below
assert stats[0.3]["top1"] > 0.9
assert stats[2.0]["top10_mass"] < 0.9
assert p_gold[2.0] > 2 * p_gold[1.0]

At T = 0.3 the top token holds 99% of the probability. At T = 2 the ten tokens shown hold only 81% of it, and the gold token 5 has more than doubled its probability, from 0.024 to 0.055. The model is not any better informed; the distribution is just less sure of itself.

4.4 Choosing a token: greedy and sampling

To write more than one token, the model repeats the trace: it picks a token, appends it to the input, and predicts again. How it picks decides everything about the output.

  • Greedy decoding always takes the most likely token. Nothing is random, so the same input gives the same output.
  • Sampling draws the next token at random from the (temperature-adjusted) distribution, so the output varies from run to run. A random-number seed makes a sampled run repeatable.

Before we try both, one pitfall. The model ships with default generation settings:

Code
gc = model.generation_config
print({k: getattr(gc, k) for k in ("do_sample", "temperature", "top_p", "top_k")})
{'do_sample': True, 'temperature': 0.7, 'top_p': 0.8, 'top_k': 20}

These are sampling settings, so model.generate() with no arguments samples. Whenever we want greedy decoding we pass do_sample=False explicitly, and for sampling we pass every setting explicitly too.

Code
def continuation(max_new_tokens=14, **kwargs):
    with torch.no_grad():
        out = model.generate(ids, max_new_tokens=max_new_tokens, **kwargs)
    return tok.decode(out[0, ids.shape[1]:])

greedy = dict(do_sample=False, temperature=None, top_p=None, top_k=None)
sampled = dict(do_sample=True, temperature=1.0, top_p=1.0, top_k=0)

rows = [
    ("greedy", "-", continuation(**greedy)),
    ("greedy again", "-", continuation(**greedy))
]
for seed in (0, 1, 2, 0):
    torch.manual_seed(seed)
    rows.append(("sampled", seed, continuation(**sampled)))
assert rows[0][2] == rows[1][2]   # greedy is repeatable
assert rows[2][2] == rows[5][2]   # so is sampling with the same seed

# pandas cuts longer cells at 50 characters
pd.set_option("display.max_colwidth", 80)

pd.DataFrame(rows, columns=["decoding", "seed", "first 14 tokens"])
decoding seed first 14 tokens
0 greedy - ```convfinqa\nSELECT 5363\n```<|im_end|>
1 greedy again - ```convfinqa\nSELECT 5363\n```<|im_end|>
2 sampled 0 program {\n  // The question asks for the total net sales in
3 sampled 1 program {\n  // Given data from the table\n  // net sales
4 sampled 2 ```convfinqa\nSELECT 5363\n```<|im_end|>
5 sampled 0 program {\n  // The question asks for the total net sales in

Greedy gives the same text twice. Sampling gives different text for different seeds, and the same text again for the same seed. The untuned model’s greedy answer is not our DSL, but it does contain the right number. It is also the reason this matters later: greedy evaluation is deterministic, so the evaluation chapter has no generation seed to report. Only the training seed varies between runs.

Code
# the same prompt alone and padded inside a batch with a longer one
tok.padding_side = "left"
batch = tok(
    [prompt, prompt + "\nAnswer:"], add_special_tokens=False,
    padding=True, return_tensors="pt"
).to("cuda")
positions = (batch.attention_mask.cumsum(-1) - 1).clamp(min=0)
with torch.no_grad():
    alone = model(ids).logits[0, -1].float()
    together = model(**batch, position_ids=positions).logits[0, -1].float()
batch_diff = (alone - together).abs().max().item()
same_top = alone.argmax() == together.argmax()
print(f"largest change in the logits: {batch_diff:.2f} "
      f"(logits range from {alone.min().item():.0f} to {alone.max().item():.0f}); "
      f"same top token: {bool(same_top)}")
assert batch_diff > 0 and same_top
largest change in the logits: 0.45 (logits range from -20 to 27); same top token: True
NoteDeterministic, but only for the same setup

Greedy decoding is deterministic given the same input, model weights, software and hardware. Running the same prompt inside a different batch can change the logits in bf16, as the check above shows: by up to 0.45 on logits ranging from -20 to 27, with the same top token. That can flip a near-tie. We evaluate with a fixed batch size and a fixed order of examples for this reason.

4.5 Editing the logits

A logits processor is a function that edits the logits after the model produces them and before the softmax. The simplest edit is a mask: adding \(-\infty\) to a token’s logit makes its probability exactly 0, and the probabilities of the remaining tokens renormalise to sum to 1.

Say we know the answer to this question is a number, and we allow only the ten digit tokens:

Code
from convfinqa.constrained import AllowedTokens

digit_ids = [
    tok(str(d), add_special_tokens=False)["input_ids"] for d in range(10)
]
assert all(len(x) == 1 for x in digit_ids)
digit_ids = [x[0] for x in digit_ids]

processor = AllowedTokens(digit_ids)
masked = processor(ids, last[None])[0]
p_masked = torch.softmax(masked, dim=-1)
top = p_masked.topk(3)

print(
    f"P('5') before the mask: {probs[gold_id]:.3f}, after: {p_masked[gold_id]:.3f}"
)
print(
    "top three now:",
    [
        (tok.decode([int(i)]), round(v, 3))
        for v, i in zip(top.values.tolist(), top.indices)
    ]
)
P('5') before the mask: 0.024, after: 0.831
top three now: [('5', 0.831), ('2', 0.164), ('3', 0.002)]

The code fence and program are gone, and the model’s own ranking of the digits decides the rest. Using the processor inside generate:

Code
from transformers import LogitsProcessorList

print(repr(continuation(
    max_new_tokens=4,
    logits_processor=LogitsProcessorList([processor]),
    **greedy
)))
'5363'

The model writes 5363, the gold answer. The mask taught the model nothing: it only removed the options it would have preferred, and the digits it already ranked highest did the rest. That works here because the right answer was already the model’s favourite digit sequence, and it is not guaranteed in general. This mask is also far too crude. It can never end the answer, because the end-of-turn token is not allowed, and it cannot write an operator or a parenthesis. A useful constraint changes the allowed set at every step depending on what has been written so far. That is a grammar, the subject of the constrained-decoding chapter.