Concepts / Weight Parameters and Model Structure

Weight Parameters and Model Structure

LoRA, short for Low-Rank Adaptation, adapts a pretrained model to a specific, often smaller dataset by adjusting only a small subset of its weight parameters.

  • Programming

The Cost of Adapting a Large Model

Imagine you already have a large model that has been trained before. It knows a lot in general, but you now need it to perform well on one specific task, and you only have a modest amount of data for that task. Adapting the model to that data is called fine-tuning. The question this article answers is how that adaptation can be done without paying the full cost that fine-tuning usually demands.

The technique this course introduces for that purpose is LoRA. Its name stands for Low-Rank Adaptation. Before looking at either word in the name, it helps to see the problem it targets: the computational costs and resources that fine-tuning a large model normally requires. Large models are expensive to adjust in full, so the goal is a method that changes the model in a much more limited way while still producing a task-specific result.

What LoRA Changes and What It Leaves Alone

Definition

LoRA, short for Low-Rank Adaptation, adapts a pretrained model to a specific, often smaller dataset by adjusting only a small subset of its weight parameters.

The key phrase in that definition is only a small subset. The model's weights are not all treated the same way under LoRA. Instead of changing the entire set of weight parameters to match the new data, LoRA restricts the changes to a small portion of them. The pretrained model remains the starting point, and the adaptation is focused on that limited part. Think of the pretrained weights as a fixed foundation and the LoRA parameters as a small, trainable addition built on top of it.

Pretrained modelstarting pointPretrained weights WfrozenAdapted modelfrozen base plus learnedupdateMatrix AtrainableMatrix Btrainablekeepsaddsaddscombined withupdateupdate
Which parts of the pretrained model stay fixed during fine-tuning, and which new matrices A and B are the only ones updated?

The diagram shows the division of labor. The pretrained weight matrix W is not the thing being trained. The matrices A and B are the only ones that receive updates during training, and the adapted model is the result of combining the frozen base with what those two matrices have learned. The diagram describes the standard LoRA arrangement, in which the low-rank update is kept separate from the original weights.

Following One Weight Matrix W Through a Layer

Large language models contain many layers, and each layer is built from weight matrices. Suppose a large weight matrix $W$ is associated with a specific layer. LoRA can be applied to all linear layers in an LLM, but it is easier to understand the idea by focusing on a single layer. Everything that follows about one layer applies in the same way to each linear layer where LoRA is used.

Keep one matrix in view as you read the rest of the article. Call it $W$. In a standard fine-tune, $W$ itself would be changed during training. Under LoRA, $W$ stays as it was in the pretrained model, and the learning happens in a separate, much smaller set of parameters attached to that same layer.

Input xFrozen Wpretrained, not updatedSumOutputMatrix AtrainableMatrix Btrainablethrough frozen paththrough low-rank pathpasses throughbase outputlearned updatelayer output
When an input passes through a LoRA-adapted layer, how do the frozen weights and the trainable low-rank matrices combine to produce the output?

Read the diagram from left to right. The input enters the layer along two routes. One route passes through the frozen matrix W, which produces the base output that the pretrained model would have produced. The other route passes through the trainable matrices A and B, which produce the learned adjustment. The two results are added together to give the layer's output. Only the second route is affected by training, so every update lands in the small trainable part while the pretrained base remains intact.

The Low-Rank Idea as a Smaller Subspace

The word low-rank refers to a mathematical idea: limiting model adjustments to a smaller dimensional subspace of the total weight parameter space. You can think of the full weight parameter space as the complete set of directions in which the weights could possibly move during training. A smaller subspace is a restricted set of those directions. Adjustments made within that restricted set are the only ones LoRA allows.

Why would restricting the directions be a good idea? The source explains that this restriction effectively captures the most influential directions of the weight parameter changes during training. In other words, the changes that matter most for the task are kept, while the space of possible changes is made smaller. This is the reason the approach can be both focused and efficient.

In the standard LoRA formulation, the full weight matrix $W$ has shape $d \times d$ in the case discussed here. Rather than learning an update of that full size, LoRA learns two thin matrices. Matrix A has shape $r \times d$ and matrix B has shape $d \times r$, where $r$ is much smaller than $d$. Multiplying B by A produces an update of the same $d \times d$ shape as $W$, but every such update must pass through an $r$-dimensional bottleneck. That bottleneck is the smaller subspace the word low-rank describes.

Full updated x d matrixd squared valuesevery entry trainableLow-rank updateB times AMatrix Ar x d, the first thinmatrixMatrix Bd x r, the second thinmatrixr-dimensionalsubspacer much smaller than dhasbuilt frombuilt frommaps intomaps out of
How does a large weight matrix get replaced by two thin matrices that pass information through a much smaller r-dimensional subspace, and what shape does each matrix have?
Formula, step by step
  1. 1
    W is the frozen pretrained weight matrix and is not changed during training.
  2. 2
    A has shape $r \times d$ and is one of the two trainable low-rank matrices.
  3. 3
    B has shape $d \times r$ and is the other trainable low-rank matrix.
  4. 4
    The product BA has shape $d \times d$, so it can be added to W.
  5. 5
    Because the product passes through an r-dimensional middle step, the adjustment is confined to a low-rank subspace.

