class
AutoPeftModelForFeatureExtraction
extends
_BaseAutoPeftModelAutoPeftModelForFeatureExtraction(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
1from_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.PathLikeA local adapter directory. Hub ids are not resolved.
adapter_ namestr= "default"Name to give the adapter.
is_ trainablebool= FalseKeep the adapter trainable. By default it is frozen and the
model is in
eval() mode.Use this config for the adapter instead of the directory's. The
base model is still the one the directory's config names.
revisionstr= NoneA revision on the hub. There is no hub here: anything but
None
raises NotImplementedError.import_ allowlistlist of str= NoneOnly 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= NoneThe 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
PeftModelThe base model, adapted and loaded.
Raises
ValueErrorThe 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.TypeErrorThe
auto_mapping names something that is not a model class.FileNotFoundErrorThe directory lacks
adapter_config.json or the weights.NotImplementedErrorrevision 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'