Skip to content
class

CartridgeEncoder

extends_PromptEncoder
CartridgeEncoder(config: CartridgeConfig)

A cache of keys and values that is trained, as a matrix of rows (one per virtual token) of every layer's key and value.

A row holds 2 * num_layers * token_dim numbers laid out as prefix tuning's are, the key and the value of each layer one after another, so the cache goes in front of the model's own like a prefix's. The first num_frozen_tokens rows are stored as a parameter that is never trained (the Cartridges paper recommends freezing the first token as an attention sink), the rest as the trained one. Both start as a standard normal draw, which the reference's embedding table also starts as; load_prompt_embeddings replaces them, from the first keys and values of a document the model has read (initialize_kv_prefix_from_text), say.

Parameters

num_virtual_tokens, num_layers, token_dim (all set; the tuner fills them in from the model), num_frozen_tokens and inference_mode.

Attributes

frozen_embeddingParameter or None
(num_frozen_tokens, hidden) and never trained; None when no token is frozen.
trainable_embeddingParameter
(num_virtual_tokens - num_frozen_tokens, hidden), trained unless inference_mode.

Raises

ValueError
num_frozen_tokens is outside [0, num_virtual_tokens].

Examples

>>> import lucid
>>> from lucid.peft import CartridgeConfig, CartridgeEncoder
>>> config = CartridgeConfig(num_virtual_tokens=5, num_frozen_tokens=2, num_layers=3, token_dim=4)
>>> encoder = CartridgeEncoder(config)
>>> tuple(encoder.frozen_embedding.shape), tuple(encoder.trainable_embedding.shape)
((2, 24), (3, 24))
>>> encoder(lucid.zeros(2, 5).long()).shape
(2, 5, 24)
>>> encoder.frozen_embedding.requires_grad, encoder.trainable_embedding.requires_grad
(False, True)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: CartridgeConfig)

Build the frozen and the trained rows of a cache config sizes, and draw them.

Properties

3
prop

num_embeddings

→int
num_embeddings: int

How many virtual tokens the cache holds: the frozen rows and the trained ones.

prop

stores_prompt

→bool
stores_prompt: bool

True: the parameters are the cache itself, nothing maps them.

prop

weight

→Tensor
weight: Tensor

The whole cache (num_virtual_tokens, hidden): the frozen rows, then the trained ones.

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

stored: Mapping[str, str]

The encoder's own tensors by the file's key: the trained rows are the file's prompt_embeddings (the frozen rows join them on the way out).

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

6
fn

forward

→Tensor
forward(prefix_tokens: Tensor)

The cache for each row of a batch: (batch, ...) ids to (batch, num_virtual_tokens, hidden).

The ids are ignored; they are there so a cache is called as every prompt is.

fn

indices

→Tensor
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 whole cache (num_virtual_tokens, hidden) in, the first rows into the frozen ones.

Parameters

prompt_embeddingsTensor
The flattened cache an adapter file holds.

Raises

ValueError
The shape is not (num_virtual_tokens, hidden).
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.

fn

reset_parameters

→None
reset_parameters()

Draw every row again from a standard normal.

In-place ops

1
fn

requires_grad_

→Self
requires_grad_(requires_grad: bool = True)

Train (or freeze) the trained rows; the frozen rows are never trained.

The tuner switches whole encoders on and off with this call, and a plain Module.requires_grad_ would make the frozen tokens trainable with the rest.