LoRA Training Loop #
Teacher-forcing training loop for Alpaca-style instruction finetuning of BitNet models with LoRA adapters.
Training Algorithm #
For each example:
- Tokenize: instruction + input → prompt tokens, output → target tokens
- For each position t in the sequence: a. Forward: run model with LoRA to get logits b. If t >= promptLen: compute cross-entropy loss on target token c. Backward: compute LoRA gradients (dA, dB) from loss
- Adam update on all LoRA parameters
Simplification (v1) #
The backward pass only computes gradients for the LoRA parameters. The gradient signal flows through the residual stream, and LoRA gradients are computed using saved activations from the forward pass. This is standard practice in LoRA finetuning.
Training state maintained across steps
- adapter : LoRA.Adapter
LoRA adapter weights
- grads : LoRA.AdapterGrad
Gradient accumulators
- adamState : LoRA.AdapterAdamState
Adam optimizer state
- savedActs : LoRA.SavedActivations
Saved activations for backward
- dhBuf : WebGPU.Buffer
Temporary buffers
- dInputBuf : WebGPU.Buffer
- hBuf : WebGPU.Buffer
- yBufQ : WebGPU.Buffer
- yBufV : WebGPU.Buffer
- totalLoss : Float
Loss tracking
- numTokens : Nat
Instances For
Create training state with all necessary buffers
Equations
- One or more equations did not get rendered due to their size.
Instances For
Zero all gradient buffers (call before each training step)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Apply LoRA forward pass for a single attention layer. Called after BitLinear.forward has already written the base output to qBuf/vBuf. This adds the LoRA contribution: qBuf += scale * B_Q @ (A_Q @ inputBuf)
@param device GPU device @param layerAdapter LoRA weights for this layer @param scale alpha/rank scaling factor @param inputBuf Input to attention (after RMSNorm) [dim] @param qBuf Q projection output buffer [dim] (already has base output) @param vBuf V projection output buffer [kvDim] (already has base output) @param state Training state (for temp buffers and activation saving) @param layerIdx Layer index for saving activations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Apply LoRA backward pass for a single attention layer. Computes dA, dB for Q and V projections using saved activations.
@param device GPU device @param layerAdapter LoRA weights for this layer @param layerGrad Gradient accumulators for this layer @param scale alpha/rank scaling factor @param dQBuf Gradient w.r.t. Q output [dim] @param dVBuf Gradient w.r.t. V output [kvDim] @param state Training state (temp buffers, saved activations) @param layerIdx Layer index
Equations
- One or more equations did not get rendered due to their size.
Instances For
Run a single training step on one tokenized example.
This is the main entry point for training. It:
- Runs forward pass token-by-token with LoRA
- Computes cross-entropy loss on output tokens
- Runs backward pass to accumulate LoRA gradients
- Runs Adam optimizer to update LoRA weights
Note: This function is designed to be called with the model's
existing forward infrastructure. The caller is responsible for
orchestrating the per-token forward pass with the model and
calling applyLoRAForward at each attention layer.
@param device GPU device @param state Training state @param losses Array of per-token losses (populated during forward) @param config Optimizer config @return Updated training state
Equations
- One or more equations did not get rendered due to their size.
Instances For
Zero a GPU buffer via GPU kernel (safe to use inside batch)
Equations
- Hesper.Training.TrainLoop.zeroBuffer device buf numElements = Hesper.Optimizer.GradientClip.executeScale device buf numElements 0.0
Instances For
Read loss value from GPU buffer (safe, returns 0.0 on failure)
Equations
- Hesper.Training.TrainLoop.readLoss device lossBuf = Hesper.Training.SafeBuffer.safeReadF32 device lossBuf