Transformers documentation

PEGASUS-X

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

and get access to the augmented documentation experience

to get started

このモデルは2022年8月8日にHF papersで公開され、2022年9月2日にHugging Face Transformersに貢献されました。

FlashAttention

PEGASUS-X

PEGASUS-Xは、長い入力の要約のためのエンコーダー・デコーダー(シーケンス・ツー・シーケンス)transformerモデルです。Pegasusモデルを拡張し、staggered block-local attention、グローバルエンコーダートークン、および長いテキストシーケンスでの追加事前学習により、最大16,000トークンまでの入力を処理できます。PEGASUS-Xは、より少ないパラメータ数で、はるかに大きなモデルと同等の性能を実現します。

Google組織の下に、すべてのオリジナルのPEGASUS-Xチェックポイントを見つけることができます。

このモデルは zphang によって提供されました。

右側のサイドバーにある PEGASUS-X モデルをクリックすると、PEGASUS-X を異なる言語タスクに適用する他の例を確認できます。

以下の例では、Pipeline、AutoModel、およびコマンドラインからテキストを要約する方法を示します。

AutoModel
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer


tokenizer = AutoTokenizer.from_pretrained(
    "google/pegasus-x-large"
)
model = AutoModelForSeq2SeqLM.from_pretrained(
    "google/pegasus-x-large",
    device_map="auto",
)

input_text = """Plants are among the most remarkable and essential life forms on Earth, possessing a unique ability to produce their own food through a process known as photosynthesis. This complex biochemical process is fundamental not only to plant life but to virtually all life on the planet.
Through photosynthesis, plants capture energy from sunlight using a green pigment called chlorophyll, which is located in specialized cell structures called chloroplasts. In the presence of light, plants absorb carbon dioxide from the atmosphere through small pores in their leaves called stomata, and take in water from the soil through their root systems.
These ingredients are then transformed into glucose, a type of sugar that serves as a source of chemical energy, and oxygen, which is released as a byproduct into the atmosphere. The glucose produced during photosynthesis is not just used immediately; plants also store it as starch or convert it into other organic compounds like cellulose, which is essential for building their cellular structure.
This energy reserve allows them to grow, develop leaves, produce flowers, bear fruit, and carry out various physiological processes throughout their lifecycle."""
input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)

output = model.generate(**input_ids, cache_implementation="static")
print(tokenizer.decode(output[0], skip_special_tokens=True))

Quantization は、重みを低精度で表現することで、大規模モデルのメモリ負荷を軽減します。利用可能な Quantization バックエンドの詳細については、Quantization の概要を参照してください。

以下の例では、bitsandbytesを使用して、重みを int4 にのみ量子化します。

from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, BitsAndBytesConfig


quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4"
)
model = AutoModelForSeq2SeqLM.from_pretrained(
    "google/pegasus-x-large",
    device_map="auto",
    quantization_config=quantization_config
)

tokenizer = AutoTokenizer.from_pretrained(
    "google/pegasus-x-large"
)

input_text = """Plants are among the most remarkable and essential life forms on Earth, possessing a unique ability to produce their own food through a process known as photosynthesis. This complex biochemical process is fundamental not only to plant life but to virtually all life on the planet.
Through photosynthesis, plants capture energy from sunlight using a green pigment called chlorophyll, which is located in specialized cell structures called chloroplasts. In the presence of light, plants absorb carbon dioxide from the atmosphere through small pores in their leaves called stomata, and take in water from the soil through their root systems.
These ingredients are then transformed into glucose, a type of sugar that serves as a source of chemical energy, and oxygen, which is released as a byproduct into the atmosphere. The glucose produced during photosynthesis is not just used immediately; plants also store it as starch or convert it into other organic compounds like cellulose, which is essential for building their cellular structure.
This energy reserve allows them to grow, develop leaves, produce flowers, bear fruit, and carry out various physiological processes throughout their lifecycle."""
input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)

output = model.generate(**input_ids, cache_implementation="static")
print(tokenizer.decode(output[0], skip_special_tokens=True))

