get_peft_model_state_dict(model: nn.Module, state_dict: Mapping[str, Tensor] | None = None, adapter_name: str = 'default', unwrap_compiled: bool = False, save_embedding_layers: bool | Literal['auto'] = 'auto')Return the adapter's tensors under the keys an adapter file uses.
That is lora_A / lora_B of every adapted layer, the trained
copy of every modules_to_save module and the biases bias
trains — keyed as the reference writes them, the adapter name taken
out. Called on a PeftModel the keys start base_model.model.,
which is the reference's file layout.
Parameters
modelnn.ModuleA
PeftModel, or a model adapted by inject_adapter_in_model.Read the values from here instead of
model.state_dict() —
keyed as that would be.adapter_ namestr= "default"The adapter to extract.
unwrap_ compiledbool= FalseThe reference looks through a compiled model's wrapper. A compiled Lucid callable
is not a module to look through:
True raises NotImplementedError.save_ embedding_ layersbool or auto= "auto"The reference adds the input and output embeddings when the vocabulary was resized.
The adapter alone is returned here (with its
modules_to_save), which is what
"auto" and False ask for; True raises NotImplementedError.Returns
dict of str to TensorOn-disk key to tensor.
Raises
ValueErrormodel holds no adapter named adapter_name.NotImplementedErrorunwrap_compiled or save_embedding_layers is True.RuntimeErrorThe adapter was merged or unloaded, so no adapter layer is left.
KeyErrorstate_dict lacks one of the adapter's tensors.Examples
>>> import lucid.nn as nn
>>> from lucid.peft import LoraConfig, get_peft_model, get_peft_model_state_dict
>>> net = nn.Sequential(nn.Linear(4, 4), nn.Linear(4, 2))
>>> peft = get_peft_model(net, LoraConfig(r=2, target_modules=["0"]))
>>> sorted(get_peft_model_state_dict(peft))
['base_model.model.0.lora_A.weight', 'base_model.model.0.lora_B.weight']