Feature Extraction
Transformers
PyTorch
Safetensors
Hebrew
bert
custom_code
text-embeddings-inference
Instructions to use dicta-il/dictabert-joint with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use dicta-il/dictabert-joint with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="dicta-il/dictabert-joint", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("dicta-il/dictabert-joint", trust_remote_code=True) model = AutoModel.from_pretrained("dicta-il/dictabert-joint", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Sync modeling code from FinetuningBert@ebbd87d466c6
Browse files- BertForJointParsing.py +3 -3
- BertForPrefixMarking.py +3 -0
- BertForSyntaxParsing.py +8 -2
BertForJointParsing.py
CHANGED
|
@@ -10,11 +10,11 @@ from transformers.utils import ModelOutput
|
|
| 10 |
# transformers' get_imports() strips the try body but keeps the except body, so the absolute
|
| 11 |
# imports have to sit in the try or trust_remote_code demands them as pip packages
|
| 12 |
try:
|
| 13 |
-
from BertForSyntaxParsing import BertSyntaxParsingHead, SyntaxLabels, SyntaxLogitsOutput, parse_logits as syntax_parse_logits
|
| 14 |
from BertForPrefixMarking import BertPrefixMarkingHead, parse_logits as prefix_parse_logits, encode_sentences_for_bert_for_prefix_marking, get_prefixes_from_str
|
| 15 |
from BertForMorphTagging import BertMorphTaggingHead, MorphLogitsOutput, MorphLabels, parse_logits as morph_parse_logits
|
| 16 |
except ImportError:
|
| 17 |
-
from .BertForSyntaxParsing import BertSyntaxParsingHead, SyntaxLabels, SyntaxLogitsOutput, parse_logits as syntax_parse_logits
|
| 18 |
from .BertForPrefixMarking import BertPrefixMarkingHead, parse_logits as prefix_parse_logits, encode_sentences_for_bert_for_prefix_marking, get_prefixes_from_str
|
| 19 |
from .BertForMorphTagging import BertMorphTaggingHead, MorphLogitsOutput, MorphLabels, parse_logits as morph_parse_logits
|
| 20 |
|
|
@@ -142,7 +142,7 @@ class BertForJointParsing(BertPreTrainedModel):
|
|
| 142 |
# calculate the extended attention mask for any child that might need it
|
| 143 |
extended_attention_mask = None
|
| 144 |
if attention_mask is not None:
|
| 145 |
-
extended_attention_mask =
|
| 146 |
|
| 147 |
# extract the hidden states, and apply the dropout
|
| 148 |
hidden_states = self.dropout(bert_outputs[0])
|
|
|
|
| 10 |
# transformers' get_imports() strips the try body but keeps the except body, so the absolute
|
| 11 |
# imports have to sit in the try or trust_remote_code demands them as pip packages
|
| 12 |
try:
|
| 13 |
+
from BertForSyntaxParsing import BertSyntaxParsingHead, SyntaxLabels, SyntaxLogitsOutput, parse_logits as syntax_parse_logits, make_extended_attention_mask
|
| 14 |
from BertForPrefixMarking import BertPrefixMarkingHead, parse_logits as prefix_parse_logits, encode_sentences_for_bert_for_prefix_marking, get_prefixes_from_str
|
| 15 |
from BertForMorphTagging import BertMorphTaggingHead, MorphLogitsOutput, MorphLabels, parse_logits as morph_parse_logits
|
| 16 |
except ImportError:
|
| 17 |
+
from .BertForSyntaxParsing import BertSyntaxParsingHead, SyntaxLabels, SyntaxLogitsOutput, parse_logits as syntax_parse_logits, make_extended_attention_mask
|
| 18 |
from .BertForPrefixMarking import BertPrefixMarkingHead, parse_logits as prefix_parse_logits, encode_sentences_for_bert_for_prefix_marking, get_prefixes_from_str
|
| 19 |
from .BertForMorphTagging import BertMorphTaggingHead, MorphLogitsOutput, MorphLabels, parse_logits as morph_parse_logits
|
| 20 |
|
|
|
|
| 142 |
# calculate the extended attention mask for any child that might need it
|
| 143 |
extended_attention_mask = None
|
| 144 |
if attention_mask is not None:
|
| 145 |
+
extended_attention_mask = make_extended_attention_mask(attention_mask, bert_outputs[0].dtype)
|
| 146 |
|
| 147 |
# extract the hidden states, and apply the dropout
|
| 148 |
hidden_states = self.dropout(bert_outputs[0])
|
BertForPrefixMarking.py
CHANGED
|
@@ -297,3 +297,6 @@ def get_predicted_prefix_len_from_logits(token, token_logits, config: PrefixConf
|
|
| 297 |
break
|
| 298 |
|
| 299 |
return cur_len
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
break
|
| 298 |
|
| 299 |
return cur_len
|
| 300 |
+
|
| 301 |
+
# name referenced by the auto_map in dicta-il/dictabert-seg config.json
|
| 302 |
+
BertForPrefixMarking = BaseForPrefixMarking
|
BertForSyntaxParsing.py
CHANGED
|
@@ -254,6 +254,10 @@ def wrap_bert_layer_with_partial_info(layer_idx: int, bert_layer: nn.Module, par
|
|
| 254 |
return hidden_states
|
| 255 |
bert_layer.forward = new_forward
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
class BaseForSyntaxParsing(BertPreTrainedModel):
|
| 258 |
base_model_prefix = ""
|
| 259 |
def __init__(self, config, syntax_head_size=128, bert_cls=BertModel, is_partial_info_model=False, is_cls_model=False, syntax_attn_label_count=1, syntax_func_label_idx=0):
|
|
@@ -331,7 +335,7 @@ class BaseForSyntaxParsing(BertPreTrainedModel):
|
|
| 331 |
|
| 332 |
extended_attention_mask = None
|
| 333 |
if attention_mask is not None:
|
| 334 |
-
extended_attention_mask =
|
| 335 |
|
| 336 |
hidden_states = self.dropout(bert_outputs[0])
|
| 337 |
|
|
@@ -546,4 +550,6 @@ def choose_contracting_arc(indices: torch.LongTensor, sorted_indices: List[List[
|
|
| 546 |
return best_base_idx, best_head_idx
|
| 547 |
|
| 548 |
def replace_tensor_value(tensor, orig_value, replacement_value):
|
| 549 |
-
return torch.where(tensor == orig_value, replacement_value, tensor)
|
|
|
|
|
|
|
|
|
| 254 |
return hidden_states
|
| 255 |
bert_layer.forward = new_forward
|
| 256 |
|
| 257 |
+
# PreTrainedModel.get_extended_attention_mask was removed in transformers 5
|
| 258 |
+
def make_extended_attention_mask(attention_mask: torch.Tensor, dtype: torch.dtype) -> torch.Tensor:
|
| 259 |
+
return (1.0 - attention_mask[:, None, None, :].to(dtype)) * torch.finfo(dtype).min
|
| 260 |
+
|
| 261 |
class BaseForSyntaxParsing(BertPreTrainedModel):
|
| 262 |
base_model_prefix = ""
|
| 263 |
def __init__(self, config, syntax_head_size=128, bert_cls=BertModel, is_partial_info_model=False, is_cls_model=False, syntax_attn_label_count=1, syntax_func_label_idx=0):
|
|
|
|
| 335 |
|
| 336 |
extended_attention_mask = None
|
| 337 |
if attention_mask is not None:
|
| 338 |
+
extended_attention_mask = make_extended_attention_mask(attention_mask, bert_outputs[0].dtype)
|
| 339 |
|
| 340 |
hidden_states = self.dropout(bert_outputs[0])
|
| 341 |
|
|
|
|
| 550 |
return best_base_idx, best_head_idx
|
| 551 |
|
| 552 |
def replace_tensor_value(tensor, orig_value, replacement_value):
|
| 553 |
+
return torch.where(tensor == orig_value, replacement_value, tensor)
|
| 554 |
+
# name referenced by the auto_map in dicta-il/dictabert-syntax config.json
|
| 555 |
+
BertForSyntaxParsing = BaseForSyntaxParsing
|