-
Notifications
You must be signed in to change notification settings - Fork 11
/
dataloaders.py
134 lines (100 loc) · 4.54 KB
/
dataloaders.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import json
from tqdm import tqdm
import random
from copy import deepcopy
from pprint import pprint
import numpy as np
import torch
from torch.utils.data import Dataset
from utils import print_rank_0
from utils import read_json_or_jsonl_data
from utils import DEFAULT_PAD_TOKEN, DEFAULT_BOS_TOKEN, DEFAULT_EOS_TOKEN, DEFAULT_UNK_TOKEN
from utils import SEP_TOKEN, IGNORE_INDEX
class TextDataset(Dataset):
def __init__(self, data):
self.data = data
def __getitem__(self, index):
return self.data[index]
def __len__(self,):
return len(self.data)
def sft_data_collactor(args, batch, tokenizer):
input_ids, attention_mask, labels, weights, rewards = [], [], [], [], []
if args.debug_mode:
print_rank_0(" >>>>> debug mode >>>>>")
print_rank_0(" >>>>> begin checking batch info:")
print_rank_0(batch)
for item in batch:
if 'query' in item:
query = item['query']
elif 'prompt' in item:
query = item['prompt']
else:
query = item['text'][0].split(SEP_TOKEN)[0]
query_ids = item['query_id'] if 'query_id' in item else query
if 'target' in item:
target = item['target']
elif 'answer' in item:
target = item['answer']
else:
target_idx = np.argmax(item['scores'])
target = item['text'][target_idx].split(SEP_TOKEN)[-1]
query_token_ids = tokenizer.encode(query, add_special_tokens=False)
query_token_len = len(query_token_ids)
target_token_ids = tokenizer.encode(target, add_special_tokens=False)
input_ids.append(
[tokenizer.bos_token_id] + deepcopy(query_token_ids) + deepcopy(target_token_ids) + [tokenizer.eos_token_id]
)
labels.append(
[IGNORE_INDEX] * (query_token_len+1) + deepcopy(target_token_ids) + [tokenizer.eos_token_id]
)
outputs = batch_padding(input_ids, tokenizer)
label_outputs = batch_padding(labels, tokenizer, pad_token_id=IGNORE_INDEX)
outputs['labels'] = label_outputs['input_ids']
if args.debug_mode:
print_rank_0(" >>>>>>> checking tokenization results")
print_rank_0(outputs)
return {
"input_ids": torch.Tensor(outputs['input_ids']).long(),
"labels": torch.Tensor(outputs['labels']).long(),
"attention_mask": torch.Tensor(outputs['attention_mask']).float(),
}
def weighted_sft_data_collactor(args, batch, tokenizer):
results = sft_data_collactor(args, batch, tokenizer)
weights = [item.get("weight", 1.) for item in batch]
rewards = [item.get("reward", 1.) for item in batch]
results['weights'] = torch.Tensor(weights).float()
results['rewards'] = torch.Tensor(rewards).float()
return results
def offline_ppo_data_collactor(args, batch, tokenizer):
results = weighted_sft_data_collactor(args, batch, tokenizer)
sft_mask = [ 1. if item.get('type', 'sample') == 'sft' else 0. for item in batch]
results['sft_mask'] = torch.Tensor(sft_mask).float()
return results
def batch_padding(input_ids, tokenizer, padding='longest', max_length=None, pad_token_id=None):
if pad_token_id is None:
pad_token_id = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else 0
max_length = tokenizer.model_max_length if max_length is None else max_length
if padding == 'longest':
max_input_length = max([len(inp_ids) for inp_ids in input_ids])
max_length = min(tokenizer.model_max_length, max_input_length)
outputs = {"input_ids": [], "attention_mask": []}
for inp_ids in input_ids:
attn_mask = [1] * len(inp_ids)
if len(inp_ids) >= max_length:
if tokenizer.truncation_side == 'left':
inp_ids = inp_ids[-max_length :]
attn_mask = attn_mask[-max_length :]
else:
inp_ids = inp_ids[:max_length]
attn_mask = attn_mask[:max_length]
else:
if tokenizer.padding_side == 'left':
inp_ids = [pad_token_id] * (max_length - len(inp_ids)) + inp_ids
attn_mask = [0] * (max_length - len(attn_mask)) + attn_mask
else:
inp_ids = inp_ids + [pad_token_id] * (max_length - len(inp_ids))
attn_mask = attn_mask + [0] * (max_length - len(attn_mask))
outputs['input_ids'].append(deepcopy(inp_ids))
outputs['attention_mask'].append(deepcopy(attn_mask))
return outputs