LoRA forward pass and adapter gradients
Implement a LoRA adapter around a frozen linear layer.
What’s going wrong
Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.
Input x is B×D, w is D×O, a is D×R and b is R×O with R > 0. Compute output = x·w + (alpha/R)·(x·a)·b. upstream is B×O. Return output, grad_a and grad_b for the summed downstream objective, without returning or changing w. No implicit batch averaging.
Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.
Expected behavior
Implement a LoRA adapter around a frozen linear layer.
Reproduction
{"x":[[1,2]],"w":[[2,1],[1,0]],"a":[[1,0],[0,1]],"b":[[3,1],[-1,3]],"upstream":[[2,-1]],"alpha":1}{"output":[[4.5,4.5]],"grad_a":[[2.5,-2.5],[5,-5]],"grad_b":[[1,-0.5],[2,-1]]}{"x":[[1]],"w":[[2]],"a":[[3]],"b":[[4]],"upstream":[[5]],"alpha":0}{"output":[[2]],"grad_a":[[0]],"grad_b":[[0]]}{"x":[[1]],"w":[[2]],"a":[[3]],"b":[[0]],"upstream":[[5]],"alpha":1}{"output":[[2]],"grad_a":[[0]],"grad_b":[[15]]}Before you begin
- Python functions, lists and dictionaries
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.