Upload 4 files
Browse files- conditional_gpt2_model.py +43 -0
- config.json +36 -0
- generation_config.json +6 -0
- pytorch_model.bin +3 -0
conditional_gpt2_model.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import GPT2LMHeadModel
|
2 |
+
|
3 |
+
class ConditionalGPT2LMHeadModel(GPT2LMHeadModel):
|
4 |
+
def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs):
|
5 |
+
# this is the same as `GPT2LMHeadModel` except `encoder_hidden_states` are added to inputs
|
6 |
+
|
7 |
+
token_type_ids = kwargs.get("token_type_ids", None)
|
8 |
+
# only last token for inputs_ids if past is defined in kwargs
|
9 |
+
if past_key_values:
|
10 |
+
input_ids = input_ids[:, -1].unsqueeze(-1)
|
11 |
+
if token_type_ids is not None:
|
12 |
+
token_type_ids = token_type_ids[:, -1].unsqueeze(-1)
|
13 |
+
|
14 |
+
attention_mask = kwargs.get("attention_mask", None)
|
15 |
+
position_ids = kwargs.get("position_ids", None)
|
16 |
+
|
17 |
+
if attention_mask is not None and position_ids is None:
|
18 |
+
# create position_ids on the fly for batch generation
|
19 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
20 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
21 |
+
if past_key_values:
|
22 |
+
position_ids = position_ids[:, -1].unsqueeze(-1)
|
23 |
+
else:
|
24 |
+
position_ids = None
|
25 |
+
|
26 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
27 |
+
if inputs_embeds is not None and past_key_values is None:
|
28 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
29 |
+
else:
|
30 |
+
model_inputs = {"input_ids": input_ids}
|
31 |
+
|
32 |
+
model_inputs['encoder_hidden_states'] = kwargs.get('encoder_hidden_states', None)
|
33 |
+
|
34 |
+
model_inputs.update(
|
35 |
+
{
|
36 |
+
"past_key_values": past_key_values,
|
37 |
+
"use_cache": kwargs.get("use_cache"),
|
38 |
+
"position_ids": position_ids,
|
39 |
+
"attention_mask": attention_mask,
|
40 |
+
"token_type_ids": token_type_ids,
|
41 |
+
}
|
42 |
+
)
|
43 |
+
return model_inputs
|
config.json
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "mean_roberta/checkpoint-50000/",
|
3 |
+
"activation_function": "gelu_new",
|
4 |
+
"add_cross_attention": true,
|
5 |
+
"architectures": [
|
6 |
+
"ConditionalGPT2LMHeadModel"
|
7 |
+
],
|
8 |
+
"attn_pdrop": 0.1,
|
9 |
+
"auto_map": {
|
10 |
+
"AutoModelForCausalLM": "conditional_gpt2_model.ConditionalGPT2LMHeadModel"
|
11 |
+
},
|
12 |
+
"bos_token_id": 0,
|
13 |
+
"embd_pdrop": 0.1,
|
14 |
+
"eos_token_id": 2,
|
15 |
+
"initializer_range": 0.02,
|
16 |
+
"layer_norm_epsilon": 1e-05,
|
17 |
+
"model_type": "gpt2",
|
18 |
+
"n_embd": 768,
|
19 |
+
"n_head": 8,
|
20 |
+
"n_inner": null,
|
21 |
+
"n_layer": 6,
|
22 |
+
"n_positions": 256,
|
23 |
+
"reorder_and_upcast_attn": false,
|
24 |
+
"resid_pdrop": 0.1,
|
25 |
+
"scale_attn_by_inverse_layer_idx": false,
|
26 |
+
"scale_attn_weights": true,
|
27 |
+
"summary_activation": null,
|
28 |
+
"summary_first_dropout": 0.1,
|
29 |
+
"summary_proj_to_labels": true,
|
30 |
+
"summary_type": "cls_index",
|
31 |
+
"summary_use_proj": true,
|
32 |
+
"torch_dtype": "float32",
|
33 |
+
"transformers_version": "4.28.1",
|
34 |
+
"use_cache": true,
|
35 |
+
"vocab_size": 2707
|
36 |
+
}
|
generation_config.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 0,
|
4 |
+
"eos_token_id": 2,
|
5 |
+
"transformers_version": "4.28.1"
|
6 |
+
}
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:995b549b74a984dc299582d9f444288d58380405d5441757f577e67a71d701e4
|
3 |
+
size 236784093
|