TorchLean API

NN.API.Adapters.LoRA

LoRA Adapters #

LoRA is a parameter-efficient adapter for linear layers: instead of updating the full weight matrix W, training learns two small matrices A and B and uses

W_eff = W + scale * (A * B).

The convention in this file matches the rest of TorchLean's linear specs, where row-batch inputs multiply a weight matrix on the right. If a base linear layer uses W : inDim × outDim, then:

Reference: Hu et al., “LoRA: Low-Rank Adaptation of Large Language Models”, https://arxiv.org/abs/2106.09685.

structure NN.API.Adapters.LoRA.Params (α : Type) (inDim rank outDim : ) :

LoRA adapter parameters for a linear weight matrix of shape inDim × outDim.

The usual LoRA scaling is alpha / rank; TorchLean keeps the final scalar as an explicit scale argument so callers can choose that convention, a schedule, or a test value.

Instances For
    def NN.API.Adapters.LoRA.delta {α : Type} [Add α] [Mul α] [Zero α] {inDim rank outDim : } (p : Params α inDim rank outDim) (scale : α) :

    The low-rank matrix scale * (A * B) added to a base linear weight.

    Instances For
      def NN.API.Adapters.LoRA.effectiveWeight {α : Type} [Add α] [Mul α] [Sub α] [Zero α] {inDim rank outDim : } (base : Spec.Tensor α (Spec.Shape.dim inDim (Spec.Shape.dim outDim Spec.Shape.scalar))) (p : Params α inDim rank outDim) (scale : α) :

      Apply a LoRA adapter to a base linear weight matrix.

      Instances For
        def NN.API.Adapters.LoRA.linear {α : Type} [Add α] [Mul α] [Sub α] [Zero α] {batch inDim rank outDim : } (x : Spec.Tensor α (Spec.Shape.dim batch (Spec.Shape.dim inDim Spec.Shape.scalar))) (base : Spec.Tensor α (Spec.Shape.dim inDim (Spec.Shape.dim outDim Spec.Shape.scalar))) (p : Params α inDim rank outDim) (scale : α) :

        Run a batched linear projection using the base weight plus the LoRA delta.

        Instances For