MCPcopy Create free account
hub / github.com/RingBDStack/GDAP / LegacySeq2SeqDataset

Class LegacySeq2SeqDataset

seq2seq/utils.py:204–247  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

202
203
204class LegacySeq2SeqDataset(AbstractSeq2SeqDataset):
205 def __getitem__(self, index) -> Dict[str, torch.Tensor]:
206 """Call tokenizer on src and tgt_lines"""
207 index = index + 1 # linecache starts at 1
208 source_line = self.prefix + linecache.getline(str(self.src_file), index).rstrip("\n")
209 tgt_line = linecache.getline(str(self.tgt_file), index).rstrip("\n")
210 assert source_line, f"empty source line for index {index}"
211 assert tgt_line, f"empty tgt line for index {index}"
212 source_inputs = self.encode_line(self.tokenizer, source_line, self.max_source_length)
213 target_inputs = self.encode_line(self.tokenizer, tgt_line, self.max_target_length)
214
215 source_ids = source_inputs["input_ids"].squeeze()
216 target_ids = target_inputs["input_ids"].squeeze()
217 src_mask = source_inputs["attention_mask"].squeeze()
218 return {
219 "input_ids": source_ids,
220 "attention_mask": src_mask,
221 "labels": target_ids,
222 }
223
224 def encode_line(self, tokenizer, line, max_length, pad_to_max_length=True, return_tensors="pt"):
225 """Only used by LegacyDataset"""
226 return tokenizer(
227 [line],
228 max_length=max_length,
229 padding="max_length" if pad_to_max_length else None,
230 truncation=True,
231 return_tensors=return_tensors,
232 **self.dataset_kwargs,
233 )
234
235 def collate_fn(self, batch) -> Dict[str, torch.Tensor]:
236 input_ids = torch.stack([x["input_ids"] for x in batch])
237 masks = torch.stack([x["attention_mask"] for x in batch])
238 target_ids = torch.stack([x["labels"] for x in batch])
239 pad_token_id = self.pad_token_id
240 y = trim_batch(target_ids, pad_token_id)
241 source_ids, source_mask = trim_batch(input_ids, pad_token_id, attention_mask=masks)
242 batch = {
243 "input_ids": source_ids,
244 "attention_mask": source_mask,
245 "labels": y,
246 }
247 return batch
248
249
250class Seq2SeqDataset(AbstractSeq2SeqDataset):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected