Fill-Mask
Transformers
ONNX
Safetensors
Bashkir
bashkir-roberta-preln
bashkir
masked-language-modeling
roberta
sentencepiece
custom-code
onnxruntime
custom_code
Instructions to use failed09/bashkir-roberta with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use failed09/bashkir-roberta with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="failed09/bashkir-roberta", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForMaskedLM model = AutoModelForMaskedLM.from_pretrained("failed09/bashkir-roberta", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Fix dynamic import of configuration_bashkir_roberta
Browse files- modeling_bashkir_roberta.py +117 -114
modeling_bashkir_roberta.py
CHANGED
|
@@ -1,114 +1,117 @@
|
|
| 1 |
-
"""Exact Hugging Face-compatible implementation of BashkirRoBERTa Pre-LN."""
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
-
from torch import nn
|
| 5 |
-
from transformers import PreTrainedModel
|
| 6 |
-
from transformers.modeling_outputs import MaskedLMOutput
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
)
|
| 17 |
-
self.
|
| 18 |
-
config.
|
| 19 |
-
)
|
| 20 |
-
self.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
nn.Linear(config.
|
| 49 |
-
nn.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
)
|
| 74 |
-
self.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
def
|
| 82 |
-
self.embeddings.word_embeddings
|
| 83 |
-
|
| 84 |
-
def
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def
|
| 88 |
-
self.lm_head
|
| 89 |
-
|
| 90 |
-
def
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
hidden_states = self.
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Exact Hugging Face-compatible implementation of BashkirRoBERTa Pre-LN."""
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from torch import nn
|
| 5 |
+
from transformers import PreTrainedModel
|
| 6 |
+
from transformers.modeling_outputs import MaskedLMOutput
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
from .configuration_bashkir_roberta import BashkirRobertaConfig
|
| 10 |
+
except (ImportError, ValueError):
|
| 11 |
+
from configuration_bashkir_roberta import BashkirRobertaConfig
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class BashkirRobertaEmbeddings(nn.Module):
|
| 15 |
+
def __init__(self, config):
|
| 16 |
+
super().__init__()
|
| 17 |
+
self.word_embeddings = nn.Embedding(
|
| 18 |
+
config.vocab_size, config.hidden_size, padding_idx=config.pad_token_id
|
| 19 |
+
)
|
| 20 |
+
self.position_embeddings = nn.Embedding(
|
| 21 |
+
config.max_position_embeddings, config.hidden_size
|
| 22 |
+
)
|
| 23 |
+
self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=1e-12)
|
| 24 |
+
self.dropout = nn.Dropout(config.hidden_dropout_prob)
|
| 25 |
+
|
| 26 |
+
def forward(self, input_ids):
|
| 27 |
+
sequence_length = input_ids.shape[1]
|
| 28 |
+
positions = torch.arange(
|
| 29 |
+
sequence_length, dtype=torch.long, device=input_ids.device
|
| 30 |
+
).unsqueeze(0)
|
| 31 |
+
hidden_states = self.word_embeddings(input_ids) + self.position_embeddings(positions)
|
| 32 |
+
return self.dropout(self.LayerNorm(hidden_states))
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class BashkirRobertaLayer(nn.Module):
|
| 36 |
+
"""Pre-LayerNorm transformer block, matching ``train_flagship.py`` exactly."""
|
| 37 |
+
|
| 38 |
+
def __init__(self, config):
|
| 39 |
+
super().__init__()
|
| 40 |
+
self.self_attn = nn.MultiheadAttention(
|
| 41 |
+
config.hidden_size,
|
| 42 |
+
config.num_attention_heads,
|
| 43 |
+
dropout=config.hidden_dropout_prob,
|
| 44 |
+
batch_first=True,
|
| 45 |
+
)
|
| 46 |
+
self.norm1 = nn.LayerNorm(config.hidden_size, eps=1e-12)
|
| 47 |
+
self.ffn = nn.Sequential(
|
| 48 |
+
nn.Linear(config.hidden_size, config.intermediate_size),
|
| 49 |
+
nn.GELU(),
|
| 50 |
+
nn.Dropout(config.hidden_dropout_prob),
|
| 51 |
+
nn.Linear(config.intermediate_size, config.hidden_size),
|
| 52 |
+
nn.Dropout(config.hidden_dropout_prob),
|
| 53 |
+
)
|
| 54 |
+
self.norm2 = nn.LayerNorm(config.hidden_size, eps=1e-12)
|
| 55 |
+
|
| 56 |
+
def forward(self, hidden_states, padding_mask=None):
|
| 57 |
+
normalized = self.norm1(hidden_states)
|
| 58 |
+
attention, _ = self.self_attn(
|
| 59 |
+
normalized, normalized, normalized, key_padding_mask=padding_mask
|
| 60 |
+
)
|
| 61 |
+
hidden_states = hidden_states + attention
|
| 62 |
+
return hidden_states + self.ffn(self.norm2(hidden_states))
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class BashkirRobertaForMaskedLM(PreTrainedModel):
|
| 66 |
+
config_class = BashkirRobertaConfig
|
| 67 |
+
base_model_prefix = "bashkir_roberta"
|
| 68 |
+
main_input_name = "input_ids"
|
| 69 |
+
_tied_weights_keys = {"lm_head.weight": "embeddings.word_embeddings.weight"}
|
| 70 |
+
|
| 71 |
+
def __init__(self, config):
|
| 72 |
+
super().__init__(config)
|
| 73 |
+
self.embeddings = BashkirRobertaEmbeddings(config)
|
| 74 |
+
self.layers = nn.ModuleList(
|
| 75 |
+
[BashkirRobertaLayer(config) for _ in range(config.num_hidden_layers)]
|
| 76 |
+
)
|
| 77 |
+
self.norm_final = nn.LayerNorm(config.hidden_size, eps=1e-12)
|
| 78 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 79 |
+
self.post_init()
|
| 80 |
+
|
| 81 |
+
def get_input_embeddings(self):
|
| 82 |
+
return self.embeddings.word_embeddings
|
| 83 |
+
|
| 84 |
+
def set_input_embeddings(self, value):
|
| 85 |
+
self.embeddings.word_embeddings = value
|
| 86 |
+
|
| 87 |
+
def get_output_embeddings(self):
|
| 88 |
+
return self.lm_head
|
| 89 |
+
|
| 90 |
+
def set_output_embeddings(self, new_embeddings):
|
| 91 |
+
self.lm_head = new_embeddings
|
| 92 |
+
|
| 93 |
+
def tie_weights(self, **kwargs):
|
| 94 |
+
if self.config.tie_word_embeddings:
|
| 95 |
+
self.lm_head.weight = self.embeddings.word_embeddings.weight
|
| 96 |
+
|
| 97 |
+
def forward(self, input_ids, attention_mask=None, labels=None, return_dict=True, **kwargs):
|
| 98 |
+
if input_ids.shape[1] > self.config.max_position_embeddings:
|
| 99 |
+
raise ValueError(
|
| 100 |
+
f"Sequence length {input_ids.shape[1]} exceeds "
|
| 101 |
+
f"max_position_embeddings={self.config.max_position_embeddings}."
|
| 102 |
+
)
|
| 103 |
+
padding_mask = input_ids.eq(self.config.pad_token_id) if attention_mask is None else attention_mask.eq(0)
|
| 104 |
+
hidden_states = self.embeddings(input_ids)
|
| 105 |
+
for layer in self.layers:
|
| 106 |
+
hidden_states = layer(hidden_states, padding_mask=padding_mask)
|
| 107 |
+
hidden_states = self.norm_final(hidden_states)
|
| 108 |
+
logits = self.lm_head(hidden_states)
|
| 109 |
+
|
| 110 |
+
loss = None
|
| 111 |
+
if labels is not None:
|
| 112 |
+
loss = nn.functional.cross_entropy(
|
| 113 |
+
logits.view(-1, self.config.vocab_size), labels.view(-1), ignore_index=-100
|
| 114 |
+
)
|
| 115 |
+
if not return_dict:
|
| 116 |
+
return (loss, logits) if loss is not None else (logits,)
|
| 117 |
+
return MaskedLMOutput(loss=loss, logits=logits)
|