PromptEmbedding
_PromptEncoderPromptEmbedding(config: PromptTuningConfig, word_embeddings: nn.Module | None, tokenizer: _TextEncoder | None = None)The prompt of prompt tuning: one learned embedding per virtual token.
Parameters
configPromptTuningConfignum_virtual_tokens, num_transformer_submodules and
token_dim set) and the initialisation.word_ embeddingsnn.Module"SAMPLE_VOCAB" and "TEXT"
copy rows of.tokenizerobject with encode(text) -> list[int]= Noneprompt_tuning_init_text into ids for "TEXT". Left out, the
tokenizer config.tokenizer_name_or_path names is resolved (a registered
name, built from config.tokenizer_kwargs, or a directory).Attributes
embeddingnn.Embedding(num_virtual_tokens * num_transformer_submodules, token_dim).Raises
ValueError"TEXT" initialisation whose tokenizer cannot be resolved or tokenizes the text
to nothing.Examples
>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft import PromptTuningConfig
>>> from lucid.peft.tuners.prompt_tuning import PromptEmbedding
>>> config = PromptTuningConfig(num_virtual_tokens=3, token_dim=8, num_transformer_submodules=1)
>>> PromptEmbedding(config, nn.Embedding(10, 8))(lucid.arange(3).long()).shape
(3, 8)Used by 2
Constructors
1__init__
→None__init__(config: PromptTuningConfig, word_embeddings: nn.Module | None, tokenizer: _TextEncoder | None = None)Build the virtual-token embedding.
Its rows start from text, from sampled vocabulary rows or at random, as
config.prompt_tuning_init says; word_embeddings is the model's token table and
tokenizer turns prompt_tuning_init_text into ids.
Properties
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
4Embeddings of the virtual-token ids: (*, ...) ids to (*, ..., token_dim).
The virtual-token ids (1, num_embeddings) the encoder is called with.
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.
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.