Instructions to use mideind/IceBERT-PoS with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mideind/IceBERT-PoS with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="mideind/IceBERT-PoS", trust_remote_code=True, device_map="auto")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("mideind/IceBERT-PoS", trust_remote_code=True, dtype="auto", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Add support for transformers version>5 as well as 4 (backwards compatible).
Browse filesThe RobertaTokenizer v5 removed `encode_plus`, rather use `__call__` when `encode_plus` is missing.
- modeling.py +22 -16
modeling.py
CHANGED
|
@@ -172,8 +172,6 @@ class IceBertPosForTokenClassification(PreTrainedModel):
|
|
| 172 |
head_mask: Optional[torch.Tensor] = None,
|
| 173 |
inputs_embeds: Optional[torch.Tensor] = None,
|
| 174 |
output_attentions: Optional[bool] = None,
|
| 175 |
-
output_hidden_states: Optional[bool] = None,
|
| 176 |
-
return_dict: Optional[bool] = None,
|
| 177 |
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 178 |
"""
|
| 179 |
B = batch_size, L = seq_len, H = hidden_size, C = num_categories, A = num_attributes, W = max_words
|
|
@@ -187,10 +185,6 @@ class IceBertPosForTokenClassification(PreTrainedModel):
|
|
| 187 |
cat_logits: Category logits (B x W x C)
|
| 188 |
attr_logits: Attribute logits (B x W x A)
|
| 189 |
"""
|
| 190 |
-
return_dict = (
|
| 191 |
-
return_dict if return_dict is not None else self.config.use_return_dict
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
# Get RoBERTa outputs
|
| 195 |
outputs = self.roberta(
|
| 196 |
input_ids,
|
|
@@ -201,7 +195,6 @@ class IceBertPosForTokenClassification(PreTrainedModel):
|
|
| 201 |
inputs_embeds=inputs_embeds,
|
| 202 |
output_attentions=output_attentions,
|
| 203 |
output_hidden_states=True,
|
| 204 |
-
return_dict=return_dict,
|
| 205 |
)
|
| 206 |
|
| 207 |
hidden_states = outputs[0] # (B x L x H)
|
|
@@ -370,15 +363,28 @@ class IceBertPosForTokenClassification(PreTrainedModel):
|
|
| 370 |
Tuple of (input_ids, attention_mask, word_mask) without batch dimension.
|
| 371 |
"""
|
| 372 |
# Encode with word boundary preservation
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
input_ids = encoding["input_ids"].squeeze(0) # (L,)
|
| 384 |
attention_mask = torch.ones_like(input_ids)
|
|
|
|
| 172 |
head_mask: Optional[torch.Tensor] = None,
|
| 173 |
inputs_embeds: Optional[torch.Tensor] = None,
|
| 174 |
output_attentions: Optional[bool] = None,
|
|
|
|
|
|
|
| 175 |
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 176 |
"""
|
| 177 |
B = batch_size, L = seq_len, H = hidden_size, C = num_categories, A = num_attributes, W = max_words
|
|
|
|
| 185 |
cat_logits: Category logits (B x W x C)
|
| 186 |
attr_logits: Attribute logits (B x W x A)
|
| 187 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
# Get RoBERTa outputs
|
| 189 |
outputs = self.roberta(
|
| 190 |
input_ids,
|
|
|
|
| 195 |
inputs_embeds=inputs_embeds,
|
| 196 |
output_attentions=output_attentions,
|
| 197 |
output_hidden_states=True,
|
|
|
|
| 198 |
)
|
| 199 |
|
| 200 |
hidden_states = outputs[0] # (B x L x H)
|
|
|
|
| 363 |
Tuple of (input_ids, attention_mask, word_mask) without batch dimension.
|
| 364 |
"""
|
| 365 |
# Encode with word boundary preservation
|
| 366 |
+
# For transformers version <5.0 .encode_plus exists.
|
| 367 |
+
# For versions >=5.0, use __call__ instead.
|
| 368 |
+
if hasattr(tokenizer, "encode_plus"):
|
| 369 |
+
encoding = tokenizer.encode_plus(
|
| 370 |
+
words,
|
| 371 |
+
return_tensors="pt",
|
| 372 |
+
is_split_into_words=True,
|
| 373 |
+
add_special_tokens=True,
|
| 374 |
+
truncation=truncate,
|
| 375 |
+
# The model was probably trained with a lot shorter sequences
|
| 376 |
+
max_length=self.config.max_position_embeddings - 2,
|
| 377 |
+
)
|
| 378 |
+
else:
|
| 379 |
+
encoding = tokenizer(
|
| 380 |
+
words,
|
| 381 |
+
return_tensors="pt",
|
| 382 |
+
is_split_into_words=True,
|
| 383 |
+
add_special_tokens=True,
|
| 384 |
+
truncation=truncate,
|
| 385 |
+
# The model was probably trained with a lot shorter sequences
|
| 386 |
+
max_length=self.config.max_position_embeddings - 2,
|
| 387 |
+
)
|
| 388 |
|
| 389 |
input_ids = encoding["input_ids"].squeeze(0) # (L,)
|
| 390 |
attention_mask = torch.ones_like(input_ids)
|