2023-03-15 03:26:10 +00:00
|
|
|
import torch
|
|
|
|
import transformers
|
|
|
|
|
|
|
|
from ..registry import ModelAttribute, model_zoo
|
|
|
|
|
|
|
|
# ===============================
|
|
|
|
# Register single-sentence T5
|
|
|
|
# ===============================
|
|
|
|
|
|
|
|
|
2023-06-21 01:32:46 +00:00
|
|
|
# define data gen function
|
2023-03-15 03:26:10 +00:00
|
|
|
def data_gen_for_encoder_only():
|
2023-06-21 01:32:46 +00:00
|
|
|
# Generated from following code snippet
|
|
|
|
#
|
|
|
|
# from transformers import T5Config, T5Tokenizer
|
|
|
|
# config = T5Config(decoder_start_token_id=0)
|
|
|
|
# tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
|
|
|
# input_ids = tokenizer("translate English to German: The house is wonderful.", return_tensors="pt").input_ids
|
2023-08-14 07:49:13 +00:00
|
|
|
input_ids = torch.Tensor([[13959, 1566, 12, 2968, 10, 37, 629, 19, 1627, 5, 1, 12, 1627, 5, 1, 12]]).long()
|
|
|
|
attention_mask = torch.Tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]]).long()
|
2023-08-07 08:41:07 +00:00
|
|
|
return dict(input_ids=input_ids, attention_mask=attention_mask)
|
2023-03-15 03:26:10 +00:00
|
|
|
|
|
|
|
|
2023-06-21 01:32:46 +00:00
|
|
|
def data_gen_for_conditional_generation():
|
|
|
|
# labels is generated with the following code
|
|
|
|
#
|
|
|
|
# labels = tokenizer("Das Haus ist wunderbar.", return_tensors="pt").input_ids
|
|
|
|
data = data_gen_for_encoder_only()
|
2023-08-14 07:49:13 +00:00
|
|
|
labels = torch.Tensor([[644, 4598, 229, 19250, 5, 1, 644, 4598, 229, 19250, 5, 1, 229, 19250, 5, 1]]).long()
|
2023-09-19 06:20:26 +00:00
|
|
|
data["labels"] = labels
|
2023-06-21 01:32:46 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def data_gen_for_t5_model():
|
|
|
|
# decoder_inputs_ids is obtained with the following code
|
|
|
|
# decoder_input_ids = model._shift_right(input_ids)
|
|
|
|
data = data_gen_for_encoder_only()
|
2023-08-14 07:49:13 +00:00
|
|
|
decoder_input_ids = torch.Tensor([[0, 13959, 1566, 12, 2968, 10, 37, 629, 19, 1627, 5, 5, 19, 1627, 5, 5]]).long()
|
2023-09-19 06:20:26 +00:00
|
|
|
data["decoder_input_ids"] = decoder_input_ids
|
2023-06-21 01:32:46 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
# output transform function
|
2023-03-15 03:26:10 +00:00
|
|
|
output_transform_fn = lambda x: x
|
|
|
|
|
2023-09-12 09:41:52 +00:00
|
|
|
# define loss function
|
2023-06-21 01:32:46 +00:00
|
|
|
loss_fn_for_t5_model = lambda x: x.last_hidden_state.mean()
|
|
|
|
loss_fn_for_encoder_only = lambda x: x.last_hidden_state.mean()
|
|
|
|
loss_fn_for_conditional_generation = lambda x: x.loss
|
|
|
|
|
|
|
|
# define model config
|
|
|
|
config = transformers.T5Config(d_model=128, num_layers=2, dropout_rate=0, decoder_start_token_id=0)
|
2023-03-15 03:26:10 +00:00
|
|
|
|
|
|
|
# register the following models
|
|
|
|
# transformers.T5Model,
|
|
|
|
# transformers.T5ForConditionalGeneration,
|
|
|
|
# transformers.T5EncoderModel,
|
2023-09-19 06:20:26 +00:00
|
|
|
model_zoo.register(
|
|
|
|
name="transformers_t5",
|
|
|
|
model_fn=lambda: transformers.T5Model(config),
|
|
|
|
data_gen_fn=data_gen_for_t5_model,
|
|
|
|
output_transform_fn=output_transform_fn,
|
|
|
|
loss_fn=loss_fn_for_t5_model,
|
|
|
|
model_attribute=ModelAttribute(has_control_flow=True),
|
|
|
|
)
|
|
|
|
model_zoo.register(
|
|
|
|
name="transformers_t5_for_conditional_generation",
|
|
|
|
model_fn=lambda: transformers.T5ForConditionalGeneration(config),
|
|
|
|
data_gen_fn=data_gen_for_conditional_generation,
|
|
|
|
output_transform_fn=output_transform_fn,
|
|
|
|
loss_fn=loss_fn_for_conditional_generation,
|
|
|
|
model_attribute=ModelAttribute(has_control_flow=True),
|
|
|
|
)
|
|
|
|
model_zoo.register(
|
|
|
|
name="transformers_t5_encoder_model",
|
|
|
|
model_fn=lambda: transformers.T5EncoderModel(config),
|
|
|
|
data_gen_fn=data_gen_for_encoder_only,
|
|
|
|
output_transform_fn=output_transform_fn,
|
|
|
|
loss_fn=loss_fn_for_encoder_only,
|
|
|
|
model_attribute=ModelAttribute(has_control_flow=True),
|
|
|
|
)
|