LoRA attaches to linear layers. But a model has many kinds of linear layers. Which ones should get an adapter? That's what the target modules setting decides.
Let's find every linear layer, and add up its weights, grouped by name. Named modules walks through every part of the model. We keep only the linear ones. Then we print the totals, biggest first. Run it. The LM head is the biggest. It turns meaning into a score for every token. Then the three MLP grids. Then the four attention grids, q, o, k, and v.
app.pypython
from collections import Counterfrom torch import nnfrom transformers import AutoModelForCausalLMmodel = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")sizes = Counter()for name, module in model.named_modules(): if isinstance(module, nn.Linear): sizes[name.split(".")[-1]] += module.weight.numel()for name, count in sizes.most_common(): print(f"{name:10} {count:>12,} weights")
There are two common choices. The original LoRA paper used only the attention grids, q proj and v proj. It's the cheapest option. Today, most people use all linear, which puts LoRA on every linear layer, except the LM head. It costs a little more, but usually learns better.
Let's recap. LoRA attaches to linear layers, chosen with target modules. q proj and v proj is the cheapest choice. And all linear usually learns best. Next, writing the LoRA settings in code.