← All writingLLMs, starting with one token · 06

A real-model lab: tokens, hidden states, attention and embeddings

Load MiniLM in the browser and GPT-2 in a notebook; inspect actual tensors to distinguish encoders, decoders and pooling.

阅读中文版 →

A real-model lab: tokens, hidden states, attention and embeddings

Decide what to observe

“Load a model” hides three stages: tokenization, the forward pass and task-specific postprocessing. The browser uses Xenova/all-MiniLM-L6-v2, a lightweight encoder for semantic similarity. It is not an autoregressive chat model.

The button downloads real ONNX weights and runs CPU/WASM inference locally. A failure produces an error, never a canned similarity score. The first run needs runtime and weight downloads. An English-oriented model does not gain equivalent Chinese retrieval quality because the interface is bilingual.

From token states to a sentence vector

The model produces token representations, followed by masked mean pooling and L2 normalization. For valid-token indicators m, , then divide by . The dot product of normalized vectors is cosine similarity, in [-1,1], not a calibrated confidence probability.

Inspect real token IDs, padding masks, output shape [2,384] and the first 32 dimensions. An individual embedding coordinate rarely has a stable nameable meaning. The bars visualize values, not attention.

Go deeper in the notebook

The companion loads actual distilgpt2 weights and inspects [B,T,V] logits, layer-wise hidden states and [B,H,T,T] attention. It also compares cached and full forward passes. Eager attention is selected to expose weights; optimized attention backends may not return the same observables.

from transformers import AutoTokenizer, AutoModelForCausalLM
tok = AutoTokenizer.from_pretrained('distilgpt2')
model = AutoModelForCausalLM.from_pretrained(
    'distilgpt2', attn_implementation='eager').eval()
inputs = tok('The cat is', return_tensors='pt')
# The complete notebook adds no_grad, plots and cache equivalence checks.

Three useful counterexamples

Compare a sentence with a paraphrase, then add a negation, then try domain terminology or Chinese. High lexical overlap can still produce high similarity despite opposite logical meaning. Make an input longer than 128 tokens and inspect whether truncation removes critical information. Save inputs, effective length and model version with results.

Question: Does cosine similarity 0.9 mean a 90% probability of mutual entailment?

AnswerNo. It measures geometric proximity in the representation space. Entailment needs an appropriate task and evaluation set; an uncalibrated similarity score does not supply that probability.

Actual output from this run

Actual DistilGPT2 attention, layer 0 head 0. The zero upper triangle reflects causal masking.

Actual DistilGPT2 attention, layer 0 head 0. The zero upper triangle reflects causal masking.

Primary sources and further reading

Sources checked on 2026-09-10. Teaching examples are not production benchmarks; confirm APIs and model support against the linked version.

PRETRAINED MODEL · MiniLM-L6 · WASM / CPU

Load a real model. Inspect tokens and embeddings.

On click, download quantized weights from Hugging Face (~23 MB plus runtime). Text is processed locally, not sent to an inference service. Requires Hugging Face and jsDelivr access. This is primarily an English embedding model, not a chat LLM; inputs are capped at 128 tokens.

Companion notebook

Inspect tensors, run experiments, and check the stated environment and execution status in the first cell.

Open in Colab ↗Download notebook