Documentation

Hesper.LoRA.Types

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:

References #

LoRA configuration for finetuning

  • rank : Nat

    Rank of the low-rank matrices (typical: 4, 8, 16)

  • alpha : Float

    Scaling factor: output is multiplied by alpha/rank

  • targetModules : List String

    Which attention projections to apply LoRA to

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

      Compute the LoRA scaling factor: alpha / rank

      Equations
      Instances For

        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 matrix: [rank, inDim] FP32, Kaiming initialized

        • 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

          Instances For

            Adam optimizer state for a single LoRA weight pair

            Instances For

              LoRA adapter for a single attention layer (Q and V projections)

              • loraQ : Weight

                LoRA weights for Q projection

              • loraV : Weight

                LoRA weights for V projection

              Instances For

                Gradient buffers for a single attention layer

                Instances For

                  Adam state for a single attention layer

                  Instances For

                    Full LoRA adapter for the entire model (all transformer layers)

                    Instances For

                      Full gradient state for the entire model

                      Instances For

                        Full Adam optimizer state for the entire model

                        Instances For

                          Saved activations from forward pass, needed for backward. For each LoRA layer, we save the input x and intermediate h = A @ x.

                          Instances For

                            Training configuration

                            • lr : Float

                              Learning rate

                            • beta1 : Float

                              Adam beta1

                            • beta2 : Float

                              Adam beta2

                            • eps : Float

                              Adam epsilon

                            • epochs : Nat

                              Number of training epochs

                            • logEvery : Nat

                              Log every N steps

                            • maxSeqLen : Nat

                              Max sequence length for training

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