LLM FundamentalsQwen2.5-0.5B-Instruct · transformers 5.17 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
18 small wins to finish your pathNext lesson

Loading a model with transformers

A language model is two things you download: a tokenizer that turns text into numbers, and weights, the numbers it learned. transformers loads both by name.

Example
from transformers import AutoModelForCausalLM, AutoTokenizer

name = "Qwen/Qwen2.5-0.5B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForCausalLM.from_pretrained(name)

print(type(tokenizer).__name__)
print(type(model).__name__)

from_pretrained downloads the files from Hugging Face the first time and reads them from a cache on your disk after that, so only the first run waits for the download.

Keep this session open
Run the lessons in one Python session or notebook. Every example after this one uses tokenizer and model from here, and each lesson names anything else it reuses from an earlier one.

Auto in the class names means transformers reads the model's configuration and picks the right class for you: here a tokenizer and a model made for Qwen2.

How big is it

Example
parameters = sum(p.numel() for p in model.parameters())
print(f"{parameters:,} parameters")
print(model.config.num_hidden_layers, "layers")
print(model.config.vocab_size, "tokens in the vocabulary")

Parameters are the learned numbers. Half a billion sounds large, and it is small: hosted models are thought to have hundreds of billions. More parameters usually means better answers and more memory, time and money per answer.

numel counts the numbers in one block of weights, and the sum adds up every block. The :, in the f-string puts commas in a long number.

Try it yourself
  • Print model.config and find the max_position_embeddings setting. Lesson 14 explains it.
  • Print next(model.parameters()).dtype. How many bits does each number use?
  • Load "Qwen/Qwen2.5-1.5B-Instruct" instead, if you have the disk space, and compare the parameter count.

Little by little, you're building something great.