Skip to content
class

CPTModel

extends_PromptLearningTuner
CPTModel(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

The causal language model to prompt.
adapter_namestr= "default"
Name of the adapter.
configCPTConfig
What to prompt with.

Raises

ValueError
The model already holds adapters.
NotImplementedError
The model cannot take inputs_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

1
prop

active_adapters

→list[str]from _PromptLearningTuner
active_adapters: list[str]

The adapter the model is prompted with, as a list.

Prompt learning applies one at a time.

Static methods

1
static

get_model_config

→dict of str to objectfrom _AdapterHost
get_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

The model to read the config of.

Returns

dict of str to object

The config's entries; a new dict, whose changes do not reach the model.

Raises

TypeError
model.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

9
fn

add_adapter

→Nonefrom _PromptLearningTuner
add_adapter(adapter_name: str, config: PeftConfig)

Add a prompt beside the ones held; it starts frozen, and set_adapter switches to it.

Raises

ValueError
The name is taken, or the config is another method's.
fn

delete_adapter

→Nonefrom _PromptLearningTuner
delete_adapter(adapter_name: str)

Remove a prompt and its modules_to_save copies; another takes over if it was in use.

fn

disable_adapter_layers

→Nonefrom _PromptLearningTuner
disable_adapter_layers()

Stop prompting.

The model runs as the base does, and its modules_to_save modules run their originals.

fn

enable_adapter_layers

→Nonefrom _PromptLearningTuner
enable_adapter_layers()

Prompt the model again, and run the trained modules_to_save copies.

fn

forward

→object
forward(args: object = (), kwargs: object = {})

Call the model; with labels, return its output with the CPT loss in place of its own.

fn

merge_and_unload

→nn.Modulefrom _PromptLearningTuner
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.

fn

set_adapter

→Nonefrom _PromptLearningTuner
set_adapter(adapter_name: str | list[str], inference_mode: bool = False)

Apply the named prompt — one at a time — and train it unless inference_mode.

fn

set_requires_grad

→Nonefrom _PromptLearningTuner
set_requires_grad(adapter_names: str | Sequence[str], requires_grad: bool = True)

Set requires_grad on the named adapters' encoders and modules_to_save copies.

fn

unload

→nn.Modulefrom _PromptLearningTuner
unload()

Take the hooks and the modules_to_save copies out and give every parameter its old requires_grad.