注記

PegasusXConfig

クラス transformers.PegasusXConfig

< >

( transformers_version: str | None = Nonearchitectures: list[str] | None = Noneoutput_hidden_states: bool | None = Falsereturn_dict: bool | None = Truedtype: str | torch.dtype | None = Nonechunk_size_feed_forward: int = 0id2label: dict[int, str] | dict[str, str] | None = Nonelabel2id: dict[str, int] | dict[str, str] | None = Noneproblem_type: Literal['regression', 'single_label_classification', 'multi_label_classification'] | None = Noneis_encoder_decoder: bool = Truevocab_size: int = 96103max_position_embeddings: int = 16384encoder_layers: int = 16encoder_ffn_dim: int = 4096encoder_attention_heads: int = 16decoder_layers: int = 16decoder_ffn_dim: int = 4096decoder_attention_heads: int = 16encoder_layerdrop: float | int = 0.0decoder_layerdrop: float | int = 0.0use_cache: bool = Trueactivation_function: str = 'gelu'd_model: int = 1024dropout: float | int = 0.1attention_dropout: float | int = 0.0activation_dropout: float | int = 0.0init_std: float = 0.02decoder_start_token_id: int | None = 0scale_embedding: bool = Truepad_token_id: int | None = 0eos_token_id: int | list[int] | None = 1forced_eos_token_id: int | list[int] | None = 1num_global_tokens: int = 32block_size: int = 512stagger_local_blocks: bool = Truetie_word_embeddings: bool = True )

