CPTModel
_PromptLearningTunerCPTModel(model: nn.Module, peft_config: PeftConfig, adapter_name: str = 'default', low_cpu_mem_usage: bool = False, state_dict: dict[str, Tensor] | None = None)The tuner of context-aware prompt tuning: a CPTEmbedding per adapter, in front of the input.
The prompt goes in front of the input embeddings as in prompt tuning. A call that passes
labels is the one that differs: the model is run without them, and the loss is CPT's — the
next-token cross-entropy over the output tokens of the prompt followed by the input, as the
type mask marks them, averaged and optionally decayed (CPTConfig). input_type_mask
((batch, tokens) integers, all outputs when omitted) gives the types of the input's
tokens; give labels and input_type_mask as keywords. A call without labels, and
generate, are prompt tuning's.
Parameters
Raises
ValueErrorNotImplementedErrorinputs_embeds.Examples
>>> import lucid
>>> from lucid.models import create_model
>>> from lucid.peft import CPTConfig, CPTModel
>>> lm = create_model("gpt2_small_lm", vocab_size=32, hidden_size=16, num_hidden_layers=1,
... num_attention_heads=2, intermediate_size=32, max_position_embeddings=16)
>>> tuned = CPTModel(lm, CPTConfig(cpt_token_ids=[3, 4, 5], cpt_tokens_type_mask=[1, 2, 4],
... task_type="CAUSAL_LM"))
>>> ids = lucid.randint(0, 32, (2, 5))
>>> tuned(ids, labels=ids).logits.shape # the prompt's 3 positions come first
(2, 8, 32)Used by 1
Properties
1Static methods
1get_model_config
→dict of str to objectfrom _AdapterHostget_model_config(model: nn.Module)model's config as a dict, whatever it is held as.
A config with a to_dict method is converted by it, a dataclass by asdict, a
mapping is copied. A model with no config (or one that is None) has the
reference's stand-in, {"model_type": "custom"}, which is what the default target
modules and the prompt geometry read for a model of no known type.
Parameters
modelnn.ModuleReturns
dict of str to objectThe config's entries; a new dict, whose changes do not reach the model.
Raises
TypeErrormodel.config is none of a mapping, a dataclass or an object with to_dict.Examples
>>> import lucid.nn as nn
>>> from lucid.peft import LoraModel
>>> LoraModel.get_model_config(nn.Linear(2, 2))
{'model_type': 'custom'}Instance methods
9add_adapter
→Nonefrom _PromptLearningTuneradd_adapter(adapter_name: str, config: PeftConfig)Add a prompt beside the ones held; it starts frozen, and set_adapter switches to it.
Raises
ValueErrorRemove a prompt and its modules_to_save copies; another takes over if it was in use.
Stop prompting.
The model runs as the base does, and its modules_to_save modules run their originals.
Prompt the model again, and run the trained modules_to_save copies.
Call the model; with labels, return its output with the CPT loss in place of its own.
merge_and_unload(progressbar: bool = False, safe_merge: bool = False, adapter_names: list[str] | None = None)Refused: a prompt has no weight to fold into the model.
set_adapter
→Nonefrom _PromptLearningTunerset_adapter(adapter_name: str | list[str], inference_mode: bool = False)Apply the named prompt — one at a time — and train it unless inference_mode.
set_requires_grad
→Nonefrom _PromptLearningTunerset_requires_grad(adapter_names: str | Sequence[str], requires_grad: bool = True)Set requires_grad on the named adapters' encoders and modules_to_save copies.
Take the hooks and the modules_to_save copies out and give every parameter its old requires_grad.