LoRA Adapter Parameter Count
Problem Statement
LoRA freezes a weight matrix W (d_out x d_in) and learns a low-rank update B A with rank r. Count the trainable parameters the adapter adds.
Background
For a base matrix of shape (d_out, d_in), LoRA introduces A of shape (r, d_in) and B of shape (d_out, r), so the update W + BA adds
trainable parameters, versus d_out * d_in for full fine-tuning. That ratio is why a rank-8 adapter trains a fraction of a percent of the weights.
Your Task
Implement:
def lora_params(d_in, d_out, r):
Return the number of trainable LoRA parameters as an int.
Input Format
- d_in, d_out, r (int).
Output Format
- A single int.
Sample
print(lora_params(4096, 4096, 8))
Output:
65536
Example
print(lora_params(4096, 4096, 8))
65536
- Identify the dimensions of the two low-rank matrices: matrix has shape and matrix has shape , where the number of parameters in each is the product of its dimensions.
- Calculate the parameter count for matrix using the input values and : .
- Calculate the parameter count for matrix using the input values and : .
- Sum the parameters from both matrices to get the total trainable parameters, which is equivalent to computing : .
- The final output is 65536
Constraints
1 <= d_in, d_out <= 100000,1 <= r <= min(d_in, d_out).- Parameters are
r*(d_in + d_out)(no biases). - Return an int.