Method Stacking
About
Complex fine-tuning often requires hybrid approaches.
The d9d.peft.all package facilitates this by grouping multiple PEFT configurations into a single PeftStack.
Usage Example
Applying LoRA to attention layers while fully fine-tuning normalization layers.
import re
from d9d.peft.all import PeftStackConfig, peft_method_from_config
from d9d.peft.lora import LoRAConfig, LoRAParameters
from d9d.peft.full_tune import FullTuneConfig
from d9d.peft import inject_peft_and_freeze, merge_peft
# 1. Define your Strategy
config = PeftStackConfig(
methods=[
# Method A: LoRA on attention projections
LoRAConfig(
module_name_pattern=re.compile(r".*attention\..*_proj.*"),
params=LoRAParameters(r=8, alpha=16, dropout=0.05)
),
# Method B: Full Tune on LayerNorms
FullTuneConfig(
module_name_pattern=re.compile(r".*norm.*")
)
]
)
# 2. Create Factory
# This automatically creates a PeftStack containing the sub-methods
method = peft_method_from_config(config)
# 3. Inject
mapper = inject_peft_and_freeze(method, model)
# ... pass 'mapper' object to d9d's Trainer or manually load a model checkpoint ...
# ... train a model ...
# 4. Merge - for exporting a model
merge_peft(method, model)
d9d.peft.all
Package for composing multiple PEFT methods into a stack.
PeftStack
Bases: PeftMethod[PeftStackConfig]
A composite PEFT method that applies a list of methods sequentially.
Source code in d9d/peft/all/method.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
__init__(methods)
Constructs a PeftStack object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
methods
|
list[PeftMethod]
|
A list of instantiated PEFT methods to apply in order. |
required |
Source code in d9d/peft/all/method.py
19 20 21 22 23 24 25 26 27 | |
PeftStackConfig
Bases: BaseModel
Configuration for applying a stack of multiple PEFT methods sequentially.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['stack']
|
Discriminator field, always "stack". |
methods |
list[AnyPeftConfig]
|
A list of specific PEFT configurations (e.g., LoRA, FullTune) to apply in order. |
Source code in d9d/peft/all/config.py
9 10 11 12 13 14 15 16 17 18 19 20 | |
peft_method_from_config(config)
Factory function to instantiate the correct PeftMethod based on the configuration type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TConfig
|
A specific PEFT configuration object (e.g., LoRAConfig). |
required |
Returns:
| Type | Description |
|---|---|
PeftMethod[TConfig]
|
The corresponding method instance. |
Source code in d9d/peft/all/method.py
64 65 66 67 68 69 70 71 72 73 74 75 76 | |