Skip to content
class

ModulesToSaveWrapper

extends_ForwardsAttributes
ModulesToSaveWrapper(module_to_save: nn.Module, adapter_name: str)

Hold module beside a trained copy per adapter; the active adapter's copy runs.

Building one changes nothing about module — the tuner builds them while it is still planning, and a plan that fails must leave the model as it was. Which of the copies train is the tuner's freezing step. It answers the tuner's _AdapterSlot protocol, as a tuner layer does.

At most one adapter is active here: two copies of one module would have no rule for whose weights the forward pass uses. active_adapter is the active adapter that has a copy, or "" when none does, in which case the original runs.

get_peft_model(..., modules_to_save=[...]) puts one of these where each named module was, so isinstance(m, ModulesToSaveWrapper) finds them in an adapted model. The module as the base model had it is original_module, and the trained copies are in the modules_to_save dict, keyed by adapter.

Parameters

module_to_savenn.Module
The module trained in full (a classification head, an embedding).
adapter_namestr
Key of the first copy in modules_to_save.

Examples

>>> import lucid.nn as nn
>>> from lucid.peft import LoraConfig, ModulesToSaveWrapper, get_peft_model
>>> net = nn.Sequential(nn.Linear(4, 4), nn.Linear(4, 2))
>>> config = LoraConfig(r=2, target_modules=["0"], modules_to_save=["1"])
>>> _ = get_peft_model(net, config)
>>> head = net[1]
>>> isinstance(head, ModulesToSaveWrapper), head.active_adapter
(True, 'default')
>>> head.get_base_layer() is head.original_module, list(head.modules_to_save)
(True, ['default'])

Used by 8

Constructors

1
dunder

__init__

→None
__init__(module_to_save: nn.Module, adapter_name: str)

Hold module_to_save and a trainable copy of it for adapter_name.

Instance methods

8
fn

delete_adapter

→None
delete_adapter(adapter_name: str)

Drop adapter_name's copy; if it was the active one the original runs.

fn

enable_adapters

→None
enable_adapters(enabled: bool)

Run the trained copy (True) or the original module (False).

fn

extra_repr

→str
extra_repr()

The active adapter and whether adapters are disabled.

fn

forward

→object
forward(args: object = (), kwargs: object = {})

Run the active adapter's copy of the module.

The original runs when no adapter is active or adapters are disabled.

fn

get_base_layer

→nn.Module
get_base_layer()

Return the original module, the one the base model had.

fn

set_adapter

→None
set_adapter(adapter_names: str | list[str], inference_mode: bool = False)

Run the named adapter's copy, and train it unless inference_mode.

The names this module has no copy for are skipped: it runs the original while only such adapters are active.

Parameters

adapter_namesstr or list of str
The adapter, or adapters, to apply.
inference_modebool= False
Freeze the copy that runs as well.

Raises

ValueError
More than one of the names has a copy here.
fn

set_requires_grad

→None
set_requires_grad(adapter_names: str | Sequence[str], requires_grad: bool = True)

Set requires_grad on the copies of the named adapters.

fn

update

→None
update(adapter_name: str)

Give adapter_name a trained copy of the original, if it has none yet.