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
configPrefixTuningConfignum_virtual_tokens, num_layers, token_dim,
encoder_hidden_size set) and the projection.Attributes
embeddingnn.Embeddingtransformnn.Modulenn.Identity.prefix_ projectionboolload_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
1Properties
2Attributes
2Whether the prompt depends on the task each batch row belongs to (task_ids).
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
1Instance methods
5Key/value rows of the virtual-token ids: (B, V) ids to (B, V, 2 * num_layers * token_dim).
The virtual-token ids (1, num_embeddings) the encoder is called with.
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_ embeddingsTensorprompt_embeddings.Raises
ValueErrorprefix_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.0The 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.
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.