Skip to content
class

MultitaskPromptEmbedding

extendsPromptEmbedding
MultitaskPromptEmbedding(config: MultitaskPromptTuningConfig, word_embeddings: nn.Module | None, tokenizer: _TextEncoder | None = None)

The prompt of multitask prompt tuning: a shared prompt scaled by a low-rank factor pair per task.

For a row of task kk the prompt is embedding(ids) * (cols[k] @ rows[k]) — the shared (num_virtual_tokens, token_dim) prompt times the product of (num_virtual_tokens, num_ranks) and (num_ranks, token_dim) factors.

Parameters

Sizes (num_virtual_tokens, num_transformer_submodules and token_dim set), the tasks and ranks, and the initialisation.
word_embeddingsnn.Module
The model's token table, which "TEXT" copies rows of.
tokenizerobject with encode(text) -> list[int]= None
As for lucid.peft.PromptEmbedding.

Attributes

embeddingnn.Embedding
The shared prompt.
prefix_task_colsParameter
(num_tasks, num_virtual_tokens * num_transformer_submodules, num_ranks).
prefix_task_rowsParameter
(num_tasks, num_ranks, token_dim).

Raises

ValueError
A source initialisation without prompt_tuning_init_state_dict_path, a source file that is not .safetensors, lacks a tensor, or whose shapes do not fit this adapter.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft import MultitaskPromptEmbedding, MultitaskPromptTuningConfig
>>> config = MultitaskPromptTuningConfig(
...     num_virtual_tokens=3, token_dim=8, num_transformer_submodules=1, num_tasks=2
... )
>>> prompt = MultitaskPromptEmbedding(config, nn.Embedding(10, 8))
>>> prompt(lucid.arange(3).long().reshape(1, 3), lucid.tensor([1])).shape
(1, 3, 8)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(config: MultitaskPromptTuningConfig, word_embeddings: nn.Module | None, tokenizer: _TextEncoder | None = None)

Build the shared prompt and each task's low-rank pair.

The pair is uku_k and vkv_k for each of config.num_tasks tasks; the shared prompt starts as PromptEmbedding's does, or from a source-task prompt when config.prompt_tuning_init asks.

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

2
fn

forward

→Tensor
forward(indices: Tensor, task_ids: Tensor | None = None)

The prompt of each row's task: (B, rows) ids and (B,) task ids to (B, rows, token_dim).

Raises

ValueError
task_ids is missing, or is not one integer per row.
IndexError
A task id is not in [0, num_tasks).
fn

prompts

→Tensor
prompts(batch: int, tasks: Tensor | None)

The prompt of each of batch rows: the shared prompt scaled by that row's task.

Parameters

batchint
Number of rows.
tasksTensor or None
The task id of each row, (batch,).

Returns

Tensor

(batch, num_virtual_tokens, token_dim).