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
class PeftStack(PeftMethod[PeftStackConfig]):
    """
    A composite PEFT method that applies a list of methods sequentially.
    """

    def __init__(self, methods: list[PeftMethod]):
        """
        Constructs a PeftStack object.

        Args:
            methods: A list of instantiated PEFT methods to apply in order.
        """

        self._methods = methods

    def inject(self, module: nn.Module) -> PeftInjectionResult:
        params_to_train = []
        state_mappers = []

        for method in self._methods:
            result = method.inject(module)
            params_to_train.extend(result.parameters_to_train)
            state_mappers.extend(result.load_state_mappers)

        return PeftInjectionResult(
            parameters_to_train=params_to_train,
            load_state_mappers=state_mappers
        )

    def merge(self, module: nn.Module):
        for method in self._methods[::-1]:
            method.merge(module)

    @classmethod
    def from_config(cls, config: PeftStackConfig) -> Self:
        methods = []

        for method in config.methods:
            methods.append(peft_method_from_config(method))

        return cls(methods)

__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
def __init__(self, methods: list[PeftMethod]):
    """
    Constructs a PeftStack object.

    Args:
        methods: A list of instantiated PEFT methods to apply in order.
    """

    self._methods = methods

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
class PeftStackConfig(BaseModel):
    """
    Configuration for applying a stack of multiple PEFT methods sequentially.

    Attributes:
        kind: Discriminator field, always "stack".
        methods: A list of specific PEFT configurations (e.g., LoRA, FullTune) to apply in order.
    """

    kind: Literal["stack"] = "stack"

    methods: list["AnyPeftConfig"]

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
def peft_method_from_config(config: TConfig) -> PeftMethod[TConfig]:
    """
    Factory function to instantiate the correct PeftMethod based on the configuration type.

    Args:
        config: A specific PEFT configuration object (e.g., LoRAConfig).

    Returns:
        The corresponding method instance.
    """

    method_cls = cast(type[PeftMethod[TConfig]], _PEFT_CONFIG_MAP[type(config)])
    return method_cls.from_config(config)