パラメータ

  • is_encoder_decoder (bool、任意、デフォルトはTrue) — モデルがエンコーダー/デコーダーとして使用されるかどうかを示します。
  • vocab_size (int、任意、デフォルトは 96103) — モデルの語彙サイズです。input_ids で表現できるトークンの種類数を定義します。
  • max_position_embeddings (int、任意、デフォルトは 16384) — このモデルが使用される可能性のある最大シーケンス長です。
  • encoder_layers (int、任意、デフォルト値は 16) — Transformer エンコーダーの隠れ層の数です。設定されていない場合、num_layers と同じ値が使用されます。
  • encoder_ffn_dim (int、任意、デフォルトは4096) — エンコーダー内の「中間」層(フィードフォワード層と呼ばれることが多い)の次元数。
  • encoder_attention_heads (int、任意、デフォルトは 16) — Transformer エンコーダーの各アテンション層におけるアテンションヘッドの数。
  • decoder_layers (int、任意、デフォルトは 16) — Transformer デコーダーの隠れ層の数です。設定されていない場合は、num_layers と同じ値が使用されます。
  • decoder_ffn_dim (int、任意、デフォルトは 4096) — デコーダー内の「中間」層(フィードフォワード層と呼ばれることが多い)の次元数。
  • decoder_attention_heads (int、任意、デフォルトは 16) — Transformer デコーダーの各アテンション層におけるアテンションヘッドの数。
  • encoder_layerdrop (Union[float, int]、任意、デフォルトは 0.0) — エンコーダーの LayerDrop 確率です。詳細は [LayerDrop の論文](https://huggingface.co/papers/1909.11556 を参照)をご覧ください。
  • decoder_layerdrop (Union[float, int]、省略可、デフォルト値は 0.0) — デコーダーの LayerDrop 確率です。詳細は [LayerDrop の論文](https://huggingface.co/papers/1909.11556 を参照)をご覧ください。
  • use_cache (bool、任意、デフォルトは True) — モデルが最後のキー/バリューの注意を返すかどうか(すべてのモデルで利用されるわけではありません)。config.is_decoder=True の場合、またはモデルがデコーダー専用の生成モデルである場合にのみ関連します。
  • activation_function (str、任意、デフォルトは gelu) — デコーダーで使用する非線形活性化関数(関数または文字列)。例えば、"gelu"、 "relu"、"silu" など。
  • d_model (int、任意、デフォルトは 1024) — エンコーダ層とプーラ層のサイズ。
  • dropout (Union[float, int]、任意、デフォルト値は0.1) — すべての dropout レイヤーの比率です。
  • attention_dropout (Union[float, int]、任意、デフォルトは 0.0) — アテンション確率のドロップアウト率です。
  • activation_dropout (Union[float, int]、任意、デフォルトは 0.0) — 全結合層内の活性化に対する dropout 率です。
  • init_std (float、任意、デフォルトは 0.02) — すべての重み行列を初期化するための truncated_normal_initializer の標準偏差です。
  • decoder_start_token_id (int、任意、デフォルトは 0) — エンコーダー・デコーダーモデルが bos とは異なるトークンでデコーディングを開始する場合、そのトークンの ID。
  • scale_embedding (bool、任意、デフォルトはTrue) — 埋め込みを sqrt(d_model) で割ってスケーリングするかどうかを指定します。
  • pad_token_id (int、任意、デフォルトは 0) — 語彙内でパディングに使用されるトークン ID。
  • eos_token_id (Union[int, list[int]]、任意、デフォルトは 1) — 語彙内でストリーム終了に使用されるトークン ID。
  • forced_eos_token_id (Union[int, list[int]]、任意、デフォルトは 1) — max_length に達した際に、最後に生成されるトークンとして強制するトークンの ID です。通常は eos_token_id に設定されます。
  • num_global_tokens (int、任意、デフォルトは 128) — エンコーダーに使用するグローバルトークンの数
  • block_size (int, 任意、デフォルトは 512) — エンコーダーのローカルアテンションに使用するブロックサイズ。シーケンス長はブロックサイズの整数倍である必要があります。 stagger_local_block が True の場合、block_size は 2 の倍数である必要があります。
  • stagger_local_blocks (bool、任意、デフォルトは True) — ローカルアテンションを交互に半ブロックずらすかどうか
  • tie_word_embeddings (bool、任意、デフォルトは True) — モデルの tied_weights_keys マッピングに従って重み埋め込みを共有するかどうかを指定します。

これは PegasusXModel の設定を保存するための設定クラスです。指定された引数に基づいて Pegasus X モデルをインスタンス化し、モデルのアーキテクチャを定義するために使用されます。デフォルト値で設定をインスタンス化すると、google/pegasus-x-large と類似した設定が得られます。

設定オブジェクトはPreTrainedConfigを継承し、モデルの出力を制御するために使用できます。詳細については、PreTrainedConfigのドキュメントを参照してください。

例:

>>> from transformers import PegasusXConfig, PegasusXModel

>>> # Initializing a PEGASUS google/pegasus-x-large style configuration
>>> configuration = PegasusXConfig()

>>> # Initializing a model (with random weights) from the google/pegasus-x-large style configuration
>>> model = PegasusXModel(configuration)

>>> # Accessing the model configuration
>>> configuration = model.config

PegasusXModel

class transformers.PegasusXModel

< >

( config: PegasusXConfig )

パラメータ

  • config (PegasusXConfig) — モデルのパラメータをすべて含むモデル設定クラスです。設定ファイルで初期化しても、モデルに関連する重みは読み込まれず、設定のみが読み込まれます。モデルの重みをロードするには、from_pretrained() メソッドを参照してください。

裸の Pegasus X Modelは、上部に特定のヘッドを持たず、生の hidden-states を出力します。

このモデルは PreTrainedModel を継承しています。すべてのモデルに実装されている汎用的なメソッド(ダウンロードや保存、入力埋め込みのリサイズ、ヘッドのプルーニングなど)については、スーパークラスのドキュメントを参照してください。

このモデルは、PyTorchのtorch.nn.Moduleのサブクラスでもあります。 通常のPyTorch Moduleとして使用し、一般的な使用方法や動作に関する詳細については、PyTorchのドキュメントを参照してください。

forward

< >

( input_ids: typing.Optional[torch.Tensor] = Noneattention_mask: typing.Optional[torch.Tensor] = Nonedecoder_input_ids: typing.Optional[torch.Tensor] = Nonedecoder_attention_mask: typing.Optional[torch.Tensor] = Noneencoder_outputs: tuple[torch.FloatTensor] | None = Nonepast_key_values: transformers.cache_utils.Cache | None = Noneinputs_embeds: typing.Optional[torch.Tensor] = Nonedecoder_inputs_embeds: typing.Optional[torch.Tensor] = Noneuse_cache: bool | None = None**kwargs: *<class 'transformers.utils.generic.TransformersKwargs'> ) → Seq2SeqModelOutput or tuple(torch.FloatTensor)

パラメータ

  • input_ids (torch.Tensor の形状 (batch_size, sequence_length), 、任意) — 語彙における入力シーケンスのトークンインデックスです。デフォルトでは、パディングは無視されます。

    インデックスはAutoTokenizerを使用して取得できます。詳細については、PreTrainedTokenizer.encode()およびPreTrainedTokenizer.call()を参照してください。

    入力IDとは何ですか?

  • attention_mask (torch.Tensor の形状 (batch_size, sequence_length), 、任意) — パディングトークンのインデックスに対するアテンションを回避するためのマスク。 [0, 1]:
    • マスクされていないトークンに対して1
    • マスクされたトークンに対しては 0 です。

    アテンションマスクとは?

  • decoder_input_ids (torch.LongTensor 形状 (batch_size, target_sequence_length), 、任意) — 語彙におけるデコーダー入力シーケンスのトークンインデックスです。インデックスは AutoTokenizer を使用して取得できます。詳細については PreTrainedTokenizer.encode() および PreTrainedTokenizer.call() を参照してください。デコーダー入力 ID とは何か? PEGASUS-X は、decoder_input_ids の生成の開始トークンとして pad_token_id を使用します。past_key_values を使用する場合、任意で最後の decoder_input_ids だけを入力すれば十分です (past_key_values を参照)。

    インデックスはAutoTokenizerを使用して取得できます。詳細については、PreTrainedTokenizer.encode()およびPreTrainedTokenizer.call()を参照してください。

    デコーダー入力IDとは何ですか?

    PEGASUS-X は decoder_input_ids の生成の開始トークンとして pad_token_id を使用します。past_key_values を使用する場合、任意で最後の decoder_input_ids だけを入力すれば十分です(past_key_values を参照)。

  • decoder_attention_mask (torch.LongTensor、形状 (batch_size, target_sequence_length)、任意) — デフォルトの動作: decoder_input_ids 内の pad トークンを無視するテンソルを生成します。デフォルトでは、causal mask も使用されます。
  • encoder_outputs (tuple[torch.FloatTensor], 任意) — last_hidden_state、任意: hidden_states、任意: attentions のタプルです。 last_hidden_state は形状 (batch_size, sequence_length, hidden_size)、任意)であり、エンコーダーの最終層の出力における隠れ状態のシーケンスです。デコーダーのクロスアテンションで使用されます。
  • past_key_values (~cache_utils.Cache, 省略可) — 逐次デコードを高速化するために使用できる、事前計算された隠れ状態(自己アテンションブロックおよびクロスアテンションブロックのキーとバリュー)。通常、これはデコードの以前の段階でモデルが返した past_key_values であり、 use_cache=True または config.use_cache=True.

    入力として許可されるのは Cache インスタンスのみです。kv cache ガイドを参照してください。 past_key_values が渡されない場合、デフォルトで DynamicCache が初期化されます。

    モデルは、入力として与えられたのと同じキャッシュ形式を出力します。

    past_key_values を使用する場合、すべての input_ids(形状 (batch_size, sequence_length))ではなく、このモデルに過去のキー・バリュー状態が与えられていない未処理の input_ids(形状 (batch_size, unprocessed_length))のみを入力することになります。

  • inputs_embeds (torch.Tensor、形状 (batch_size, sequence_length, hidden_size)、任意) — input_ids を渡す代わりに、埋め込み表現を直接渡すこともできます。これは、モデル内部の埋め込み参照行列よりも、input_ids のインデックスを対応するベクトルに変換する方法をより細かく制御したい場合に有用です。
  • decoder_inputs_embeds (torch.Tensor の形状 (batch_size, target_sequence_length, hidden_size), 、任意) — 、任意で、 decoder_input_ids の代わりに、埋め込み表現を直接渡すこともできます。 past_key_values が使用されている場合、最後の decoder_inputs_embeds のみを入力すれば十分です( past_key_values). 、 decoder_input_ids のインデックスを関連するベクトルに変換する方法を、モデル内部の埋め込みルックアップ行列よりも細かく制御したい場合に便利です。

    decoder_input_ids と decoder_inputs_embeds の両方が設定されていない場合、decoder_inputs_embeds は inputs_embeds の値を採ります。

  • use_cache (bool、任意) — True に設定すると、past_key_values のキーバリュー状態が返され、デコーディングの高速化に利用できます(past_key_values を参照)。

戻り値

Seq2SeqModelOutput または tuple(torch.FloatTensor)

torch.FloatTensor のタプル(return_dict=False が渡された場合、または config.return_dict=False の場合)、または Seq2SeqModelOutput。設定(PegasusXConfig)や入力に応じて、さまざまな要素で構成されます。

PegasusXModelのforwardメソッドは、__call__特殊メソッドをオーバーライドします。

この関数内でフォワードパスの手順を定義する必要がありますが、その後、この関数ではなくModuleインスタンスを呼び出すべきです。前者は前処理と後処理の実行を適切に処理するのに対し、後者はそれらを黙って無視するためです。

  • last_hidden_state (torch.FloatTensor、shape (batch_size, sequence_length, hidden_size)) — モデルのデコーダーの最終層の出力における、隠れ状態のシーケンス。

    past_key_valuesが使用されている場合、出力されるのは、形状(batch_size, 1, hidden_size)のシーケンスの最後の隠れ状態のみです。

  • past_key_values (EncoderDecoderCache、任意、use_cache=True が渡された場合または config.use_cache=True の場合に返されます) — EncoderDecoderCache のインスタンスです。詳細については、kv cache ガイドを参照してください。

    自己注意ブロックおよび相互注意ブロック内のキーとバリューの事前計算済み隠れ状態(hidden-states)を保持しており、シーケンシャルデコーディングを高速化するために使用できます(past_key_values 入力参照)。

  • decoder_hidden_states (tuple(torch.FloatTensor)、optional、output_hidden_states=True が渡された場合または config.output_hidden_states=True の場合に返される) — torch.FloatTensor のタプル(埋め込み層を持つモデルの場合、埋め込みの出力用が 1 つ、各層の出力用が 1 つずつ)で、形状は (batch_size, sequence_length, hidden_size) です。

    各層の出力におけるデコーダーのHidden-states、および任意の初期埋め込み出力。

  • decoder_attentions (tuple(torch.FloatTensor)、optional、output_attentions=True が渡された場合または config.output_attentions=True の場合に返されます) — torch.FloatTensor のタプル(各レイヤーごとに1つ)、形状は (batch_size, num_heads, sequence_length, sequence_length)。

    自己アテンションヘッドにおける加重平均を計算するために使用される、アテンションのsoftmax後のデコーダーのアテンション重み。

  • cross_attentions (tuple(torch.FloatTensor)、optional、output_attentions=True が渡された場合または config.output_attentions=True の場合に返される) — torch.FloatTensor のタプル(各レイヤーごとに1つ)、形状は (batch_size, num_heads, sequence_length, sequence_length)。

    クロスアテンション層のアテンション重みは、アテンションのsoftmax適用後に計算され、クロスアテンションヘッドにおける加重平均の計算に使用されます。

  • encoder_last_hidden_state (torch.FloatTensor、shape (batch_size, sequence_length, hidden_size)、optional) — モデルのエンコーダーの最終層の出力における、隠れ状態のシーケンス。

  • encoder_hidden_states (tuple(torch.FloatTensor)、optional、output_hidden_states=True が渡された場合または config.output_hidden_states=True の場合に返される) — torch.FloatTensor のタプル(埋め込み層を持つモデルの場合、埋め込みの出力が 1 つ、各層の出力がそれぞれ 1 つずつ)で、形状は (batch_size, sequence_length, hidden_size) です。

    各層の出力におけるエンコーダーのHidden-states、および任意の初期埋め込み出力。

  • encoder_attentions (tuple(torch.FloatTensor)、optional、output_attentions=True が渡された場合または config.output_attentions=True の場合に返される) — torch.FloatTensor のタプル(各レイヤーごとに1つ)、形状は (batch_size, num_heads, sequence_length, sequence_length)。

    アテンションのsoftmaxを適用した後のエンコーダーのアテンション重み。自己アテンションヘッドにおける加重平均の計算に使用されます。

例:

>>> from transformers import AutoTokenizer, PegasusModel

>>> tokenizer = AutoTokenizer.from_pretrained("google/pegasus-x-large")
>>> model = PegasusModel.from_pretrained("google/pegasus-x-large")

>>> inputs = tokenizer("Studies have been shown that owning a dog is good for you", return_tensors="pt")
>>> decoder_inputs = tokenizer("Studies show that", return_tensors="pt")
>>> outputs = model(input_ids=inputs.input_ids, decoder_input_ids=decoder_inputs.input_ids)

>>> last_hidden_states = outputs.last_hidden_state
>>> list(last_hidden_states.shape)
[1, 4, 1024]

PegasusXForConditionalGeneration

クラス transformers.PegasusXForConditionalGeneration

< >

( config: PegasusXConfig )

パラメータ

  • config (PegasusXConfig) — モデルのパラメータをすべて含むモデル設定クラスです。設定ファイルで初期化しても、モデルに関連する重みは読み込まれず、設定のみが読み込まれます。モデルの重みをロードするには、from_pretrained() メソッドを参照してください。

条件付き生成(要約など)向けの PEGASUS-X。

このモデルは PreTrainedModel を継承しています。すべてのモデルに実装されている汎用的なメソッド(ダウンロードや保存、入力埋め込みのリサイズ、ヘッドのプルーニングなど)については、スーパークラスのドキュメントを参照してください。

このモデルは、PyTorchのtorch.nn.Moduleのサブクラスでもあります。 通常のPyTorch Moduleとして使用し、一般的な使用方法や動作に関する詳細については、PyTorchのドキュメントを参照してください。

forward

< >

( input_ids: typing.Optional[torch.Tensor] = Noneattention_mask: typing.Optional[torch.Tensor] = Nonedecoder_input_ids: typing.Optional[torch.Tensor] = Nonedecoder_attention_mask: typing.Optional[torch.Tensor] = Noneencoder_outputs: tuple[torch.FloatTensor] | None = Nonepast_key_values: transformers.cache_utils.Cache | None = Noneinputs_embeds: typing.Optional[torch.Tensor] = Nonedecoder_inputs_embeds: typing.Optional[torch.Tensor] = Nonelabels: typing.Optional[torch.Tensor] = Noneuse_cache: bool | None = None**kwargs: *<class 'transformers.utils.generic.TransformersKwargs'> ) → Seq2SeqLMOutput or tuple(torch.FloatTensor)

パラメータ

  • input_ids (torch.Tensor の形状 (batch_size, sequence_length), 、任意) — 語彙における入力シーケンスのトークンインデックスです。デフォルトでは、パディングは無視されます。

    インデックスはAutoTokenizerを使用して取得できます。詳細については、PreTrainedTokenizer.encode()およびPreTrainedTokenizer.call()を参照してください。

    入力IDとは何ですか?

  • attention_mask (torch.Tensor の形状 (batch_size, sequence_length), 、任意) — パディングトークンのインデックスに対するアテンションを回避するためのマスク。 [0, 1]:
    • マスクされていないトークンに対して1
    • マスクされたトークンに対しては 0 です。

    アテンションマスクとは?

  • decoder_input_ids (torch.LongTensor の形状 (batch_size, target_sequence_length), 、任意) — 語彙におけるデコーダー入力シーケンスのトークンインデックスです。インデックスは AutoTokenizer を使用して取得できます。詳細については PreTrainedTokenizer.encode() および PreTrainedTokenizer.call() を参照してください。デコーダー入力 ID とは何か? PEGASUS-X は、decoder_input_ids の生成の開始トークンとして pad_token_id を使用します。past_key_values を使用する場合、オプションとして最後の decoder_input_ids だけを入力すれば十分です (past_key_values を参照)。

    インデックスはAutoTokenizerを使用して取得できます。詳細については、PreTrainedTokenizer.encode()およびPreTrainedTokenizer.call()を参照してください。

    デコーダー入力IDとは何ですか?

    PEGASUS-X は、decoder_input_ids の生成の開始トークンとして pad_token_id を使用します。past_key_values を使用する場合、任意で最後の decoder_input_ids だけを入力すれば十分です(past_key_values を参照)。

  • decoder_attention_mask (torch.LongTensor、形状 (batch_size, target_sequence_length)、任意) — デフォルトの動作: decoder_input_ids 内の pad トークンを無視するテンソルを生成します。デフォルトでは、causal mask も使用されます。
  • encoder_outputs (tuple[torch.FloatTensor], 任意) — last_hidden_state、任意: hidden_states、任意: attentions のタプルです。 last_hidden_state は形状 (batch_size, sequence_length, hidden_size)、任意)であり、エンコーダーの最終層の出力における隠れ状態のシーケンスです。デコーダーのクロスアテンションで使用されます。
  • past_key_values (~cache_utils.Cache, 省略可) — 逐次デコードを高速化するために使用できる、事前計算された隠れ状態(自己アテンションブロックおよびクロスアテンションブロックのキーとバリュー)。通常、これはデコードの以前の段階でモデルが返した past_key_values であり、 use_cache=True または config.use_cache=True.

    入力として許可されるのは Cache インスタンスのみです。kv cache ガイドを参照してください。 past_key_values が渡されない場合、デフォルトで DynamicCache が初期化されます。

    モデルは、入力として与えられたのと同じキャッシュ形式を出力します。

    past_key_values を使用する場合、すべての input_ids(形状 (batch_size, sequence_length))ではなく、このモデルに過去のキー・バリュー状態が与えられていない未処理の input_ids(形状 (batch_size, unprocessed_length))のみを入力することになります。

  • inputs_embeds (torch.Tensor、形状 (batch_size, sequence_length, hidden_size)、任意) — input_ids を渡す代わりに、埋め込み表現を直接渡すこともできます。これは、モデル内部の埋め込み参照行列よりも、input_ids のインデックスを対応するベクトルに変換する方法をより細かく制御したい場合に有用です。
  • decoder_inputs_embeds (torch.Tensor の形状 (batch_size, target_sequence_length, hidden_size), 、任意) — 、任意で、 decoder_input_ids の代わりに、埋め込み表現を直接渡すこともできます。 past_key_values が使用されている場合、最後の decoder_inputs_embeds のみを入力すれば十分です( past_key_values). 、 decoder_input_ids のインデックスを関連するベクトルに変換する方法を、モデル内部の埋め込みルックアップ行列よりも細かく制御したい場合に便利です。

    decoder_input_ids と decoder_inputs_embeds の両方が設定されていない場合、decoder_inputs_embeds は inputs_embeds の値を採ります。

  • labels (torch.LongTensor、shape (batch_size, sequence_length)、任意) — マスク言語モデリング損失を計算するためのラベルです。インデックスは [0, ..., config.vocab_size] の範囲内であるか、-100 である必要があります(input_ids の docstring を参照)。インデックスが -100 に設定されたトークンは無視(マスク)され、損失は [0, ..., config.vocab_size] の範囲内のラベルを持つトークンに対してのみ計算されます。
  • use_cache (bool、任意) — True に設定すると、past_key_values のキーバリュー状態が返され、デコーディングの高速化に利用できます(past_key_values を参照)。

