Skip to content
class

PromptEncoder

extends_PromptEncoder
PromptEncoder(config: PromptEncoderConfig)

The prompt of p-tuning: virtual-token embeddings through an MLP, or a bidirectional LSTM and an MLP.

Built for a trainable adapter it holds the whole encoder; built with inference_mode it holds embedding alone, whose rows are then the prompt (as an adapter file stores it).

Parameters

Sizes (num_virtual_tokens, num_transformer_submodules, token_dim, encoder_hidden_size set) and the encoder's type.

Attributes

embeddingnn.Embedding
(num_virtual_tokens * num_transformer_submodules, token_dim).
mlp_headnn.Module
The MLP, or nn.Identity.
lstm_headnn.LSTM or None
The LSTM of the "LSTM" type.

Examples

>>> import lucid
>>> from lucid.peft import PromptEncoderConfig
>>> from lucid.peft.tuners.p_tuning import PromptEncoder
>>> config = PromptEncoderConfig(num_virtual_tokens=3, token_dim=8, encoder_hidden_size=16,
...                              num_transformer_submodules=1)
>>> PromptEncoder(config)(lucid.arange(3).long().reshape(1, 3)).shape
(1, 3, 8)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(config: PromptEncoderConfig)

Build the virtual-token embedding and the head that maps it to the prompt.

The head is the MLP, or the bidirectional LSTM and an MLP, that config.encoder_reparameterization_type names; an inference_mode encoder has no head.

Properties

2
prop

num_embeddings

→intfrom _PromptEncoder
num_embeddings: int

How many virtual tokens the encoder produces.

prop

stores_prompt

→bool
stores_prompt: bool

True when no head maps the embedding (an inference_mode encoder).

Its rows are then the finished 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 encoder for config.

word_embeddings is not read, the encoder makes its own.

Instance methods

4
fn

forward

→Tensor
forward(indices: Tensor)

Prompt rows of the virtual-token ids: (B, total) ids to (B, total, 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.