Load, Tokenize & Inspect the Model

Sixteen gigabytes of verified bytes are still just bytes. This lesson turns the artifact into a living object: the model, loaded onto the GPU in its shipped precision, and beside it the tokenizer, the small tool that converts between your text and the token IDs the model actually consumes. By the end you will have both in a Python session and know how to check that what loaded matches the artifact you downloaded earlier.

The tokenizer outputs on this page are real, produced this session against the actual Llama 3.1 tokenizer; the model-side outputs are representative of what the load prints on the GPU box, since a 16 GB load needs the box, not a laptop; their numbers derive from the artifact's own config and index.

Loading the model

Two objects, two lines, both from the transformers library the field defaults to for a first load:

The model line is the slow one: the seconds it takes are the four safetensors shards streaming from the cache you filled when downloading the model, into memory and onto the GPU. dtype="auto" says load at the shipped precision, the BF16 the model card and config declare, and it is already the default on the transformers 5.x line this course pins. Stating it records the choice; on the older 4.x line the default was float32, and this kwarg was what stood between you and an accidental 32 GB load. device_map="cuda" places the tensors on the GPU as they load, and model.eval() is the eval-mode habit from Python and PyTorch setup; from_pretrained already returns the model in eval mode, so the line records intent rather than changing state.

The tokenizer

The model consumes token IDs, positions in its 128,256-entry vocabulary; the tokenizer is the converter between them and your text. Run the course's canonical prompt through it:

Five words became five IDs, [791, 6864, 315, 9822, 374], plus a sixth the tokenizer added on its own: 128000, the begin-of-text marker, prepended to every encode so the model knows where a document starts. The decode round trip returns the text with that marker visible. And the vocabulary size is exactly the config's vocab_size from tensor shapes: tokenizer and model are two halves of one artifact, and this is the line where you see them agree.

Common misconception. Tokens are not words. The vocabulary's entries are frequent text fragments, so common words ride as single tokens while rarer ones split: this course's own subject tokenizes as ["in", "ference"], IDs [258, 2251]. Different model families split the same text differently, so when anything is budgeted per token, cost, context length, generation speed, count tokens with the model's own tokenizer, never words.

What the model does with these IDs, the embedding rows they select and the trace that follows, is covered in understanding one token. Here the tokenizer is a tool; the round trip is the test that it works.

Inspecting what loaded

Three lines check what loaded against what the course already knows:

The first line is the exact parameter count the safetensors index promised in the model download lesson, now counted from live tensors: 8,030,261,248. The second confirms precision and placement, the two facts the lesson on Python and PyTorch setup taught you to check, now on a real model. The third is the multiplication from memory and throughput arithmetic, landing in reality: two bytes per parameter is 16.06 GB on the device (the figure nvidia-smi shows will be somewhat higher, and the gap between the two readings is part of the nvidia-smi lesson two steps ahead).

Printing model itself is worth one scroll: the structure that comes back is the map from the transformer architecture refresher verbatim, an embedding, 32 decoder layers each holding the q/k/v/o projections and the gated MLP trio, the final norm, and the output matrix. The architecture refresher was a diagram; this is the same thing as an object.

The model is loaded and the tokenizer round-trips; the counts agree with the paperwork from the model download lesson. What remains is to ask the model for a token, in the next lesson.