Skip to content
fn

prompt_embeddings_from_past_key_values

→Tensor
prompt_embeddings_from_past_key_values(past_key_values: object, num_virtual_tokens: int)

Pack the first keys and values of a cache into the matrix an adapter file holds.

Row tt is the key and the value of token tt for layer 0, then for layer 1, and so on, each of the heads * head_dim numbers a layer keeps per token; that is the layout prefix tuning's and a Cartridge's prompt_embeddings have, so the matrix can be loaded into either.

Parameters

past_key_valuesobject
The cache of one sequence: a legacy tuple (or list) of (key, value) per layer, an object with to_legacy_cache() (lucid.utils.cache.DynamicCache), or an iterable of (key, value) tuples. Each tensor is (1, heads, tokens, head_dim).
num_virtual_tokensint
How many leading tokens to keep.

Returns

Tensor

(num_virtual_tokens, num_layers * 2 * heads * head_dim), in the cache's dtype and device.

Raises

TypeError
past_key_values is none of the three accepted kinds, or a layer of it is not a pair of tensors.
ValueError
The cache is empty, is of a batch (not one sequence), holds fewer than num_virtual_tokens tokens, or its layers disagree on heads, head size or length.

Examples

>>> import lucid
>>> from lucid.peft import prompt_embeddings_from_past_key_values
>>> layer = (lucid.zeros(1, 2, 5, 4), lucid.ones(1, 2, 5, 4))
>>> prompt_embeddings_from_past_key_values((layer, layer), num_virtual_tokens=3).shape
(3, 32)

Used by 1