MCPcopy Create free account
hub / github.com/microsoft/LoRA / DataCollatorWithPadding

Class DataCollatorWithPadding

examples/NLU/src/transformers/data/data_collator.py:86–131  ·  view source on GitHub ↗

Data collator that will dynamically pad the inputs received. Args: tokenizer (:class:`~transformers.PreTrainedTokenizer` or :class:`~transformers.PreTrainedTokenizerFast`): The tokenizer used for encoding the data. padding (:obj:`bool`, :obj:`str` or :class:`~tr

Source from the content-addressed store, hash-verified

84
85@dataclass
86class DataCollatorWithPadding:
87 """
88 Data collator that will dynamically pad the inputs received.
89
90 Args:
91 tokenizer (:class:`~transformers.PreTrainedTokenizer` or :class:`~transformers.PreTrainedTokenizerFast`):
92 The tokenizer used for encoding the data.
93 padding (:obj:`bool`, :obj:`str` or :class:`~transformers.file_utils.PaddingStrategy`, `optional`, defaults to :obj:`True`):
94 Select a strategy to pad the returned sequences (according to the model's padding side and padding index)
95 among:
96
97 * :obj:`True` or :obj:`'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
98 sequence if provided).
99 * :obj:`'max_length'`: Pad to a maximum length specified with the argument :obj:`max_length` or to the
100 maximum acceptable input length for the model if that argument is not provided.
101 * :obj:`False` or :obj:`'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of
102 different lengths).
103 max_length (:obj:`int`, `optional`):
104 Maximum length of the returned list and optionally padding length (see above).
105 pad_to_multiple_of (:obj:`int`, `optional`):
106 If set will pad the sequence to a multiple of the provided value.
107
108 This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability >=
109 7.5 (Volta).
110 """
111
112 tokenizer: PreTrainedTokenizerBase
113 padding: Union[bool, str, PaddingStrategy] = True
114 max_length: Optional[int] = None
115 pad_to_multiple_of: Optional[int] = None
116
117 def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]:
118 batch = self.tokenizer.pad(
119 features,
120 padding=self.padding,
121 max_length=self.max_length,
122 pad_to_multiple_of=self.pad_to_multiple_of,
123 return_tensors="pt",
124 )
125 if "label" in batch:
126 batch["labels"] = batch["label"]
127 del batch["label"]
128 if "label_ids" in batch:
129 batch["labels"] = batch["label_ids"]
130 del batch["label_ids"]
131 return batch
132
133
134@dataclass

Callers 11

mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
__init__Method · 0.50

Calls

no outgoing calls

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…