LoRA Weight Initialization #
Creates and initializes LoRA adapter weights for BitNet finetuning.
Initialization Strategy #
- A matrix: Kaiming uniform initialization (preserves signal magnitude)
- B matrix: Zero initialization (LoRA output starts at zero, preserving base model behavior)
This ensures that at the start of training, the LoRA-augmented model produces exactly the same output as the base model.
Simple pseudo-random number generator (xoshiro128+) for weight initialization. Deterministic given a seed, which is important for reproducibility.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Generate Kaiming uniform initialization values. bound = sqrt(3 / fanIn) where fanIn = inDim for the A matrix. This preserves the variance of activations through the network.
Equations
- Hesper.LoRA.kaimingUniformBound fanIn = (3.0 / fanIn.toFloat).sqrt
Instances For
Create a ByteArray of FP32 values with Kaiming uniform initialization
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create a ByteArray of zeros (numElements FP32 values)
Equations
- Hesper.LoRA.generateZeroWeights numElements = { data := Array.replicate (numElements * 4) 0 }
Instances For
Create a single LoRA weight pair for one projection. A is Kaiming initialized, B is zero initialized.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create gradient buffers for a single LoRA weight pair (initialized to zero)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create Adam optimizer state for a single LoRA weight pair (initialized to zero)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create a full LoRA adapter for a BitNet model. Applies LoRA to Q and V attention projections in all transformer layers.
@param device GPU device @param config LoRA configuration @param numLayers Number of transformer layers (e.g., 30 for BitNet-2B) @param dim Model hidden dimension (e.g., 2560 for BitNet-2B) @param kvDim KV dimension for V projection (e.g., 640 for BitNet-2B with GQA 4:1) @param seed Random seed for weight initialization
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create gradient buffers for the full adapter
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create Adam optimizer state for the full adapter
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create saved activation buffers for backward pass
Equations
- One or more equations did not get rendered due to their size.