Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sandeshrajbhandari/f272ab4111755d4057c91a0511f823c1 to your computer and use it in GitHub Desktop.
Save sandeshrajbhandari/f272ab4111755d4057c91a0511f823c1 to your computer and use it in GitHub Desktop.
## elaborate text from https://lightning.ai/pages/community/tutorial/lora-llm/ using bing chat
*understanding lora for PEFT and LLM*
```
Why would we do this? For now, this alternative formulation serves a pedagogical goal to illustrate LoRA, but we will come back to it. So, when we train fully connected (i.e., “dense”) layers in a neural network, as shown above, the weight matrices usually have full rank, which is a technical term meaning that a matrix does not have any linearly dependent (i.e., “redundant”) rows or columns. In contrast, to full rank, low rank means that the matrix has redundant rows or columns. So, while the weights of a pretrained model have full rank on the pretrained tasks, the LoRA authors point out that pretrained large language models have a low “intrinsic dimension” when they are adapted to a new task, according to Aghajanyan et al. (2020). A low intrinsic dimension means the data can be effectively represented or approximated by a lower-dimensional space while retaining most of its essential information or structure. In other words, this means we can decompose the new weight matrix for the adapted task into lower-dimensional (smaller) matrices without losing too much important information. For example, suppose ΔW is the weight update for an A × B weight matrix. Then, we can decompose the weight update matrix into two smaller matrices: ΔW = WA WB, where WA is an an A × r-dimensional matrix, and WB is an an r × B-dimensional matrix. Here, we keep the original weight W frozen and only train the new matrices WA and WB. This, in a nutshell, is the LoRA method, which is illustrated in the figure below. what does the above text mean? explain like I'm five
explain it like I've some knowledge about ML but in a simplistic way, elaborate on Rank
```
Sure, I’ll try to explain the text in a more detailed way. Suppose you have a pretrained model, which is a neural network that has been trained on a large amount of data to perform various natural language processing tasks, such as text classification, sentiment analysis, question answering, etc. The pretrained model has many parameters, which are the weights and biases of the neural network. These parameters are stored in matrices, which are tables of numbers that represent how the neural network transforms the input data into the output data.
Now, suppose you want to use the pretrained model to perform a specific task that is different from the ones it was trained on. For example, you want to use the pretrained model to classify movie reviews into positive or negative. To do this, you need to fine-tune the pretrained model on a smaller amount of data that is relevant to your task. Fine-tuning means updating the parameters of the pretrained model slightly so that it can perform better on your task.
However, fine-tuning the whole pretrained model can be expensive and inefficient. This is because the pretrained model has many parameters that may not be relevant or useful for your task. For example, some parameters may be related to how the pretrained model handles questions or sentences that are not present in your movie review data. Updating these parameters may not improve your performance and may even harm it.
This is where LoRA comes in. LoRA is a method that allows you to fine-tune only a subset of parameters that are important for your task. LoRA does this by decomposing the parameter matrices into smaller matrices that have lower rank. Rank is a measure of how many independent rows or columns a matrix has. A matrix with full rank has no redundant rows or columns, while a matrix with low rank has some rows or columns that can be expressed as combinations of other rows or columns.
LoRA assumes that the parameter matrices of the pretrained model have full rank, which means they capture all the information and variation in the data that the pretrained model was trained on. However, LoRA also assumes that when you fine-tune the pretrained model on your task, the parameter updates have low rank, which means they capture only a small fraction of information and variation in your data. This implies that you can approximate the parameter updates by using smaller matrices that have lower rank.
For example, suppose W is a parameter matrix of size A x B that you want to fine-tune. Then, instead of updating W directly, you can create two smaller matrices W_A and W_B of size A x r and r x B respectively, where r is much smaller than A or B. Then, you can update W_A and W_B instead of W, and use their product W_A * W_B as an approximation of W. This way, you can reduce the number of parameters that you need to update and store.
LoRA allows you to fine-tune only W_A and W_B while keeping W fixed. This means that you can preserve the information and variation in W that are useful for other tasks while adding new information and variation in W_A and W_B that are specific to your task. LoRA also allows you to mix different types of parameters for different layers of the neural network. For example, you can use LoRA for some layers and use regular fine-tuning for other layers.
So, the text is saying that LoRA is a method that can help you fine-tune a pretrained model more efficiently and effectively by using low-rank matrices to approximate the parameter updates for your task.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment