PIXELBANKv9.1.0
Menu

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

r⋅din+dout⋅r=r (din+dout)r \cdot d_{in} + d_{out} \cdot r = r\,(d_{in} + d_{out})

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:

Input:
print(lora_params(4096, 4096, 8))
Output:
65536
Reasoning:
  • Identify the dimensions of the two low-rank matrices: matrix AA has shape (r,din)(r, d_{in}) and matrix BB has shape (dout,r)(d_{out}, r), where the number of parameters in each is the product of its dimensions.
  • Calculate the parameter count for matrix AA using the input values r=8r=8 and din=4096d_{in}=4096: 8×4096=327688 \times 4096 = 32768.
  • Calculate the parameter count for matrix BB using the input values dout=4096d_{out}=4096 and r=8r=8: 4096×8=327684096 \times 8 = 32768.
  • Sum the parameters from both matrices to get the total trainable parameters, which is equivalent to computing r(din+dout)r(d_{in} + d_{out}): 32768+32768=6553632768 + 32768 = 65536.
  • 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.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.