2023-08-03 06:51:36 +00:00
|
|
|
import copy
|
|
|
|
|
2023-03-15 03:26:10 +00:00
|
|
|
import torch
|
|
|
|
import transformers
|
|
|
|
|
|
|
|
from ..registry import ModelAttribute, model_zoo
|
|
|
|
|
|
|
|
# ===============================
|
|
|
|
# Register single-sentence GPT
|
|
|
|
# ===============================
|
|
|
|
|
|
|
|
|
|
|
|
def data_gen():
|
2023-06-22 02:33:06 +00:00
|
|
|
# Generated from following code snippet
|
|
|
|
#
|
|
|
|
# from transformers import GPT2Tokenizer
|
|
|
|
# input = 'Hello, my dog is cute'
|
|
|
|
# tokenized_input = tokenizer(input, return_tensors='pt')
|
|
|
|
# input_ids = tokenized_input['input_ids']
|
|
|
|
# attention_mask = tokenized_input['attention_mask']
|
2023-08-07 08:41:07 +00:00
|
|
|
input_ids = torch.tensor([[15496, 11, 616, 3290, 318, 13779, 318, 13779]], dtype=torch.int64)
|
|
|
|
attention_mask = torch.tensor([[1, 1, 1, 1, 1, 1, 1, 1]], dtype=torch.int64)
|
2023-06-22 02:33:06 +00:00
|
|
|
return dict(input_ids=input_ids, attention_mask=attention_mask)
|
2023-03-15 03:26:10 +00:00
|
|
|
|
|
|
|
|
2023-06-22 02:33:06 +00:00
|
|
|
def data_gen_for_lm():
|
|
|
|
# LM data gen
|
|
|
|
# the `labels` of LM is the token of the output, cause no padding, use `input_ids` as `labels`
|
|
|
|
data = data_gen()
|
|
|
|
data['labels'] = data['input_ids'].clone()
|
|
|
|
return data
|
2023-03-22 02:40:33 +00:00
|
|
|
|
|
|
|
|
2023-07-19 01:28:27 +00:00
|
|
|
def data_gen_for_question_answering():
|
|
|
|
# question answering data gen
|
|
|
|
# `labels` is the type not the token id for token classification, 0 or 1
|
|
|
|
data = data_gen()
|
|
|
|
start_positions = torch.tensor([0], dtype=torch.int64)
|
|
|
|
data['start_positions'] = start_positions
|
|
|
|
end_positions = torch.tensor([1], dtype=torch.int64)
|
|
|
|
data['end_positions'] = end_positions
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2023-06-22 02:33:06 +00:00
|
|
|
def data_gen_for_token_classification():
|
|
|
|
# token classification data gen
|
|
|
|
# `labels` is the type not the token id for token classification, 0 or 1
|
|
|
|
data = data_gen()
|
2023-08-07 08:41:07 +00:00
|
|
|
data['labels'] = torch.tensor([[0, 0, 0, 0, 0, 0, 0, 1]], dtype=torch.int64)
|
2023-06-22 02:33:06 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def data_gen_for_sequence_classification():
|
|
|
|
# sequence classification data gen
|
|
|
|
data = data_gen()
|
2023-08-03 06:51:36 +00:00
|
|
|
data['labels'] = torch.tensor([1], dtype=torch.int64)
|
2023-06-22 02:33:06 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
# define output transform function
|
2023-03-15 03:26:10 +00:00
|
|
|
output_transform_fn = lambda x: x
|
|
|
|
|
2023-06-22 02:33:06 +00:00
|
|
|
# define loss function
|
2023-08-03 06:51:36 +00:00
|
|
|
loss_fn_for_gpt2_model = lambda x: torch.nn.functional.mse_loss(x.last_hidden_state, torch.ones_like(x.last_hidden_state
|
|
|
|
))
|
2023-06-22 02:33:06 +00:00
|
|
|
loss_fn = lambda x: x.loss
|
|
|
|
|
2023-07-21 08:23:04 +00:00
|
|
|
config = transformers.GPT2Config(n_layer=2,
|
|
|
|
n_head=4,
|
|
|
|
vocab_size=50258,
|
|
|
|
attn_pdrop=0,
|
|
|
|
embd_pdrop=0,
|
|
|
|
resid_pdrop=0,
|
|
|
|
summary_first_dropout=0,
|
2023-08-03 09:50:15 +00:00
|
|
|
hidden_dropout=0,
|
|
|
|
problem_type="single_label_classification",
|
|
|
|
pad_token_id=50256)
|
2023-08-03 06:51:36 +00:00
|
|
|
|
|
|
|
config_for_token_classification = copy.deepcopy(config)
|
|
|
|
config_for_token_classification.num_labels = 2
|
2023-03-15 03:26:10 +00:00
|
|
|
|
|
|
|
# register the following models
|
|
|
|
model_zoo.register(name='transformers_gpt',
|
|
|
|
model_fn=lambda: transformers.GPT2Model(config),
|
|
|
|
data_gen_fn=data_gen,
|
|
|
|
output_transform_fn=output_transform_fn,
|
2023-06-22 02:33:06 +00:00
|
|
|
loss_fn=loss_fn_for_gpt2_model,
|
2023-03-15 03:26:10 +00:00
|
|
|
model_attribute=ModelAttribute(has_control_flow=True))
|
|
|
|
model_zoo.register(name='transformers_gpt_lm',
|
|
|
|
model_fn=lambda: transformers.GPT2LMHeadModel(config),
|
2023-06-22 02:33:06 +00:00
|
|
|
data_gen_fn=data_gen_for_lm,
|
2023-03-15 03:26:10 +00:00
|
|
|
output_transform_fn=output_transform_fn,
|
2023-06-22 02:33:06 +00:00
|
|
|
loss_fn=loss_fn,
|
2023-03-15 03:26:10 +00:00
|
|
|
model_attribute=ModelAttribute(has_control_flow=True))
|
|
|
|
model_zoo.register(name='transformers_gpt_double_heads',
|
|
|
|
model_fn=lambda: transformers.GPT2DoubleHeadsModel(config),
|
2023-06-22 02:33:06 +00:00
|
|
|
data_gen_fn=data_gen_for_lm,
|
2023-03-15 03:26:10 +00:00
|
|
|
output_transform_fn=output_transform_fn,
|
2023-06-22 02:33:06 +00:00
|
|
|
loss_fn=loss_fn,
|
2023-03-15 03:26:10 +00:00
|
|
|
model_attribute=ModelAttribute(has_control_flow=True))
|
2023-07-19 01:28:27 +00:00
|
|
|
model_zoo.register(name='transformers_gpt_for_question_answering',
|
|
|
|
model_fn=lambda: transformers.GPT2ForQuestionAnswering(config),
|
|
|
|
data_gen_fn=data_gen_for_question_answering,
|
|
|
|
output_transform_fn=output_transform_fn,
|
|
|
|
loss_fn=loss_fn,
|
|
|
|
model_attribute=ModelAttribute(has_control_flow=True))
|
2023-03-15 03:26:10 +00:00
|
|
|
model_zoo.register(name='transformers_gpt_for_token_classification',
|
2023-08-03 06:51:36 +00:00
|
|
|
model_fn=lambda: transformers.GPT2ForTokenClassification(config_for_token_classification),
|
2023-06-22 02:33:06 +00:00
|
|
|
data_gen_fn=data_gen_for_token_classification,
|
2023-03-15 03:26:10 +00:00
|
|
|
output_transform_fn=output_transform_fn,
|
2023-06-22 02:33:06 +00:00
|
|
|
loss_fn=loss_fn,
|
2023-03-15 03:26:10 +00:00
|
|
|
model_attribute=ModelAttribute(has_control_flow=True))
|
|
|
|
model_zoo.register(name='transformers_gpt_for_sequence_classification',
|
2023-08-03 06:51:36 +00:00
|
|
|
model_fn=lambda: transformers.GPT2ForSequenceClassification(config_for_token_classification),
|
2023-06-22 02:33:06 +00:00
|
|
|
data_gen_fn=data_gen_for_sequence_classification,
|
2023-03-15 03:26:10 +00:00
|
|
|
output_transform_fn=output_transform_fn,
|
2023-06-22 02:33:06 +00:00
|
|
|
loss_fn=loss_fn,
|
2023-03-15 03:26:10 +00:00
|
|
|
model_attribute=ModelAttribute(has_control_flow=True))
|