Add DualEmbLM
Browse files- README.md +3 -3
- config.json +8 -13
- configuration_dual.py +38 -0
- embeddings.py +1 -1
- model.safetensors +2 -2
- modeling_dual.py +83 -0
README.md
CHANGED
|
@@ -58,9 +58,9 @@ model = AutoModelForMaskedLM.from_pretrained(
|
|
| 58 |
|
| 59 |
## Tasks
|
| 60 |
|
| 61 |
-
- **Generated lacunae restoration** (Test A Hit@1: 0.
|
| 62 |
-
- **Real lacunae restoration** (Test B char Hit@1: 0.
|
| 63 |
|
| 64 |
## Contact
|
| 65 |
|
| 66 |
-
Maxim Eremeev, maeremeev@edu.hse.ru
|
|
|
|
| 58 |
|
| 59 |
## Tasks
|
| 60 |
|
| 61 |
+
- **Generated lacunae restoration** (Test A Hit@1: 0.825, CER: 0.175)
|
| 62 |
+
- **Real lacunae restoration** (Test B char Hit@1: 0.47, span Hit@1: 0.237)
|
| 63 |
|
| 64 |
## Contact
|
| 65 |
|
| 66 |
+
Maxim Eremeev, maeremeev@edu.hse.ru
|
config.json
CHANGED
|
@@ -1,23 +1,14 @@
|
|
| 1 |
{
|
| 2 |
-
"add_cross_attention": false,
|
| 3 |
"architectures": [
|
| 4 |
"DualBertForMaskedLM"
|
| 5 |
],
|
| 6 |
"attention_probs_dropout_prob": 0.1,
|
| 7 |
-
"auto_map": {
|
| 8 |
-
"AutoConfig": "config.DualBertConfig",
|
| 9 |
-
"AutoModelForMaskedLM": "model.DualBertForMaskedLM"
|
| 10 |
-
},
|
| 11 |
-
"bos_token_id": null,
|
| 12 |
"classifier_dropout": null,
|
| 13 |
-
"dtype": "float32",
|
| 14 |
-
"eos_token_id": null,
|
| 15 |
"hidden_act": "gelu",
|
| 16 |
"hidden_dropout_prob": 0.1,
|
| 17 |
"hidden_size": 512,
|
| 18 |
"initializer_range": 0.02,
|
| 19 |
"intermediate_size": 2048,
|
| 20 |
-
"is_decoder": false,
|
| 21 |
"layer_norm_eps": 1e-12,
|
| 22 |
"max_position_embeddings": 256,
|
| 23 |
"model_type": "dual_bert",
|
|
@@ -25,12 +16,16 @@
|
|
| 25 |
"num_hidden_layers": 6,
|
| 26 |
"pad_token_id": 0,
|
| 27 |
"position_embedding_type": "absolute",
|
| 28 |
-
"
|
| 29 |
-
"transformers_version": "
|
| 30 |
"type_vocab_size": 2,
|
| 31 |
"use_cache": true,
|
| 32 |
"vocab_char_size": 156,
|
| 33 |
"vocab_size": 156,
|
| 34 |
"vocab_word_size": 50000,
|
| 35 |
-
"word_char_emb_dim": 192
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
{
|
|
|
|
| 2 |
"architectures": [
|
| 3 |
"DualBertForMaskedLM"
|
| 4 |
],
|
| 5 |
"attention_probs_dropout_prob": 0.1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"classifier_dropout": null,
|
|
|
|
|
|
|
| 7 |
"hidden_act": "gelu",
|
| 8 |
"hidden_dropout_prob": 0.1,
|
| 9 |
"hidden_size": 512,
|
| 10 |
"initializer_range": 0.02,
|
| 11 |
"intermediate_size": 2048,
|
|
|
|
| 12 |
"layer_norm_eps": 1e-12,
|
| 13 |
"max_position_embeddings": 256,
|
| 14 |
"model_type": "dual_bert",
|
|
|
|
| 16 |
"num_hidden_layers": 6,
|
| 17 |
"pad_token_id": 0,
|
| 18 |
"position_embedding_type": "absolute",
|
| 19 |
+
"torch_dtype": "float32",
|
| 20 |
+
"transformers_version": "4.48.0",
|
| 21 |
"type_vocab_size": 2,
|
| 22 |
"use_cache": true,
|
| 23 |
"vocab_char_size": 156,
|
| 24 |
"vocab_size": 156,
|
| 25 |
"vocab_word_size": 50000,
|
| 26 |
+
"word_char_emb_dim": 192,
|
| 27 |
+
"auto_map": {
|
| 28 |
+
"AutoConfig": "configuration_dual.DualBertConfig",
|
| 29 |
+
"AutoModelForMaskedLM": "modeling_dual.DualBertForMaskedLM"
|
| 30 |
+
}
|
| 31 |
+
}
|
configuration_dual.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BertConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class DualBertConfig(BertConfig):
|
| 5 |
+
model_type = "dual_bert"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
vocab_char_size: int = 256,
|
| 10 |
+
vocab_word_size: int = 50000,
|
| 11 |
+
word_char_emb_dim: int = 192,
|
| 12 |
+
hidden_size: int = 512,
|
| 13 |
+
num_hidden_layers: int = 6,
|
| 14 |
+
num_attention_heads: int = 8,
|
| 15 |
+
intermediate_size: int = 2048,
|
| 16 |
+
max_position_embeddings: int = 512,
|
| 17 |
+
hidden_dropout_prob: float = 0.1,
|
| 18 |
+
attention_probs_dropout_prob: float = 0.1,
|
| 19 |
+
**kwargs,
|
| 20 |
+
):
|
| 21 |
+
# HuggingFace passes vocab_size via kwargs when loading from config.json;
|
| 22 |
+
# remove it to avoid conflict with our explicit vocab_size=vocab_char_size.
|
| 23 |
+
kwargs.pop("vocab_size", None)
|
| 24 |
+
|
| 25 |
+
super().__init__(
|
| 26 |
+
vocab_size=vocab_char_size,
|
| 27 |
+
hidden_size=hidden_size,
|
| 28 |
+
num_hidden_layers=num_hidden_layers,
|
| 29 |
+
num_attention_heads=num_attention_heads,
|
| 30 |
+
intermediate_size=intermediate_size,
|
| 31 |
+
max_position_embeddings=max_position_embeddings,
|
| 32 |
+
hidden_dropout_prob=hidden_dropout_prob,
|
| 33 |
+
attention_probs_dropout_prob=attention_probs_dropout_prob,
|
| 34 |
+
**kwargs,
|
| 35 |
+
)
|
| 36 |
+
self.vocab_char_size = vocab_char_size
|
| 37 |
+
self.vocab_word_size = vocab_word_size
|
| 38 |
+
self.word_char_emb_dim = word_char_emb_dim
|
embeddings.py
CHANGED
|
@@ -21,7 +21,7 @@ class DualEmbeddings(nn.Module):
|
|
| 21 |
self.dropout = nn.Dropout(config.hidden_dropout_prob)
|
| 22 |
|
| 23 |
self.register_buffer(
|
| 24 |
-
"position_ids", torch.arange(config.max_position_embeddings).unsqueeze(0), persistent=
|
| 25 |
)
|
| 26 |
|
| 27 |
def forward(self, input_ids, word_ids):
|
|
|
|
| 21 |
self.dropout = nn.Dropout(config.hidden_dropout_prob)
|
| 22 |
|
| 23 |
self.register_buffer(
|
| 24 |
+
"position_ids", torch.arange(config.max_position_embeddings).unsqueeze(0), persistent=False
|
| 25 |
)
|
| 26 |
|
| 27 |
def forward(self, input_ids, word_ids):
|
model.safetensors
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:804d554654bf3ec17609d0794f24ac71ca2c311e4dd2be9253672e958f12e1c6
|
| 3 |
+
size 115899720
|
modeling_dual.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import BertPreTrainedModel
|
| 4 |
+
from transformers.modeling_outputs import MaskedLMOutput
|
| 5 |
+
from transformers.models.bert.modeling_bert import BertEncoder
|
| 6 |
+
|
| 7 |
+
from config import DualBertConfig
|
| 8 |
+
from embeddings import DualEmbeddings
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class DualBertForMaskedLM(BertPreTrainedModel):
|
| 12 |
+
config_class = DualBertConfig
|
| 13 |
+
|
| 14 |
+
def __init__(self, config: DualBertConfig):
|
| 15 |
+
super().__init__(config)
|
| 16 |
+
self.dual_embeddings = DualEmbeddings(config)
|
| 17 |
+
self.encoder = BertEncoder(config)
|
| 18 |
+
|
| 19 |
+
self.mlm_dense = nn.Linear(config.hidden_size, config.word_char_emb_dim)
|
| 20 |
+
self.mlm_act = nn.GELU()
|
| 21 |
+
self.mlm_norm = nn.LayerNorm(config.word_char_emb_dim, eps=config.layer_norm_eps)
|
| 22 |
+
self.mlm_bias = nn.Parameter(torch.zeros(config.vocab_char_size))
|
| 23 |
+
|
| 24 |
+
self.post_init()
|
| 25 |
+
|
| 26 |
+
def get_input_embeddings(self):
|
| 27 |
+
return self.dual_embeddings.char_embeddings
|
| 28 |
+
|
| 29 |
+
def set_input_embeddings(self, value):
|
| 30 |
+
self.dual_embeddings.char_embeddings = value
|
| 31 |
+
|
| 32 |
+
def forward(
|
| 33 |
+
self,
|
| 34 |
+
input_ids=None,
|
| 35 |
+
word_ids=None,
|
| 36 |
+
attention_mask=None,
|
| 37 |
+
labels=None,
|
| 38 |
+
return_dict=True,
|
| 39 |
+
**kwargs,
|
| 40 |
+
):
|
| 41 |
+
if input_ids is None or word_ids is None:
|
| 42 |
+
raise ValueError("Both input_ids and word_ids are required.")
|
| 43 |
+
|
| 44 |
+
if attention_mask is None:
|
| 45 |
+
attention_mask = torch.ones_like(input_ids, dtype=torch.long)
|
| 46 |
+
|
| 47 |
+
emb = self.dual_embeddings(input_ids=input_ids, word_ids=word_ids)
|
| 48 |
+
ext_mask = self.get_extended_attention_mask(attention_mask, input_ids.shape, input_ids.device)
|
| 49 |
+
|
| 50 |
+
enc_out = self.encoder(
|
| 51 |
+
emb,
|
| 52 |
+
attention_mask=ext_mask,
|
| 53 |
+
head_mask=[None] * self.config.num_hidden_layers,
|
| 54 |
+
return_dict=True,
|
| 55 |
+
)
|
| 56 |
+
seq = enc_out.last_hidden_state
|
| 57 |
+
|
| 58 |
+
x = self.mlm_dense(seq)
|
| 59 |
+
x = self.mlm_act(x)
|
| 60 |
+
x = self.mlm_norm(x)
|
| 61 |
+
|
| 62 |
+
char_emb = self.dual_embeddings.char_embeddings.weight
|
| 63 |
+
logits = x @ char_emb.T + self.mlm_bias
|
| 64 |
+
|
| 65 |
+
logits = x @ char_emb.T + self.mlm_bias
|
| 66 |
+
|
| 67 |
+
# DEBUG: мониторим норму эмбеддингов
|
| 68 |
+
if torch.isnan(logits).any() or torch.isinf(logits).any():
|
| 69 |
+
emb_norm = self.dual_embeddings.char_embeddings.weight.norm()
|
| 70 |
+
x_norm = x.norm()
|
| 71 |
+
raise RuntimeError(
|
| 72 |
+
f"NaN/Inf in logits! char_emb_norm={emb_norm:.2f}, x_norm={x_norm:.2f}"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
loss = None
|
| 76 |
+
if labels is not None:
|
| 77 |
+
loss_fct = nn.CrossEntropyLoss(ignore_index=-100, label_smoothing=0.1)
|
| 78 |
+
loss = loss_fct(logits.view(-1, self.config.vocab_char_size), labels.view(-1))
|
| 79 |
+
|
| 80 |
+
if not return_dict:
|
| 81 |
+
return (loss, logits) if loss is not None else (logits,)
|
| 82 |
+
|
| 83 |
+
return MaskedLMOutput(loss=loss, logits=logits)
|