Low-Rank Adaptation (LoRA)
About
The d9d.peft.lora package implements Low-Rank Adaptation. It works by wrapping existing Linear layers (both standard nn.Linear and d9d's GroupedLinear) with a container that holds the original frozen layer (base) and two low-rank trainable matrices (lora_A and lora_B).
Because the original layer is moved to a submodule (base), the state keys change. The LoRA method automatically generates a ModelStateMapperRename to handle this transparently during checkpoint loading.
Usage Example
d9d.peft.lora
Package for Low-Rank Adaptation (LoRA) implementation.
LoRA
Bases: PeftMethod[LoRAConfig]
Implements the Low-Rank Adaptation (LoRA) injection strategy.
It scans the module structure for nn.Linear or GroupedLinear layers matching
the configured name pattern. Matched layers are replaced with LoRA wrappers.
It also generates ModelStateMapperRename objects. Since the original weight
layer.weight is now at layer.base.weight inside the wrapper, the mapper
ensures that loading a standard checkpoint still works by redirecting the key.
__init__(config)
Constructs a LoRA method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LoRAConfig
|
LoRA configuration containing patterns and hyperparameters. |
required |
LoRAConfig
Bases: BaseModel
Configuration for LoRA application.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['lora']
|
Discriminator field, always "lora". |
module_name_pattern |
Pattern
|
Regular expression matching module names to wrap with LoRA. |
params |
LoRAParameters
|
Hyperparameters for the LoRA layers. |
LoRAGroupedLinear
Bases: Module
A LoRA wrapper around a GroupedLinear layer (commonly used in MoE or grouped query attention).
Attributes:
| Name | Type | Description |
|---|---|---|
lora_A |
The A matrix (grouped linear). |
|
lora_B |
The B matrix (grouped linear). |
|
base |
The original base GroupedLinear layer. |
|
dropout |
Scaling dropout layer. |
__init__(base_layer, params)
Constructs a LoRAGroupedLinear layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_layer
|
GroupedLinear
|
The original GroupedLinear layer to wrap. |
required |
params
|
LoRAParameters
|
LoRA hyperparameters. |
required |
forward(x, x_groups)
merge_with_base_()
Collapse the LoRA weights into the base GroupedLinear layer.
Returns:
| Type | Description |
|---|---|
GroupedLinear
|
The modified GroupedLinear layer. |
reset_parameters()
Resets LoRA parameters. A is random, B is zeroed.
LoRALinear
Bases: Module
A LoRA wrapper around a standard PyTorch Linear layer.
Wraps a base linear layer and adds low-rank adaptation matrices A and B.
Attributes:
| Name | Type | Description |
|---|---|---|
lora_A |
The A matrix (in_features -> r). |
|
lora_B |
The B matrix (r -> out_features). |
|
base |
The original base Linear layer. |
|
dropout |
Dropout
|
Scaling dropout layer. |
__init__(base_layer, params)
Constructs a LoRALinear layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_layer
|
Linear
|
The original Linear layer to wrap. |
required |
params
|
LoRAParameters
|
LoRA hyperparameters (r, alpha, dropout). |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the base layer has a bias (currently unsupported). |
forward(x)
merge_with_base_()
Collapse the LoRA weights into the base linear layer.
Returns:
| Type | Description |
|---|---|
Linear
|
The modified base linear layer with updated weights. |
reset_parameters()
Resets LoRA parameters. A is random, B is zeroed.