Pad a single encoded input or a batch of encoded inputs up to predefined length or to the max sequence length in the batch. Padding side (left/right) padding token ids are defined at the tokenizer level (with ``self.padding_side``, ``self.pad_token_id`` and ``self.pad_token
(
self,
encoded_inputs: Union[
BatchEncoding,
List[BatchEncoding],
Dict[str, EncodedInput],
Dict[str, List[EncodedInput]],
List[Dict[str, EncodedInput]],
],
padding: Union[bool, str] = True,
max_length: Optional[int] = None,
pad_to_multiple_of: Optional[int] = None,
return_attention_mask: Optional[bool] = None,
return_tensors: Optional[Union[str, TensorType]] = None,
verbose: bool = True,
)
| 1862 | raise NotImplementedError |
| 1863 | |
| 1864 | def pad( |
| 1865 | self, |
| 1866 | encoded_inputs: Union[ |
| 1867 | BatchEncoding, |
| 1868 | List[BatchEncoding], |
| 1869 | Dict[str, EncodedInput], |
| 1870 | Dict[str, List[EncodedInput]], |
| 1871 | List[Dict[str, EncodedInput]], |
| 1872 | ], |
| 1873 | padding: Union[bool, str] = True, |
| 1874 | max_length: Optional[int] = None, |
| 1875 | pad_to_multiple_of: Optional[int] = None, |
| 1876 | return_attention_mask: Optional[bool] = None, |
| 1877 | return_tensors: Optional[Union[str, TensorType]] = None, |
| 1878 | verbose: bool = True, |
| 1879 | ) -> BatchEncoding: |
| 1880 | """ Pad a single encoded input or a batch of encoded inputs up to predefined length or to the max sequence length in the batch. |
| 1881 | |
| 1882 | Padding side (left/right) padding token ids are defined at the tokenizer level |
| 1883 | (with ``self.padding_side``, ``self.pad_token_id`` and ``self.pad_token_type_id``) |
| 1884 | |
| 1885 | Args: |
| 1886 | encoded_inputs: Dictionary of tokenized inputs (`Dict[str, List[int]]`) or batch of tokenized inputs. |
| 1887 | Batch of tokenized inputs can be given as dicts of lists or lists of dicts, both work so you can |
| 1888 | use ``tokenizer.pad()`` during pre-processing as well as in a PyTorch Dataloader collate function. |
| 1889 | (`Dict[str, List[List[int]]]` or `List[Dict[str, List[int]]]`). |
| 1890 | padding: Boolean or specific strategy to use for padding. |
| 1891 | Select a strategy to pad the returned sequences (according to the model's padding side and padding index) among: |
| 1892 | - 'longest' (or `True`) Pad to the longest sequence in the batch |
| 1893 | - 'max_length': Pad to the max length (default) |
| 1894 | - 'do_not_pad' (or `False`): Do not pad |
| 1895 | max_length: maximum length of the returned list and optionally padding length (see below). |
| 1896 | Will truncate by taking into account the special tokens. |
| 1897 | pad_to_multiple_of: (optional) Integer if set will pad the sequence to a multiple of the provided value. |
| 1898 | This is especially useful to enable the use of Tensor Core on NVIDIA hardware with compute capability |
| 1899 | >= 7.5 (Volta). |
| 1900 | return_attention_mask: (optional) Set to False to avoid returning attention mask (default: set to model specifics) |
| 1901 | return_tensors (:obj:`str`, `optional`, defaults to :obj:`None`): |
| 1902 | Can be set to 'tf', 'pt' or 'np' to return respectively TensorFlow :obj:`tf.constant`, |
| 1903 | PyTorch :obj:`torch.Tensor` or Numpy :oj: `np.ndarray` instead of a list of python integers. |
| 1904 | verbose (:obj:`bool`, `optional`, defaults to :obj:`True`): |
| 1905 | Set to ``False`` to avoid printing infos and warnings. |
| 1906 | """ |
| 1907 | # If we have a list of dicts, let's convert it in a dict of lists |
| 1908 | if isinstance(encoded_inputs, (list, tuple)) and isinstance(encoded_inputs[0], (dict, BatchEncoding)): |
| 1909 | encoded_inputs = {key: [example[key] for example in encoded_inputs] for key in encoded_inputs[0].keys()} |
| 1910 | |
| 1911 | assert "input_ids" in encoded_inputs, ( |
| 1912 | "You should supply an encoding or a list of encodings to this method. " |
| 1913 | "An encoding is the output of one the encoding methods of the tokenizer, i.e. " |
| 1914 | "__call__/encode_plus/batch_encode_plus. " |
| 1915 | ) |
| 1916 | |
| 1917 | if not encoded_inputs["input_ids"]: |
| 1918 | if return_attention_mask: |
| 1919 | encoded_inputs["attention_mask"] = [] |
| 1920 | return encoded_inputs |
| 1921 |
no test coverage detected