bugger.
All challenges/Pythonhardllm-lora-v1
THE INVESTIGATION

LoRA forward pass and adapter gradients

Implement a LoRA adapter around a frozen linear layer.

LLM ArchitecturePost-training

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

01 Rank two adapter changes outputs and gradients
{"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]]}
02 Zero alpha freezes the entire adapter branch
{"x":[[1]],"w":[[2]],"a":[[3]],"b":[[4]],"upstream":[[5]],"alpha":0}
{"output":[[2]],"grad_a":[[0]],"grad_b":[[0]]}
03 Zero B can still learn
{"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.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.