Skip to content
module

CPT

3 members
lucid.peft.tuners.cpt

lucid.peft.tuners.cpt — context-aware prompt tuning: a prompt of in-context examples with a bounded learned offset.

CPT (Blau et al., 2024, arXiv:2410.17222) tunes a prompt that is itself a few in-context examples. Its tokens start as the embeddings of cpt_token_ids and each gets a trainable delta, held near zero by a projection after every step (templates and inputs may move, outputs may not), so the examples keep their meaning while the prompt adapts. The loss counts the output tokens of the prompt as well as of the batch, optionally weighting earlier examples less. Pick it for few-shot fine-tuning of a causal language model when the examples should stay in context; for a free prompt that starts anywhere, use prompt tuning.

Like the other prompt methods it adds no layer: the prompt is put in front of the input by hooks on the model's call, so model(...) and generate both carry it, and the adapter file holds the finished prompt as prompt_embeddings. CPTConfig and CPTModel are here and exported from lucid.peft; CPTEmbedding is the learned part. Importing the package registers the method under PeftType.CPT.

Paper

Blau, Tsachi, et al. "Context-Aware Prompt Tuning: Advancing In-Context Learning with Adversarial Methods." [arXiv:2410.17222](https://arxiv.org/abs/2410.17222), 2024.

Overview

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").

Classes