LoRA
About
The d9d.peft.lora package implements Low-Rank Adaptation. It wraps linear layers, both nn.Linear and d9d's GroupedLinear. The wrapper holds the original frozen layer (base) and two trainable low-rank layers (lora_A and lora_B). The low-rank layers of a GroupedLinear use the same grouped GEMM backend as the base layer. LoRA does not support nn.Linear layers with a bias.
State Mapping
Because the original layer moves to the base submodule, its state keys change. LoRA returns a ModelStateMapperRename for each wrapped layer. Chain these mappers after your checkpoint mapper, so standard checkpoints still load (see the example below).
Usage
API Reference
d9d.peft.lora
Low-rank adaptation (LoRA).
LoRA
Bases: PeftMethod[LoRAConfig]
PEFT method for Low-Rank Adaptation (LoRA).
inject replaces every nn.Linear or GroupedLinear layer whose name matches the configured pattern
with a LoRA wrapper. The original layer.weight moves to layer.base.weight. For each wrapped layer,
inject returns a ModelStateMapperRename, so standard checkpoints still load.
__init__(config)
Constructs the LoRA object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LoRAConfig
|
LoRA configuration with the module name pattern and hyperparameters. |
required |
LoRAConfig
Bases: BaseModel
Configuration for LoRA application.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['lora']
|
Discriminator field. Always |
module_name_pattern |
Pattern
|
Regular expression that must fully match the names of modules to wrap with LoRA. |
params |
LoRAParameters
|
Hyperparameters for the LoRA layers. |
LoRAGroupedLinear
Bases: Module
Wrapper that adds low-rank adaptation matrices A and B to a GroupedLinear layer, as used by MoE experts.
Attributes:
| Name | Type | Description |
|---|---|---|
lora_A |
The A matrix ( |
|
lora_B |
The B matrix ( |
|
base |
The original |
|
dropout |
Dropout applied to the input of the LoRA path. |
__init__(base_layer, params)
Constructs the LoRAGroupedLinear object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_layer
|
GroupedLinear
|
The original |
required |
params
|
LoRAParameters
|
LoRA hyperparameters. |
required |
forward(x, x_groups)
Computes the base output plus the scaled LoRA update for grouped inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tokens of all groups, with the tokens of each group stored together.
Shape: |
required |
x_groups
|
ExpertTokenGroups
|
How the tokens are split between groups. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The sum of the base and LoRA outputs. Shape: |
merge_with_base_()
Merges the LoRA weights into the base GroupedLinear layer in place.
Returns:
| Type | Description |
|---|---|
GroupedLinear
|
The base |
reset_parameters()
Resets the LoRA parameters.
lora_A gets a random initialization and lora_B is zeroed, so the LoRA update starts at zero.
LoRALinear
Bases: Module
Wrapper that adds low-rank adaptation matrices A and B to an nn.Linear layer.
Attributes:
| Name | Type | Description |
|---|---|---|
lora_A |
The A matrix ( |
|
lora_B |
The B matrix ( |
|
base |
The original |
|
dropout |
Dropout
|
Dropout applied to the input of the LoRA path. |
__init__(base_layer, params)
Constructs the LoRALinear object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_layer
|
Linear
|
The original |
required |
params
|
LoRAParameters
|
LoRA hyperparameters. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
forward(x)
merge_with_base_()
Merges the LoRA weights into the base linear layer in place.
Returns:
| Type | Description |
|---|---|
Linear
|
The base linear layer with updated weights. |
reset_parameters()
Resets the LoRA parameters.
lora_A gets a random initialization and lora_B is zeroed, so the LoRA update starts at zero.