Skip to content
class

PromptEmbedding

extends_PromptEncoder
PromptEmbedding(config: PromptTuningConfig, word_embeddings: nn.Module | None, tokenizer: _TextEncoder | None = None)

The prompt of prompt tuning: one learned embedding per virtual token.

Parameters

Sizes (num_virtual_tokens, num_transformer_submodules and token_dim set) and the initialisation.
word_embeddingsnn.Module
The model's token table, which "SAMPLE_VOCAB" and "TEXT" copy rows of.
tokenizerobject with encode(text) -> list[int]= None
Turns prompt_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
dunder

__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

2
prop

num_embeddings

→intfrom _PromptEncoder
num_embeddings: int

How many virtual tokens the encoder produces.

prop

stores_prompt

→bool
stores_prompt: bool

Always True: the embedding's rows are the prompt.

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

from _PromptEncoder
stored: Mapping[str, str]

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

1
cls

from_config

→Self
from_config(config: PromptLearningConfig, word_embeddings: nn.Module | None)

The embedding for config, started from word_embeddings.

Instance methods

4
fn

forward

→Tensor
forward(indices: Tensor)

Embeddings of the virtual-token ids: (*, ...) ids to (*, ..., token_dim).

fn

indices

→Tensorfrom _PromptEncoder
indices()

The virtual-token ids (1, num_embeddings) the encoder is called with.

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.