Page cover image

What is Low-Rank Adaptation (LoRA) - explained by the inventor

Edward Hu

Edward Hu, formerly a researcher at Microsoft, introduces Low Rank Adaptation (LoRa), a method to efficiently customise pretrained neural networks like diffusion models or language models.

LoRa enhances training speed and significantly reduces checkpoint sizes by fine-tuning a minimal number of parameters, maintaining the performance level of comprehensive fine-tuning.

Originating in early 2021 during Microsoft's collaboration with OpenAI, LoRa addressed the limitations of few-shot prompting and the prohibitive expense of full fine-tuning, especially for models with vast numbers of parameters.

LoRa operates by questioning the necessity and extent of parameter fine-tuning, using a 2D plane to illustrate the range of possible configurations. It controls matrix update expressivity through rank limitation, enabling substantial parameter reduction without compromising transformation capabilities.

This method proved nearly as effective as full fine-tuning but with vastly smaller storage and quicker deployment benefits.

LoRa's applicability extends beyond language models to any architecture involving matrix multiplication, offering a clear path for adjustment if underperformance occurs.

It significantly cuts down checkpoint sizes (e.g., reducing a 1 TB checkpoint to 25 MB) and allows for additional low-rank matrices without introducing inference latency.

The updates are additive, allowing seamless model switching and parallel training of multiple modules. LoRa's additive nature also supports hierarchical model specialization, enabling efficient, layered fine-tuning and rapid task or user-specific model adaptation.

Full Transcript

Low Rank Adaptation, or LoRa, allows you to efficiently customise pre trained neural networks, such as diffusion models or language models.

It speeds up training and drastically reduces the size of model checkpoints by training very few parameters compared to the base model, while preserving the performance of full fine tuning. And it has become one of the go to methods for customizing AI models.

My name is Edward Hu, and I led the invention of LoRa when I was a researcher at Microsoft. In this video, I'll share the research story behind LoRa, how I understand it, and its technical benefits.

My team at Microsoft was tasked with answering, can this GPU 3 stuff actually make money? Somewhat surprising finding was that few shot prompting was not enough to get even the largest models performed well enough for production, especially for tasks like natural language to code because it rarely appears in the training data.

Fine tuning through gradient updates was a necessity. However, full fine tuning is prohibitively expensive.

A single model checkpoint for the 175,000,000,000 parameter variant is 1 terabyte large, which is hard to store and takes minutes to load when deployed. And that's not gonna work when we need to switch among tasks and users rapidly. We tried many off the shelf, parameter efficient fine tuning methods, but all of them had a compromise for our case.

It was with product impact in mind that we invented LoRa.

So what is LoRa? I like to see it as a generalization of full fine tuning by asking 2 questions.

Question 1: Do we need to fine tune all the parameters?

Question 2: For the weight matrices we fine tune, how expressive should the updates be in terms of matrix rank?

We can turn these two questions into the 2 axes of a 2 d plane.

Full fine tuning is all the way in the upper right corner and the axes, including the origin, correspond to the original model. Any point in this box is a valid LoRa configuration. Let's quickly talk about how we control the expressivity of a matrix update by controlling its rank. A d by d matrix can represent any linear transformation in a d dimensional vector space.

However, if we start with a vector in Rd, first transform it to rr, where r is less than d, and finally transform it back to rd, we restrict the kind of linear transformations we can represent.

How does stopping by rr achieve that? Imagine an extreme case where r, or rank, equals 1. Whatever the input does boils down to just one number, which can only scale the output. By picking a small r, the kind of linear transformation we can represent is greatly limited, even though the output is still in rd.

Now, we only have to store 2 times d times r parameters instead of d squared. This is how LoRa stores matrix updates. Now back to the 2 d plane we talked about.

The surprising result of the LoRa paper is that a point near the origin performed just as well as full fine tuning all the way in the corner. Once we see LoRa as a generalization of full fine tuning, we can easily answer some commonly asked questions for using LoRa, such as how to choose the rank R, or when to use full fine tuning.

Since full fine tuning is a special case of LoRa, and we know that full fine tuning works, we can start with a point near the origin and work our way back to the corner. At some point, this has to work, and more likely than not, what ended up working will be near the origin. Otherwise, just give up and do full fine tuning. How might that happen? Let's consider a thought experiment.

If we take a language model pre trained on English and English only, but we wanna adapt it for some tasks, say in Martian. And let's say that English and Martian have little in common, since we're basically training a model all over again. Parameter efficient fine tuning methods shouldn't work too well, So we might as well do full fine tuning instead. Another question is, can I use LoRa for a certain model of architecture? Say a wave net or a support vector machine.

And by the way, nobody really asks about the latter, but as long as the model uses matrix multiplication, we can ask, do we need to fine tune all the parameters and how expressive should the updates be? As long as we can ask these 2 questions, we can use LoRa, which makes it very generally applicable. Indeed, while we invented LoRa for large language models, people later found it to be very effective for diffusion models as well. I wanna point out that one advantage of LoRa is that it's clear what to do next if underperformance. We adapt more parameters and increase the rank.

For approaches like prefix tuning, midfit, or adapter, it is not clear what we can do next because there isn't a knob to turn that allow these methods to recover full fine tuning, unlike LoRa. Now, let's dive into the specifics of the benefits of LoRa. The most visible one is a reduction of checkpoint sizes. On GP3, we reduce the checkpoint size from 1 terabyte down to 25 megabytes. This is a direct result of training much fewer parameters 4,700,000 in this case compared to 175,000,000,000.

Another important benefit is that LoRa doesn't introduce any inference latency. You might say, hold on, don't we have these additional low rank matrices on the side while that's truly trained? What happens during inference stats? Since LoRa updates are additive to the original parameters, we can expand the low rank matrices by multiplying out the low rank bottleneck and add the updates to the original parameters. Now, we can perform inference literally the same way as with a base model, and there is no additional latency by definition.

When we need to switch tasks, we simply repeat the process but this time subtract the updates. By being careful about numerical precisions, we can recover the original parameters and we repeat to load another LoRa module. This process can be done in parallel for all parameters and is faster than a single forward pass. This is how we can switch models quickly without introducing any additional inference latency. Finally, I want to mention a few engineering ideas enabled by LoRa.

The first one is to cache many LoRa modules in RAM during deployment. So model switching simply involves data transfer between RAM and VRAM. Since RAM is usually much larger than VRAM, we can cache thousands of LoRa modules and never worry about reading from the disk again. Another idea is to train multiple LoRa modules in parallel, each on its own task. This is achieved by sharing the same base model and routing different inputs in a single batch through different LoRa modules.

This way, we can batch different LoRa jobs together and fully utilize the GPUs. There are several community implementations of this, which are linked in the video description. The final idea uses the fact that LoRa models are additive. Imagine a pipeline where a pre trained model is gradually specialized, or maybe it's first fine tuned for a particular language, and then a particular domain, and finally a particular task or even a specific user. The adaptive models form a tree, Each non root node can be a Lua module on top of the sum of its ancestors.

The model rank can be larger near the root and smaller near the leaves to accommodate different dataset sizes. Model switching here becomes tree traversal and we never have to load the base model more than once. Let me know in the comments if you have any cool ideas on how LoRa can be used or extended or if you just have any questions. I'll see you in the next video.

Last updated

Logo

Continuum - Accelerated Artificial Intelligence

Continuum WebsiteAxolotl Platform

Copyright Continuum Labs - 2023