Skip to content
fn

set_base_model_state_dict

→_IncompatibleKeys
set_base_model_state_dict(model: PeftModel, state_dict: dict[str, Tensor], strict: bool = True)

Load a state dict of the base model's original key names into an adapted model.

The counterpart of lucid.peft.get_base_model_state_dict: the keys are the ones the model had before it was adapted (q.weight), and each is loaded into the place the adapted model keeps that tensor (q.base_layer.weight). The adapters' tensors are not touched.

Parameters

An adapted model.
state_dictdict of str to Tensor
The base model's tensors by their original names.
strictbool= True
Raise when a base tensor is missing from state_dict or state_dict holds a key the base model does not.

Returns

_IncompatibleKeys

The base keys state_dict lacks (missing_keys) and the keys of state_dict that match no base tensor (unexpected_keys).

Raises

TypeError
model is not a PeftModel.
RuntimeError
strict is true and the keys do not match; nothing is loaded.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft import (
...     LoraConfig, get_base_model_state_dict, get_peft_model, set_base_model_state_dict
... )
>>> peft = get_peft_model(nn.Sequential(nn.Linear(4, 4)), LoraConfig(r=2, target_modules=["0"]))
>>> weights = {k: lucid.zeros(*v.shape) for k, v in get_base_model_state_dict(peft).items()}
>>> set_base_model_state_dict(peft, weights)
_IncompatibleKeys(missing_keys=[], unexpected_keys=[])
>>> float(peft.get_base_model()[0].base_layer.weight.abs().sum())
0.0

Used by 1