Truncates a sequence pair in place to the maximum length. Args: ids: list of tokenized input ids. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. pair_ids: Optional second list of input ids. Can be obtain
(
self,
ids: List[int],
pair_ids: Optional[List[int]] = None,
num_tokens_to_remove: int = 0,
truncation_strategy: Union[str, TruncationStrategy] = "longest_first",
stride: int = 0,
)
| 2101 | return batch_outputs |
| 2102 | |
| 2103 | def truncate_sequences( |
| 2104 | self, |
| 2105 | ids: List[int], |
| 2106 | pair_ids: Optional[List[int]] = None, |
| 2107 | num_tokens_to_remove: int = 0, |
| 2108 | truncation_strategy: Union[str, TruncationStrategy] = "longest_first", |
| 2109 | stride: int = 0, |
| 2110 | ) -> Tuple[List[int], List[int], List[int]]: |
| 2111 | """ Truncates a sequence pair in place to the maximum length. |
| 2112 | |
| 2113 | Args: |
| 2114 | ids: list of tokenized input ids. Can be obtained from a string by chaining the |
| 2115 | `tokenize` and `convert_tokens_to_ids` methods. |
| 2116 | pair_ids: Optional second list of input ids. Can be obtained from a string by chaining the |
| 2117 | `tokenize` and `convert_tokens_to_ids` methods. |
| 2118 | num_tokens_to_remove (:obj:`int`, `optional`, defaults to ``0``): |
| 2119 | number of tokens to remove using the truncation strategy |
| 2120 | truncation_strategy (:obj:`string`, `optional`, defaults to "longest_first"): |
| 2121 | String selected in the following options: |
| 2122 | |
| 2123 | - 'longest_first' (default): Iteratively reduce the inputs sequence until the input is under max_length |
| 2124 | starting from the longest one at each token (when there is a pair of input sequences). |
| 2125 | Overflowing tokens only contains overflow from the first sequence. |
| 2126 | - 'only_first': Only truncate the first sequence. raise an error if the first sequence is shorter or equal to than num_tokens_to_remove. |
| 2127 | - 'only_second': Only truncate the second sequence |
| 2128 | - 'do_not_truncate' |
| 2129 | stride (:obj:`int`, `optional`, defaults to ``0``): |
| 2130 | If set to a number along with max_length, the overflowing tokens returned will contain some tokens |
| 2131 | from the main sequence returned. The value of this argument defines the number of additional tokens. |
| 2132 | """ |
| 2133 | if num_tokens_to_remove <= 0: |
| 2134 | return ids, pair_ids, [] |
| 2135 | |
| 2136 | if not isinstance(truncation_strategy, TruncationStrategy): |
| 2137 | truncation_strategy = TruncationStrategy(truncation_strategy) |
| 2138 | |
| 2139 | overflowing_tokens = [] |
| 2140 | if truncation_strategy == TruncationStrategy.LONGEST_FIRST: |
| 2141 | for _ in range(num_tokens_to_remove): |
| 2142 | if pair_ids is None or len(ids) > len(pair_ids): |
| 2143 | if not overflowing_tokens: |
| 2144 | window_len = min(len(ids), stride + 1) |
| 2145 | else: |
| 2146 | window_len = 1 |
| 2147 | overflowing_tokens.extend(ids[-window_len:]) |
| 2148 | ids = ids[:-1] |
| 2149 | else: |
| 2150 | if not overflowing_tokens: |
| 2151 | window_len = min(len(pair_ids), stride + 1) |
| 2152 | else: |
| 2153 | window_len = 1 |
| 2154 | overflowing_tokens.extend(pair_ids[-window_len:]) |
| 2155 | pair_ids = pair_ids[:-1] |
| 2156 | elif truncation_strategy == TruncationStrategy.ONLY_FIRST: |
| 2157 | if len(ids) > num_tokens_to_remove: |
| 2158 | window_len = min(len(ids), stride + num_tokens_to_remove) |
| 2159 | overflowing_tokens = ids[-window_len:] |
| 2160 | ids = ids[:-num_tokens_to_remove] |
no test coverage detected