AI Hints
Home›Fine-Tuning LLMs Tutorial›Models, Logits and Generation
Fine-Tuning LLMs · Lesson 13 of 120

Loading a Model

AutoModelForCausalLM.

⏱ 2 min read▶ Video at 20:08🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

We've used pipeline, which hides everything. Now let's load a model ourselves, and look inside. It's less scary than it sounds.

  • No more hiding
  • Load a model ourselves
  • Look inside

The Auto classes

Transformers has Auto classes, which pick the right code for any model. Auto tokenizer loads the tokenizer. Auto model for causal LM loads a model that predicts the next token. Causal means it only looks backwards, at the tokens before. That's exactly what chat models do.

keyvalue
AutoTokenizerthe matching tokenizer
AutoModelForCausalLMa next-token model
Causalonly looks at earlier tokens
from_pretrainedload from the Hub

Count the parameters

We load the model, with from pretrained. Each parameter tensor has a num el method, which counts its numbers. We add them all up. Each number takes four bytes, in normal precision. Then we print the first layer. Run it. One hundred and thirty five million parameters, about half a gigabyte. And inside each layer, we see attention, with q, k, v, and o projections, and an MLP. Remember these names. LoRA will attach to them later.

app.pypython
from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-135M")
total = sum(p.numel() for p in model.parameters())
print(f"Parameters: {total:,} ({total * 4 / 1e6:.0f} MB in float32)")
print(model.model.layers[0])
Output
Parameters: 134,515,008 (538 MB in float32)
LlamaDecoderLayer(
  (self_attn): LlamaAttention(
    (q_proj): Linear(in_features=576, out_features=576, bias=False)
    (k_proj): Linear(in_features=576, out_features=192, bias=False)
    (v_proj): Linear(in_features=576, out_features=192, bias=False)
    (o_proj): Linear(in_features=576, out_features=576, bias=False)
  )
  (mlp): LlamaMLP(
    (gate_proj): Linear(in_features=576, out_features=1536, bias=False)
    (up_proj): Linear(in_features=576, out_features=1536, bias=False)
    (down_proj): Linear(in_features=1536, out_features=576, bias=False)
    (act_fn): SiLUActivation()
  )
  (input_layernorm): LlamaRMSNorm((576,), eps=1e-05)
  (post_attention_layernorm): LlamaRMSNorm((576,), eps=1e-05)
)

What is a Linear layer?

You saw the word linear many times. A linear layer is a grid of weights, called a matrix. It takes a list of numbers in, multiplies by the grid, and gives a list of numbers out. For example, q proj takes five hundred and seventy six numbers in, and gives five hundred and seventy six out. Most of a model's weights live in these grids.

keyvalue
Linear layera grid of weights (a matrix)
Ina list of numbers
Outanother list of numbers
q_proj576 in → 576 out

Summary

Let's recap. Auto model for causal LM loads a next token model. Our model has one hundred and thirty five million parameters. And most of them live in linear layers, like q proj and v proj. Next, what the model actually outputs.

Key takeaways
  • AutoModelForCausalLM
  • 135M parameters
  • Linear layers = weight grids
Next up: Logits and Probabilities — what the model outputs