Skip to content
class

CPTEmbedding

extends_PromptEncoder
CPTEmbedding(config: CPTConfig, word_embeddings: nn.Module | None)

The prompt of CPT: frozen token embeddings plus a trainable delta kept near zero.

Built to train it holds embedding (the model's embeddings of cpt_token_ids, frozen) and delta_embedding (zeros). Each call first projects every delta row onto the ball of radius ϵt\epsilon_t — the input or template epsilon of its token type, times d/2048\sqrt{d/2048} — writing the result back into the parameter (the optimiser steps from the projected value, as the reference's does), then returns embedding + delta. Only the deltas of types 1 to 3 (templates and inputs) receive a gradient. Built for inference_mode it holds embedding alone, whose rows are the finished prompt.

Parameters

configCPTConfig
The token ids and types, the epsilons, and token_dim set.
word_embeddingsnn.Module
The model's token table; its rows for cpt_token_ids start the prompt.

Attributes

embeddingnn.Embedding
(num_virtual_tokens, token_dim), never trained.
delta_embeddingnn.Embedding or None
The trainable offset; None for an inference adapter.

Raises

TypeError
A trainable prompt is built from a table that is not an nn.Embedding.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft import CPTConfig
>>> from lucid.peft.tuners.cpt import CPTEmbedding
>>> config = CPTConfig(cpt_token_ids=[1, 2], cpt_tokens_type_mask=[2, 4], token_dim=8,
...                    task_type="CAUSAL_LM")
>>> prompt = CPTEmbedding(config, nn.Embedding(5, 8))
>>> prompt(lucid.arange(2).long()).shape
(2, 8)
>>> [p.requires_grad for p in prompt.parameters()]
[False, True]

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: CPTConfig, word_embeddings: nn.Module | None)

Build the frozen embeddings (from word_embeddings) and, to train, the zero delta.

Properties

2
prop

num_embeddings

→intfrom _PromptEncoder
num_embeddings: int

How many virtual tokens the encoder produces.

prop

stores_prompt

→bool
stores_prompt: bool

True for an inference adapter, whose embedding rows are the prompt.

Attributes

2
attr

per_task

from _PromptEncoder
per_task: bool

Whether the prompt depends on the task each batch row belongs to (task_ids).

attr

stored

from _PromptEncoder
stored: Mapping[str, str]

The tensors an adapter file holds, by the encoder's own key and the file's: the prompt always, and whatever else the method keeps beside it.

Class methods

1
cls

from_config

→Self
from_config(config: PromptLearningConfig, word_embeddings: nn.Module | None)

The prompt for config, started from word_embeddings.

Instance methods

5
fn

forward

→Tensor
forward(indices: Tensor)

The prompt rows embedding + delta of the virtual-token ids indices.

fn

indices

→Tensorfrom _PromptEncoder
indices()

The virtual-token ids (1, num_embeddings) the encoder is called with.

fn

project

→None
project()

Pull every delta row back inside its ball and write that into the parameter.

fn

prompt_embeddings

→Tensorfrom _PromptEncoder
prompt_embeddings()

The prompt as an adapter file stores it: (rows, width), one row per virtual token.

A per-task encoder's is the shared prompt its tasks scale, which is what the file holds.

fn

prompts

→Tensorfrom _PromptEncoder
prompts(batch: int, tasks: Tensor | None)

The prompt of each of batch rows: (batch, num_embeddings, width).

tasks is the task of each row, which a per_task encoder needs and the others do not read.

In-place ops

1
fn

requires_grad_

→Self
requires_grad_(requires_grad: bool = True)

Train (or freeze) the delta; the embeddings it offsets stay frozen.