forked from NTDXYG/SOTitle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht5_utils.py
76 lines (64 loc) · 2.42 KB
/
t5_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import logging
import os
import pickle
from torch.utils.data import Dataset
from tqdm.auto import tqdm
logger = logging.getLogger(__name__)
def preprocess_data(data):
prefix, input_text, target_text, tokenizer, args = data
# Add EOS again if truncated?
if args.preprocess_inputs:
batch = tokenizer.prepare_seq2seq_batch(
src_texts=[prefix + ": " + input_text],
tgt_texts=[target_text],
max_length=args.max_seq_length,
padding="max_length",
return_tensors="pt",
truncation=True,
)
else:
batch = tokenizer.prepare_seq2seq_batch(
src_texts=[prefix + input_text],
tgt_texts=[target_text],
max_length=args.max_seq_length,
padding="max_length",
return_tensors="pt",
truncation=True,
)
input_ids = batch["input_ids"][0]
attention_mask = batch["attention_mask"][0]
labels = batch["labels"][0]
return (input_ids, attention_mask, labels)
class T5Dataset(Dataset):
def __init__(self, tokenizer, args, data, mode):
cached_features_file = os.path.join(
args.cache_dir,
mode + "_cached_"
+ str(args.max_seq_length)
+ str(len(data)),
)
print(cached_features_file)
if os.path.exists(cached_features_file):
logger.info(" Loading features from cached file %s", cached_features_file)
with open(cached_features_file, "rb") as handle:
self.examples = pickle.load(handle)
else:
logger.info(" Creating features from dataset file at %s", args.cache_dir)
data = [
(str(prefix), str(input_text), str(target_text), tokenizer, args)
for prefix, input_text, target_text in zip(
data["prefix"], data["input_text"], data["target_text"]
)
]
self.examples = [
preprocess_data(d) for d in tqdm(data, disable=args.silent)
]
logger.info(
" Saving features into cached file %s", cached_features_file
)
with open(cached_features_file, "wb") as handle:
pickle.dump(self.examples, handle, protocol=pickle.HIGHEST_PROTOCOL)
def __len__(self):
return len(self.examples)
def __getitem__(self, index):
return self.examples[index]