Documentation

Hesper.LoRA.Init

LoRA Weight Initialization #

Creates and initializes LoRA adapter weights for BitNet finetuning.

Initialization Strategy #

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 next random UInt64 and advance state

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For

        Generate a Float in [0, 1)

        Equations
        Instances For

          Generate a Float in [-bound, bound) using uniform distribution. Used for Kaiming uniform initialization.

          Equations
          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
            Instances For
              def Hesper.LoRA.generateKaimingWeights (numElements fanIn : Nat) (seed : UInt64) :

              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
                Instances For
                  def Hesper.LoRA.createWeight (device : WebGPU.Device) (inDim outDim rank : Nat) (seed : UInt64) :

                  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
                        def Hesper.LoRA.createAdapter (device : WebGPU.Device) (config : Config) (numLayers dim kvDim : Nat) (seed : UInt64 := 42) :

                        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.
                              Instances For