Commit
•
7763efd
1
Parent(s):
16872b6
Upload model
Browse files- hf.py +88 -0
- pytorch_model.bin +1 -1
hf.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Tuple, Union
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from transformers import PretrainedConfig
|
5 |
+
from transformers.modeling_outputs import BaseModelOutputWithPoolingAndCrossAttentions
|
6 |
+
from transformers.models.bert.modeling_bert import BertModel
|
7 |
+
|
8 |
+
|
9 |
+
class GoldenRetrieverConfig(PretrainedConfig):
|
10 |
+
model_type = "bert"
|
11 |
+
|
12 |
+
def __init__(
|
13 |
+
self,
|
14 |
+
vocab_size=30522,
|
15 |
+
hidden_size=768,
|
16 |
+
num_hidden_layers=12,
|
17 |
+
num_attention_heads=12,
|
18 |
+
intermediate_size=3072,
|
19 |
+
hidden_act="gelu",
|
20 |
+
hidden_dropout_prob=0.1,
|
21 |
+
attention_probs_dropout_prob=0.1,
|
22 |
+
max_position_embeddings=512,
|
23 |
+
type_vocab_size=2,
|
24 |
+
initializer_range=0.02,
|
25 |
+
layer_norm_eps=1e-12,
|
26 |
+
pad_token_id=0,
|
27 |
+
position_embedding_type="absolute",
|
28 |
+
use_cache=True,
|
29 |
+
classifier_dropout=None,
|
30 |
+
**kwargs,
|
31 |
+
):
|
32 |
+
super().__init__(pad_token_id=pad_token_id, **kwargs)
|
33 |
+
|
34 |
+
self.vocab_size = vocab_size
|
35 |
+
self.hidden_size = hidden_size
|
36 |
+
self.num_hidden_layers = num_hidden_layers
|
37 |
+
self.num_attention_heads = num_attention_heads
|
38 |
+
self.hidden_act = hidden_act
|
39 |
+
self.intermediate_size = intermediate_size
|
40 |
+
self.hidden_dropout_prob = hidden_dropout_prob
|
41 |
+
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
42 |
+
self.max_position_embeddings = max_position_embeddings
|
43 |
+
self.type_vocab_size = type_vocab_size
|
44 |
+
self.initializer_range = initializer_range
|
45 |
+
self.layer_norm_eps = layer_norm_eps
|
46 |
+
self.position_embedding_type = position_embedding_type
|
47 |
+
self.use_cache = use_cache
|
48 |
+
self.classifier_dropout = classifier_dropout
|
49 |
+
|
50 |
+
|
51 |
+
class GoldenRetrieverModel(BertModel):
|
52 |
+
config_class = GoldenRetrieverConfig
|
53 |
+
|
54 |
+
def __init__(self, config, *args, **kwargs):
|
55 |
+
super().__init__(config)
|
56 |
+
self.layer_norm_layer = torch.nn.LayerNorm(
|
57 |
+
config.hidden_size, eps=config.layer_norm_eps
|
58 |
+
)
|
59 |
+
|
60 |
+
def forward(
|
61 |
+
self, **kwargs
|
62 |
+
) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]:
|
63 |
+
attention_mask = kwargs.get("attention_mask", None)
|
64 |
+
model_outputs = super().forward(**kwargs)
|
65 |
+
if attention_mask is None:
|
66 |
+
pooler_output = model_outputs.pooler_output
|
67 |
+
else:
|
68 |
+
token_embeddings = model_outputs.last_hidden_state
|
69 |
+
input_mask_expanded = (
|
70 |
+
attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
71 |
+
)
|
72 |
+
pooler_output = torch.sum(
|
73 |
+
token_embeddings * input_mask_expanded, 1
|
74 |
+
) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
75 |
+
|
76 |
+
pooler_output = self.layer_norm_layer(pooler_output)
|
77 |
+
|
78 |
+
if not kwargs.get("return_dict", True):
|
79 |
+
return (model_outputs[0], pooler_output) + model_outputs[2:]
|
80 |
+
|
81 |
+
return BaseModelOutputWithPoolingAndCrossAttentions(
|
82 |
+
last_hidden_state=model_outputs.last_hidden_state,
|
83 |
+
pooler_output=pooler_output,
|
84 |
+
past_key_values=model_outputs.past_key_values,
|
85 |
+
hidden_states=model_outputs.hidden_states,
|
86 |
+
attentions=model_outputs.attentions,
|
87 |
+
cross_attentions=model_outputs.cross_attentions,
|
88 |
+
)
|
pytorch_model.bin
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 437999665
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cedb009f460ea8ff29d0acaec36f92f7e89edff73e55d5d85c96b0ba41bed25a
|
3 |
size 437999665
|