fn
set_base_model_state_dict
→_IncompatibleKeysset_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
Returns
_IncompatibleKeysThe base keys state_dict lacks (missing_keys) and the keys of state_dict
that match no base tensor (unexpected_keys).
Raises
TypeErrormodel is not a PeftModel.RuntimeErrorstrict 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