Skip to content
class

PrefixEncoder

extends_PromptEncoder
PrefixEncoder(config: PrefixTuningConfig)

The prefix of prefix tuning: key and value vectors for every layer, one row per virtual token.

A row holds 2 * num_layers * token_dim numbers — the key and the value of each layer. With prefix_projection (and not inference_mode) the rows come from a token_dim-wide embedding through a two-layer MLP; otherwise embedding holds them directly.

Parameters

Sizes (num_virtual_tokens, num_layers, token_dim, encoder_hidden_size set) and the projection.

Attributes

embeddingnn.Embedding
The rows (or the projection's input).
transformnn.Module
The projection MLP, or nn.Identity.
prefix_projectionbool
Whether the config asked for the projection (the reference keeps this on the encoder, and load_prompt_embeddings reads it).

Examples

>>> import lucid
>>> from lucid.peft import PrefixTuningConfig
>>> from lucid.peft.tuners.prefix_tuning import PrefixEncoder
>>> config = PrefixTuningConfig(num_virtual_tokens=3, token_dim=8, num_layers=2,
...                             encoder_hidden_size=8)
>>> PrefixEncoder(config)(lucid.arange(3).long().reshape(1, 3)).shape
(1, 3, 32)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: PrefixTuningConfig)

Build the prefix: one row per virtual token holding its keys and values.

With config.prefix_projection the rows come from a smaller embedding through a two-layer MLP while training.

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 without the projection MLP (no prefix_projection, or inference_mode).

The embedding then holds the keys and values themselves.

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 encoder for config; word_embeddings is not read.

Instance methods

5
fn

forward

→Tensor
forward(prefix: Tensor)

Key/value rows of the virtual-token ids: (B, V) ids to (B, V, 2 * num_layers * token_dim).

fn

indices

→Tensorfrom _PromptEncoder
indices()

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

fn

load_prompt_embeddings

→None
load_prompt_embeddings(prompt_embeddings: Tensor)

Copy a flattened prefix (num_virtual_tokens, 2 * num_layers * token_dim) into the embedding.

Only a prefix without prefix_projection can take one: its embedding is the keys and values, where a projection's parameters (an embedding and an MLP) cannot be recovered from them.

Parameters

prompt_embeddingsTensor
The prefix an adapter file holds as prompt_embeddings.

Raises

ValueError
The config has prefix_projection, or the shape is not the embedding's.

Examples

>>> import lucid
>>> from lucid.peft import PrefixTuningConfig
>>> from lucid.peft.tuners.prefix_tuning import PrefixEncoder
>>> config = PrefixTuningConfig(num_virtual_tokens=3, token_dim=8, num_layers=2,
...                             encoder_hidden_size=8)
>>> encoder = PrefixEncoder(config)
>>> encoder.load_prompt_embeddings(lucid.ones(3, 32))
>>> encoder.embedding.weight.sum().item()
96.0
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.