load_peft_weights(model_id: str, device: str | None = None, key_mapping: dict[str, str] | None = None, hf_hub_download_kwargs: object = {})Read the tensors of a saved adapter, by their file keys.
What PeftModel.load_adapter reads before it copies the tensors into a model, for
code that wants the tensors alone: the keys are the adapter file's
(base_model.model.q.lora_A.weight), with no adapter name in them.
Parameters
model_ idstrThe directory that holds
adapter_model.safetensors (the one save_pretrained
wrote).devicestr= NoneThe device to read the tensors onto; the default device when
None.key_ mappingdict of str to str= NoneRenames applied to the keys: the
base_model.model. prefix is set aside, the
patterns are tried in order with re.sub on the rest of the key (the first that
matches wins), and the prefix goes back.**hf_ hub_ download_ kwargsobject= {}The reference forwards these to its hub download.
subfolder names a directory
under model_id; any other key raises NotImplementedError, as there is no hub.Returns
dict of str to TensorThe adapter's tensors.
Raises
FileNotFoundErrorThe directory holds no
adapter_model.safetensors.NotImplementedErrorThe directory holds a pickle (
adapter_model.bin) and no safetensors file, or a
hub download option was passed.ValueErrorkey_mapping is given and a key does not start with base_model..Examples
>>> import tempfile
>>> import lucid.nn as nn
>>> from lucid.peft import LoraConfig, get_peft_model, load_peft_weights
>>> peft = get_peft_model(nn.Sequential(nn.Linear(4, 4)), LoraConfig(r=2, target_modules=["0"]))
>>> with tempfile.TemporaryDirectory() as directory:
... peft.save_pretrained(directory)
... weights = load_peft_weights(directory)
>>> sorted(weights)
['base_model.model.0.lora_A.weight', 'base_model.model.0.lora_B.weight']