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
modelnn.Modulegenerate and heads through the wrapper.peft_ configPeftConfigLoraConfig, IA3Config, LoHaConfig or
LoKrConfig; its method picks the tuner.adapter_ namestr= "default"lora_A, ia3_l …).mixedbool= Falselucid.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= TrueFalse raises NotImplementedError.revisionstr= Nonerevision (metadata) when
given.low_ cpu_ mem_ usagebool= FalseTrue raises
NotImplementedError.Returns
PeftModel or PeftMixedModelThe adapted model: a PeftMixedModel for mixed=True.
Raises
TypeErrorpeft_config is not an adapter config.ValueErrortarget_modules, a match is not an
nn.Linear, or, for mixed=True, the method cannot be mixed.NotImplementedErrorlow_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)