Upload model
Browse files- AbLang_bert_model.py +158 -0
- config.json +9 -7
- pytorch_model.bin +2 -2
AbLang_bert_model.py
ADDED
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers.models.bert.modeling_bert import BertEncoder, BertPooler, BertEmbeddings, BaseModelOutputWithPoolingAndCrossAttentions
|
2 |
+
from transformers import BertModel
|
3 |
+
import torch
|
4 |
+
|
5 |
+
class BertEmbeddingsV2(BertEmbeddings):
|
6 |
+
def __init__(self, config):
|
7 |
+
super().__init__(config)
|
8 |
+
self.pad_token_id = config.pad_token_id
|
9 |
+
self.word_embeddings = torch.nn.Embedding(config.vocab_size, config.hidden_size, padding_idx=self.pad_token_id)
|
10 |
+
self.position_embeddings = torch.nn.Embedding(config.max_position_embeddings, config.hidden_size, padding_idx=0) # here padding_idx is always 0
|
11 |
+
self.LayerNorm = torch.nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
12 |
+
self.dropout = torch.nn.Dropout(config.hidden_dropout_prob)
|
13 |
+
|
14 |
+
def forward(self, input_ids=None, pos_tag_ids=None, token_type_ids=None, position_ids=None, inputs_embeds=None, past_key_values_length=0):
|
15 |
+
inputs_embeds = self.word_embeddings(input_ids)
|
16 |
+
position_ids = self.create_position_ids_from_input_ids(input_ids)
|
17 |
+
position_embeddings = self.position_embeddings(position_ids)
|
18 |
+
embeddings = inputs_embeds + position_embeddings
|
19 |
+
return self.dropout(self.LayerNorm(embeddings))
|
20 |
+
|
21 |
+
def create_position_ids_from_input_ids(self, input_ids):
|
22 |
+
mask = input_ids.ne(self.pad_token_id).int()
|
23 |
+
return torch.cumsum(mask, dim=1).long() * mask
|
24 |
+
|
25 |
+
|
26 |
+
class BertModelV2(BertModel):
|
27 |
+
def __init__(self, config):
|
28 |
+
super().__init__(config)
|
29 |
+
self.config = config
|
30 |
+
self.embeddings = BertEmbeddingsV2(config)
|
31 |
+
self.encoder = BertEncoder(config)
|
32 |
+
self.pooler = BertPooler(config) if config.add_pooling_layer else None
|
33 |
+
self.init_weights()
|
34 |
+
|
35 |
+
def forward(
|
36 |
+
self,
|
37 |
+
input_ids=None,
|
38 |
+
attention_mask=None,
|
39 |
+
token_type_ids=None,
|
40 |
+
pos_tag_ids=None,
|
41 |
+
position_ids=None,
|
42 |
+
head_mask=None,
|
43 |
+
inputs_embeds=None,
|
44 |
+
encoder_hidden_states=None,
|
45 |
+
encoder_attention_mask=None,
|
46 |
+
past_key_values=None,
|
47 |
+
use_cache=None,
|
48 |
+
output_attentions=None,
|
49 |
+
output_hidden_states=None,
|
50 |
+
return_dict=None,
|
51 |
+
):
|
52 |
+
r"""
|
53 |
+
encoder_hidden_states (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`, `optional`):
|
54 |
+
Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if
|
55 |
+
the model is configured as a decoder.
|
56 |
+
encoder_attention_mask (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`):
|
57 |
+
Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in
|
58 |
+
the cross-attention if the model is configured as a decoder. Mask values selected in ``[0, 1]``:
|
59 |
+
- 1 for tokens that are **not masked**,
|
60 |
+
- 0 for tokens that are **masked**.
|
61 |
+
past_key_values (:obj:`tuple(tuple(torch.FloatTensor))` of length :obj:`config.n_layers` with each tuple having 4 tensors of shape :obj:`(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`):
|
62 |
+
Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding.
|
63 |
+
If :obj:`past_key_values` are used, the user can optionally input only the last :obj:`decoder_input_ids`
|
64 |
+
(those that don't have their past key value states given to this model) of shape :obj:`(batch_size, 1)`
|
65 |
+
instead of all :obj:`decoder_input_ids` of shape :obj:`(batch_size, sequence_length)`.
|
66 |
+
use_cache (:obj:`bool`, `optional`):
|
67 |
+
If set to :obj:`True`, :obj:`past_key_values` key value states are returned and can be used to speed up
|
68 |
+
decoding (see :obj:`past_key_values`).
|
69 |
+
"""
|
70 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
71 |
+
output_hidden_states = (
|
72 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
73 |
+
)
|
74 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
75 |
+
|
76 |
+
if self.config.is_decoder:
|
77 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
78 |
+
else:
|
79 |
+
use_cache = False
|
80 |
+
|
81 |
+
if input_ids is not None and inputs_embeds is not None:
|
82 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
83 |
+
elif input_ids is not None:
|
84 |
+
input_shape = input_ids.size()
|
85 |
+
batch_size, seq_length = input_shape
|
86 |
+
elif inputs_embeds is not None:
|
87 |
+
input_shape = inputs_embeds.size()[:-1]
|
88 |
+
batch_size, seq_length = input_shape
|
89 |
+
else:
|
90 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
91 |
+
|
92 |
+
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
93 |
+
|
94 |
+
# past_key_values_length
|
95 |
+
past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0
|
96 |
+
|
97 |
+
if attention_mask is None:
|
98 |
+
attention_mask = torch.ones(((batch_size, seq_length + past_key_values_length)), device=device)
|
99 |
+
if token_type_ids is None:
|
100 |
+
token_type_ids = torch.zeros(input_shape, dtype=torch.long, device=device)
|
101 |
+
|
102 |
+
# We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length]
|
103 |
+
# ourselves in which case we just need to make it broadcastable to all heads.
|
104 |
+
extended_attention_mask: torch.Tensor = self.get_extended_attention_mask(attention_mask, input_shape, device)
|
105 |
+
|
106 |
+
# If a 2D or 3D attention mask is provided for the cross-attention
|
107 |
+
# we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length]
|
108 |
+
if self.config.is_decoder and encoder_hidden_states is not None:
|
109 |
+
encoder_batch_size, encoder_sequence_length, _ = encoder_hidden_states.size()
|
110 |
+
encoder_hidden_shape = (encoder_batch_size, encoder_sequence_length)
|
111 |
+
if encoder_attention_mask is None:
|
112 |
+
encoder_attention_mask = torch.ones(encoder_hidden_shape, device=device)
|
113 |
+
encoder_extended_attention_mask = self.invert_attention_mask(encoder_attention_mask)
|
114 |
+
else:
|
115 |
+
encoder_extended_attention_mask = None
|
116 |
+
|
117 |
+
# Prepare head mask if needed
|
118 |
+
# 1.0 in head_mask indicate we keep the head
|
119 |
+
# attention_probs has shape bsz x n_heads x N x N
|
120 |
+
# input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
|
121 |
+
# and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
|
122 |
+
head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
|
123 |
+
|
124 |
+
embedding_output = self.embeddings(
|
125 |
+
input_ids=input_ids,
|
126 |
+
position_ids=position_ids,
|
127 |
+
token_type_ids=token_type_ids,
|
128 |
+
pos_tag_ids=pos_tag_ids,
|
129 |
+
inputs_embeds=inputs_embeds,
|
130 |
+
past_key_values_length=past_key_values_length,
|
131 |
+
)
|
132 |
+
encoder_outputs = self.encoder(
|
133 |
+
embedding_output,
|
134 |
+
attention_mask=extended_attention_mask,
|
135 |
+
head_mask=head_mask,
|
136 |
+
encoder_hidden_states=encoder_hidden_states,
|
137 |
+
encoder_attention_mask=encoder_extended_attention_mask,
|
138 |
+
past_key_values=past_key_values,
|
139 |
+
use_cache=use_cache,
|
140 |
+
output_attentions=output_attentions,
|
141 |
+
output_hidden_states=output_hidden_states,
|
142 |
+
return_dict=return_dict,
|
143 |
+
)
|
144 |
+
sequence_output = encoder_outputs[0]
|
145 |
+
pooled_output = self.pooler(sequence_output) if self.pooler is not None else None
|
146 |
+
|
147 |
+
if not return_dict:
|
148 |
+
return (sequence_output, pooled_output) + encoder_outputs[1:]
|
149 |
+
|
150 |
+
return BaseModelOutputWithPoolingAndCrossAttentions(
|
151 |
+
last_hidden_state=sequence_output,
|
152 |
+
pooler_output=pooled_output,
|
153 |
+
past_key_values=encoder_outputs.past_key_values,
|
154 |
+
hidden_states=encoder_outputs.hidden_states,
|
155 |
+
attentions=encoder_outputs.attentions,
|
156 |
+
cross_attentions=encoder_outputs.cross_attentions,
|
157 |
+
)
|
158 |
+
|
config.json
CHANGED
@@ -1,14 +1,13 @@
|
|
1 |
{
|
2 |
-
"
|
3 |
"architectures": [
|
4 |
-
"
|
5 |
],
|
6 |
"attention_probs_dropout_prob": 0.1,
|
7 |
"auto_map": {
|
8 |
-
"
|
9 |
-
"AutoModel": "model.AbLang"
|
10 |
},
|
11 |
-
"
|
12 |
"hidden_act": "gelu",
|
13 |
"hidden_dropout_prob": 0.1,
|
14 |
"hidden_size": 768,
|
@@ -19,8 +18,11 @@
|
|
19 |
"model_type": "bert",
|
20 |
"num_attention_heads": 12,
|
21 |
"num_hidden_layers": 12,
|
22 |
-
"
|
|
|
23 |
"torch_dtype": "float32",
|
24 |
-
"transformers_version": "4.
|
|
|
|
|
25 |
"vocab_size": 24
|
26 |
}
|
|
|
1 |
{
|
2 |
+
"add_pooling_layer": false,
|
3 |
"architectures": [
|
4 |
+
"BertModelV2"
|
5 |
],
|
6 |
"attention_probs_dropout_prob": 0.1,
|
7 |
"auto_map": {
|
8 |
+
"AutoModel": "AbLang_bert_model.BertModelV2"
|
|
|
9 |
},
|
10 |
+
"classifier_dropout": null,
|
11 |
"hidden_act": "gelu",
|
12 |
"hidden_dropout_prob": 0.1,
|
13 |
"hidden_size": 768,
|
|
|
18 |
"model_type": "bert",
|
19 |
"num_attention_heads": 12,
|
20 |
"num_hidden_layers": 12,
|
21 |
+
"pad_token_id": 21,
|
22 |
+
"position_embedding_type": "absolute",
|
23 |
"torch_dtype": "float32",
|
24 |
+
"transformers_version": "4.28.1",
|
25 |
+
"type_vocab_size": 2,
|
26 |
+
"use_cache": true,
|
27 |
"vocab_size": 24
|
28 |
}
|
pytorch_model.bin
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ae9d0ea3772a3dd43f2d7600dcb8ca6264fed3d59f6c73a1a98df223e6a11a16
|
3 |
+
size 340860389
|