Add prompt_clue.
This commit is contained in:
parent
55fed4bc5a
commit
65578680cf
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,175 @@
|
||||||
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||||
|
# Copyright 2020, The T5 Authors and HuggingFace Inc.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
""" T5 model configuration"""
|
||||||
|
from typing import Mapping
|
||||||
|
|
||||||
|
from transformers.configuration_utils import PretrainedConfig
|
||||||
|
from transformers.onnx import OnnxSeq2SeqConfigWithPast
|
||||||
|
|
||||||
|
from modelscope.utils.logger import get_logger
|
||||||
|
|
||||||
|
logger = get_logger()
|
||||||
|
|
||||||
|
|
||||||
|
class T5Config(PretrainedConfig):
|
||||||
|
r"""
|
||||||
|
This is the configuration class to store the configuration of a [`T5Model`] or a [`TFT5Model`]. It is used to
|
||||||
|
instantiate a T5 model according to the specified arguments, defining the model architecture. Instantiating a
|
||||||
|
configuration with the defaults will yield a similar configuration to that of the T5
|
||||||
|
[t5-small](https://huggingface.co/t5-small) architecture.
|
||||||
|
|
||||||
|
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
||||||
|
documentation from [`PretrainedConfig`] for more information.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
vocab_size (`int`, *optional*, defaults to 32128):
|
||||||
|
Vocabulary size of the T5 model. Defines the number of different tokens that can be represented by the
|
||||||
|
`inputs_ids` passed when calling [`T5Model`] or [`TFT5Model`].
|
||||||
|
d_model (`int`, *optional*, defaults to 512):
|
||||||
|
Size of the encoder layers and the pooler layer.
|
||||||
|
d_kv (`int`, *optional*, defaults to 64):
|
||||||
|
Size of the key, query, value projections per attention head. `d_kv` has to be equal to `d_model //
|
||||||
|
num_heads`.
|
||||||
|
d_ff (`int`, *optional*, defaults to 2048):
|
||||||
|
Size of the intermediate feed forward layer in each `T5Block`.
|
||||||
|
num_layers (`int`, *optional*, defaults to 6):
|
||||||
|
Number of hidden layers in the Transformer encoder.
|
||||||
|
num_decoder_layers (`int`, *optional*):
|
||||||
|
Number of hidden layers in the Transformer decoder. Will use the same value as `num_layers` if not set.
|
||||||
|
num_heads (`int`, *optional*, defaults to 8):
|
||||||
|
Number of attention heads for each attention layer in the Transformer encoder.
|
||||||
|
relative_attention_num_buckets (`int`, *optional*, defaults to 32):
|
||||||
|
The number of buckets to use for each attention layer.
|
||||||
|
relative_attention_max_distance (`int`, *optional*, defaults to 128):
|
||||||
|
The maximum distance of the longer sequences for the bucket separation.
|
||||||
|
dropout_rate (`float`, *optional*, defaults to 0.1):
|
||||||
|
The ratio for all dropout layers.
|
||||||
|
layer_norm_eps (`float`, *optional*, defaults to 1e-6):
|
||||||
|
The epsilon used by the layer normalization layers.
|
||||||
|
initializer_factor (`float`, *optional*, defaults to 1):
|
||||||
|
A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
|
||||||
|
testing).
|
||||||
|
feed_forward_proj (`string`, *optional*, defaults to `"relu"`):
|
||||||
|
Type of feed forward layer to be used. Should be one of `"relu"` or `"gated-gelu"`. T5v1.1 uses the
|
||||||
|
`"gated-gelu"` feed forward projection. Original T5 uses `"relu"`.
|
||||||
|
use_cache (`bool`, *optional*, defaults to `True`):
|
||||||
|
Whether or not the model should return the last key/values attentions (not used by all models).
|
||||||
|
"""
|
||||||
|
model_type = 't5'
|
||||||
|
keys_to_ignore_at_inference = ['past_key_values']
|
||||||
|
attribute_map = {
|
||||||
|
'hidden_size': 'd_model',
|
||||||
|
'num_attention_heads': 'num_heads',
|
||||||
|
'num_hidden_layers': 'num_layers'
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
vocab_size=32128,
|
||||||
|
d_model=512,
|
||||||
|
d_kv=64,
|
||||||
|
d_ff=2048,
|
||||||
|
num_layers=6,
|
||||||
|
num_decoder_layers=None,
|
||||||
|
num_heads=8,
|
||||||
|
relative_attention_num_buckets=32,
|
||||||
|
relative_attention_max_distance=128,
|
||||||
|
dropout_rate=0.1,
|
||||||
|
layer_norm_epsilon=1e-6,
|
||||||
|
initializer_factor=1.0,
|
||||||
|
feed_forward_proj='relu',
|
||||||
|
is_encoder_decoder=True,
|
||||||
|
use_cache=True,
|
||||||
|
pad_token_id=0,
|
||||||
|
eos_token_id=1,
|
||||||
|
**kwargs):
|
||||||
|
self.vocab_size = vocab_size
|
||||||
|
self.d_model = d_model
|
||||||
|
self.d_kv = d_kv
|
||||||
|
self.d_ff = d_ff
|
||||||
|
self.num_layers = num_layers
|
||||||
|
self.num_decoder_layers = (num_decoder_layers if num_decoder_layers
|
||||||
|
is not None else self.num_layers
|
||||||
|
) # default = symmetry
|
||||||
|
self.num_heads = num_heads
|
||||||
|
self.relative_attention_num_buckets = relative_attention_num_buckets
|
||||||
|
self.relative_attention_max_distance = relative_attention_max_distance
|
||||||
|
self.dropout_rate = dropout_rate
|
||||||
|
self.layer_norm_epsilon = layer_norm_epsilon
|
||||||
|
self.initializer_factor = initializer_factor
|
||||||
|
self.feed_forward_proj = feed_forward_proj
|
||||||
|
self.use_cache = use_cache
|
||||||
|
|
||||||
|
act_info = self.feed_forward_proj.split('-')
|
||||||
|
self.dense_act_fn = act_info[-1]
|
||||||
|
self.is_gated_act = act_info[0] == 'gated'
|
||||||
|
|
||||||
|
if len(act_info) > 1 and act_info[0] != 'gated' or len(act_info) > 2:
|
||||||
|
raise ValueError(
|
||||||
|
f'`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.'
|
||||||
|
'Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '
|
||||||
|
"'gated-gelu' or 'relu'")
|
||||||
|
|
||||||
|
# for backwards compatibility
|
||||||
|
if feed_forward_proj == 'gated-gelu':
|
||||||
|
self.dense_act_fn = 'gelu_new'
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
pad_token_id=pad_token_id,
|
||||||
|
eos_token_id=eos_token_id,
|
||||||
|
is_encoder_decoder=is_encoder_decoder,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class T5OnnxConfig(OnnxSeq2SeqConfigWithPast):
|
||||||
|
|
||||||
|
@property
|
||||||
|
def inputs(self) -> Mapping[str, Mapping[int, str]]:
|
||||||
|
common_inputs = {
|
||||||
|
'input_ids': {
|
||||||
|
0: 'batch',
|
||||||
|
1: 'encoder_sequence'
|
||||||
|
},
|
||||||
|
'attention_mask': {
|
||||||
|
0: 'batch',
|
||||||
|
1: 'encoder_sequence'
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if self.use_past:
|
||||||
|
common_inputs['attention_mask'][
|
||||||
|
1] = 'past_encoder_sequence + sequence'
|
||||||
|
common_inputs['decoder_input_ids'] = {0: 'batch'}
|
||||||
|
common_inputs['decoder_attention_mask'] = {
|
||||||
|
0: 'batch',
|
||||||
|
1: 'past_decoder_sequence + sequence'
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
common_inputs['decoder_input_ids'] = {
|
||||||
|
0: 'batch',
|
||||||
|
1: 'decoder_sequence'
|
||||||
|
}
|
||||||
|
common_inputs['decoder_attention_mask'] = {
|
||||||
|
0: 'batch',
|
||||||
|
1: 'decoder_sequence'
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.use_past:
|
||||||
|
self.fill_with_past_key_values_(common_inputs, direction='inputs')
|
||||||
|
|
||||||
|
return common_inputs
|
||||||
|
|
||||||
|
@property
|
||||||
|
def default_onnx_opset(self) -> int:
|
||||||
|
return 13
|
|
@ -0,0 +1,59 @@
|
||||||
|
from modelscope.pipelines import pipeline
|
||||||
|
from modelscope.utils.constant import Tasks
|
||||||
|
|
||||||
|
# from modelscope.models.nlp import T5ForConditionalGeneration
|
||||||
|
from modelscope.preprocessors import TextGenerationTransformersPreprocessor
|
||||||
|
|
||||||
|
from modeling_t5 import T5ForConditionalGeneration
|
||||||
|
|
||||||
|
|
||||||
|
model = T5ForConditionalGeneration.from_pretrained(
|
||||||
|
"ClueAI/PromptCLUE-base-v1-5", revision="v0.1"
|
||||||
|
)
|
||||||
|
preprocessor = TextGenerationTransformersPreprocessor(model.model_dir)
|
||||||
|
pipeline_t2t = pipeline(
|
||||||
|
task=Tasks.text2text_generation, model=model, preprocessor=preprocessor
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
print(pipeline_t2t("生成与下列文字相同意思的句子:\n白云遍地无人扫\n答案:", do_sample=True, top_p=0.8))
|
||||||
|
# {'text': '白云散去无踪,没人扫。'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t('改写下面的文字,确保意思相同:\n一个如此藐视本国人民民主权利的人,怎么可能捍卫外国人的民权?\n答案:', do_sample=True, top_p=0.8))
|
||||||
|
# # {'text': '对一个如此藐视本国人民民主权利的人,怎么能捍卫外国人的民权?'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t('根据问题给出答案:\n问题:手指发麻的主要可能病因是:\n答案'))
|
||||||
|
# # {'text': '神经损伤,颈椎病,贫血,高血压'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t('问答:\n问题:黄果悬钩子的目是:\n答案:'))
|
||||||
|
# # {'text': '蔷薇目'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t('情感分析:\n这个看上去还可以,但其实我不喜欢\n选项:积极,消极'))
|
||||||
|
# # {'text': '消极'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t("下面句子是否表示了相同的语义:\n文本1:糖尿病腿麻木怎么办?\n文本2:糖尿病怎样控制生活方式\n选项:相似,不相似\n答案:"))
|
||||||
|
# # {'text': '不相似'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t('这是关于哪方面的新闻:\n如果日本沉没,中国会接收日本难民吗?\n选项:故事,文化,娱乐,体育,财经,房产,汽车,教育,科技,军事,旅游,国际,股票,农业,游戏'))
|
||||||
|
# # {'text': '国际'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t("阅读文本抽取关键信息:\n张玄武1990年出生中国国籍无境外居留权博士学历现任杭州线锁科技技术总监。\n问题:机构,人名,职位,籍贯,专业,国籍,学历,种族\n答案:"))
|
||||||
|
# # {'text': '机构:杭州线锁科技技术_人名:张玄武_职位:博士学历'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t("翻译成英文:\n杀不死我的只会让我更强大\n答案:"))
|
||||||
|
# # {'text': 'To kill my life only let me stronger'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t('为下面的文章生成摘要:\n北京时间9月5日12时52分,四川甘孜藏族自治州泸定县发生6.8级地震。地震发生后,领导高度重视并作出重要指示,要求把抢救生命作为首要任务,全力救援受灾群众,最大限度减少人员伤亡'))
|
||||||
|
# # {'text': '四川甘孜发生6.8级地震'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t("推理关系判断:\n前提:小明今天在北京\n假设:小明在深圳旅游\n选项:矛盾,蕴含,中立\n答案:"))
|
||||||
|
# # {'text': '蕴涵'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t('阅读以下对话并回答问题。\n男:今天怎么这么晚才来上班啊?女:昨天工作到很晚,而且我还感冒了。男:那你回去休息吧,我帮你请假。女:谢谢你。\n问题:女的怎么样?\n选项:正在工作,感冒了,在打电话,要出差。'))
|
||||||
|
# # {'text': '感冒了'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t("文本纠错:\n告诉二营长,叫他彻回来,我李云龙从不打没有准备的杖\n答案:"))
|
||||||
|
# #{'text':'告诉二营长,叫他下来,我李云龙从不打没有准备的仗'}
|
||||||
|
|
||||||
|
# print(pipeline_t2t("问答:\n问题:小米的创始人是谁?\n答案:"))
|
||||||
|
# # {'text': '小米创始人:雷军'}
|
|
@ -0,0 +1,471 @@
|
||||||
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||||
|
# Copyright 2018 Mesh TensorFlow authors, T5 Authors and HuggingFace Inc. team.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
import copy
|
||||||
|
import warnings
|
||||||
|
from typing import Optional, Tuple, Union
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from torch import nn
|
||||||
|
from torch.nn import CrossEntropyLoss
|
||||||
|
from transformers.utils.model_parallel_utils import assert_device_map, get_device_map
|
||||||
|
|
||||||
|
from modelscope.metainfo import Models
|
||||||
|
from modelscope.models.builder import MODELS
|
||||||
|
from modelscope.outputs import (
|
||||||
|
AttentionBackboneModelOutput,
|
||||||
|
Seq2SeqLMOutput,
|
||||||
|
TokenGeneratorOutput,
|
||||||
|
)
|
||||||
|
from modelscope.utils.constant import Tasks
|
||||||
|
from modelscope.utils.logger import get_logger
|
||||||
|
from backbone import T5PreTrainedModel, T5Stack
|
||||||
|
from configuration import T5Config
|
||||||
|
|
||||||
|
logger = get_logger()
|
||||||
|
|
||||||
|
# Warning message for FutureWarning: head_mask was separated into two input args - head_mask, decoder_head_mask
|
||||||
|
__HEAD_MASK_WARNING_MSG = """
|
||||||
|
The input argument `head_mask` was split into two arguments `head_mask` and
|
||||||
|
`decoder_head_mask`. Currently, `decoder_head_mask` is set to copy `head_mask`,
|
||||||
|
but this feature is deprecated and will be removed in future versions. If you do
|
||||||
|
not want to use any `decoder_head_mask` now, please set `decoder_head_mask =
|
||||||
|
torch.ones(num_layers, num_heads)`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class T5ForConditionalGeneration(T5PreTrainedModel):
|
||||||
|
_keys_to_ignore_on_load_missing = [
|
||||||
|
r"encoder\.embed_tokens\.weight",
|
||||||
|
r"decoder\.embed_tokens\.weight",
|
||||||
|
r"lm_head\.weight",
|
||||||
|
]
|
||||||
|
_keys_to_ignore_on_load_unexpected = [
|
||||||
|
r"decoder\.block\.0\.layer\.1\.EncDecAttention\.relative_attention_bias\.weight",
|
||||||
|
]
|
||||||
|
|
||||||
|
def __init__(self, config: T5Config, device_map=None, **kwargs):
|
||||||
|
super().__init__(config)
|
||||||
|
self.model_dim = config.d_model
|
||||||
|
|
||||||
|
self.shared = nn.Embedding(config.vocab_size, config.d_model)
|
||||||
|
|
||||||
|
encoder_config = copy.deepcopy(config)
|
||||||
|
encoder_config.is_decoder = False
|
||||||
|
encoder_config.use_cache = False
|
||||||
|
encoder_config.is_encoder_decoder = False
|
||||||
|
self.encoder = T5Stack(encoder_config, self.shared)
|
||||||
|
|
||||||
|
decoder_config = copy.deepcopy(config)
|
||||||
|
decoder_config.is_decoder = True
|
||||||
|
decoder_config.is_encoder_decoder = False
|
||||||
|
decoder_config.num_layers = config.num_decoder_layers
|
||||||
|
self.decoder = T5Stack(decoder_config, self.shared)
|
||||||
|
|
||||||
|
self.lm_head = nn.Linear(config.d_model, config.vocab_size, bias=False)
|
||||||
|
|
||||||
|
# Initialize weights and apply final processing
|
||||||
|
self.post_init()
|
||||||
|
|
||||||
|
# Model parallel
|
||||||
|
self.model_parallel = False
|
||||||
|
if device_map == "auto":
|
||||||
|
self.parallelize()
|
||||||
|
|
||||||
|
def parallelize(self, device_map=None):
|
||||||
|
self.device_map = (
|
||||||
|
get_device_map(len(self.encoder.block), range(torch.cuda.device_count()))
|
||||||
|
if device_map is None
|
||||||
|
else device_map
|
||||||
|
)
|
||||||
|
assert_device_map(self.device_map, len(self.encoder.block))
|
||||||
|
self.encoder.parallelize(self.device_map)
|
||||||
|
self.decoder.parallelize(self.device_map)
|
||||||
|
self.lm_head = self.lm_head.to(self.decoder.first_device)
|
||||||
|
self.model_parallel = True
|
||||||
|
|
||||||
|
def deparallelize(self):
|
||||||
|
self.encoder.deparallelize()
|
||||||
|
self.decoder.deparallelize()
|
||||||
|
self.encoder = self.encoder.to("cpu")
|
||||||
|
self.decoder = self.decoder.to("cpu")
|
||||||
|
self.lm_head = self.lm_head.to("cpu")
|
||||||
|
self.model_parallel = False
|
||||||
|
self.device_map = None
|
||||||
|
torch.cuda.empty_cache()
|
||||||
|
|
||||||
|
def get_input_embeddings(self):
|
||||||
|
return self.shared
|
||||||
|
|
||||||
|
def set_input_embeddings(self, new_embeddings):
|
||||||
|
self.shared = new_embeddings
|
||||||
|
self.encoder.set_input_embeddings(new_embeddings)
|
||||||
|
self.decoder.set_input_embeddings(new_embeddings)
|
||||||
|
|
||||||
|
def set_output_embeddings(self, new_embeddings):
|
||||||
|
self.lm_head = new_embeddings
|
||||||
|
|
||||||
|
def get_output_embeddings(self):
|
||||||
|
return self.lm_head
|
||||||
|
|
||||||
|
def get_encoder(self):
|
||||||
|
return self.encoder
|
||||||
|
|
||||||
|
def get_decoder(self):
|
||||||
|
return self.decoder
|
||||||
|
|
||||||
|
def forward(
|
||||||
|
self,
|
||||||
|
input_ids: Optional[torch.LongTensor] = None,
|
||||||
|
attention_mask: Optional[torch.FloatTensor] = None,
|
||||||
|
decoder_input_ids: Optional[torch.LongTensor] = None,
|
||||||
|
decoder_attention_mask: Optional[torch.BoolTensor] = None,
|
||||||
|
head_mask: Optional[torch.FloatTensor] = None,
|
||||||
|
decoder_head_mask: Optional[torch.FloatTensor] = None,
|
||||||
|
cross_attn_head_mask: Optional[torch.Tensor] = None,
|
||||||
|
encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None,
|
||||||
|
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
|
||||||
|
inputs_embeds: Optional[torch.FloatTensor] = None,
|
||||||
|
decoder_inputs_embeds: Optional[torch.FloatTensor] = None,
|
||||||
|
labels: Optional[torch.LongTensor] = None,
|
||||||
|
use_cache: Optional[bool] = None,
|
||||||
|
output_attentions: Optional[bool] = None,
|
||||||
|
output_hidden_states: Optional[bool] = None,
|
||||||
|
return_dict: Optional[bool] = None,
|
||||||
|
**kwargs,
|
||||||
|
) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]:
|
||||||
|
r"""
|
||||||
|
Args:
|
||||||
|
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
||||||
|
Indices of input sequence tokens in the vocabulary. T5 is a model
|
||||||
|
with relative position embeddings so you should be able to pad the
|
||||||
|
inputs on both the right and the left.
|
||||||
|
|
||||||
|
Indices can be obtained using [`T5Tokenizer`]. See
|
||||||
|
[`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`]
|
||||||
|
for detail.
|
||||||
|
|
||||||
|
[What are input IDs?](../glossary#input-ids)
|
||||||
|
|
||||||
|
To know more on how to prepare `input_ids` for pretraining take a
|
||||||
|
look a [T5 Training](./t5#training).
|
||||||
|
attention_mask (`torch.FloatTensor` of shape `(batch_size,sequence_length)`, *optional*):
|
||||||
|
Mask to avoid performing attention on padding token indices. Mask
|
||||||
|
values selected in `[0, 1]`:
|
||||||
|
|
||||||
|
- 1 for tokens that are **not masked**,
|
||||||
|
- 0 for tokens that are **masked**.
|
||||||
|
|
||||||
|
[What are attention masks?](../glossary#attention-mask)
|
||||||
|
decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
|
||||||
|
Indices of decoder input sequence tokens in the vocabulary.
|
||||||
|
|
||||||
|
Indices can be obtained using [`T5Tokenizer`]. See
|
||||||
|
[`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`]
|
||||||
|
for details.
|
||||||
|
|
||||||
|
[What are decoder input IDs?](../glossary#decoder-input-ids)
|
||||||
|
|
||||||
|
T5 uses the `pad_token_id` as the starting token for
|
||||||
|
`decoder_input_ids` generation. If `past_key_values` is used,
|
||||||
|
optionally only the last `decoder_input_ids` have to be input (see
|
||||||
|
`past_key_values`).
|
||||||
|
|
||||||
|
To know more on how to prepare `decoder_input_ids` for pretraining
|
||||||
|
take a look at [T5 Training](./t5#training).
|
||||||
|
decoder_attention_mask (`torch.BoolTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
|
||||||
|
Default behavior: generate a tensor that ignores pad tokens in
|
||||||
|
`decoder_input_ids`. Causal mask will also be used by default.
|
||||||
|
head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
|
||||||
|
Mask to nullify selected heads of the self-attention modules in the
|
||||||
|
encoder. Mask values selected in `[0, 1]`:
|
||||||
|
|
||||||
|
- 1 indicates the head is **not masked**,
|
||||||
|
- 0 indicates the head is **masked**.
|
||||||
|
|
||||||
|
decoder_head_mask (`torch.FloatTensor` of shape `(num_heads,)` or
|
||||||
|
`(num_layers, num_heads)`, *optional*):
|
||||||
|
Mask to nullify selected heads of the self-attention modules in the
|
||||||
|
decoder. Mask values selected in `[0, 1]`:
|
||||||
|
|
||||||
|
- 1 indicates the head is **not masked**,
|
||||||
|
- 0 indicates the head is **masked**.
|
||||||
|
|
||||||
|
cross_attn_head_mask (`torch.Tensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
|
||||||
|
Mask to nullify selected heads of the cross-attention modules in
|
||||||
|
the decoder. Mask values selected in `[0, 1]`:
|
||||||
|
|
||||||
|
- 1 indicates the head is **not masked**,
|
||||||
|
- 0 indicates the head is **masked**.
|
||||||
|
|
||||||
|
encoder_outputs (`tuple(tuple(torch.FloatTensor)`, *optional*):
|
||||||
|
Tuple consists of (`last_hidden_state`, `optional`: *hidden_states*,
|
||||||
|
`optional`: *attentions*) `last_hidden_state` of shape `(batch_size,
|
||||||
|
sequence_length, hidden_size)` is a sequence of hidden states at the
|
||||||
|
output of the last layer of the encoder. Used in the cross-attention
|
||||||
|
of the decoder.
|
||||||
|
past_key_values (`tuple(tuple(torch.FloatTensor))` of length
|
||||||
|
`config.n_layers` with each tuple having 4 tensors of shape
|
||||||
|
`(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`):
|
||||||
|
|
||||||
|
Contains precomputed key and value hidden states of the attention
|
||||||
|
blocks. Can be used to speed up decoding.
|
||||||
|
|
||||||
|
If `past_key_values` are used, the user can optionally input only
|
||||||
|
the last `decoder_input_ids` (those that don't have their past key
|
||||||
|
value states given to this model) of shape `(batch_size, 1)` instead
|
||||||
|
of all `decoder_input_ids` of shape `(batch_size, sequence_length)`.
|
||||||
|
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
|
||||||
|
Optionally, instead of passing `input_ids` you can choose to
|
||||||
|
directly pass an embedded representation. This is useful if you want
|
||||||
|
more control over how to convert `input_ids` indices into associated
|
||||||
|
vectors than the model's internal embedding lookup matrix.
|
||||||
|
decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, target_sequence_length, hidden_size)`,
|
||||||
|
*optional*):
|
||||||
|
Optionally, instead of passing `decoder_input_ids` you can choose to
|
||||||
|
directly pass an embedded representation. If `past_key_values` is
|
||||||
|
used, optionally only the last `decoder_inputs_embeds` have to be
|
||||||
|
input (see `past_key_values`). This is useful if you want more
|
||||||
|
control over how to convert `decoder_input_ids` indices into
|
||||||
|
associated vectors than the model's internal embedding lookup
|
||||||
|
matrix.
|
||||||
|
|
||||||
|
If `decoder_input_ids` and `decoder_inputs_embeds` are both unset,
|
||||||
|
`decoder_inputs_embeds` takes the value of `inputs_embeds`.
|
||||||
|
|
||||||
|
use_cache (`bool`, *optional*):
|
||||||
|
If set to `True`, `past_key_values` key value states are returned
|
||||||
|
and can be used to speed up decoding (see `past_key_values`).
|
||||||
|
|
||||||
|
output_attentions (`bool`, *optional*):
|
||||||
|
Whether or not to return the attentions tensors of all attention
|
||||||
|
layers. See `attentions` under returned tensors for more detail.
|
||||||
|
output_hidden_states (`bool`, *optional*):
|
||||||
|
Whether or not to return the hidden states of all layers. See
|
||||||
|
`hidden_states` under returned tensors for more detail.
|
||||||
|
return_dict (`bool`, *optional*):
|
||||||
|
Whether or not to return a [`~utils.ModelOutput`] instead of a plain
|
||||||
|
tuple.
|
||||||
|
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
||||||
|
Labels for computing the sequence classification/regression loss.
|
||||||
|
Indices should be in `[-100, 0, ..., config.vocab_size - 1]`. All
|
||||||
|
labels set to `-100` are ignored (masked), the loss is only computed
|
||||||
|
for labels in `[0, ..., config.vocab_size]`
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
>>> from transformers import T5Tokenizer, T5ForConditionalGeneration
|
||||||
|
|
||||||
|
>>> tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
||||||
|
>>> model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
||||||
|
|
||||||
|
>>> # training
|
||||||
|
>>> input_ids = tokenizer("The <extra_id_0> walks in <extra_id_1> park", return_tensors="pt").input_ids
|
||||||
|
>>> labels = tokenizer("<extra_id_0> cute dog <extra_id_1> the <extra_id_2>", return_tensors="pt").input_ids
|
||||||
|
>>> outputs = model(input_ids=input_ids, labels=labels)
|
||||||
|
>>> loss = outputs.loss
|
||||||
|
>>> logits = outputs.logits
|
||||||
|
|
||||||
|
>>> # inference
|
||||||
|
>>> input_ids = tokenizer(
|
||||||
|
... "summarize: studies have shown that owning a dog is good for you", return_tensors="pt"
|
||||||
|
>>> ).input_ids # Batch size 1
|
||||||
|
>>> outputs = model.generate(input_ids)
|
||||||
|
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
||||||
|
>>> # studies have shown that owning a dog is good for you.
|
||||||
|
"""
|
||||||
|
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
||||||
|
return_dict = (
|
||||||
|
return_dict if return_dict is not None else self.config.use_return_dict
|
||||||
|
)
|
||||||
|
|
||||||
|
# FutureWarning: head_mask was separated into two input args - head_mask, decoder_head_mask
|
||||||
|
if head_mask is not None and decoder_head_mask is None:
|
||||||
|
if self.config.num_layers == self.config.num_decoder_layers:
|
||||||
|
warnings.warn(__HEAD_MASK_WARNING_MSG, FutureWarning)
|
||||||
|
decoder_head_mask = head_mask
|
||||||
|
|
||||||
|
# Encode if needed (training, first prediction pass)
|
||||||
|
if encoder_outputs is None:
|
||||||
|
# Convert encoder inputs in embeddings if needed
|
||||||
|
encoder_outputs = self.encoder(
|
||||||
|
input_ids=input_ids,
|
||||||
|
attention_mask=attention_mask,
|
||||||
|
inputs_embeds=inputs_embeds,
|
||||||
|
head_mask=head_mask,
|
||||||
|
output_attentions=output_attentions,
|
||||||
|
output_hidden_states=output_hidden_states,
|
||||||
|
return_dict=return_dict,
|
||||||
|
)
|
||||||
|
elif return_dict and not isinstance(
|
||||||
|
encoder_outputs, AttentionBackboneModelOutput
|
||||||
|
):
|
||||||
|
encoder_outputs = AttentionBackboneModelOutput(
|
||||||
|
last_hidden_state=encoder_outputs[0],
|
||||||
|
hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None,
|
||||||
|
attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None,
|
||||||
|
)
|
||||||
|
|
||||||
|
hidden_states = encoder_outputs[0]
|
||||||
|
|
||||||
|
if self.model_parallel:
|
||||||
|
torch.cuda.set_device(self.decoder.first_device)
|
||||||
|
|
||||||
|
if (
|
||||||
|
labels is not None
|
||||||
|
and decoder_input_ids is None
|
||||||
|
and decoder_inputs_embeds is None
|
||||||
|
):
|
||||||
|
# get decoder inputs from shifting lm labels to the right
|
||||||
|
decoder_input_ids = self._shift_right(labels)
|
||||||
|
|
||||||
|
# Set device for model parallelism
|
||||||
|
if self.model_parallel:
|
||||||
|
torch.cuda.set_device(self.decoder.first_device)
|
||||||
|
hidden_states = hidden_states.to(self.decoder.first_device)
|
||||||
|
if decoder_input_ids is not None:
|
||||||
|
decoder_input_ids = decoder_input_ids.to(self.decoder.first_device)
|
||||||
|
if attention_mask is not None:
|
||||||
|
attention_mask = attention_mask.to(self.decoder.first_device)
|
||||||
|
if decoder_attention_mask is not None:
|
||||||
|
decoder_attention_mask = decoder_attention_mask.to(
|
||||||
|
self.decoder.first_device
|
||||||
|
)
|
||||||
|
|
||||||
|
# Decode
|
||||||
|
decoder_outputs = self.decoder(
|
||||||
|
input_ids=decoder_input_ids,
|
||||||
|
attention_mask=decoder_attention_mask,
|
||||||
|
inputs_embeds=decoder_inputs_embeds,
|
||||||
|
past_key_values=past_key_values,
|
||||||
|
encoder_hidden_states=hidden_states,
|
||||||
|
encoder_attention_mask=attention_mask,
|
||||||
|
head_mask=decoder_head_mask,
|
||||||
|
cross_attn_head_mask=cross_attn_head_mask,
|
||||||
|
use_cache=use_cache,
|
||||||
|
output_attentions=output_attentions,
|
||||||
|
output_hidden_states=output_hidden_states,
|
||||||
|
return_dict=return_dict,
|
||||||
|
)
|
||||||
|
|
||||||
|
sequence_output = decoder_outputs[0]
|
||||||
|
|
||||||
|
# Set device for model parallelism
|
||||||
|
if self.model_parallel:
|
||||||
|
torch.cuda.set_device(self.encoder.first_device)
|
||||||
|
self.lm_head = self.lm_head.to(self.encoder.first_device)
|
||||||
|
sequence_output = sequence_output.to(self.lm_head.weight.device)
|
||||||
|
|
||||||
|
if self.config.tie_word_embeddings:
|
||||||
|
# Rescale output before projecting on vocab See
|
||||||
|
# https://github.com/tensorflow/mesh/blob/fa19d69eafc9a482aff0b59ddd96b025c0cb207d/mesh_tensorflow/transformer/transformer.py#L586
|
||||||
|
sequence_output = sequence_output * (self.model_dim ** -0.5)
|
||||||
|
|
||||||
|
lm_logits = self.lm_head(sequence_output)
|
||||||
|
|
||||||
|
loss = None
|
||||||
|
if labels is not None:
|
||||||
|
loss_fct = CrossEntropyLoss(ignore_index=-100)
|
||||||
|
loss = loss_fct(lm_logits.view(-1, lm_logits.size(-1)), labels.view(-1))
|
||||||
|
# TODO(thom): Add z_loss
|
||||||
|
# https://github.com/tensorflow/mesh/blob/fa19d69eafc9a482aff0b59ddd96b025c0cb207d/mesh_tensorflow/layers.py#L666
|
||||||
|
|
||||||
|
if not return_dict:
|
||||||
|
output = (lm_logits,) + decoder_outputs[1:] + encoder_outputs
|
||||||
|
return ((loss,) + output) if loss is not None else output
|
||||||
|
|
||||||
|
return Seq2SeqLMOutput(
|
||||||
|
loss=loss,
|
||||||
|
logits=lm_logits,
|
||||||
|
past_key_values=decoder_outputs.past_key_values,
|
||||||
|
decoder_hidden_states=decoder_outputs.hidden_states,
|
||||||
|
decoder_attentions=decoder_outputs.attentions,
|
||||||
|
cross_attentions=decoder_outputs.cross_attentions,
|
||||||
|
encoder_last_hidden_state=encoder_outputs.last_hidden_state,
|
||||||
|
encoder_hidden_states=encoder_outputs.hidden_states,
|
||||||
|
encoder_attentions=encoder_outputs.attentions,
|
||||||
|
)
|
||||||
|
|
||||||
|
def prepare_inputs_for_generation(
|
||||||
|
self,
|
||||||
|
input_ids,
|
||||||
|
past=None,
|
||||||
|
attention_mask=None,
|
||||||
|
head_mask=None,
|
||||||
|
decoder_head_mask=None,
|
||||||
|
cross_attn_head_mask=None,
|
||||||
|
use_cache=None,
|
||||||
|
encoder_outputs=None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
|
||||||
|
# cut decoder_input_ids if past is used
|
||||||
|
if past is not None:
|
||||||
|
input_ids = input_ids[:, -1:]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"decoder_input_ids": input_ids,
|
||||||
|
"past_key_values": past,
|
||||||
|
"encoder_outputs": encoder_outputs,
|
||||||
|
"attention_mask": attention_mask,
|
||||||
|
"head_mask": head_mask,
|
||||||
|
"decoder_head_mask": decoder_head_mask,
|
||||||
|
"cross_attn_head_mask": cross_attn_head_mask,
|
||||||
|
"use_cache": use_cache,
|
||||||
|
}
|
||||||
|
|
||||||
|
def prepare_decoder_input_ids_from_labels(self, labels: torch.Tensor):
|
||||||
|
return self._shift_right(labels)
|
||||||
|
|
||||||
|
def generate(
|
||||||
|
self,
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
output = super().generate(*args, **kwargs)
|
||||||
|
return TokenGeneratorOutput(
|
||||||
|
sequences=output if isinstance(output, torch.Tensor) else output[0]
|
||||||
|
)
|
||||||
|
|
||||||
|
def _reorder_cache(self, past, beam_idx):
|
||||||
|
# if decoder past is not included in output
|
||||||
|
# speedy decoding is disabled and no need to reorder
|
||||||
|
if past is None:
|
||||||
|
logger.warning(
|
||||||
|
"You might want to consider setting `use_cache=True` to speed up decoding"
|
||||||
|
)
|
||||||
|
return past
|
||||||
|
|
||||||
|
reordered_decoder_past = ()
|
||||||
|
for layer_past_states in past:
|
||||||
|
# get the correct batch idx from layer past batch dim
|
||||||
|
# batch dim of `past` is at 2nd position
|
||||||
|
reordered_layer_past_states = ()
|
||||||
|
for layer_past_state in layer_past_states:
|
||||||
|
# need to set correct `past` for each of the four key / value states
|
||||||
|
reordered_layer_past_states = reordered_layer_past_states + (
|
||||||
|
layer_past_state.index_select(
|
||||||
|
0, beam_idx.to(layer_past_state.device)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert reordered_layer_past_states[0].shape == layer_past_states[0].shape
|
||||||
|
assert len(reordered_layer_past_states) == len(layer_past_states)
|
||||||
|
|
||||||
|
reordered_decoder_past = reordered_decoder_past + (
|
||||||
|
reordered_layer_past_states,
|
||||||
|
)
|
||||||
|
return reordered_decoder_past
|
Loading…
Reference in New Issue