Upload ZettHypernet
Browse files- README.md +199 -0
- config.json +86 -0
- configuration_hypernet.py +56 -0
- model.safetensors +3 -0
- modeling_hypernet.py +265 -0
README.md
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
library_name: transformers
|
3 |
+
tags: []
|
4 |
+
---
|
5 |
+
|
6 |
+
# Model Card for Model ID
|
7 |
+
|
8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
## Model Details
|
13 |
+
|
14 |
+
### Model Description
|
15 |
+
|
16 |
+
<!-- Provide a longer summary of what this model is. -->
|
17 |
+
|
18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
19 |
+
|
20 |
+
- **Developed by:** [More Information Needed]
|
21 |
+
- **Funded by [optional]:** [More Information Needed]
|
22 |
+
- **Shared by [optional]:** [More Information Needed]
|
23 |
+
- **Model type:** [More Information Needed]
|
24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
25 |
+
- **License:** [More Information Needed]
|
26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
+
|
28 |
+
### Model Sources [optional]
|
29 |
+
|
30 |
+
<!-- Provide the basic links for the model. -->
|
31 |
+
|
32 |
+
- **Repository:** [More Information Needed]
|
33 |
+
- **Paper [optional]:** [More Information Needed]
|
34 |
+
- **Demo [optional]:** [More Information Needed]
|
35 |
+
|
36 |
+
## Uses
|
37 |
+
|
38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
39 |
+
|
40 |
+
### Direct Use
|
41 |
+
|
42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
43 |
+
|
44 |
+
[More Information Needed]
|
45 |
+
|
46 |
+
### Downstream Use [optional]
|
47 |
+
|
48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
49 |
+
|
50 |
+
[More Information Needed]
|
51 |
+
|
52 |
+
### Out-of-Scope Use
|
53 |
+
|
54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
55 |
+
|
56 |
+
[More Information Needed]
|
57 |
+
|
58 |
+
## Bias, Risks, and Limitations
|
59 |
+
|
60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
61 |
+
|
62 |
+
[More Information Needed]
|
63 |
+
|
64 |
+
### Recommendations
|
65 |
+
|
66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
67 |
+
|
68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
69 |
+
|
70 |
+
## How to Get Started with the Model
|
71 |
+
|
72 |
+
Use the code below to get started with the model.
|
73 |
+
|
74 |
+
[More Information Needed]
|
75 |
+
|
76 |
+
## Training Details
|
77 |
+
|
78 |
+
### Training Data
|
79 |
+
|
80 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
81 |
+
|
82 |
+
[More Information Needed]
|
83 |
+
|
84 |
+
### Training Procedure
|
85 |
+
|
86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
87 |
+
|
88 |
+
#### Preprocessing [optional]
|
89 |
+
|
90 |
+
[More Information Needed]
|
91 |
+
|
92 |
+
|
93 |
+
#### Training Hyperparameters
|
94 |
+
|
95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
96 |
+
|
97 |
+
#### Speeds, Sizes, Times [optional]
|
98 |
+
|
99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
100 |
+
|
101 |
+
[More Information Needed]
|
102 |
+
|
103 |
+
## Evaluation
|
104 |
+
|
105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
106 |
+
|
107 |
+
### Testing Data, Factors & Metrics
|
108 |
+
|
109 |
+
#### Testing Data
|
110 |
+
|
111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
112 |
+
|
113 |
+
[More Information Needed]
|
114 |
+
|
115 |
+
#### Factors
|
116 |
+
|
117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
118 |
+
|
119 |
+
[More Information Needed]
|
120 |
+
|
121 |
+
#### Metrics
|
122 |
+
|
123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
124 |
+
|
125 |
+
[More Information Needed]
|
126 |
+
|
127 |
+
### Results
|
128 |
+
|
129 |
+
[More Information Needed]
|
130 |
+
|
131 |
+
#### Summary
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
## Model Examination [optional]
|
136 |
+
|
137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
138 |
+
|
139 |
+
[More Information Needed]
|
140 |
+
|
141 |
+
## Environmental Impact
|
142 |
+
|
143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
144 |
+
|
145 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
146 |
+
|
147 |
+
- **Hardware Type:** [More Information Needed]
|
148 |
+
- **Hours used:** [More Information Needed]
|
149 |
+
- **Cloud Provider:** [More Information Needed]
|
150 |
+
- **Compute Region:** [More Information Needed]
|
151 |
+
- **Carbon Emitted:** [More Information Needed]
|
152 |
+
|
153 |
+
## Technical Specifications [optional]
|
154 |
+
|
155 |
+
### Model Architecture and Objective
|
156 |
+
|
157 |
+
[More Information Needed]
|
158 |
+
|
159 |
+
### Compute Infrastructure
|
160 |
+
|
161 |
+
[More Information Needed]
|
162 |
+
|
163 |
+
#### Hardware
|
164 |
+
|
165 |
+
[More Information Needed]
|
166 |
+
|
167 |
+
#### Software
|
168 |
+
|
169 |
+
[More Information Needed]
|
170 |
+
|
171 |
+
## Citation [optional]
|
172 |
+
|
173 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
+
|
175 |
+
**BibTeX:**
|
176 |
+
|
177 |
+
[More Information Needed]
|
178 |
+
|
179 |
+
**APA:**
|
180 |
+
|
181 |
+
[More Information Needed]
|
182 |
+
|
183 |
+
## Glossary [optional]
|
184 |
+
|
185 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
+
|
187 |
+
[More Information Needed]
|
188 |
+
|
189 |
+
## More Information [optional]
|
190 |
+
|
191 |
+
[More Information Needed]
|
192 |
+
|
193 |
+
## Model Card Authors [optional]
|
194 |
+
|
195 |
+
[More Information Needed]
|
196 |
+
|
197 |
+
## Model Card Contact
|
198 |
+
|
199 |
+
[More Information Needed]
|
config.json
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "FacebookAI/xlm-roberta-base",
|
3 |
+
"architectures": [
|
4 |
+
"ZettHypernet"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoConfig": "configuration_hypernet.ZettHypernetConfig",
|
9 |
+
"AutoModel": "modeling_hypernet.ZettHypernet"
|
10 |
+
},
|
11 |
+
"bos_token_id": 0,
|
12 |
+
"classifier_dropout": null,
|
13 |
+
"eos_token_id": 2,
|
14 |
+
"hidden_act": "gelu",
|
15 |
+
"hidden_dropout_prob": 0.1,
|
16 |
+
"hidden_size": 768,
|
17 |
+
"hn_add_inter_token_attention": false,
|
18 |
+
"hn_concat_last_hidden_state": false,
|
19 |
+
"hn_embed_lang_id": true,
|
20 |
+
"hn_embed_target_priors": false,
|
21 |
+
"hn_embed_using_source_embeddings": true,
|
22 |
+
"hn_hidden_size": 768,
|
23 |
+
"hn_inter_token_attention_bias_by_priors": true,
|
24 |
+
"hn_inter_token_attention_bias_scaler": 1.0,
|
25 |
+
"hn_intermediate_size": 1536,
|
26 |
+
"hn_language_adapter_bottleneck_dim": 0,
|
27 |
+
"hn_model_name_or_path": "roberta-base",
|
28 |
+
"hn_model_type": "roberta",
|
29 |
+
"hn_n_extra_tokens": 161,
|
30 |
+
"hn_n_inter_token_blocks": 16,
|
31 |
+
"hn_n_layers": 3,
|
32 |
+
"hn_num_attention_heads": 12,
|
33 |
+
"hn_predict_bias": true,
|
34 |
+
"hn_rescale_embeddings": true,
|
35 |
+
"hn_single_head": false,
|
36 |
+
"hn_surface_maxlen": 7,
|
37 |
+
"initializer_range": 0.02,
|
38 |
+
"intermediate_size": 3072,
|
39 |
+
"langs": [
|
40 |
+
"en",
|
41 |
+
"ru",
|
42 |
+
"de",
|
43 |
+
"es",
|
44 |
+
"fr",
|
45 |
+
"it",
|
46 |
+
"pt",
|
47 |
+
"el",
|
48 |
+
"ko",
|
49 |
+
"fi",
|
50 |
+
"id",
|
51 |
+
"tr",
|
52 |
+
"ar",
|
53 |
+
"vi",
|
54 |
+
"bg",
|
55 |
+
"ca",
|
56 |
+
"hi",
|
57 |
+
"et",
|
58 |
+
"bn",
|
59 |
+
"ta",
|
60 |
+
"ur",
|
61 |
+
"sw",
|
62 |
+
"te",
|
63 |
+
"eu",
|
64 |
+
"ht",
|
65 |
+
"qu"
|
66 |
+
],
|
67 |
+
"layer_norm_eps": 1e-05,
|
68 |
+
"max_position_embeddings": 514,
|
69 |
+
"n_embd": 768,
|
70 |
+
"n_langs": 26,
|
71 |
+
"name": "v7:xlmr:multilingual_long:lw=0.5_26l",
|
72 |
+
"num_attention_heads": 12,
|
73 |
+
"num_hidden_layers": 12,
|
74 |
+
"original_vocab_size": 250002,
|
75 |
+
"output_past": true,
|
76 |
+
"pad_token_id": 1,
|
77 |
+
"position_embedding_type": "absolute",
|
78 |
+
"separate_out_embeddings": false,
|
79 |
+
"torch_dtype": "float32",
|
80 |
+
"transformers_version": "4.39.0.dev0",
|
81 |
+
"type_vocab_size": 1,
|
82 |
+
"use_cache": true,
|
83 |
+
"use_unigram_bias": true,
|
84 |
+
"vocab_size": 32896,
|
85 |
+
"wandb_run_id": "eyql5ryv"
|
86 |
+
}
|
configuration_hypernet.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PretrainedConfig
|
2 |
+
|
3 |
+
class ZettHypernetConfig(PretrainedConfig):
|
4 |
+
def __init__(
|
5 |
+
self,
|
6 |
+
hn_model_name_or_path: str = "roberta-base",
|
7 |
+
hn_surface_maxlen: int = 16,
|
8 |
+
hn_n_layers: int = 3,
|
9 |
+
n_embd: int = 768,
|
10 |
+
hn_hidden_size: int = None,
|
11 |
+
hn_intermediate_size: int = None,
|
12 |
+
hn_rescale_embeddings: bool = False,
|
13 |
+
use_unigram_bias: bool = False,
|
14 |
+
hn_embed_target_priors: bool = False,
|
15 |
+
hn_add_inter_token_attention: bool = False,
|
16 |
+
hn_inter_token_attention_bias_by_priors: bool = False,
|
17 |
+
hn_inter_token_attention_bias_scaler: float = 1.0,
|
18 |
+
hn_n_inter_token_blocks: int = 16,
|
19 |
+
hn_language_adapter_bottleneck_dim: int = 0,
|
20 |
+
hn_embed_using_source_embeddings: bool = False,
|
21 |
+
hn_concat_last_hidden_state: bool = False,
|
22 |
+
hn_single_head: bool = False,
|
23 |
+
hn_predict_bias: bool = True,
|
24 |
+
hn_num_attention_heads: int = None,
|
25 |
+
hn_embed_lang_id: bool = False,
|
26 |
+
hn_model_type: str = "roberta",
|
27 |
+
n_langs: int = None, # set in train.py
|
28 |
+
**kwargs
|
29 |
+
):
|
30 |
+
super().__init__(**kwargs)
|
31 |
+
|
32 |
+
self.model_type = "zett_hypernetwork"
|
33 |
+
self.hn_model_name_or_path = hn_model_name_or_path
|
34 |
+
self.hn_surface_maxlen = hn_surface_maxlen
|
35 |
+
self.hn_n_layers = hn_n_layers
|
36 |
+
self.n_embd = n_embd
|
37 |
+
self.hn_hidden_size = hn_hidden_size
|
38 |
+
self.hn_intermediate_size = hn_intermediate_size
|
39 |
+
self.hn_rescale_embeddings = hn_rescale_embeddings
|
40 |
+
self.use_unigram_bias = use_unigram_bias
|
41 |
+
self.hn_embed_target_priors = hn_embed_target_priors
|
42 |
+
self.hn_add_inter_token_attention = hn_add_inter_token_attention
|
43 |
+
self.hn_inter_token_attention_bias_by_priors = (
|
44 |
+
hn_inter_token_attention_bias_by_priors
|
45 |
+
)
|
46 |
+
self.hn_inter_token_attention_bias_scaler = hn_inter_token_attention_bias_scaler
|
47 |
+
self.hn_n_inter_token_blocks = hn_n_inter_token_blocks
|
48 |
+
self.hn_language_adapter_bottleneck_dim = hn_language_adapter_bottleneck_dim
|
49 |
+
self.hn_embed_using_source_embeddings = hn_embed_using_source_embeddings
|
50 |
+
self.hn_concat_last_hidden_state = hn_concat_last_hidden_state
|
51 |
+
self.hn_single_head = hn_single_head
|
52 |
+
self.hn_predict_bias = hn_predict_bias
|
53 |
+
self.hn_num_attention_heads = hn_num_attention_heads
|
54 |
+
self.hn_embed_lang_id = hn_embed_lang_id
|
55 |
+
self.hn_model_type = hn_model_type
|
56 |
+
self.n_langs = n_langs
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:634c7cf5edd6467b80cd8434446a6d4ce76cfa070e4c632231621ac027c84263
|
3 |
+
size 82546892
|
modeling_hypernet.py
ADDED
@@ -0,0 +1,265 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .configuration_hypernet import ZettHypernetConfig
|
2 |
+
from transformers import PreTrainedModel, RobertaConfig, RobertaModel
|
3 |
+
from functools import partial
|
4 |
+
|
5 |
+
from torch import nn as nn
|
6 |
+
import torch
|
7 |
+
from torch.nn import functional as F
|
8 |
+
|
9 |
+
class Rescaler(nn.Module):
|
10 |
+
def __init__(self, dim: int):
|
11 |
+
super().__init__()
|
12 |
+
|
13 |
+
self.dim = dim
|
14 |
+
|
15 |
+
self.w = nn.Parameter(torch.ones((1, self.dim)), requires_grad=False)
|
16 |
+
self.b = nn.Parameter(torch.ones((1, self.dim)), requires_grad=False)
|
17 |
+
|
18 |
+
def __call__(self, x):
|
19 |
+
return self.w * x + self.b
|
20 |
+
|
21 |
+
|
22 |
+
class ProjectorBlock(nn.Module):
|
23 |
+
def __init__(self, input_dim: int, dim: int, intermediate_dim: int):
|
24 |
+
super().__init__()
|
25 |
+
|
26 |
+
self.input_dim = input_dim
|
27 |
+
self.dim = dim
|
28 |
+
self.intermediate_dim = intermediate_dim
|
29 |
+
|
30 |
+
self.dense1 = nn.Linear(self.input_dim, self.intermediate_dim)
|
31 |
+
self.dense2 = nn.Linear(self.intermediate_dim, self.dim)
|
32 |
+
|
33 |
+
self.ln = nn.LayerNorm(self.dim, eps=1e-6)
|
34 |
+
|
35 |
+
def __call__(self, x):
|
36 |
+
h = F.gelu(
|
37 |
+
self.dense2(F.gelu(self.dense1(x), approximate="tanh")),
|
38 |
+
approximate="tanh",
|
39 |
+
)
|
40 |
+
return self.ln(h + x)
|
41 |
+
|
42 |
+
|
43 |
+
class ZettHypernet(PreTrainedModel):
|
44 |
+
config_class = ZettHypernetConfig
|
45 |
+
|
46 |
+
def __init__(self, config: ZettHypernetConfig):
|
47 |
+
super().__init__(config)
|
48 |
+
|
49 |
+
self.config = config
|
50 |
+
self.has_separate_out_embeddings = getattr(
|
51 |
+
self.config, "separate_out_embeddings", False
|
52 |
+
)
|
53 |
+
self.lang_embeddings = nn.Embedding(
|
54 |
+
self.config.n_langs, self.config.hn_hidden_size
|
55 |
+
)
|
56 |
+
|
57 |
+
if self.has_separate_out_embeddings:
|
58 |
+
n_in_embd = self.config.n_embd * 2
|
59 |
+
n_out_embd = self.config.n_embd
|
60 |
+
else:
|
61 |
+
n_in_embd = self.config.n_embd
|
62 |
+
n_out_embd = self.config.n_embd
|
63 |
+
|
64 |
+
if self.config.hn_model_type == "roberta":
|
65 |
+
config = RobertaConfig.from_pretrained(
|
66 |
+
self.config.hn_model_name_or_path
|
67 |
+
)
|
68 |
+
config.num_hidden_layers = self.config.hn_n_layers
|
69 |
+
config.hidden_size = self.config.hn_hidden_size
|
70 |
+
config.intermediate_size = self.config.hn_intermediate_size
|
71 |
+
if getattr(self.config, "hn_num_attention_heads", None) is None:
|
72 |
+
self.config.hn_num_attention_heads = self.config.hn_hidden_size // 64
|
73 |
+
config.num_attention_heads = self.config.hn_num_attention_heads
|
74 |
+
self.embed_init_range = config.initializer_range
|
75 |
+
module_class = partial(RobertaModel, add_pooling_layer=False)
|
76 |
+
elif self.config.hn_model_type == "t5":
|
77 |
+
raise NotImplementedError()
|
78 |
+
|
79 |
+
if self.config.hn_embed_using_source_embeddings:
|
80 |
+
# do not need to alloc embeddings since inputs_embeds is always used
|
81 |
+
config.vocab_size = self.config.pad_token_id + 1
|
82 |
+
|
83 |
+
if (
|
84 |
+
self.config.hn_add_inter_token_attention
|
85 |
+
or self.config.hn_embed_target_priors
|
86 |
+
):
|
87 |
+
raise NotImplementedError()
|
88 |
+
|
89 |
+
self.pad_token_id = self.config.pad_token_id
|
90 |
+
assert self.pad_token_id is not None
|
91 |
+
self.model = module_class(config)
|
92 |
+
|
93 |
+
# need at least one embedding
|
94 |
+
self.fallback_embeddings = nn.Embedding(
|
95 |
+
max(self.config.hn_n_extra_tokens, 1), n_in_embd
|
96 |
+
)
|
97 |
+
|
98 |
+
if self.config.hn_embed_using_source_embeddings:
|
99 |
+
self.input_projection = nn.Sequential(
|
100 |
+
*[
|
101 |
+
nn.Linear(n_in_embd, self.config.hn_hidden_size),
|
102 |
+
ProjectorBlock(
|
103 |
+
self.config.hn_hidden_size,
|
104 |
+
self.config.hn_hidden_size,
|
105 |
+
self.config.hn_intermediate_size,
|
106 |
+
),
|
107 |
+
]
|
108 |
+
)
|
109 |
+
|
110 |
+
if self.config.hn_single_head:
|
111 |
+
self.output_projection = nn.Sequential(
|
112 |
+
*[
|
113 |
+
ProjectorBlock(
|
114 |
+
self.config.hn_hidden_size,
|
115 |
+
self.config.hn_hidden_size,
|
116 |
+
self.config.hn_intermediate_size,
|
117 |
+
),
|
118 |
+
nn.Linear(self.config.hn_hidden_size, n_in_embd),
|
119 |
+
]
|
120 |
+
)
|
121 |
+
else:
|
122 |
+
self.output_projection = nn.Sequential(
|
123 |
+
*[
|
124 |
+
ProjectorBlock(
|
125 |
+
self.config.hn_hidden_size,
|
126 |
+
self.config.hn_hidden_size,
|
127 |
+
self.config.hn_intermediate_size,
|
128 |
+
),
|
129 |
+
nn.Linear(self.config.hn_hidden_size, n_out_embd),
|
130 |
+
]
|
131 |
+
)
|
132 |
+
if self.has_separate_out_embeddings:
|
133 |
+
self.output_projection_out = nn.Sequential(
|
134 |
+
*[
|
135 |
+
ProjectorBlock(
|
136 |
+
self.config.hn_hidden_size,
|
137 |
+
self.config.hn_hidden_size,
|
138 |
+
self.config.hn_intermediate_size,
|
139 |
+
),
|
140 |
+
nn.Linear(self.config.hn_hidden_size, self.config.n_embd),
|
141 |
+
]
|
142 |
+
)
|
143 |
+
|
144 |
+
if self.config.hn_rescale_embeddings:
|
145 |
+
self.in_scaler = Rescaler(n_in_embd)
|
146 |
+
self.scaler = Rescaler(n_out_embd)
|
147 |
+
|
148 |
+
if self.has_separate_out_embeddings:
|
149 |
+
self.out_scaler = Rescaler(self.config.n_embd)
|
150 |
+
|
151 |
+
if getattr(self.config, "hn_predict_bias", False):
|
152 |
+
self.bias_projection = nn.Linear(self.config.hn_hidden_size, 1)
|
153 |
+
|
154 |
+
def __call__(
|
155 |
+
self,
|
156 |
+
target_surface_forms,
|
157 |
+
target_priors=None,
|
158 |
+
source_embeddings=None,
|
159 |
+
lang_index=None,
|
160 |
+
deterministic: bool = True,
|
161 |
+
):
|
162 |
+
if target_priors is not None:
|
163 |
+
raise NotImplementedError()
|
164 |
+
|
165 |
+
if not self.config.hn_embed_using_source_embeddings:
|
166 |
+
raise NotImplementedError()
|
167 |
+
|
168 |
+
use_fallback = target_surface_forms >= self.config.original_vocab_size
|
169 |
+
|
170 |
+
main_ids = torch.minimum(
|
171 |
+
target_surface_forms, torch.tensor(self.config.original_vocab_size - 1, device=self.device)
|
172 |
+
)
|
173 |
+
fallback_ids = torch.maximum(
|
174 |
+
target_surface_forms - self.config.original_vocab_size, torch.tensor(0, device=self.device)
|
175 |
+
)
|
176 |
+
|
177 |
+
source_embeds = F.embedding(main_ids, weight=source_embeddings)
|
178 |
+
|
179 |
+
if self.config.hn_rescale_embeddings:
|
180 |
+
source_embeds = self.in_scaler(source_embeds)
|
181 |
+
|
182 |
+
inputs_embeds = torch.where(
|
183 |
+
use_fallback[..., None],
|
184 |
+
self.fallback_embeddings(fallback_ids),
|
185 |
+
source_embeds,
|
186 |
+
)
|
187 |
+
inputs_embeds = self.input_projection(inputs_embeds)
|
188 |
+
attention_mask = target_surface_forms != self.pad_token_id
|
189 |
+
|
190 |
+
if self.config.hn_embed_lang_id:
|
191 |
+
lang_embedding = self.lang_embeddings(lang_index).squeeze()
|
192 |
+
# position embed and type embed are added afterwards only in PT version so we need to subtract them here
|
193 |
+
lang_embedding -= self.model.embeddings.token_type_embeddings(
|
194 |
+
torch.tensor(0, device=self.device)
|
195 |
+
) + self.model.embeddings.position_embeddings(
|
196 |
+
torch.tensor(attention_mask.shape[1], device=self.device)
|
197 |
+
)
|
198 |
+
|
199 |
+
lang_embedding = lang_embedding[None, None, :].expand(
|
200 |
+
inputs_embeds.shape[0], -1, -1
|
201 |
+
)
|
202 |
+
|
203 |
+
inputs_embeds = torch.cat(
|
204 |
+
[
|
205 |
+
inputs_embeds,
|
206 |
+
lang_embedding,
|
207 |
+
],
|
208 |
+
axis=1,
|
209 |
+
)
|
210 |
+
attention_mask = torch.cat(
|
211 |
+
[
|
212 |
+
attention_mask,
|
213 |
+
torch.ones(lang_embedding.shape[:-1], dtype=torch.bool, device=self.device),
|
214 |
+
],
|
215 |
+
axis=1,
|
216 |
+
)
|
217 |
+
|
218 |
+
position_ids = torch.broadcast_to(
|
219 |
+
torch.arange(torch.atleast_2d(attention_mask).shape[-1], device=self.device),
|
220 |
+
attention_mask.shape,
|
221 |
+
)
|
222 |
+
|
223 |
+
hidden_states = self.model(
|
224 |
+
inputs_embeds=inputs_embeds,
|
225 |
+
attention_mask=attention_mask,
|
226 |
+
position_ids=position_ids,
|
227 |
+
).last_hidden_state
|
228 |
+
|
229 |
+
if self.config.hn_concat_last_hidden_state:
|
230 |
+
hidden_states = hidden_states.reshape(target_surface_forms.shape[0], -1)
|
231 |
+
else:
|
232 |
+
hidden_states = hidden_states[:, 0]
|
233 |
+
|
234 |
+
predicted_embeddings = self.output_projection(hidden_states)
|
235 |
+
|
236 |
+
if self.config.hn_single_head:
|
237 |
+
predicted_embeddings_in = predicted_embeddings[..., : self.config.n_embd]
|
238 |
+
|
239 |
+
if self.has_separate_out_embeddings:
|
240 |
+
predicted_embeddings_out = predicted_embeddings[
|
241 |
+
..., self.config.n_embd :
|
242 |
+
]
|
243 |
+
else:
|
244 |
+
predicted_embeddings_out = None
|
245 |
+
else:
|
246 |
+
predicted_embeddings_in = predicted_embeddings
|
247 |
+
if self.has_separate_out_embeddings:
|
248 |
+
predicted_embeddings_out = self.output_projection_out(hidden_states)
|
249 |
+
else:
|
250 |
+
predicted_embeddings_out = None
|
251 |
+
|
252 |
+
if self.config.hn_rescale_embeddings:
|
253 |
+
predicted_embeddings_in = self.scaler(predicted_embeddings_in)
|
254 |
+
|
255 |
+
if predicted_embeddings_out is not None:
|
256 |
+
predicted_embeddings_out = self.out_scaler(predicted_embeddings_out)
|
257 |
+
|
258 |
+
if getattr(self.config, "hn_predict_bias", False):
|
259 |
+
predicted_bias = self.bias_projection(hidden_states)[..., 0]
|
260 |
+
else:
|
261 |
+
predicted_bias = torch.zeros_like(
|
262 |
+
target_surface_forms[..., 0], dtype=self.dtype
|
263 |
+
)
|
264 |
+
|
265 |
+
return predicted_embeddings_in, predicted_embeddings_out, predicted_bias
|