set_peft_model_state_dict
→_IncompatibleKeysset_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
modelnn.ModulePeftModel, or a model adapted by inject_adapter_in_model.peft_ model_ state_ dictMapping of str to Tensorget_peft_model_state_dict returns.adapter_ namestr= "default"ignore_ mismatched_ sizesbool= FalseTrue raises NotImplementedError.low_ cpu_ mem_ usagebool= FalseTrue raises
NotImplementedError.Returns
_IncompatibleKeysWhat 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
ValueErrorTypeErrorNotImplementedErrorignore_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