Skip to content
fn

get_peft_model_state_dict

→dict of str to Tensor
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

A PeftModel, or a model adapted by inject_adapter_in_model.
state_dictMapping of str to Tensor= None
Read the values from here instead of model.state_dict() — keyed as that would be.
adapter_namestr= "default"
The adapter to extract.
unwrap_compiledbool= False
The 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 Tensor

On-disk key to tensor.

Raises

ValueError
model holds no adapter named adapter_name.
NotImplementedError
unwrap_compiled or save_embedding_layers is True.
RuntimeError
The adapter was merged or unloaded, so no adapter layer is left.
KeyError
state_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']

Used by 2