Skip to content
fn

set_peft_model_state_dict

→_IncompatibleKeys
set_peft_model_state_dict(model: nn.Module, peft_model_state_dict: Mapping[str, Tensor], adapter_name: str = 'default', ignore_mismatched_sizes: bool = False, low_cpu_mem_usage: bool = False)

Load an adapter's tensors, keyed as an adapter file keys them, into model.

Each key is mapped back to where the tensor lives in memory (the adapter name put back in) and copied into the existing parameter, which keeps its dtype, device and identity. Every key is checked before anything is copied: one that lands nowhere, or a shape that differs, raises with the model untouched — an adapter loaded into the wrong model must not pass for a fresh one. An adapter tensor the dict does not supply is reported in a warning and keeps its value.

Parameters

A PeftModel, or a model adapted by inject_adapter_in_model.
peft_model_state_dictMapping of str to Tensor
On-disk key to tensor, as get_peft_model_state_dict returns.
adapter_namestr= "default"
The adapter to load into.
ignore_mismatched_sizesbool= False
The reference skips a tensor whose shape differs from the model's. Here it raises; True raises NotImplementedError.
low_cpu_mem_usagebool= False
Load into an adapter built on the meta device. Not implemented: True raises NotImplementedError.

Returns

_IncompatibleKeys

What model.load_state_dict(..., strict=False) returns: the (missing_keys, unexpected_keys) named tuple, whose missing_keys lists the base parameters (no adapter file holds them) and whose unexpected_keys is empty.

Raises

ValueError
A key matches no tensor of the model, or a shape differs.
TypeError
A value is not a Tensor.
NotImplementedError
ignore_mismatched_sizes or low_cpu_mem_usage is True.

Examples

>>> import lucid.nn as nn
>>> from lucid.peft import (LoraConfig, get_peft_model,
...                         get_peft_model_state_dict, set_peft_model_state_dict)
>>> make = lambda: get_peft_model(nn.Sequential(nn.Linear(4, 2)),
...                               LoraConfig(r=2, target_modules=["0"], init_lora_weights=False))
>>> source, target = make(), make()
>>> _ = set_peft_model_state_dict(target, get_peft_model_state_dict(source))
>>> a = target.base_model.model[0].lora_A["default"].weight
>>> bool((a == source.base_model.model[0].lora_A["default"].weight).all().item())
True

Used by 2