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 is the key and the value of token 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_ valuesobjectThe 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_ tokensintHow many leading tokens to keep.
Returns
Tensor(num_virtual_tokens, num_layers * 2 * heads * head_dim), in the cache's dtype and device.
Raises
TypeErrorpast_key_values is none of the three accepted kinds, or a layer of it is not a pair of tensors.ValueErrorThe 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)