LoRA (Low-Rank Adaptation) Types #
Core data structures for LoRA finetuning of BitNet models.
Overview #
LoRA injects trainable low-rank matrices alongside frozen ternary weights:
output = BitLinear(x) + (alpha / rank) * B @ A @ x
Where:
- BitLinear(x): frozen ternary base model output
- A: [rank, inDim] FP32 matrix (Kaiming initialized)
- B: [outDim, rank] FP32 matrix (zero initialized)
- alpha: scaling factor (typically equal to rank)
References #
- "LoRA: Low-Rank Adaptation of Large Language Models" (Hu et al., 2021)
- Stanford Alpaca: instruction-following finetuning
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Hesper.LoRA.instReprConfig = { reprPrec := Hesper.LoRA.instReprConfig.repr }
A single LoRA weight pair (A and B matrices) for one projection. Forward: output += scale * B @ (A @ x) A is [rank, inDim], B is [outDim, rank] in row-major FP32.
- a : WebGPU.Buffer
A matrix: [rank, inDim] FP32, Kaiming initialized
- b : WebGPU.Buffer
B matrix: [outDim, rank] FP32, zero initialized (so LoRA starts as identity)
- inDim : Nat
Input dimension
- outDim : Nat
Output dimension
- rank : Nat
Rank
Instances For
Gradient buffers for a single LoRA weight pair
- dA : WebGPU.Buffer
Gradient for A: [rank, inDim] FP32
- dB : WebGPU.Buffer
Gradient for B: [outDim, rank] FP32
Instances For
Adam optimizer state for a single LoRA weight pair
- mA : WebGPU.Buffer
First moment for A
- vA : WebGPU.Buffer
Second moment for A
- mB : WebGPU.Buffer
First moment for B
- vB : WebGPU.Buffer
Second moment for B
Instances For
LoRA adapter for a single attention layer (Q and V projections)
Instances For
Adam state for a single attention layer
Instances For
Full LoRA adapter for the entire model (all transformer layers)
- config : Config
- layers : Array LayerAdapter
Per-layer adapter weights, indexed by layer number
Instances For
Full Adam optimizer state for the entire model
- layers : Array LayerAdapterAdamState
- step : Nat
Current optimizer step (for bias correction)
Instances For
Saved activations from forward pass, needed for backward. For each LoRA layer, we save the input x and intermediate h = A @ x.
- layers : Array (WebGPU.Buffer × WebGPU.Buffer × WebGPU.Buffer × WebGPU.Buffer)
Per-layer saved activations: (inputToQ, hQ, inputToV, hV)
Instances For
Equations
- One or more equations did not get rendered due to their size.