PEFT documentation

プロンプトチューニング

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.21.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

プロンプトチューニング

Only train and store a significantly smaller set of task-specific prompt parameters (image source).

Prompt tuningは、埋め込み空間内の学習可能なベクトルで構成されるタスク固有の仮想プロンプトを入力に追加します。仮想トークンのパラメータは、凍結されている事前学習モデルのパラメータとは独立して更新されます。

論文の抄録は以下の通りです:

In this work, we explore “prompt tuning”, a simple yet effective mechanism for learning “soft prompts” to condition frozen language models to perform specific downstream tasks. Unlike the discrete text prompts used by GPT-3, soft prompts are learned through backpropagation and can be tuned to incorporate signal from any number of labeled examples. Our end-to-end learned approach outperforms GPT-3’s “few-shot” learning by a large margin. More remarkably, through ablations on model size using T5, we show that prompt tuning becomes more competitive with scale: as models exceed billions of parameters, our method “closes the gap” and matches the strong performance of model tuning (where all model weights are tuned). This finding is especially relevant in that large models are costly to share and serve, and the ability to reuse one frozen model for multiple downstream tasks can ease this burden. Our method can be seen as a simplification of the recently proposed “prefix tuning” of Li and Liang (2021), and we provide a comparison to this and other similar approaches. Finally, we show that conditioning a frozen model with soft prompts confers benefits in robustness to domain transfer, as compared to full model tuning.

prefix tuningとは異なり、仮想トークンは最初の層の入力のみを受け取ります。

使用方法

2つの決定事項があります。モデルの入力に追加する仮想トークンの数(num_virtual_tokens)です。これにより、学習可能なパラメータの数が決まります。もう1つは、これらのトークンの初期化方法です。

タスクタイプ、モデルの学習に使用する初期のプロンプトチューニングテキスト、追加して学習する仮想トークンの数、およびトークナイザーを指定して、PromptTuningConfig を作成します。

from peft import PromptTuningConfig, PromptTuningInit, get_peft_model

prompt_tuning_init_text = "Classify if the tweet is a complaint or no complaint.\n"
peft_config = PromptTuningConfig(
    task_type="CAUSAL_LM",
    prompt_tuning_init=PromptTuningInit.TEXT,
    num_virtual_tokens=len(tokenizer(prompt_tuning_init_text)["input_ids"]),
    prompt_tuning_init_text=prompt_tuning_init_text,
    tokenizer_name_or_path="bigscience/bloomz-560m",
)
model = get_peft_model(model, peft_config)
model.print_healthcheck()

ベンチマーク概要

API

PromptTuningConfig

クラス peft.PromptTuningConfig

< >

( task_type: Optional[Union[str, TaskType]] = Nonepeft_type: Optional[Union[str, PeftType]] = Noneauto_mapping: Optional[dict] = Nonepeft_version: Optional[str] = Nonebase_model_name_or_path: Optional[str] = Nonerevision: Optional[str] = Noneinference_mode: bool = Falsenum_virtual_tokens: int = Nonetoken_dim: int = Nonenum_transformer_submodules: Optional[int] = Nonenum_attention_heads: Optional[int] = Nonenum_layers: Optional[int] = Nonemodules_to_save: Optional[list[str]] = Noneprompt_tuning_init: peft.tuners.prompt_tuning.config.PromptTuningInit | str = <PromptTuningInit.RANDOM: 'RANDOM'>prompt_tuning_init_text: str | None = Nonetokenizer_name_or_path: str | None = Nonetokenizer_kwargs: dict | None = None )

パラメータ

  • prompt_tuning_init (Union[PromptTuningInit, str]) — プロンプト埋め込みの初期化です。TEXT を指定すると、指定したテキストで初期化されます。SAMPLE_VOCAB を指定すると、モデルの語彙からランダムにサンプリングされたトークンで初期化されます。RANDOM を指定すると、ランダムにサンプリングされた連続的なソフトトークンで初期化されます(警告: サンプリングされたソフトトークンは、埋め込み多様体の外に存在する可能性があります)。
  • prompt_tuning_init_text (str, 省略可) — プロンプト埋め込みを初期化するためのテキスト。prompt_tuning_init が TEXT の場合のみ使用されます。
  • tokenizer_name_or_path (str, 省略可) — トークナイザーの名前またはパス。prompt_tuning_init が TEXT の場合のみ使用されます。
  • tokenizer_kwargs (dict、省略可) — AutoTokenizer.from_pretrained に渡すキーワード引数です。prompt_tuning_init が TEXT の場合のみ使用されます。

これは、PromptEmbeddingの設定を保存するための設定クラスです。

PromptEmbedding

クラス peft.PromptEmbedding

< >

( configword_embeddings )

パラメータ

  • config (PromptTuningConfig) — プロンプト埋め込みの設定です。
  • word_embeddings (torch.nn.Module) — ベースとなる Transformer モデルの単語埋め込みです。

仮想トークンをプロンプト埋め込みにエンコードするためのモデル。

属性:

  • embedding (torch.nn.Embedding) — プロンプト埋め込みの埋め込み層です。

例:

>>> from peft import PromptEmbedding, PromptTuningConfig

>>> config = PromptTuningConfig(
...     peft_type="PROMPT_TUNING",
...     task_type="SEQ_2_SEQ_LM",
...     num_virtual_tokens=20,
...     token_dim=768,
...     num_transformer_submodules=1,
...     num_attention_heads=12,
...     num_layers=12,
...     prompt_tuning_init="TEXT",
...     prompt_tuning_init_text="Predict if sentiment of this review is positive, negative or neutral",
...     tokenizer_name_or_path="t5-base",
... )

>>> # t5_model.shared is the word embeddings of the base model
>>> prompt_embedding = PromptEmbedding(config, t5_model.shared)

入力形状: (batch_size, total_virtual_tokens)

出力の形状: (batch_size, total_virtual_tokens, token_dim)

Update on GitHub