Workspace/Coding labs
Loading progress

Compute a dimension-safe LoRA forward pass

Intermediate45 min

Implement lora_forward(W,A,B,x,scale=1.0) returning W*x + scale*B*(A*x). Require nonempty rectangular matrices, finite numeric entries, W shaped out by in, A shaped rank by in, B shaped out by rank, and x length in. Reject booleans as numbers and any incompatible dimension. Inputs must not be mutated. Standard-library loops are sufficient.

Your task

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

InputW=[[1,0],[0,1]], A=[[1,-1]], B=[[2],[1]], x=[3,1], scale=0.5

Output[5.0,2.0]

The rank-one update contributes [2,1].
solution.pyPython 3.12