Skip to content
fn

get_peft_model

→PeftModel or PeftMixedModel
get_peft_model(model: nn.Module, peft_config: PeftConfig, adapter_name: str = 'default', mixed: bool = False, autocast_adapter_dtype: bool = True, revision: str | None = None, low_cpu_mem_usage: bool = False)

Adapt model with the method peft_config names and return it wrapped as a lucid.peft.PeftModel (or, with mixed=True, a lucid.peft.PeftMixedModel).

The model is changed in place: each module peft_config selects becomes an adapter layer of that method (LoRA, (IA)^3, LoHa, LoKr) around the original, and every parameter but the adapter's is frozen. peft_config is not changed — the wrapper keeps a copy — with two additions the reference makes too: a base_model_name_or_path read from the model's name_or_path, when it has one, and, for a classification or question-answering task_type, the model's classifier / score / qa_outputs head added to modules_to_save so that it trains.

The class returned follows peft_config.task_type: PeftModelForCausalLM for "CAUSAL_LM", PeftModelForSeq2SeqLM, PeftModelForSequenceClassification, PeftModelForTokenClassification, PeftModelForQuestionAnswering and PeftModelForFeatureExtraction for theirs (MODEL_TYPE_TO_PEFT_MODEL_MAPPING is the table), and a plain PeftModel when the config has no task. With mixed=True it is always a PeftMixedModel whatever the task, which holds the adapters of several methods at once.

Parameters

The model to adapt — any module; a zoo model keeps its generate and heads through the wrapper.
peft_configPeftConfig
What to adapt and how — a LoraConfig, IA3Config, LoHaConfig or LoKrConfig; its method picks the tuner.
adapter_namestr= "default"
Name of the adapter, the key in each of its tables (lora_A, ia3_l …).
mixedbool= False
Return a lucid.peft.PeftMixedModel, which holds adapters of different methods at once, each module's layers stacked; peft_config must be a method of PEFT_TYPE_TO_MIXED_MODEL_MAPPING.
autocast_adapter_dtypebool= True
Cast a half-precision base's adapter weights to float32. Always the case here; False raises NotImplementedError.
revisionstr= None
The revision of the base model, recorded in the config's revision (metadata) when given.
low_cpu_mem_usagebool= False
Build the adapter on the meta device. Not implemented: True raises NotImplementedError.

Returns

PeftModel or PeftMixedModel

The adapted model: a PeftMixedModel for mixed=True.

Raises

TypeError
peft_config is not an adapter config.
ValueError
No module matches target_modules, a match is not an nn.Linear, or, for mixed=True, the method cannot be mixed.
NotImplementedError
low_cpu_mem_usage or autocast_adapter_dtype is set to what this library cannot honour.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft import LoraConfig, get_peft_model
>>> net = nn.Sequential(nn.Linear(16, 16), nn.ReLU(), nn.Linear(16, 4))
>>> peft = get_peft_model(net, LoraConfig(r=4, lora_alpha=8, target_modules=["0", "2"]))
>>> peft.print_trainable_parameters()
trainable params: 208 || all params: 548 || trainable%: 37.9562
>>> peft(lucid.randn(2, 16)).shape
(2, 4)

Used by 1