Instructions to use smangla/ModernBERT-base-squad2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use smangla/ModernBERT-base-squad2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("question-answering", model="smangla/ModernBERT-base-squad2", trust_remote_code=True)# Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("smangla/ModernBERT-base-squad2", trust_remote_code=True) model = AutoModel.from_pretrained("smangla/ModernBERT-base-squad2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload model
Browse files- config.json +3 -3
- models.py +55 -0
config.json
CHANGED
|
@@ -4,11 +4,11 @@
|
|
| 4 |
"CustomQAModel"
|
| 5 |
],
|
| 6 |
"auto_map": {
|
| 7 |
-
"AutoConfig": "
|
| 8 |
-
"AutoModelForImageClassification": "
|
| 9 |
},
|
| 10 |
"base_model_name_or_path": "answerdotai/ModernBERT-base",
|
| 11 |
-
"model_type": "
|
| 12 |
"torch_dtype": "float32",
|
| 13 |
"transformers_version": "4.48.1"
|
| 14 |
}
|
|
|
|
| 4 |
"CustomQAModel"
|
| 5 |
],
|
| 6 |
"auto_map": {
|
| 7 |
+
"AutoConfig": "models.CustomQAModelConfig",
|
| 8 |
+
"AutoModelForImageClassification": "models.CustomQAModel"
|
| 9 |
},
|
| 10 |
"base_model_name_or_path": "answerdotai/ModernBERT-base",
|
| 11 |
+
"model_type": "modernbert",
|
| 12 |
"torch_dtype": "float32",
|
| 13 |
"transformers_version": "4.48.1"
|
| 14 |
}
|
models.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from torch import nn
|
| 2 |
+
from transformers import AutoModel, PretrainedConfig, PreTrainedModel
|
| 3 |
+
from transformers.modeling_outputs import QuestionAnsweringModelOutput
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class CustomQAModelConfig(PretrainedConfig):
|
| 7 |
+
model_type = "modernbert"
|
| 8 |
+
|
| 9 |
+
def __init__(self, base_model_name_or_path="answerdotai/ModernBERT-base", **kwargs):
|
| 10 |
+
self.base_model_name_or_path = base_model_name_or_path
|
| 11 |
+
super().__init__(**kwargs)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class CustomQAModel(PreTrainedModel):
|
| 15 |
+
config_class = CustomQAModelConfig
|
| 16 |
+
|
| 17 |
+
def __init__(self, config):
|
| 18 |
+
super().__init__(config)
|
| 19 |
+
self.base = AutoModel.from_pretrained(config.base_model_name_or_path)
|
| 20 |
+
hidden_size = self.base.config.hidden_size
|
| 21 |
+
self.qa_outputs = nn.Linear(hidden_size, 2)
|
| 22 |
+
self.loss_fn = nn.CrossEntropyLoss()
|
| 23 |
+
|
| 24 |
+
def forward(
|
| 25 |
+
self,
|
| 26 |
+
input_ids=None,
|
| 27 |
+
attention_mask=None,
|
| 28 |
+
start_positions=None,
|
| 29 |
+
end_positions=None,
|
| 30 |
+
):
|
| 31 |
+
outputs = self.base(
|
| 32 |
+
input_ids=input_ids,
|
| 33 |
+
attention_mask=attention_mask,
|
| 34 |
+
)
|
| 35 |
+
hidden_states = outputs.last_hidden_state
|
| 36 |
+
|
| 37 |
+
logits = self.qa_outputs(hidden_states)
|
| 38 |
+
start_logits, end_logits = logits.split(1, dim=-1)
|
| 39 |
+
start_logits = start_logits.squeeze(-1)
|
| 40 |
+
end_logits = end_logits.squeeze(-1)
|
| 41 |
+
|
| 42 |
+
loss = None
|
| 43 |
+
if start_positions is not None and end_positions is not None:
|
| 44 |
+
start_positions = start_positions.clamp(0, start_logits.size(1) - 1)
|
| 45 |
+
end_positions = end_positions.clamp(0, end_logits.size(1) - 1)
|
| 46 |
+
|
| 47 |
+
start_loss = self.loss_fn(start_logits, start_positions)
|
| 48 |
+
end_loss = self.loss_fn(end_logits, end_positions)
|
| 49 |
+
loss = (start_loss + end_loss) / 2
|
| 50 |
+
|
| 51 |
+
return QuestionAnsweringModelOutput(
|
| 52 |
+
loss=loss,
|
| 53 |
+
start_logits=start_logits,
|
| 54 |
+
end_logits=end_logits,
|
| 55 |
+
)
|