Skip to content
class

AutoPeftModelForFeatureExtraction

extends_BaseAutoPeftModel
AutoPeftModelForFeatureExtraction(args: object = (), kwargs: object = {})

Load a feature encoder's adapter with its base model.

The base model is built by AutoModel, the zoo's loader for a backbone; the result is a lucid.peft.PeftModelForFeatureExtraction.

Examples

>>> from lucid.peft import AutoPeftModelForFeatureExtraction
>>> AutoPeftModelForFeatureExtraction._target_peft_class.__name__
'PeftModelForFeatureExtraction'

Used by 1

Class methods

1
cls

from_pretrained

→PeftModelfrom _BaseAutoPeftModel
from_pretrained(pretrained_model_name_or_path: str | os.PathLike[str], adapter_name: str = 'default', is_trainable: bool = False, config: PeftConfig | None = None, revision: str | None = None, import_allowlist: list[str] | None = None, subfolder: str | None = None, convert: ConvertMode = 'auto', kwargs: object = {})

Build the base model the adapter names and load the adapter onto it.

Reads adapter_config.json in pretrained_model_name_or_path, builds the base model from its base_model_name_or_path with the zoo's Auto class for the task, and returns PeftModel.from_pretrained(base, ...) — the task's wrapper class, the adapter loaded and frozen unless is_trainable.

Parameters

pretrained_model_name_or_pathstr or os.PathLike
A local adapter directory. Hub ids are not resolved.
adapter_namestr= "default"
Name to give the adapter.
is_trainablebool= False
Keep the adapter trainable. By default it is frozen and the model is in eval() mode.
configPeftConfig= None
Use this config for the adapter instead of the directory's. The base model is still the one the directory's config names.
revisionstr= None
A revision on the hub. There is no hub here: anything but None raises NotImplementedError.
import_allowlistlist of str= None
Only for AutoPeftModel: the packages whose classes the adapter's auto_mapping may name; ["lucid.models"] by default. The mapping is data from a file, and importing what it names would run that package's code.
subfolderstr= None
The directory under pretrained_model_name_or_path that holds the adapter. (The reference takes it among **kwargs; it is named here.)
convertbool or auto= "auto"
Read an adapter written for the reference library's model in the zoo's layout; see PeftModel.from_pretrained.
**kwargsobject= {}
The reference forwards these to its hub download and to the base model's loader. Any key raises NotImplementedError.

Returns

PeftModel

The base model, adapted and loaded.

Raises

ValueError
The adapter's task is not this class's, the adapter names no base model, or (AutoPeftModel) it has a task, or no usable auto_mapping, or the mapping names a package outside the allowlist.
TypeError
The auto_mapping names something that is not a model class.
FileNotFoundError
The directory lacks adapter_config.json or the weights.
NotImplementedError
revision or a key of kwargs is given.

Examples

>>> import tempfile
>>> from lucid.models import create_model
>>> from lucid.peft import AutoPeftModelForCausalLM, LoraConfig, get_peft_model
>>> small = dict(vocab_size=32, hidden_size=16, num_hidden_layers=1,
...              num_attention_heads=2, intermediate_size=32, max_position_embeddings=16)
>>> with tempfile.TemporaryDirectory() as base, tempfile.TemporaryDirectory() as adapter:
...     model = create_model("gpt2_small_lm", **small)
...     model.save_pretrained(base)
...     config = LoraConfig(r=2, target_modules=["c_attn"], task_type="CAUSAL_LM",
...                         base_model_name_or_path=base)
...     get_peft_model(model, config).save_pretrained(adapter)
...     loaded = AutoPeftModelForCausalLM.from_pretrained(adapter)
>>> type(loaded).__name__
'PeftModelForCausalLM'