戻り値

Seq2SeqLMOutput または tuple(torch.FloatTensor)

torch.FloatTensor のタプル、または Seq2SeqLMOutput(return_dict=False が渡された場合、または config.return_dict=False の場合)。設定(PegasusXConfig)や入力に応じて、さまざまな要素で構成されます。

PegasusXForConditionalGenerationのforwardメソッドは、__call__特殊メソッドをオーバーライドします。

この関数内でフォワードパスの手順を定義する必要がありますが、その後、この関数ではなくModuleインスタンスを呼び出すべきです。前者は前処理と後処理の実行を適切に処理するのに対し、後者はそれらを黙って無視するためです。

  • loss (torch.FloatTensor、shape (1,)、optional、labels が指定された場合に返されます)— 言語モデリングの損失。

  • logits (torch.FloatTensor、shape (batch_size, sequence_length, config.vocab_size)) — 言語モデリングヘッドの予測スコア(SoftMax 適用前の各語彙トークンのスコア)。

  • past_key_values (EncoderDecoderCache、任意、use_cache=True が渡された場合または config.use_cache=True の場合に返されます) — EncoderDecoderCache のインスタンスです。詳細については、kv cache ガイドを参照してください。

    自己注意ブロックおよび相互注意ブロック内のキーとバリューの事前計算済み隠れ状態(hidden-states)を保持しており、シーケンシャルデコーディングを高速化するために使用できます(past_key_values 入力参照)。

  • decoder_hidden_states (tuple(torch.FloatTensor)、optional、output_hidden_states=True が渡された場合または config.output_hidden_states=True の場合に返される) — torch.FloatTensor のタプル(埋め込み層を持つモデルの場合、埋め込みの出力用が 1 つ、各層の出力用が 1 つずつ)で、形状は (batch_size, sequence_length, hidden_size) です。

    各層の出力および初期埋め込み出力における、デコーダーの隠れ状態。

  • decoder_attentions (tuple(torch.FloatTensor)、optional、output_attentions=True が渡された場合または config.output_attentions=True の場合に返されます) — torch.FloatTensor のタプル(各レイヤーごとに1つ)、形状は (batch_size, num_heads, sequence_length, sequence_length)。

    自己アテンションヘッドにおける加重平均を計算するために使用される、アテンションのsoftmax後のデコーダーのアテンション重み。

  • cross_attentions (tuple(torch.FloatTensor)、optional、output_attentions=True が渡された場合または config.output_attentions=True の場合に返される) — torch.FloatTensor のタプル(各レイヤーごとに1つ)、形状は (batch_size, num_heads, sequence_length, sequence_length)。

    クロスアテンション層のアテンション重みは、アテンションのsoftmax適用後に計算され、クロスアテンションヘッドにおける加重平均の計算に使用されます。

  • encoder_last_hidden_state (torch.FloatTensor、shape (batch_size, sequence_length, hidden_size)、optional) — モデルのエンコーダーの最終層の出力における、隠れ状態のシーケンス。

  • encoder_hidden_states (tuple(torch.FloatTensor)、optional、output_hidden_states=True が渡された場合または config.output_hidden_states=True の場合に返される) — torch.FloatTensor のタプル(埋め込み層を持つモデルの場合、埋め込みの出力が 1 つ、各層の出力がそれぞれ 1 つずつ)で、形状は (batch_size, sequence_length, hidden_size) です。

    各層の出力および初期埋め込み出力におけるエンコーダーの隠れ状態。

  • encoder_attentions (tuple(torch.FloatTensor)、optional、output_attentions=True が渡された場合または config.output_attentions=True の場合に返される) — torch.FloatTensor のタプル(各レイヤーごとに1つ)、形状は (batch_size, num_heads, sequence_length, sequence_length)。

    アテンションのsoftmaxを適用した後のエンコーダーのアテンション重み。自己アテンションヘッドにおける加重平均の計算に使用されます。

Update on GitHub