Counting Parameters Before and After

The benefit of the low-rank structure becomes concrete when you count the trainable parameters. The source describes the savings in general terms, as a significant cut in computational costs and resources. The count below uses a made-up but representative size to show where those savings come from.

Worked example

Comparing Trainable Parameters for One Layer

A weight matrix W has shape 1000 x 1000, so d is 1000. A full fine-tune would train every entry of W. A LoRA adapter uses a rank r of 8. How many trainable parameters does each approach need for this one matrix?

  1. 1
    Count the full fine-tune

    A full fine-tune updates every entry of the d x d matrix, which is $d^{2}$ values. With d equal to 1000, that is $1000^{2} = 1{,}000{,}000$ trainable parameters.

  2. 2
    Count matrix A

    Matrix A has shape r x d, which is $8 \times 1000 = 8{,}000$ values.

  3. 3
    Count matrix B

    Matrix B has shape d x r, which is also $1000 \times 8 = 8{,}000$ values.

  4. 4
    Add the two thin matrices

    Together, A and B give $8{,}000 + 8{,}000 = 16{,}000$ trainable parameters, which is $2dr$.

  5. 5
    Compare the two counts

    The LoRA adapter trains 16,000 values instead of 1,000,000 for this matrix, so the adapter is 62.5 times smaller in trainable parameters for this layer.

Result

A full fine-tune trains 1,000,000 parameters for this matrix, while a rank 8 LoRA adapter trains 16,000. The frozen matrix W contributes none of the trainable count.

Notice what the example does and does not show. It shows that the trainable portion shrinks dramatically when r is much smaller than d. It does not change the pretrained weight matrix W, which still has its full size and still participates in every forward pass. The savings are in what is trained, not in what the model contains.

Why Small Trainable Parts Suit Task-Specific Data

The main reason LoRA is useful and popular is that it enables efficient fine-tuning of large models on task-specific data. Efficiency here means that the approach significantly cuts down on the computational costs and resources that fine-tuning usually requires. Large models are expensive to adjust in full, so a method that touches only a small part of the parameters is valuable when the goal is a specific task rather than a complete rebuild.

This fits the situation described at the start of the article: a large pretrained model, a specific task, and only a modest amount of data for that task. Training a small set of adapter parameters means fewer values have to be computed and stored during learning. The source does not spell out the memory or overfitting consequences, but it is reasonable to read the restriction as a way of focusing learning on the task without rewriting the whole model.

Mistakes When Reading About LoRA

  • Common mistake

    Thinking LoRA retrains the whole model at a smaller learning rate

    Why it fails

    LoRA does not adjust every weight. It restricts changes to a small subset of parameters, so the pretrained weight matrix W stays frozen.

    Do this instead

    Identify the frozen part (W) and the trainable part (A and B) before reasoning about what learning changes.

  • Common mistake

    Reading low-rank as meaning the model has few layers or a small size

    Why it fails

    The low-rank part of the name refers to restricting adjustments to a smaller dimensional subspace of the total weight parameter space. It is a statement about the update, not about the size of the model.

    Do this instead

    Read low-rank as the bottleneck of dimension r that every update passes through.

  • Common mistake

    Assuming the low-rank update replaces W

    Why it fails

    In the standard formulation the update is added to W, as W' = W + BA, so the pretrained weights remain part of the adapted layer.

    Do this instead

    Treat the adapted layer as the frozen base output plus the learned low-rank output.

  • Common mistake

    Treating LoRA as applicable to only one layer, when the source says it can be applied to all linear layers in an LLM

Checking Your Understanding

EASY

A layer has a weight matrix W of shape 500 x 500. You apply LoRA with rank r equal to 4. Without computing a number yet, explain which of the two matrices, A or B, has the r dimension as its number of rows, and then explain which parts of the layer are updated during training and which are not.

Hints
  • Matrix A has shape r x d, so its rows are the r-dimensional bottleneck.
  • Only A and B are trainable; W is the frozen pretrained weight matrix.
  • Use the formula W' = W + BA to see how the frozen and trainable parts combine.
MEDIUM

Using the same 500 x 500 matrix with rank 4, count the trainable parameters for a full fine-tune and for a LoRA adapter. Then state how many times smaller the LoRA trainable count is.

Hints
  • A full fine-tune trains d squared values.
  • The LoRA adapter trains 2dr values across A and B combined.

Key Takeaways

  • LoRA, short for Low-Rank Adaptation, adapts a pretrained model to a specific, often smaller dataset by adjusting only a small subset of its weight parameters, and the pretrained model remains the starting point.
  • The low-rank part of the name means restricting adjustments to a smaller dimensional subspace of the total weight parameter space, which captures the most influential directions of the changes made during training.
  • In the standard formulation, the frozen weight matrix W is combined with a trainable update BA, where A and B are thin matrices that pass information through an r-dimensional bottleneck.
  • LoRA enables efficient fine-tuning of large models on task-specific data by significantly cutting down on the computational costs and resources that fine-tuning normally requires.

Related concepts

More concepts starting with W