AutoPeftModel
_BaseAutoPeftModelAutoPeftModel(args: object = (), kwargs: object = {})Load an adapter that has no task, with its base model.
AutoPeftModel.from_pretrained(directory) is for an adapter saved
with no task_type — a vision model's, for one, since there is no
image task — whose adapter_config.json carries an auto_mapping
naming the class of the base model. An adapter with a task is loaded by
the class of its task (AutoPeftModelForCausalLM and the others).
The mapping is data read from a file, and building the model imports
the package it names. Only packages on import_allowlist
(["lucid.models"] by default) are imported, and what is found there
must be a class with from_pretrained (else TypeError).
Examples
>>> from lucid.peft import AutoPeftModel
>>> AutoPeftModel()
Traceback (most recent call last):
...
OSError: AutoPeftModel is designed to be instantiated using the `AutoPeftModel.from_pretrained(pretrained_model_name_or_path)` method.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.PathLikeadapter_ namestr= "default"is_ trainablebool= Falseeval() mode.revisionstr= NoneNone
raises NotImplementedError.import_ allowlistlist of str= NoneAutoPeftModel: 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= Nonepretrained_model_name_or_path that holds
the adapter. (The reference takes it among **kwargs; it is named here.)convertbool or auto= "auto"PeftModel.from_pretrained.**kwargsobject= {}NotImplementedError.Returns
PeftModelThe base model, adapted and loaded.
Raises
ValueErrorAutoPeftModel) it has a task, or no usable
auto_mapping, or the mapping names a package outside the
allowlist.TypeErrorauto_mapping names something that is not a model class.FileNotFoundErroradapter_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'