News for hackers. Stuff that matters.
Re:LoRA: Low-Rank Adaptation of Large Language Models by elil17 on Friday March 24, 2023 @09:43AM (#35288966)
Can someone ELI5 "LoRA reduces the number of trainable parameters by learning pairs of rank-decompostion matrices while freezing the original weights"?
Re:LoRA: Low-Rank Adaptation of Large Language Models by MacsHeadroom on Friday March 24, 2023 @10:00AM (#35289168)
LoRA finds a subset of the original weights (about 1%) which can be trained to achieve about the same result as training the whole model while using 100x less compute.

Original weights frozen = Rather than modify the original model, the training results are saved to a small file of only a few MB.

In practice this means you can fine tune a 30B parameter model on a consumer GPU in a couple of hours. Without LoRA you would need to run multiple expensive data center GPUs for days or weeks.

Re:LoRA: Low-Rank Adaptation of Large Language Models by stephanheijl on Friday March 24, 2023 @10:14AM (#35289319)
To be more exact, LoRA adds two matrices `A` and `B` to any layers that contain trainable weights. The original weights (`W_0`) have the shape `d × k`. These are frozen. Matrix `A` has dimensions `d × <rank>` (`rank` is configurable) and matrix `B` has the shape `<rank> × k`. A and B are then multiplied and added to `W_0` to get altered weights. The benefit here is that the extra matrices are small compared to `W_0`, which means less parameters need to be optimized, so less activations need to be stored in memory.
Re:LoRA: Low-Rank Adaptation of Large Language Models by grph123dot on Friday March 24, 2023 @10:54AM (#35289834)
Your explanation is crystal clear. I suppose it works well in practice, but is there any reason it works that well?
Re:LoRA: Low-Rank Adaptation of Large Language Models by stu2b50 on Friday March 24, 2023 @11:14AM (#35290121)
Per the original paper, empirically it’s been found that neural network weights often have low intrinsic rank. It follows, then, that the change in the weights as you train also have low intrinsic rank, which means that you should be able represent them with a lower rank matrix.
Re:LoRA: Low-Rank Adaptation of Large Language Models by grph123dot on Friday March 24, 2023 @11:50AM (#35290729)
Since we are in ELI5, it seems that the concept of low rank approximation is required to understand this method.

(1) https://en.wikipedia.org/wiki/Low-rank_approximation

Edited: By the way, it seems to me that there is an error in the wikipedia page because if the Low-rank approximation takes a larger rank then the bound of the error should decrease, and in this page the error increases.

Re:LoRA: Low-Rank Adaptation of Large Language Models by grph123dot on Friday March 24, 2023 @12:08PM (#35291115)
>> that the change in the weights as you train also have low intrinsic rank

It seems that the initial matrix of weights has a low rank approximation A and this implies that the difference E = W - A is small, also it seems that PCA fails when E is sparse because PCA is designed to be optimum when the error is gaussian.

Re:LoRA: Low-Rank Adaptation of Large Language Models by stu2b50 on Friday March 24, 2023 @12:16PM (#35291258)
In terms of PCA, PCA is also quite expensive computationally. Additionally, you'd probably have to do SVD instead.

Since the weights are derived from gradient descent, yeah we don't really know what the distributions would be.

A random projection empirically works quite well for very high dimensions, and is of course very cheap computationally.

Re:LoRA: Low-Rank Adaptation of Large Language Models by seydor on Friday March 24, 2023 @01:36PM (#35292666)
Does this mean the matrices are highly compressible?
Re:LoRA: Low-Rank Adaptation of Large Language Models by loxias on Friday March 24, 2023 @07:15PM (#35296955)
kinda/yes. To translate to more intuitive concepts: the matrices don't contain much variance in as many degrees of freedom as they could.

Think of a point cloud of a piece of paper floating in the wind. It would be a 3xn list of points, but "really" it's a 2d piece of paper.

Just like I can rewrite the number 27 as 333 or 8+19 or (2^3)+(2^4)+3.. Given a single matrix one can find myriad ways to rewrite it as a sequence of matrices that have the same (or similar) numeric value, but with interesting or desirable properties. :D

My favorite example (which is used in signal processing) is to take your ugly matrix and rewrite it as a set of smaller matrices where most of the elements are zero, or a power of 2.

It turns out, computers can multiply by zeros and powers of two very fast

Re:LoRA: Low-Rank Adaptation of Large Language Models by twic on Friday March 24, 2023 @11:23AM (#35290262)
Ah, so the resulting model contains both the large matrix of original weights, and also the two small matrices of alterations? But this is smaller than the alternative of a model which contains the large matrix of original weights, and an equally large matrix of alterations.

Why is fine-tuning done with separate alterations, rather than by mutating the original weights?

Re:LoRA: Low-Rank Adaptation of Large Language Models by stu2b50 on Friday March 24, 2023 @11:29AM (#35290357)
> But this is smaller than the alternative of a model which contains the large matrix of original weights, and an equally large matrix of alterations.

It's actually larger. If you just have two equally large matrices of the same dimension, one original, and one of "altercations"... then you can just add them together.

> Why is fine-tuning done with separate alterations, rather than by mutating the original weights?

Then you'd have to compute the gradients for the whole network, which is very expensive when the model has 7b, 65b, 165b parameters. The intent is to make that cheaper by only computing gradients for a low rank representation of the change in the weight matrix from training.

Re:LoRA: Low-Rank Adaptation of Large Language Models by gliptic on Friday March 24, 2023 @12:51PM (#35291890)
Correct me if I'm wrong, but I think you still need to compute gradients of non-trained weights in order to compute the gradients of the LoRA weights. What you don't have to do is store and update the optimizer state for all those non-trained weights.
Re:LoRA: Low-Rank Adaptation of Large Language Models by stu2b50 on Friday March 24, 2023 @12:56PM (#35291965)
I mean the derivative of a constant is 0. So if all of the original weights are considered constants, then computing their gradients is trivial, since they’re just zero.
Re:LoRA: Low-Rank Adaptation of Large Language Models by jprafael on Friday March 24, 2023 @01:17PM (#35292306)
Computing gradients is easy/cheap. What this technique solves is that you no longer need to store the computed values of the gradient until the backpropagation phase, which saves on expensive GPU RAM, allowing you to use commodity hardware.
Re:LoRA: Low-Rank Adaptation of Large Language Models by arugulum on Friday March 24, 2023 @02:41PM (#35293635)
>Then you'd have to compute the gradients for the whole network

You have to do that with LoRA regardless, to compute the gradients for the lowest-level LoRA weights.

Re:LoRA: Low-Rank Adaptation of Large Language Models by TuringTest on Friday March 24, 2023 @12:57PM (#35291989)
It's larger, but there are less parameters to train for your specific use case since you are training the small matrix only, while the original ones remain unaltered.
Re:LoRA: Low-Rank Adaptation of Large Language Models by arugulum on Friday March 24, 2023 @02:43PM (#35293671)
> Why is fine-tuning done with separate alterations, rather than by mutating the original weights?

The goal of most parameter-efficient methods is to store one gold copy of the original model, and learn minor modifications/additions to the model. The easiest way to think about this is in some kind of deployment setting, where you have 1 capable model and you learn different sets of LoRA weights for different tasks and applications.

The original intent of parameter-efficient methods is to reduce the amount of storage space needed for models (do you really want to keep a whole additional copy of LLaMA for each different task?). A secondary benefit is that because you are fine-tuning a smaller number of parameters, the optimizer states (can take up to 2x the size of your model) are also heavily shrunk, which makes it more economical (memory-wise) to (parameter-efficient) fine-tune your model.

Re:LoRA: Low-Rank Adaptation of Large Language Models by leobg on Saturday March 25, 2023 @01:59AM (#35299919)
That’s probably what OpenAI does with their custom fine tuned models, no?
Re:LoRA: Low-Rank Adaptation of Large Language Models by seydor on Friday March 24, 2023 @01:33PM (#35292626)
Can rank decomposition be used to reduce the original weight matrices as well? Or are they assumed to be compressed already?
Re:LoRA: Low-Rank Adaptation of Large Language Models by metanonsense on Wednesday March 29, 2023 @03:33AM (#35353840)
Those fully trained networks are usually considered full-rank. At least that is what they say in the LoRA paper.
Re:LoRA: Low-Rank Adaptation of Large Language Models by quest88 on Friday March 24, 2023 @11:29AM (#35290350)
Is this the same as Knowledge Distillation (teacher-student training)?
Re:LoRA: Low-Rank Adaptation of Large Language Models by tylerekahn on Friday March 24, 2023 @12:26PM (#35291437)
It’s actually as low as 0.01% of the original weights.

From the LoRa paper:

>When the pre-trained model is GPT-3 175B, the number of train- able parameters |Θ| can be as small as 0.01% of |Φ0|.

Re:LoRA: Low-Rank Adaptation of Large Language Models by pffft8888 on Friday March 24, 2023 @12:42PM (#35291746)
Is this the same as or similar to the Lottery Ticket concept from a few years ago?
Re:LoRA: Low-Rank Adaptation of Large Language Models by arugulum on Friday March 24, 2023 @02:52PM (#35293797)
>In practice this means you can fine tune a 30B parameter model on a consumer GPU in a couple of hours.

Consumer GPU, yes, but in practice LoRA doesn't actually reduce training time. What it mainly reduces is memory requirements. In fact LoRA training can often require more training steps than full fine-tuning and therefore be slower (you can imagine why this is the case: the optimization is trying to modify the mode's behavior a smaller number of parameters, and so has a harder job)

Re:LoRA: Low-Rank Adaptation of Large Language Models by MacsHeadroom on Sunday March 26, 2023 @12:16PM (#35316145)
Modern peft methods with LoRA actually do reduce training time by orders of magnitude.

Here's an example of 20 seconds per epoch on a single consumer GPU: https://github.com/johnsmith0031/alpaca_lora_4bit/issues/7#i...

Re:LoRA: Low-Rank Adaptation of Large Language Models by esperent on Friday March 24, 2023 @11:05PM (#35298864)
> about the same result

Can you qualify this? Is it still useful or not?

Re:LoRA: Low-Rank Adaptation of Large Language Models by MacsHeadroom on Sunday March 26, 2023 @12:22PM (#35316224)
>Is it still useful or not?

Is what still useful? A LoRA is about as good and useful as a full fine tune. If you have unlimited storage space to store them or unlimited compute to make them then I would still prefer full fine tunes. But the difference is marginal and generally not worth the storage space or increased compute costs for individuals.

Re:LoRA: Low-Rank Adaptation of Large Language Models by edwardjhu on Friday March 24, 2023 @03:57PM (#35294596)
Hi! I'm the author of the repo.

The insight is that we don't need to modify a lot of parameters to get a generally competent model to do well on specific tasks. When you have a linear layer with a weight matrix of dimension d_in x d_out, the change you undergo during full finetuning is also a matrix of d_in x d_out, which can be huge. We represent the latter using two matrices of shape d_in x r and r x d_out. You save a lot of parameters when r is small. So when you use it, the input goes through two streams 1) the orignal frozen weight turning a vector of size d_in to d_out and 2) the low-rank weights turning a vector of size d_in to r and r to d_out. The two streams are then summed together. (There's a figure in the paper.)

This way of doing thing is nice for a few reasons. It's easy to parallelize. You can change r to control how many parameters to train. You can also merge the low-rank weights with the original one to avoid latency.

Note that we don't select a subset of the original parameters. We train extra ones.

Re:LoRA: Low-Rank Adaptation of Large Language Models by eternalban on Friday March 24, 2023 @06:16PM (#35296436)
Great, we can get authoritative answers. (I'm trying to understand the ML space and have mostly done readings, not an expert.)

I am assuming you can have n LoRA fine-tunings, say each specializing in one aspect of a coherent task, with n summers, running in parallel, and then combine them at the end? Or more generally, does LoRA enable a sort of modularizing around a core (un-merged) model?

And curious if you ever tried merging 2 or more fine-tunings and then testing the resultant single model (merge all) against the original tests to check retention?

Re:LoRA: Low-Rank Adaptation of Large Language Models by edwardjhu on Friday March 24, 2023 @07:08PM (#35296885)
This paper tries something like that

https://arxiv.org/pdf/2202.13914.pdf

The gain isn't that significant. We don't understand what these low-rank updates represent, and they might not correspond to "skills" that humans have.

Re:LoRA: Low-Rank Adaptation of Large Language Models by loxias on Friday March 24, 2023 @06:59PM (#35296811)
Hi! I in _no way_ mean to detract or malign or "anything negative" the parent comment (communication is hard!!), BUT I really compliment that exact sentence. :)

My background contains signal processing, "pre-deep learning ML", systems engineering, and firmware, and that sentence jumped out at me as crystal clear in my mind, despite not knowing what HuggingFace is or PyTorch.

Correct me if I'm wrong: These huge models involve lots of weights used in large matrices. The contribution of this work is to plug in some matrix factorization and learn a lower dimensional representation, instead of a large second matrix.

Fantastic!

Also makes me wonder what other performance improvements await through proper application of established and well known Mathematics. :D

Re:LoRA: Low-Rank Adaptation of Large Language Models by leobg on Saturday March 25, 2023 @01:54AM (#35299888)
What is the difference to training an adapter? Or to adding a new task specific layer [0]? Has it been demonstrated that LoRA works best out of all of these approaches?

[0] https://towardsdatascience.com/adding-custom-layers-on-top-o...

Re:LoRA: Low-Rank Adaptation of Large Language Models by edwardjhu on Saturday March 25, 2023 @08:15AM (#35301903)
Adapters are extra layers inserted between existing layers, so they can't be parallelized. LoRA reparametrizes the weight updates and is easily parallelized or merged with the original weights during inference. Also, if you let the rank r be the hidden size you roughly recover finetuning, so you can see LoRA as a generalization of the latter.

Add a task specific layer and only training that layer doesn't work well. In practice, people combine many of these things, e.g., LoRA + task-specific final layer.

Re:LoRA: Low-Rank Adaptation of Large Language Models by leobg on Saturday March 25, 2023 @02:39PM (#35305643)
Thanks for the clarification. Does that mean then that when parallelization is not important, training an adapter might be just as good as or better than LoRA?
Re:LoRA: Low-Rank Adaptation of Large Language Models by edwardjhu on Saturday March 25, 2023 @06:11PM (#35307993)
If latency is irrelevant, I don't think there is a strong practical reason to prefer one over another. (LoRA is more elegant in my biased opinion because you roughly recover finetuning with a large r.) In practice, you see one do a little better on some tasks and vice versa on others as observed by papers after mine.