Documentation

Hesper.LoRA.Inference

LoRA-Aware Inference #

Extends BitNet inference to apply LoRA adapters during generation.

LoRA corrections are injected inside the attention layer, between BitLinear Q/V projections and RoPE. This ensures the LoRA contribution flows through the full attention computation (RoPE → KV cache → scores → softmax).

Uses Attention.forwardWithCacheLoRA and TransformerBlock.forwardWithCacheLoRA which inject LoRA at the correct point in the forward pass.

Temporary buffers needed for LoRA inference and training backward

Instances For

    Create LoRA inference state (inference only, no backward buffers)

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      def Hesper.LoRA.Inference.createLoRATrainingState (device : WebGPU.Device) (adapter : Adapter) (dim kvDim numHeads headDim maxSeqLen numLayers : Nat) :

      Create LoRA inference state with training backward buffers

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

        Single-token forward pass with LoRA. Uses TransformerBlock.forwardWithCacheLoRA which injects LoRA inside the attention layer (between BitLinear Q/V and RoPE).

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

          Combined forward + backward in a SINGLE GPU batch. All dispatches (forward 30 layers + loss + backward) are recorded into one command buffer and submitted as a single GPU submit. This eliminates ~20 GPU sync points per token compared to separate forward/backward calls.

          @param isOutputToken If true, compute loss + backward after forward. If false (prompt tokens), only forward is executed. @param targetBuf Pre-uploaded target token ID [1] u32 @param lossAccumBuf GPU-side loss accumulator (added to, not overwritten) @param dLogitsBuf Scratch buffer for dLogits [vocabSize] @param dHiddenBuf Scratch buffer for dHidden [dim] @param grads Gradient accumulators for LoRA weights @param startLayer First layer to compute LoRA backward for @param trainState Training state with temp buffers

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

            Generate text with LoRA adapter applied. Same interface as BitNetModel.generate but with LoRA corrections.

            Instances For