Prepares a sequence of input id, or a pair of sequences of inputs ids so that it can be used by the model. It adds special tokens, truncates sequences if overflowing while taking into account the special tokens and manages a moving window (with user defined stride) for overflowing t
(
self,
batch_ids_pairs: List[Union[PreTokenizedInputPair, Tuple[List[int], None]]],
add_special_tokens: bool = True,
padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
max_length: Optional[int] = None,
stride: int = 0,
pad_to_multiple_of: Optional[int] = None,
return_tensors: Optional[str] = None,
return_token_type_ids: Optional[bool] = None,
return_attention_mask: Optional[bool] = None,
return_overflowing_tokens: bool = False,
return_special_tokens_mask: bool = False,
return_length: bool = False,
verbose: bool = True,
)
| 558 | |
| 559 | @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) |
| 560 | def _batch_prepare_for_model( |
| 561 | self, |
| 562 | batch_ids_pairs: List[Union[PreTokenizedInputPair, Tuple[List[int], None]]], |
| 563 | add_special_tokens: bool = True, |
| 564 | padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, |
| 565 | truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, |
| 566 | max_length: Optional[int] = None, |
| 567 | stride: int = 0, |
| 568 | pad_to_multiple_of: Optional[int] = None, |
| 569 | return_tensors: Optional[str] = None, |
| 570 | return_token_type_ids: Optional[bool] = None, |
| 571 | return_attention_mask: Optional[bool] = None, |
| 572 | return_overflowing_tokens: bool = False, |
| 573 | return_special_tokens_mask: bool = False, |
| 574 | return_length: bool = False, |
| 575 | verbose: bool = True, |
| 576 | ) -> BatchEncoding: |
| 577 | """ Prepares a sequence of input id, or a pair of sequences of inputs ids so that it can be used by the model. |
| 578 | It adds special tokens, truncates sequences if overflowing while taking into account the special tokens and |
| 579 | manages a moving window (with user defined stride) for overflowing tokens |
| 580 | |
| 581 | Args: |
| 582 | batch_ids_pairs: list of tokenized input ids or input ids pairs |
| 583 | """ |
| 584 | |
| 585 | batch_outputs = {} |
| 586 | for first_ids, second_ids in batch_ids_pairs: |
| 587 | outputs = self.prepare_for_model( |
| 588 | first_ids, |
| 589 | second_ids, |
| 590 | add_special_tokens=add_special_tokens, |
| 591 | padding=PaddingStrategy.DO_NOT_PAD.value, # we pad in batch afterward |
| 592 | truncation=truncation_strategy.value, |
| 593 | max_length=max_length, |
| 594 | stride=stride, |
| 595 | pad_to_multiple_of=None, # we pad in batch afterward |
| 596 | return_attention_mask=False, # we pad in batch afterward |
| 597 | return_token_type_ids=return_token_type_ids, |
| 598 | return_overflowing_tokens=return_overflowing_tokens, |
| 599 | return_special_tokens_mask=return_special_tokens_mask, |
| 600 | return_length=return_length, |
| 601 | return_tensors=None, # We convert the whole batch to tensors at the end |
| 602 | prepend_batch_axis=False, |
| 603 | verbose=verbose, |
| 604 | ) |
| 605 | |
| 606 | for key, value in outputs.items(): |
| 607 | if key not in batch_outputs: |
| 608 | batch_outputs[key] = [] |
| 609 | batch_outputs[key].append(value) |
| 610 | |
| 611 | batch_outputs = self.pad( |
| 612 | batch_outputs, |
| 613 | padding=padding_strategy.value, |
| 614 | max_length=max_length, |
| 615 | pad_to_multiple_of=pad_to_multiple_of, |
| 616 | return_attention_mask=return_attention_mask, |
| 617 | ) |
no test coverage detected