relik-ie commited on
Commit
e0fed9c
1 Parent(s): bb456f4

Upload model

Browse files
Files changed (5) hide show
  1. README.md +199 -0
  2. config.json +26 -0
  3. configuration_relik.py +45 -0
  4. model.safetensors +3 -0
  5. modeling_relik.py +1007 -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,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/home/martinez/pere/relik/experiments/relik-reader-deberta-large/2024-08-24/08-30-21/wandb/run-20240824_083025-ur2mkj7n/files/hf_model/hf_model",
3
+ "activation": "gelu",
4
+ "add_entity_embedding": null,
5
+ "additional_special_symbols": 50,
6
+ "additional_special_symbols_types": 0,
7
+ "architectures": [
8
+ "RelikReaderREModel"
9
+ ],
10
+ "auto_map": {
11
+ "AutoConfig": "configuration_relik.RelikReaderConfig",
12
+ "AutoModel": "modeling_relik.RelikReaderREModel"
13
+ },
14
+ "binary_end_logits": false,
15
+ "default_reader_class": null,
16
+ "entity_type_loss": null,
17
+ "linears_hidden_size": 512,
18
+ "model_type": "relik-reader",
19
+ "num_layers": null,
20
+ "threshold": 0.5,
21
+ "torch_dtype": "float32",
22
+ "training": true,
23
+ "transformer_model": "microsoft/deberta-v3-large",
24
+ "transformers_version": "4.41.2",
25
+ "use_last_k_layers": 1
26
+ }
configuration_relik.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
+ from transformers import AutoConfig
4
+ from transformers.configuration_utils import PretrainedConfig
5
+
6
+
7
+ class RelikReaderConfig(PretrainedConfig):
8
+ model_type = "relik-reader"
9
+
10
+ def __init__(
11
+ self,
12
+ transformer_model: str = "microsoft/deberta-v3-base",
13
+ additional_special_symbols: int = 101,
14
+ additional_special_symbols_types: Optional[int] = 0,
15
+ num_layers: Optional[int] = None,
16
+ activation: str = "gelu",
17
+ linears_hidden_size: Optional[int] = 512,
18
+ use_last_k_layers: int = 1,
19
+ entity_type_loss: bool = False,
20
+ add_entity_embedding: bool = None,
21
+ binary_end_logits: bool = False,
22
+ training: bool = False,
23
+ default_reader_class: Optional[str] = None,
24
+ threshold: Optional[float] = 0.5,
25
+ **kwargs
26
+ ) -> None:
27
+ # TODO: add name_or_path to kwargs
28
+ self.transformer_model = transformer_model
29
+ self.additional_special_symbols = additional_special_symbols
30
+ self.additional_special_symbols_types = additional_special_symbols_types
31
+ self.num_layers = num_layers
32
+ self.activation = activation
33
+ self.linears_hidden_size = linears_hidden_size
34
+ self.use_last_k_layers = use_last_k_layers
35
+ self.entity_type_loss = entity_type_loss
36
+ self.add_entity_embedding = (
37
+ True
38
+ if add_entity_embedding is None and entity_type_loss
39
+ else add_entity_embedding
40
+ )
41
+ self.threshold = threshold
42
+ self.binary_end_logits = binary_end_logits
43
+ self.training = training
44
+ self.default_reader_class = default_reader_class
45
+ super().__init__(**kwargs)
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5231d19a06b35added0c28eceef830dd6a4e56af75680f931986dedb410db5be
3
+ size 1763629056
modeling_relik.py ADDED
@@ -0,0 +1,1007 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Dict, Optional
2
+
3
+ import torch
4
+ from transformers import AutoModel, PreTrainedModel
5
+ from transformers.activations import ClippedGELUActivation, GELUActivation
6
+ from transformers.configuration_utils import PretrainedConfig
7
+ from transformers.modeling_utils import PoolerEndLogits
8
+
9
+ from .configuration_relik import RelikReaderConfig
10
+
11
+
12
+ class RelikReaderSample:
13
+ def __init__(self, **kwargs):
14
+ super().__setattr__("_d", {})
15
+ self._d = kwargs
16
+
17
+ def __getattribute__(self, item):
18
+ return super(RelikReaderSample, self).__getattribute__(item)
19
+
20
+ def __getattr__(self, item):
21
+ if item.startswith("__") and item.endswith("__"):
22
+ # this is likely some python library-specific variable (such as __deepcopy__ for copy)
23
+ # better follow standard behavior here
24
+ raise AttributeError(item)
25
+ elif item in self._d:
26
+ return self._d[item]
27
+ else:
28
+ return None
29
+
30
+ def __setattr__(self, key, value):
31
+ if key in self._d:
32
+ self._d[key] = value
33
+ else:
34
+ super().__setattr__(key, value)
35
+ self._d[key] = value
36
+
37
+
38
+ activation2functions = {
39
+ "relu": torch.nn.ReLU(),
40
+ "gelu": GELUActivation(),
41
+ "gelu_10": ClippedGELUActivation(-10, 10),
42
+ }
43
+
44
+
45
+ class PoolerEndLogitsBi(PoolerEndLogits):
46
+ def __init__(self, config: PretrainedConfig):
47
+ super().__init__(config)
48
+ self.dense_1 = torch.nn.Linear(config.hidden_size, 2)
49
+
50
+ def forward(
51
+ self,
52
+ hidden_states: torch.FloatTensor,
53
+ start_states: Optional[torch.FloatTensor] = None,
54
+ start_positions: Optional[torch.LongTensor] = None,
55
+ p_mask: Optional[torch.FloatTensor] = None,
56
+ ) -> torch.FloatTensor:
57
+ if p_mask is not None:
58
+ p_mask = p_mask.unsqueeze(-1)
59
+ logits = super().forward(
60
+ hidden_states,
61
+ start_states,
62
+ start_positions,
63
+ p_mask,
64
+ )
65
+ return logits
66
+
67
+
68
+ class RelikReaderSpanModel(PreTrainedModel):
69
+ config_class = RelikReaderConfig
70
+
71
+ def __init__(self, config: RelikReaderConfig, *args, **kwargs):
72
+ super().__init__(config)
73
+ # Transformer model declaration
74
+ self.config = config
75
+ self.transformer_model = (
76
+ AutoModel.from_pretrained(self.config.transformer_model)
77
+ if self.config.num_layers is None
78
+ else AutoModel.from_pretrained(
79
+ self.config.transformer_model, num_hidden_layers=self.config.num_layers
80
+ )
81
+ )
82
+ self.transformer_model.resize_token_embeddings(
83
+ self.transformer_model.config.vocab_size
84
+ + self.config.additional_special_symbols
85
+ )
86
+
87
+ self.activation = self.config.activation
88
+ self.linears_hidden_size = self.config.linears_hidden_size
89
+ self.use_last_k_layers = self.config.use_last_k_layers
90
+
91
+ # named entity detection layers
92
+ self.ned_start_classifier = self._get_projection_layer(
93
+ self.activation, last_hidden=2, layer_norm=False
94
+ )
95
+ if self.config.binary_end_logits:
96
+ self.ned_end_classifier = PoolerEndLogitsBi(self.transformer_model.config)
97
+ else:
98
+ self.ned_end_classifier = PoolerEndLogits(self.transformer_model.config)
99
+
100
+ # END entity disambiguation layer
101
+ self.ed_start_projector = self._get_projection_layer(self.activation)
102
+ self.ed_end_projector = self._get_projection_layer(self.activation)
103
+
104
+ self.training = self.config.training
105
+
106
+ # criterion
107
+ self.criterion = torch.nn.CrossEntropyLoss()
108
+
109
+ def _get_projection_layer(
110
+ self,
111
+ activation: str,
112
+ last_hidden: Optional[int] = None,
113
+ input_hidden=None,
114
+ layer_norm: bool = True,
115
+ ) -> torch.nn.Sequential:
116
+ head_components = [
117
+ torch.nn.Dropout(0.1),
118
+ torch.nn.Linear(
119
+ (
120
+ self.transformer_model.config.hidden_size * self.use_last_k_layers
121
+ if input_hidden is None
122
+ else input_hidden
123
+ ),
124
+ self.linears_hidden_size,
125
+ ),
126
+ activation2functions[activation],
127
+ torch.nn.Dropout(0.1),
128
+ torch.nn.Linear(
129
+ self.linears_hidden_size,
130
+ self.linears_hidden_size if last_hidden is None else last_hidden,
131
+ ),
132
+ ]
133
+
134
+ if layer_norm:
135
+ head_components.append(
136
+ torch.nn.LayerNorm(
137
+ self.linears_hidden_size if last_hidden is None else last_hidden,
138
+ self.transformer_model.config.layer_norm_eps,
139
+ )
140
+ )
141
+
142
+ return torch.nn.Sequential(*head_components)
143
+
144
+ def _mask_logits(self, logits: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
145
+ mask = mask.unsqueeze(-1)
146
+ if next(self.parameters()).dtype == torch.float16:
147
+ logits = logits * (1 - mask) - 65500 * mask
148
+ else:
149
+ logits = logits * (1 - mask) - 1e30 * mask
150
+ return logits
151
+
152
+ def _get_model_features(
153
+ self,
154
+ input_ids: torch.Tensor,
155
+ attention_mask: torch.Tensor,
156
+ token_type_ids: Optional[torch.Tensor],
157
+ ):
158
+ model_input = {
159
+ "input_ids": input_ids,
160
+ "attention_mask": attention_mask,
161
+ "output_hidden_states": self.use_last_k_layers > 1,
162
+ }
163
+
164
+ if token_type_ids is not None:
165
+ model_input["token_type_ids"] = token_type_ids
166
+
167
+ model_output = self.transformer_model(**model_input)
168
+
169
+ if self.use_last_k_layers > 1:
170
+ model_features = torch.cat(
171
+ model_output[1][-self.use_last_k_layers :], dim=-1
172
+ )
173
+ else:
174
+ model_features = model_output[0]
175
+
176
+ return model_features
177
+
178
+ def compute_ned_end_logits(
179
+ self,
180
+ start_predictions,
181
+ start_labels,
182
+ model_features,
183
+ prediction_mask,
184
+ batch_size,
185
+ ) -> Optional[torch.Tensor]:
186
+ # todo: maybe when constraining on the spans,
187
+ # we should not use a prediction_mask for the end tokens.
188
+ # at least we should not during training imo
189
+ start_positions = start_labels if self.training else start_predictions
190
+ start_positions_indices = (
191
+ torch.arange(start_positions.size(1), device=start_positions.device)
192
+ .unsqueeze(0)
193
+ .expand(batch_size, -1)[start_positions > 0]
194
+ ).to(start_positions.device)
195
+
196
+ if len(start_positions_indices) > 0:
197
+ expanded_features = model_features.repeat_interleave(
198
+ torch.sum(start_positions > 0, dim=-1), dim=0
199
+ )
200
+ expanded_prediction_mask = prediction_mask.repeat_interleave(
201
+ torch.sum(start_positions > 0, dim=-1), dim=0
202
+ )
203
+ end_logits = self.ned_end_classifier(
204
+ hidden_states=expanded_features,
205
+ start_positions=start_positions_indices,
206
+ p_mask=expanded_prediction_mask,
207
+ )
208
+
209
+ return end_logits
210
+
211
+ return None
212
+
213
+ def compute_classification_logits(
214
+ self,
215
+ model_features_start,
216
+ model_features_end,
217
+ special_symbols_features,
218
+ ) -> torch.Tensor:
219
+ model_start_features = self.ed_start_projector(model_features_start)
220
+ model_end_features = self.ed_end_projector(model_features_end)
221
+ model_start_features_symbols = self.ed_start_projector(special_symbols_features)
222
+ model_end_features_symbols = self.ed_end_projector(special_symbols_features)
223
+
224
+ model_ed_features = torch.cat(
225
+ [model_start_features, model_end_features], dim=-1
226
+ )
227
+ special_symbols_representation = torch.cat(
228
+ [model_start_features_symbols, model_end_features_symbols], dim=-1
229
+ )
230
+
231
+ logits = torch.bmm(
232
+ model_ed_features,
233
+ torch.permute(special_symbols_representation, (0, 2, 1)),
234
+ )
235
+
236
+ logits = self._mask_logits(logits, (model_features_start == -100).all(2).long())
237
+ return logits
238
+
239
+ def forward(
240
+ self,
241
+ input_ids: torch.Tensor,
242
+ attention_mask: torch.Tensor,
243
+ token_type_ids: Optional[torch.Tensor] = None,
244
+ prediction_mask: Optional[torch.Tensor] = None,
245
+ special_symbols_mask: Optional[torch.Tensor] = None,
246
+ start_labels: Optional[torch.Tensor] = None,
247
+ end_labels: Optional[torch.Tensor] = None,
248
+ use_predefined_spans: bool = False,
249
+ *args,
250
+ **kwargs,
251
+ ) -> Dict[str, Any]:
252
+ batch_size, seq_len = input_ids.shape
253
+
254
+ model_features = self._get_model_features(
255
+ input_ids, attention_mask, token_type_ids
256
+ )
257
+
258
+ ned_start_labels = None
259
+
260
+ # named entity detection if required
261
+ if use_predefined_spans: # no need to compute spans
262
+ ned_start_logits, ned_start_probabilities, ned_start_predictions = (
263
+ None,
264
+ None,
265
+ (
266
+ torch.clone(start_labels)
267
+ if start_labels is not None
268
+ else torch.zeros_like(input_ids)
269
+ ),
270
+ )
271
+ ned_end_logits, ned_end_probabilities, ned_end_predictions = (
272
+ None,
273
+ None,
274
+ (
275
+ torch.clone(end_labels)
276
+ if end_labels is not None
277
+ else torch.zeros_like(input_ids)
278
+ ),
279
+ )
280
+ ned_start_predictions[ned_start_predictions > 0] = 1
281
+ ned_end_predictions[end_labels > 0] = 1
282
+ ned_end_predictions = ned_end_predictions[~(end_labels == -100).all(2)]
283
+
284
+ else: # compute spans
285
+ # start boundary prediction
286
+ ned_start_logits = self.ned_start_classifier(model_features)
287
+ ned_start_logits = self._mask_logits(ned_start_logits, prediction_mask)
288
+ ned_start_probabilities = torch.softmax(ned_start_logits, dim=-1)
289
+ ned_start_predictions = ned_start_probabilities.argmax(dim=-1)
290
+
291
+ # end boundary prediction
292
+ ned_start_labels = (
293
+ torch.zeros_like(start_labels) if start_labels is not None else None
294
+ )
295
+
296
+ if ned_start_labels is not None:
297
+ ned_start_labels[start_labels == -100] = -100
298
+ ned_start_labels[start_labels > 0] = 1
299
+
300
+ ned_end_logits = self.compute_ned_end_logits(
301
+ ned_start_predictions,
302
+ ned_start_labels,
303
+ model_features,
304
+ prediction_mask,
305
+ batch_size,
306
+ )
307
+
308
+ if ned_end_logits is not None:
309
+ ned_end_probabilities = torch.softmax(ned_end_logits, dim=-1)
310
+ if not self.config.binary_end_logits:
311
+ ned_end_predictions = torch.argmax(
312
+ ned_end_probabilities, dim=-1, keepdim=True
313
+ )
314
+ ned_end_predictions = torch.zeros_like(
315
+ ned_end_probabilities
316
+ ).scatter_(1, ned_end_predictions, 1)
317
+ else:
318
+ ned_end_predictions = torch.argmax(ned_end_probabilities, dim=-1)
319
+ else:
320
+ ned_end_logits, ned_end_probabilities = None, None
321
+ ned_end_predictions = ned_start_predictions.new_zeros(
322
+ batch_size, seq_len
323
+ )
324
+
325
+ if not self.training:
326
+ # if len(ned_end_predictions.shape) < 2:
327
+ # print(ned_end_predictions)
328
+ end_preds_count = ned_end_predictions.sum(1)
329
+ # If there are no end predictions for a start prediction, remove the start prediction
330
+ if (end_preds_count == 0).any() and (ned_start_predictions > 0).any():
331
+ ned_start_predictions[ned_start_predictions == 1] = (
332
+ end_preds_count != 0
333
+ ).long()
334
+ ned_end_predictions = ned_end_predictions[end_preds_count != 0]
335
+
336
+ if end_labels is not None:
337
+ end_labels = end_labels[~(end_labels == -100).all(2)]
338
+
339
+ start_position, end_position = (
340
+ (start_labels, end_labels)
341
+ if self.training
342
+ else (ned_start_predictions, ned_end_predictions)
343
+ )
344
+ start_counts = (start_position > 0).sum(1)
345
+ if (start_counts > 0).any():
346
+ ned_end_predictions = ned_end_predictions.split(start_counts.tolist())
347
+ # Entity disambiguation
348
+ if (end_position > 0).sum() > 0:
349
+ ends_count = (end_position > 0).sum(1)
350
+ model_entity_start = torch.repeat_interleave(
351
+ model_features[start_position > 0], ends_count, dim=0
352
+ )
353
+ model_entity_end = torch.repeat_interleave(
354
+ model_features, start_counts, dim=0
355
+ )[end_position > 0]
356
+ ents_count = torch.nn.utils.rnn.pad_sequence(
357
+ torch.split(ends_count, start_counts.tolist()),
358
+ batch_first=True,
359
+ padding_value=0,
360
+ ).sum(1)
361
+
362
+ model_entity_start = torch.nn.utils.rnn.pad_sequence(
363
+ torch.split(model_entity_start, ents_count.tolist()),
364
+ batch_first=True,
365
+ padding_value=-100,
366
+ )
367
+
368
+ model_entity_end = torch.nn.utils.rnn.pad_sequence(
369
+ torch.split(model_entity_end, ents_count.tolist()),
370
+ batch_first=True,
371
+ padding_value=-100,
372
+ )
373
+
374
+ ed_logits = self.compute_classification_logits(
375
+ model_entity_start,
376
+ model_entity_end,
377
+ model_features[special_symbols_mask].view(
378
+ batch_size, -1, model_features.shape[-1]
379
+ ),
380
+ )
381
+ ed_probabilities = torch.softmax(ed_logits, dim=-1)
382
+ ed_predictions = torch.argmax(ed_probabilities, dim=-1)
383
+ else:
384
+ ed_logits, ed_probabilities, ed_predictions = (
385
+ None,
386
+ ned_start_predictions.new_zeros(batch_size, seq_len),
387
+ ned_start_predictions.new_zeros(batch_size),
388
+ )
389
+ # output build
390
+ output_dict = dict(
391
+ batch_size=batch_size,
392
+ ned_start_logits=ned_start_logits,
393
+ ned_start_probabilities=ned_start_probabilities,
394
+ ned_start_predictions=ned_start_predictions,
395
+ ned_end_logits=ned_end_logits,
396
+ ned_end_probabilities=ned_end_probabilities,
397
+ ned_end_predictions=ned_end_predictions,
398
+ ed_logits=ed_logits,
399
+ ed_probabilities=ed_probabilities,
400
+ ed_predictions=ed_predictions,
401
+ )
402
+
403
+ # compute loss if labels
404
+ if start_labels is not None and end_labels is not None and self.training:
405
+ # named entity detection loss
406
+
407
+ # start
408
+ if ned_start_logits is not None:
409
+ ned_start_loss = self.criterion(
410
+ ned_start_logits.view(-1, ned_start_logits.shape[-1]),
411
+ ned_start_labels.view(-1),
412
+ )
413
+ else:
414
+ ned_start_loss = 0
415
+
416
+ # end
417
+ # use ents_count to assign the labels to the correct positions i.e. using end_labels -> [[0,0,4,0], [0,0,0,2]] -> [4,2] (this is just an element, for batch we need to mask it with ents_count), ie -> [[4,2,-100,-100], [3,1,2,-100], [1,3,2,5]]
418
+
419
+ if ned_end_logits is not None:
420
+ ed_labels = end_labels.clone()
421
+ ed_labels = torch.nn.utils.rnn.pad_sequence(
422
+ torch.split(ed_labels[ed_labels > 0], ents_count.tolist()),
423
+ batch_first=True,
424
+ padding_value=-100,
425
+ )
426
+ end_labels[end_labels > 0] = 1
427
+ if not self.config.binary_end_logits:
428
+ # transform label to position in the sequence
429
+ end_labels = end_labels.argmax(dim=-1)
430
+ ned_end_loss = self.criterion(
431
+ ned_end_logits.view(-1, ned_end_logits.shape[-1]),
432
+ end_labels.view(-1),
433
+ )
434
+ else:
435
+ ned_end_loss = self.criterion(
436
+ ned_end_logits.reshape(-1, ned_end_logits.shape[-1]),
437
+ end_labels.reshape(-1).long(),
438
+ )
439
+
440
+ # entity disambiguation loss
441
+ ed_loss = self.criterion(
442
+ ed_logits.view(-1, ed_logits.shape[-1]),
443
+ ed_labels.view(-1).long(),
444
+ )
445
+
446
+ else:
447
+ ned_end_loss = 0
448
+ ed_loss = 0
449
+
450
+ output_dict["ned_start_loss"] = ned_start_loss
451
+ output_dict["ned_end_loss"] = ned_end_loss
452
+ output_dict["ed_loss"] = ed_loss
453
+
454
+ output_dict["loss"] = ned_start_loss + ned_end_loss + ed_loss
455
+
456
+ return output_dict
457
+
458
+
459
+ class RelikReaderREModel(PreTrainedModel):
460
+ config_class = RelikReaderConfig
461
+
462
+ def __init__(self, config, *args, **kwargs):
463
+ super().__init__(config)
464
+ # Transformer model declaration
465
+ # self.transformer_model_name = transformer_model
466
+ self.config = config
467
+ self.transformer_model = (
468
+ AutoModel.from_pretrained(config.transformer_model)
469
+ if config.num_layers is None
470
+ else AutoModel.from_pretrained(
471
+ config.transformer_model, num_hidden_layers=config.num_layers
472
+ )
473
+ )
474
+ self.transformer_model.resize_token_embeddings(
475
+ self.transformer_model.config.vocab_size
476
+ + config.additional_special_symbols
477
+ + config.additional_special_symbols_types,
478
+ )
479
+
480
+ # named entity detection layers
481
+ self.ned_start_classifier = self._get_projection_layer(
482
+ config.activation, last_hidden=2, layer_norm=False
483
+ )
484
+
485
+ self.ned_end_classifier = PoolerEndLogitsBi(self.transformer_model.config)
486
+
487
+ self.relation_disambiguation_loss = (
488
+ config.relation_disambiguation_loss
489
+ if hasattr(config, "relation_disambiguation_loss")
490
+ else False
491
+ )
492
+
493
+ if self.config.entity_type_loss and self.config.add_entity_embedding:
494
+ input_hidden_ents = 3
495
+ else:
496
+ input_hidden_ents = 2
497
+
498
+ self.re_projector = self._get_projection_layer(
499
+ config.activation,
500
+ input_hidden=input_hidden_ents * self.transformer_model.config.hidden_size,
501
+ hidden=input_hidden_ents * self.config.linears_hidden_size,
502
+ last_hidden=2 * self.config.linears_hidden_size,
503
+ )
504
+
505
+ self.re_relation_projector = self._get_projection_layer(
506
+ config.activation,
507
+ input_hidden=self.transformer_model.config.hidden_size,
508
+ )
509
+
510
+ if self.config.entity_type_loss or self.relation_disambiguation_loss:
511
+ self.re_entities_projector = self._get_projection_layer(
512
+ config.activation,
513
+ input_hidden=2 * self.transformer_model.config.hidden_size,
514
+ )
515
+ self.re_definition_projector = self._get_projection_layer(
516
+ config.activation,
517
+ )
518
+
519
+ self.re_classifier = self._get_projection_layer(
520
+ config.activation,
521
+ input_hidden=config.linears_hidden_size,
522
+ last_hidden=2,
523
+ layer_norm=False,
524
+ )
525
+
526
+ self.training = config.training
527
+
528
+ # criterion
529
+ self.criterion = torch.nn.CrossEntropyLoss()
530
+ self.criterion_type = torch.nn.BCEWithLogitsLoss()
531
+
532
+ def _get_projection_layer(
533
+ self,
534
+ activation: str,
535
+ last_hidden: Optional[int] = None,
536
+ hidden: Optional[int] = None,
537
+ input_hidden=None,
538
+ layer_norm: bool = True,
539
+ ) -> torch.nn.Sequential:
540
+ head_components = [
541
+ torch.nn.Dropout(0.1),
542
+ torch.nn.Linear(
543
+ (
544
+ self.transformer_model.config.hidden_size
545
+ * self.config.use_last_k_layers
546
+ if input_hidden is None
547
+ else input_hidden
548
+ ),
549
+ self.config.linears_hidden_size if hidden is None else hidden,
550
+ ),
551
+ activation2functions[activation],
552
+ torch.nn.Dropout(0.1),
553
+ torch.nn.Linear(
554
+ self.config.linears_hidden_size if hidden is None else hidden,
555
+ self.config.linears_hidden_size if last_hidden is None else last_hidden,
556
+ ),
557
+ ]
558
+
559
+ if layer_norm:
560
+ head_components.append(
561
+ torch.nn.LayerNorm(
562
+ (
563
+ self.config.linears_hidden_size
564
+ if last_hidden is None
565
+ else last_hidden
566
+ ),
567
+ self.transformer_model.config.layer_norm_eps,
568
+ )
569
+ )
570
+
571
+ return torch.nn.Sequential(*head_components)
572
+
573
+ def _mask_logits(self, logits: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
574
+ mask = mask.unsqueeze(-1)
575
+ if next(self.parameters()).dtype == torch.float16:
576
+ logits = logits * (1 - mask) - 65500 * mask
577
+ else:
578
+ logits = logits * (1 - mask) - 1e30 * mask
579
+ return logits
580
+
581
+ def _get_model_features(
582
+ self,
583
+ input_ids: torch.Tensor,
584
+ attention_mask: torch.Tensor,
585
+ token_type_ids: Optional[torch.Tensor],
586
+ ):
587
+ model_input = {
588
+ "input_ids": input_ids,
589
+ "attention_mask": attention_mask,
590
+ "output_hidden_states": self.config.use_last_k_layers > 1,
591
+ }
592
+
593
+ if token_type_ids is not None:
594
+ model_input["token_type_ids"] = token_type_ids
595
+
596
+ model_output = self.transformer_model(**model_input)
597
+
598
+ if self.config.use_last_k_layers > 1:
599
+ model_features = torch.cat(
600
+ model_output[1][-self.config.use_last_k_layers :], dim=-1
601
+ )
602
+ else:
603
+ model_features = model_output[0]
604
+
605
+ return model_features
606
+
607
+ def compute_ned_end_logits(
608
+ self,
609
+ start_predictions,
610
+ start_labels,
611
+ model_features,
612
+ prediction_mask,
613
+ batch_size,
614
+ mask_preceding: bool = False,
615
+ ) -> Optional[torch.Tensor]:
616
+ # todo: maybe when constraining on the spans,
617
+ # we should not use a prediction_mask for the end tokens.
618
+ # at least we should not during training imo
619
+ start_positions = start_labels if self.training else start_predictions
620
+ start_positions_indices = (
621
+ torch.arange(start_positions.size(1), device=start_positions.device)
622
+ .unsqueeze(0)
623
+ .expand(batch_size, -1)[start_positions > 0]
624
+ ).to(start_positions.device)
625
+
626
+ if len(start_positions_indices) > 0:
627
+ expanded_features = model_features.repeat_interleave(
628
+ torch.sum(start_positions > 0, dim=-1), dim=0
629
+ )
630
+ expanded_prediction_mask = prediction_mask.repeat_interleave(
631
+ torch.sum(start_positions > 0, dim=-1), dim=0
632
+ )
633
+ if mask_preceding:
634
+ expanded_prediction_mask[
635
+ torch.arange(
636
+ expanded_prediction_mask.shape[1],
637
+ device=expanded_prediction_mask.device,
638
+ )
639
+ < start_positions_indices.unsqueeze(1)
640
+ ] = 1
641
+ end_logits = self.ned_end_classifier(
642
+ hidden_states=expanded_features,
643
+ start_positions=start_positions_indices,
644
+ p_mask=expanded_prediction_mask,
645
+ )
646
+
647
+ return end_logits
648
+
649
+ return None
650
+
651
+ def compute_relation_logits(
652
+ self,
653
+ model_entity_features,
654
+ special_symbols_features,
655
+ ) -> torch.Tensor:
656
+ model_subject_object_features = self.re_projector(model_entity_features)
657
+ model_subject_features = model_subject_object_features[
658
+ :, :, : model_subject_object_features.shape[-1] // 2
659
+ ]
660
+ model_object_features = model_subject_object_features[
661
+ :, :, model_subject_object_features.shape[-1] // 2 :
662
+ ]
663
+ special_symbols_start_representation = self.re_relation_projector(
664
+ special_symbols_features
665
+ )
666
+ re_logits = torch.einsum(
667
+ "bse,bde,bfe->bsdfe",
668
+ model_subject_features,
669
+ model_object_features,
670
+ special_symbols_start_representation,
671
+ )
672
+ re_logits = self.re_classifier(re_logits)
673
+
674
+ return re_logits
675
+
676
+ def compute_entity_logits(
677
+ self,
678
+ model_entity_features,
679
+ special_symbols_features,
680
+ ) -> torch.Tensor:
681
+ model_ed_features = self.re_entities_projector(model_entity_features)
682
+ special_symbols_ed_representation = self.re_definition_projector(
683
+ special_symbols_features
684
+ )
685
+
686
+ logits = torch.bmm(
687
+ model_ed_features,
688
+ torch.permute(special_symbols_ed_representation, (0, 2, 1)),
689
+ )
690
+ logits = self._mask_logits(
691
+ logits, (model_entity_features == -100).all(2).long()
692
+ )
693
+ return logits
694
+
695
+ def compute_loss(self, logits, labels, mask=None):
696
+ logits = logits.reshape(-1, logits.shape[-1])
697
+ labels = labels.reshape(-1).long()
698
+ if mask is not None:
699
+ return self.criterion(logits[mask], labels[mask])
700
+ return self.criterion(logits, labels)
701
+
702
+ def compute_ned_type_loss(
703
+ self,
704
+ disambiguation_labels,
705
+ re_ned_entities_logits,
706
+ ned_type_logits,
707
+ re_entities_logits,
708
+ entity_types,
709
+ mask,
710
+ ):
711
+ if self.config.entity_type_loss and self.relation_disambiguation_loss:
712
+ return self.criterion_type(
713
+ re_ned_entities_logits[disambiguation_labels != -100],
714
+ disambiguation_labels[disambiguation_labels != -100],
715
+ )
716
+ if self.config.entity_type_loss:
717
+ return self.criterion_type(
718
+ ned_type_logits[mask],
719
+ disambiguation_labels[:, :, :entity_types][mask],
720
+ )
721
+
722
+ if self.relation_disambiguation_loss:
723
+ return self.criterion_type(
724
+ re_entities_logits[disambiguation_labels != -100],
725
+ disambiguation_labels[disambiguation_labels != -100],
726
+ )
727
+ return 0
728
+
729
+ def compute_relation_loss(self, relation_labels, re_logits):
730
+ return self.compute_loss(
731
+ re_logits, relation_labels, relation_labels.view(-1) != -100
732
+ )
733
+
734
+ def forward(
735
+ self,
736
+ input_ids: torch.Tensor,
737
+ attention_mask: torch.Tensor,
738
+ token_type_ids: torch.Tensor,
739
+ prediction_mask: Optional[torch.Tensor] = None,
740
+ special_symbols_mask: Optional[torch.Tensor] = None,
741
+ special_symbols_mask_entities: Optional[torch.Tensor] = None,
742
+ start_labels: Optional[torch.Tensor] = None,
743
+ end_labels: Optional[torch.Tensor] = None,
744
+ disambiguation_labels: Optional[torch.Tensor] = None,
745
+ relation_labels: Optional[torch.Tensor] = None,
746
+ relation_threshold: float = None,
747
+ is_validation: bool = False,
748
+ is_prediction: bool = False,
749
+ use_predefined_spans: bool = False,
750
+ *args,
751
+ **kwargs,
752
+ ) -> Dict[str, Any]:
753
+ relation_threshold = (
754
+ self.config.threshold if relation_threshold is None else relation_threshold
755
+ )
756
+
757
+ batch_size = input_ids.shape[0]
758
+
759
+ model_features = self._get_model_features(
760
+ input_ids, attention_mask, token_type_ids
761
+ )
762
+
763
+ # named entity detection
764
+ if use_predefined_spans:
765
+ ned_start_logits, ned_start_probabilities, ned_start_predictions = (
766
+ None,
767
+ None,
768
+ torch.zeros_like(start_labels),
769
+ )
770
+ ned_end_logits, ned_end_probabilities, ned_end_predictions = (
771
+ None,
772
+ None,
773
+ torch.zeros_like(end_labels),
774
+ )
775
+
776
+ ned_start_predictions[start_labels > 0] = 1
777
+ ned_end_predictions[end_labels > 0] = 1
778
+ ned_end_predictions = ned_end_predictions[~(end_labels == -100).all(2)]
779
+ ned_start_labels = start_labels
780
+ ned_start_labels[start_labels > 0] = 1
781
+ else:
782
+ # start boundary prediction
783
+ ned_start_logits = self.ned_start_classifier(model_features)
784
+ if is_validation or is_prediction:
785
+ ned_start_logits = self._mask_logits(
786
+ ned_start_logits, prediction_mask
787
+ ) # why?
788
+ ned_start_probabilities = torch.softmax(ned_start_logits, dim=-1)
789
+ ned_start_predictions = ned_start_probabilities.argmax(dim=-1)
790
+
791
+ # end boundary prediction
792
+ ned_start_labels = (
793
+ torch.zeros_like(start_labels) if start_labels is not None else None
794
+ )
795
+
796
+ # start_labels contain entity id at their position, we just need 1 for start of entity
797
+ if ned_start_labels is not None:
798
+ ned_start_labels[start_labels == -100] = -100
799
+ ned_start_labels[start_labels > 0] = 1
800
+
801
+ # compute end logits only if there are any start predictions.
802
+ # For each start prediction, n end predictions are made
803
+ ned_end_logits = self.compute_ned_end_logits(
804
+ ned_start_predictions,
805
+ ned_start_labels,
806
+ model_features,
807
+ prediction_mask,
808
+ batch_size,
809
+ True,
810
+ )
811
+
812
+ if ned_end_logits is not None:
813
+ # For each start prediction, n end predictions are made based on
814
+ # binary classification ie. argmax at each position.
815
+ ned_end_probabilities = torch.softmax(ned_end_logits, dim=-1)
816
+ ned_end_predictions = ned_end_probabilities.argmax(dim=-1)
817
+ else:
818
+ ned_end_logits, ned_end_probabilities = None, None
819
+ ned_end_predictions = torch.zeros_like(ned_start_predictions)
820
+
821
+ if is_prediction or is_validation:
822
+ end_preds_count = ned_end_predictions.sum(1)
823
+ # If there are no end predictions for a start prediction, remove the start prediction
824
+ if (end_preds_count == 0).any() and (ned_start_predictions > 0).any():
825
+ ned_start_predictions[ned_start_predictions == 1] = (
826
+ end_preds_count != 0
827
+ ).long()
828
+ ned_end_predictions = ned_end_predictions[end_preds_count != 0]
829
+
830
+ if end_labels is not None:
831
+ end_labels = end_labels[~(end_labels == -100).all(2)]
832
+
833
+ start_position, end_position = (
834
+ (start_labels, end_labels)
835
+ if (not is_prediction and not is_validation)
836
+ else (ned_start_predictions, ned_end_predictions)
837
+ )
838
+
839
+ start_counts = (start_position > 0).sum(1)
840
+ if (start_counts > 0).any():
841
+ ned_end_predictions = ned_end_predictions.split(start_counts.tolist())
842
+ else:
843
+ ned_end_predictions = [torch.empty(0, input_ids.shape[1], dtype=torch.int64) for _ in range(batch_size)]
844
+ # limit to 30 predictions per document using start_counts, by setting all po after sum is 30 to 0
845
+ # if is_validation or is_prediction:
846
+ # ned_start_predictions[ned_start_predictions == 1] = start_counts
847
+ # We can only predict relations if we have start and end predictions
848
+ if (end_position > 0).sum() > 0:
849
+ ends_count = (end_position > 0).sum(1)
850
+ model_subject_features = torch.cat(
851
+ [
852
+ torch.repeat_interleave(
853
+ model_features[start_position > 0], ends_count, dim=0
854
+ ), # start position features
855
+ torch.repeat_interleave(model_features, start_counts, dim=0)[
856
+ end_position > 0
857
+ ], # end position features
858
+ ],
859
+ dim=-1,
860
+ )
861
+ ents_count = torch.nn.utils.rnn.pad_sequence(
862
+ torch.split(ends_count, start_counts.tolist()),
863
+ batch_first=True,
864
+ padding_value=0,
865
+ ).sum(1)
866
+ model_subject_features = torch.nn.utils.rnn.pad_sequence(
867
+ torch.split(model_subject_features, ents_count.tolist()),
868
+ batch_first=True,
869
+ padding_value=-100,
870
+ )
871
+
872
+ # if is_validation or is_prediction:
873
+ # model_subject_features = model_subject_features[:, :30, :]
874
+
875
+ # entity disambiguation. Here relation_disambiguation_loss would only be useful to
876
+ # reduce the number of candidate relations for the next step, but currently unused.
877
+ if self.config.entity_type_loss or self.relation_disambiguation_loss:
878
+ (re_ned_entities_logits) = self.compute_entity_logits(
879
+ model_subject_features,
880
+ model_features[
881
+ special_symbols_mask | special_symbols_mask_entities
882
+ ].view(batch_size, -1, model_features.shape[-1]),
883
+ )
884
+ entity_types = torch.sum(special_symbols_mask_entities, dim=1)[0].item()
885
+ ned_type_logits = re_ned_entities_logits[:, :, :entity_types]
886
+ re_entities_logits = re_ned_entities_logits[:, :, entity_types:]
887
+
888
+ if self.config.entity_type_loss:
889
+ ned_type_probabilities = torch.sigmoid(ned_type_logits)
890
+ ned_type_predictions = ned_type_probabilities.argmax(dim=-1)
891
+
892
+ if self.config.add_entity_embedding:
893
+ special_symbols_representation = model_features[
894
+ special_symbols_mask_entities
895
+ ].view(batch_size, entity_types, -1)
896
+
897
+ entities_representation = torch.einsum(
898
+ "bsp,bpe->bse",
899
+ ned_type_probabilities,
900
+ special_symbols_representation,
901
+ )
902
+ model_subject_features = torch.cat(
903
+ [model_subject_features, entities_representation], dim=-1
904
+ )
905
+ re_entities_probabilities = torch.sigmoid(re_entities_logits)
906
+ re_entities_predictions = re_entities_probabilities.round()
907
+ else:
908
+ (
909
+ ned_type_logits,
910
+ ned_type_probabilities,
911
+ re_entities_logits,
912
+ re_entities_probabilities,
913
+ ) = (None, None, None, None)
914
+ ned_type_predictions, re_entities_predictions = (
915
+ torch.zeros([batch_size, 1], dtype=torch.long).to(input_ids.device),
916
+ torch.zeros([batch_size, 1], dtype=torch.long).to(input_ids.device),
917
+ )
918
+
919
+ # Compute relation logits
920
+ re_logits = self.compute_relation_logits(
921
+ model_subject_features,
922
+ model_features[special_symbols_mask].view(
923
+ batch_size, -1, model_features.shape[-1]
924
+ ),
925
+ )
926
+
927
+ re_probabilities = torch.softmax(re_logits, dim=-1)
928
+ # we set a thresshold instead of argmax in cause it needs to be tweaked
929
+ re_predictions = re_probabilities[:, :, :, :, 1] > relation_threshold
930
+ re_probabilities = re_probabilities[:, :, :, :, 1]
931
+ else:
932
+ (
933
+ ned_type_logits,
934
+ ned_type_probabilities,
935
+ re_entities_logits,
936
+ re_entities_probabilities,
937
+ ) = (None, None, None, None)
938
+ ned_type_predictions, re_entities_predictions = (
939
+ torch.zeros([batch_size, 1], dtype=torch.long).to(input_ids.device),
940
+ torch.zeros([batch_size, 1], dtype=torch.long).to(input_ids.device),
941
+ )
942
+ re_logits, re_probabilities, re_predictions = (
943
+ torch.zeros(
944
+ [batch_size, 1, 1, special_symbols_mask.sum(1)[0]], dtype=torch.long
945
+ ).to(input_ids.device),
946
+ torch.zeros(
947
+ [batch_size, 1, 1, special_symbols_mask.sum(1)[0]], dtype=torch.long
948
+ ).to(input_ids.device),
949
+ torch.zeros(
950
+ [batch_size, 1, 1, special_symbols_mask.sum(1)[0]], dtype=torch.long
951
+ ).to(input_ids.device),
952
+ )
953
+
954
+ # output build
955
+ output_dict = dict(
956
+ batch_size=batch_size,
957
+ ned_start_logits=ned_start_logits,
958
+ ned_start_probabilities=ned_start_probabilities,
959
+ ned_start_predictions=ned_start_predictions,
960
+ ned_end_logits=ned_end_logits,
961
+ ned_end_probabilities=ned_end_probabilities,
962
+ ned_end_predictions=ned_end_predictions,
963
+ ned_type_logits=ned_type_logits,
964
+ ned_type_probabilities=ned_type_probabilities,
965
+ ned_type_predictions=ned_type_predictions,
966
+ re_entities_logits=re_entities_logits,
967
+ re_entities_probabilities=re_entities_probabilities,
968
+ re_entities_predictions=re_entities_predictions,
969
+ re_logits=re_logits,
970
+ re_probabilities=re_probabilities,
971
+ re_predictions=re_predictions,
972
+ )
973
+
974
+ if (
975
+ start_labels is not None
976
+ and end_labels is not None
977
+ and relation_labels is not None
978
+ and is_prediction is False
979
+ ):
980
+ ned_start_loss = self.compute_loss(ned_start_logits, ned_start_labels)
981
+ end_labels[end_labels > 0] = 1
982
+ ned_end_loss = self.compute_loss(ned_end_logits, end_labels)
983
+ if self.config.entity_type_loss or self.relation_disambiguation_loss:
984
+ ned_type_loss = self.compute_ned_type_loss(
985
+ disambiguation_labels,
986
+ re_ned_entities_logits,
987
+ ned_type_logits,
988
+ re_entities_logits,
989
+ entity_types,
990
+ (model_subject_features != -100).all(2),
991
+ )
992
+ relation_loss = self.compute_relation_loss(relation_labels, re_logits)
993
+ # compute loss. We can skip the relation loss if we are in the first epochs (optional)
994
+ if self.config.entity_type_loss or self.relation_disambiguation_loss:
995
+ output_dict["loss"] = (
996
+ ned_start_loss + ned_end_loss + relation_loss + ned_type_loss
997
+ ) / 4
998
+ output_dict["ned_type_loss"] = ned_type_loss
999
+ else:
1000
+ output_dict["loss"] = ((1 / 20) * (ned_start_loss + ned_end_loss)) + (
1001
+ (9 / 10) * relation_loss
1002
+ )
1003
+ output_dict["ned_start_loss"] = ned_start_loss
1004
+ output_dict["ned_end_loss"] = ned_end_loss
1005
+ output_dict["re_loss"] = relation_loss
1006
+
1007
+ return output_dict