PIXELBANKv9.1.0
Menu

Implement the LoRA forward pass combining frozen weights with low-rank adaptation.

In LoRA, the output is: y=xW+xΔW=xW+x(AB)y = xW + x\Delta W = xW + x(AB)

where W is the frozen pretrained weight, and A, B are the LoRA matrices.

Input:

  • Line 1: d r (dimension, rank)
  • Line 2: space-separated floats (input vector x of dimension d)

Generate W, A, B with np.random.seed(42):

  • W = np.random.randn(d, d) * 0.1
  • A = np.random.randn(d, r) * 0.1
  • B = np.random.randn(r, d) * 0.01

Output: Output vector y = x @ W + x @ A @ B, rounded to 4 decimal places.

Example:

Input:
3 2
1.0 0.0 0.0
Output:
0.0495 -0.0132 0.0636
Reasoning:
  • We start by generating the frozen pretrained weight WW and LoRA matrices AA and BB using the given dimensions and np.random.seed(42): W=np.random.randn(3,3)∗0.1W = np.random.randn(3, 3) * 0.1, A=np.random.randn(3,2)∗0.1A = np.random.randn(3, 2) * 0.1, and B=np.random.randn(2,3)∗0.01B = np.random.randn(2, 3) * 0.01.
  • Next, we compute the output vector yy by applying the LoRA forward pass formula: y=xW+xΔW=xW+x(AB)y = xW + x\Delta W = xW + x(AB), where x=[1.0,0.0,0.0]x = [1.0, 0.0, 0.0].
  • We calculate x@Wx @ W and x@A@Bx @ A @ B separately, then add the results to obtain yy.
  • Finally, we round the elements of yy to 4 decimal places to obtain the output vector: y=[0.0495,−0.0132,0.0636]y = [0.0495, -0.0132, 0.0636].

Constraints:

  • np.random.seed(42), then generate W, A, B in order
  • Output y = x @ W + x @ A @ B
  • Round to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.
LoRA Forward Pass - Easy | PixelBank