haukurpj commited on
Commit
9f03d61
·
1 Parent(s): 27880a7

Add support for transformers version>5 as well as 4 (backwards compatible).

Browse files

The RobertaTokenizer v5 removed `encode_plus`, rather use `__call__` when `encode_plus` is missing.

Files changed (1) hide show
  1. 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
- encoding = tokenizer.encode_plus(
374
- words,
375
- return_tensors="pt",
376
- is_split_into_words=True,
377
- add_special_tokens=True,
378
- truncation=truncate,
379
- # The model was probably trained with a lot shorter sequences
380
- max_length=self.config.max_position_embeddings - 2,
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)