-
Notifications
You must be signed in to change notification settings - Fork 63
/
sft_dataset.py
162 lines (131 loc) · 5.97 KB
/
sft_dataset.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import copy
from dataclasses import dataclass
from typing import Dict, List
import torch
from datasets import DatasetDict
from torch.nn.utils.rnn import pad_sequence
from torch.utils.data import Dataset
from transformers.tokenization_utils import PreTrainedTokenizer
from llamatuner.utils.constants import IGNORE_INDEX
@dataclass
class SupervisedDataset(Dataset):
"""Dataset for supervised fine-tuning of instruction following models.
Converts raw dataset containing source/target instructions
into tokenized input/target pairs with truncation and padding.
Attributes:
dataset: The raw dataset containing source/target examples
tokenizer: Tokenizer to use for encoding text
max_seq_len: Maximum sequence length for truncation
"""
def __init__(
self,
raw_data: DatasetDict,
tokenizer: PreTrainedTokenizer,
max_seq_len: int = 1024,
train_on_source: bool = False,
predict_with_generate: bool = False,
):
"""Initialize the dataset with the raw data and tokenizer.
Args:
raw_data: Raw dataset containing source/target examples
tokenizer: Tokenizer to encode text
max_seq_len: Max sequence length for truncation
train_on_source (bool): If True, the model will be trained on the source text as well as the target text.
predict_with_generate (bool): If True, the model will generate predictions instead of training.
"""
super(SupervisedDataset, self).__init__()
self.dataset = raw_data
self.tokenizer = tokenizer
self.max_seq_len = max_seq_len
self.train_on_source = train_on_source
self.predict_with_generate = predict_with_generate
def __len__(self) -> int:
"""Return number of examples in dataset."""
return len(self.dataset)
def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]:
"""Convert an raw example into tokenized input/target pair.
Args:
idx: Index of the example in the dataset
Returns:
input_ids: tokenized input sequence
labels: tokenized target sequence
"""
example = self.dataset[idx]
source_text = f"{self.tokenizer.bos_token}{example['input']}"
target_text = f"{example['output']}{self.tokenizer.eos_token}"
# Tokenize the source text
tokenized_source = self.tokenizer(source_text,
max_length=self.max_seq_len,
truncation=True,
add_special_tokens=False)
# Tokenize the target text
tokenized_target = self.tokenizer(target_text,
max_length=self.max_seq_len,
truncation=True,
add_special_tokens=False)
source_ids = tokenized_source['input_ids']
target_ids = tokenized_target['input_ids']
# Extract the input_ids tensor
if len(source_ids) > self.max_seq_len:
print(
f'Source length {len(source_ids)} exceeds max seq length of {self.max_seq_len}'
)
# Create the labels tensor
if len(target_ids) > self.max_seq_len:
print(
f'Target length {len(target_ids)} exceeds max seq length of {self.max_seq_len}'
)
if not self.predict_with_generate:
# If not generating predictions, concatenate the input and target ids
input_ids = torch.tensor(source_ids + target_ids)
if not self.train_on_source:
# If not training on the source text, set the labels to IGNORE_INDEX \
# for the input ids and the target ids
labels = torch.tensor(
[IGNORE_INDEX for _ in range(len(source_ids))] +
copy.deepcopy(target_ids))
else:
# If training on the source text, set the labels to the concatenated \
# input and target ids
labels = torch.tensor(copy.deepcopy(source_ids + target_ids))
else:
# If generating predictions, only use the source ids as input
input_ids = torch.tensor(source_ids)
labels = None
# Construct data dictionary containing inputs and labels
data_dict = {'input_ids': input_ids, 'labels': labels}
return data_dict
@dataclass
class DataCollatorForSupervisedDataset:
"""Collate and pad examples for supervised training."""
tokenizer: PreTrainedTokenizer
predict_with_generate: bool = False
def __call__(
self,
examples: List[Dict[str,
torch.Tensor]]) -> Dict[str, torch.Tensor]:
"""Collate examples into dictionary for supervised training.
Args:
examples: List of examples, each containing 'input_ids' and 'labels'
Returns:
Dictionary with padded 'input_ids', 'attention_mask' and optionally 'labels'
"""
# Extract input_ids and labels
input_ids = [example['input_ids'] for example in examples]
labels = [example['labels'] for example in examples]
# Pad input sequences
input_ids = pad_sequence(input_ids,
batch_first=True,
padding_value=self.tokenizer.pad_token_id)
# Pad labels if needed
if not self.predict_with_generate:
labels = pad_sequence(labels,
batch_first=True,
padding_value=IGNORE_INDEX)
# Create attention mask based on padded input
attention_mask = input_ids.ne(self.tokenizer.pad_token_id)
# Assemble final dict
data_dict = {'input_ids': input_ids, 'attention_mask': attention_mask}
if labels is not None:
data_dict['labels'] = labels
return data_dict