Sub topic 1
Choosing an open-weight base model
Every LoRA project starts with one decision: which pretrained model to adapt. The choice affects memory use, legal permissions, output quality, and how much of the rest of this tutorial's tooling works without changes. This chapter gives you a way to make that decision deliberately.
Prerequisites
This chapter assumes you have:
- Basic Python and PyTorch usage.
- A high-level understanding of the transformer architecture (attention, feed-forward layers, tokens).
- Experience running a Hugging Face
transformersmodel for inference, includingAutoModelForCausalLMandAutoTokenizer.
Later chapters add basic linear algebra (matrix multiplication and rank). You don't need it here.
What "open-weight" means
An open-weight model is a model whose trained parameters (the weights) you can download and run on your own hardware. Fine-tuning requires this, because you must load the weights and compute gradients through them.
Open-weight is not the same as open source. Open source software gives you the code and the right to modify and redistribute it under an approved license. An open-weight release usually gives you only the weights, often with a custom license. The training data and training code may not be published.
| Term | What you get | Typical example |
|---|---|---|
| Open source model | Weights, training code, data details, and an OSI-style license | Fully permissive research releases |
| Open-weight model | Downloadable weights, with a license that may restrict use | Many popular chat and instruct models |
| API-only model | No weights, only a hosted endpoint | Hosted proprietary models |
Some API providers offer fine-tuning as a service. That is a different workflow, and the weights stay with the provider. This tutorial covers only models whose weights you hold.
Licenses decide what you can ship
Read the license before you invest GPU hours. Training an adapter does not remove the base model's license terms. Your adapter and any merged model are generally derivative works, so the base license usually applies to them.
Common license families you will meet:
- Permissive licenses (Apache 2.0, MIT). Commercial use, modification, and redistribution are allowed, with attribution and notice requirements.
- Custom community licenses. These often allow commercial use but add conditions. Examples of conditions are a cap on monthly active users, a required "built with" notice, a ban on using outputs to train competing models, or an acceptable-use policy.
- Non-commercial or research-only licenses. You can experiment, but you cannot ship a product.
Check these points for every candidate:
- Is commercial use allowed for your situation and scale?
- Must you display a name or notice in your product?
- Are there restrictions on how the outputs may be used?
- Does the license pass to a merged model and to a published adapter?
- Is the model gated, so that you must accept terms on its Hugging Face page and authenticate before download?
You can read a model's declared license from the Hub without downloading the weights:
from huggingface_hub import model_info
info = model_info("Qwen/Qwen2.5-0.5B-Instruct")
print("license:", info.card_data.license if info.card_data else "unknown")
print("gated:", info.gated)
print("tags:", [t for t in info.tags if t.startswith("license:")])The license field is a short identifier. Open the model card for the full text, because identifiers such as other hide custom terms.
Size: the constraint that arrives first
Parameter count drives memory. A rough rule for the weights alone is:
memory in bytes ≈ parameters × bytes per parameter
At 16-bit precision (bf16 or fp16), each parameter takes 2 bytes. A 0.5B model needs about 1 GB for weights, a 7B model about 14 GB, and a 70B model about 140 GB. Training needs more than the weights, because activations, gradients for the trainable parameters, and optimizer state also use memory. The next chapter covers that cost in detail. For now, treat the weight size as a lower bound.
| Size class | Weights at 16-bit | Typical use while learning or prototyping |
|---|---|---|
| 0.5B to 1.5B | 1 to 3 GB | Free-tier GPUs, fast experiments, pipeline debugging |
| 3B to 8B | 6 to 16 GB | A single 24 GB GPU with LoRA, or smaller GPUs with QLoRA |
| 13B to 34B | 26 to 68 GB | One large GPU with QLoRA, or several GPUs |
| 70B and up | 140 GB and up | Multi-GPU setups, or QLoRA on one 80 GB GPU |
A practical workflow is to build your whole pipeline on a small model first. This tutorial's code examples use Qwen/Qwen2.5-0.5B-Instruct for that reason: it loads quickly and exposes the same architecture patterns as larger siblings. Once data formatting, training, and evaluation work, you swap in a bigger model from the same family and change very little.
Base versus instruct variants
Most families publish at least two variants of each size.
- Base (pretrained) model. Trained only to predict the next token on a large text corpus. It continues text but does not reliably follow instructions or hold a conversation.
- Instruct or chat model. A base model that has been further trained on instruction data and often preference data. It follows prompts, and it expects a specific chat template.
The choice depends on how much data you have and what you want.
| Situation | Better starting point | Reason |
|---|---|---|
| Small dataset (hundreds to a few thousand examples), narrow task | Instruct | Instruction following is already present, so your data only has to teach the task or style |
| Large dataset (tens of thousands of examples or more), a new behavior | Base or instruct | Enough data to teach the format from scratch |
| You need a custom chat format or a different system-prompt scheme | Base | No built-in template to fight against |
| You need to keep the model's safety behavior and general chat ability | Instruct | Fine-tuning from base discards that alignment work |
The chat template matters more than it first appears. An instruct model was trained with particular special tokens that mark the roles of system, user, and assistant. If your training data uses a different layout, the model has to unlearn its template and learn yours. Chapter 7 covers this. For now, remember that choosing an instruct model commits you to using its template.
Architecture and tooling compatibility
LoRA works on the linear layers of almost any transformer. Your practical constraint is whether the libraries support the model, not whether LoRA works in theory. Check these points:
- Supported in
transformers. The model should load withAutoModelForCausalLMwithouttrust_remote_code=True. Remote code means executing Python from the model repository, which is both a security risk and a compatibility risk. - Known LoRA target modules. PEFT needs layer names to attach adapters to. Most Llama-style models name them
q_proj,k_proj,v_proj,o_proj,gate_proj,up_proj, anddown_proj. Models with fused attention layers use other names. - Chat template present. Instruct models ship a template in their tokenizer configuration.
- Quantization support. For QLoRA, the architecture must work with
bitsandbytes4-bit loading. - Serving support. If you plan to serve with a specific inference engine, confirm that it supports the architecture and, if you want to serve adapters separately, supports LoRA.
You can inspect these properties in a few lines before committing:
from transformers import AutoConfig, AutoTokenizer
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
config = AutoConfig.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
print("architecture:", config.architectures)
print("layers:", config.num_hidden_layers)
print("hidden size:", config.hidden_size)
print("max context:", config.max_position_embeddings)
print("vocab size:", config.vocab_size)
print("has chat template:", tokenizer.chat_template is not None)The output tells you the architecture class, the depth, the hidden size, and whether a chat template exists. The architecture string is what you check against your tooling's support list.
Other selection criteria
Beyond size, license, and tooling, compare candidates on the following.
- Context length. The maximum number of tokens per sequence. Longer contexts cost more memory during training, so your training sequences are often shorter than the advertised maximum.
- Language coverage. A model trained mostly on English text may need more data to work well in another language. Check the model card for the languages it covers.
- Domain strength. Some families are stronger at code or mathematics. Starting from a model that is already good at your domain reduces the amount of adaptation you need.
- Tokenizer fit. A tokenizer that splits your domain text into many tokens (for example, specialized medical or legal vocabulary, or a non-Latin script) raises both training cost and sequence length.
- Benchmark scores, with caution. Public leaderboards help you shortlist, but they measure general tasks. They may not predict performance on your task, and some benchmark data leaks into training sets. Use them to eliminate weak options, not to pick a winner.
A decision process
The steps below turn the criteria into a procedure.
- Write down your constraints: GPU memory, budget, license needs, target language, and serving environment.
- Filter by license and by tooling support. Remove anything that fails.
- Filter by size using the memory table, keeping in mind that QLoRA lets you go one size class higher.
- Choose instruct or base using the table above.
- Shortlist two or three candidates and test each on 20 to 50 representative prompts, without any fine-tuning.
- Fine-tune the smallest viable candidate first and compare it with the others using the evaluation method from a later chapter.
Common mistakes
- Choosing by leaderboard rank alone. The top model on a general benchmark can lose to a smaller model on your narrow task after fine-tuning.
- Ignoring the license until deployment. Discovering a non-commercial restriction after training wastes the work.
- Starting with the biggest model that fits. Large models slow every iteration. Bugs in data formatting show up just as well on a small model.
- Mixing base and instruct artifacts. An adapter trained on one variant should not be loaded onto the other. Adapters are tied to the exact base weights they were trained against.
- Changing the base model version mid-project. A minor version bump changes the weights, so earlier adapters and evaluation results no longer apply.
- Using
trust_remote_code=Truewithout reading the code. It executes arbitrary code from the repository on your machine.
You can pin a revision like this:
from huggingface_hub import model_info
info = model_info("Qwen/Qwen2.5-0.5B-Instruct")
print("commit to pin:", info.sha)Pass that value as revision= when calling from_pretrained for the model and the tokenizer.
Exercise
You have a single 24 GB GPU, a dataset of 2,000 customer-support conversations, and a product that will be sold commercially. You are choosing between a 7B instruct model under Apache 2.0, a 7B instruct model under a non-commercial license, and a 70B base model under a custom community license. Which do you pick first, and why?
Show answer
Start with the 7B instruct model under Apache 2.0. The non-commercial model fails the license filter. The 70B model needs about 140 GB for 16-bit weights and is too large for a 24 GB GPU even with QLoRA. A 7B model needs roughly 14 GB at 16-bit, so it fits for LoRA on a 24 GB GPU. The instruct variant suits a small conversational dataset, because instruction following is already in place.
Exercise
Write the three checks you would run on a candidate model, using AutoConfig and AutoTokenizer, before deciding whether it works with the rest of this tutorial's pipeline. Name what each check tells you.
Show answer
First, print config.architectures to confirm the architecture is one your libraries support. Second, check tokenizer.chat_template is not None to confirm an instruct model ships a template. Third, print config.max_position_embeddings to confirm the context length covers your longest training examples. You can also confirm the license with model_info(model_id).card_data.license.