Skip to content
data

CPTConfig

extendsPromptLearningConfig
CPTConfig(task_type: TaskType | str | None = None, peft_type: PeftType | str | None = None, auto_mapping: dict[str, str] | None = None, peft_version: str | None = None, base_model_name_or_path: str | None = None, revision: str | None = None, inference_mode: bool = False, num_virtual_tokens: int | None = None, token_dim: int | None = None, num_transformer_submodules: int | None = None, num_attention_heads: int | None = None, num_layers: int | None = None, modules_to_save: list[str] | None = None, cpt_token_ids: list[int] | None = None, cpt_mask: list[int] | None = None, cpt_tokens_type_mask: list[int] | None = None, opt_weighted_loss_type: Literal['none', 'decay'] = 'none', opt_loss_decay_factor: float = 1.0, opt_projection_epsilon: float = 0.1, opt_projection_format_epsilon: float = 0.1, tokenizer_name_or_path: str | None = None)

How lucid.peft.get_peft_model adapts a causal language model with context-aware prompt tuning.

CPT (Blau et al., 2024, arXiv:2410.17222) tunes a prompt made of in-context examples: the prompt's tokens start as the embeddings of cpt_token_ids and a trainable delta is added to each. Each token has a type (cpt_tokens_type_mask, read modulo 4: 1 an input template, 2 an input, 3 an output template, 0 or 4 an output or padding) that says which deltas train (types 1-3 only) and how far a delta may move: after every step its norm is projected back onto a ball of radius opt_projection_epsilon (inputs) or opt_projection_format_epsilon (templates), scaled by d/2048\sqrt{d/2048}. The loss covers the output tokens of the prompt and of the batch, optionally down-weighted for earlier examples (opt_weighted_loss_type="decay").

Field names, their order, defaults and adapter_config.json are the reference adapter library's. num_virtual_tokens is len(cpt_token_ids) whatever is passed.

Parameters

task_typeTaskType | str | None= None
As in lucid.peft.PeftConfig.
peft_typeTaskType | str | None= None
As in lucid.peft.PeftConfig.
auto_mappingTaskType | str | None= None
As in lucid.peft.PeftConfig.
peft_versionTaskType | str | None= None
As in lucid.peft.PeftConfig.
base_model_name_or_pathTaskType | str | None= None
As in lucid.peft.PeftConfig.
revisionTaskType | str | None= None
As in lucid.peft.PeftConfig.
inference_modeTaskType | str | None= None
As in lucid.peft.PeftConfig.
num_virtual_tokensint | None= None
As in lucid.peft.PromptLearningConfig.
token_dimint | None= None
As in lucid.peft.PromptLearningConfig.
num_transformer_submodulesint | None= None
As in lucid.peft.PromptLearningConfig.
num_attention_headsint | None= None
As in lucid.peft.PromptLearningConfig.
num_layersint | None= None
As in lucid.peft.PromptLearningConfig.
modules_to_saveint | None= None
As in lucid.peft.PromptLearningConfig.
cpt_token_idslist of int= None
The vocabulary ids whose embeddings the prompt starts from; [0] by default.
cpt_masklist of int= None
One entry per token (kept for the file's sake); ones by default.
cpt_tokens_type_masklist of int= None
One type per token, as above; ones by default.
opt_weighted_loss_type(none, decay)= "none"
"decay" weights the loss of each earlier output segment of an example by a further factor of opt_loss_decay_factor.
opt_loss_decay_factorfloat= 1.0
The factor of "decay".
opt_projection_epsilonfloat= 0.1
Radius of the ball a delta of an input token stays in.
opt_projection_format_epsilonfloat= 0.1
Radius for the deltas of template tokens.
tokenizer_name_or_pathstr= None
Kept for the file's sake; the prompt's tokens are given as ids.

Raises

TypeError
A field holds a value of the wrong type.
ValueError
task_type is not "CAUSAL_LM", or the three per-token lists differ in length.

Examples

>>> from lucid.peft import CPTConfig
>>> config = CPTConfig(cpt_token_ids=[5, 6, 7], cpt_tokens_type_mask=[1, 2, 4], task_type="CAUSAL_LM")
>>> config.num_virtual_tokens, config.cpt_mask
(3, [1, 1, 1])
>>> CPTConfig.from_dict(config.to_dict()) == config
True

Used by 4