inject_adapter_in_model(peft_config: PeftConfig, model: nn.Module, adapter_name: str = 'default', low_cpu_mem_usage: bool = False, state_dict: dict[str, Tensor] | None = None)Put adapter layers into model in place and return it, without a wrapper.
For code that manages the model itself: the model is adapted and
frozen exactly as by lucid.peft.get_peft_model, but returned
bare, so its state dict keeps its own key names (plus base_layer /
lora_A / lora_B under each adapted layer).
lucid.peft.get_peft_model_state_dict reads the adapter out
of it. Note the argument order, the reference's: config first.
Parameters
peft_ configPeftConfigWhat to adapt and how — a
LoraConfig.modelnn.ModuleThe model to adapt.
adapter_ namestr= "default"Name of the adapter.
low_ cpu_ mem_ usagebool= FalseBuild the adapter on the meta device. Not implemented:
True raises
NotImplementedError.The reference matches targets against these keys instead of the model's modules
(for a model on the meta device). Not implemented: anything but
None raises
NotImplementedError.Returns
nn.Modulemodel itself, adapted.
Raises
NotImplementedErrorlow_cpu_mem_usage or state_dict holds a value the library cannot honour.Examples
>>> import lucid.nn as nn
>>> from lucid.peft import LoraConfig, inject_adapter_in_model
>>> net = nn.Sequential(nn.Linear(4, 4), nn.Linear(4, 2))
>>> inject_adapter_in_model(LoraConfig(r=2, target_modules=["1"]), net) is net
True
>>> sorted(net.state_dict())
['0.bias', '0.weight', '1.base_layer.bias', '1.base_layer.weight', '1.lora_A.default.weight', '1.lora_B.default.weight']