2023-03-28 12:25:36 +00:00
|
|
|
# Copyright 2023 Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import copy
|
2023-08-02 02:17:36 +00:00
|
|
|
from typing import Dict, Sequence, Tuple
|
2023-03-28 12:25:36 +00:00
|
|
|
|
|
|
|
import torch
|
2023-09-19 06:20:26 +00:00
|
|
|
from coati.models.chatglm.chatglm_tokenizer import ChatGLMTokenizer
|
2023-03-28 12:25:36 +00:00
|
|
|
from torch.utils.data import Dataset
|
|
|
|
from tqdm import tqdm
|
2023-08-02 02:17:36 +00:00
|
|
|
from transformers import PreTrainedTokenizer
|
2023-09-19 06:20:26 +00:00
|
|
|
|
2023-03-28 12:25:36 +00:00
|
|
|
from colossalai.logging import get_dist_logger
|
|
|
|
|
|
|
|
from .utils import is_rank_0, jload
|
|
|
|
|
|
|
|
logger = get_dist_logger()
|
|
|
|
|
|
|
|
IGNORE_INDEX = -100
|
|
|
|
PROMPT_DICT = {
|
2023-09-19 06:20:26 +00:00
|
|
|
"prompt_input": (
|
|
|
|
"Below is an instruction that describes a task, paired with an input that provides further context. "
|
|
|
|
"Write a response that appropriately completes the request.\n\n"
|
|
|
|
"### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:"
|
|
|
|
),
|
|
|
|
"prompt_no_input": (
|
|
|
|
"Below is an instruction that describes a task. "
|
|
|
|
"Write a response that appropriately completes the request.\n\n"
|
|
|
|
"### Instruction:\n{instruction}\n\n### Response:"
|
|
|
|
),
|
2023-03-28 12:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
def _preprocess(
|
|
|
|
sources: Sequence[str],
|
|
|
|
targets: Sequence[str],
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
|
|
|
max_length: int,
|
|
|
|
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
2023-08-02 02:17:36 +00:00
|
|
|
"""Preprocess the data by tokenizing."""
|
|
|
|
sequences = [s + t for s, t in zip(sources, targets)]
|
2023-09-19 06:20:26 +00:00
|
|
|
sequences_token = tokenizer(
|
|
|
|
sequences, max_length=max_length, padding="max_length", truncation=True, return_tensors="pt"
|
|
|
|
)
|
|
|
|
sources_token = tokenizer(
|
|
|
|
sources, max_length=max_length, padding="max_length", truncation=True, return_tensors="pt"
|
|
|
|
)
|
2023-08-02 02:17:36 +00:00
|
|
|
|
|
|
|
labels = copy.deepcopy(sequences_token["input_ids"])
|
|
|
|
for i in range(labels.shape[0]):
|
|
|
|
source_len = sources_token["attention_mask"][i].sum().item()
|
|
|
|
pad_len = max_length - sequences_token["attention_mask"][i].sum().item()
|
|
|
|
if tokenizer.padding_side == "right":
|
|
|
|
# |prompt|completion|eos|pad|
|
|
|
|
labels[i][:source_len] = IGNORE_INDEX
|
|
|
|
elif tokenizer.padding_side == "left":
|
|
|
|
# |pad|prompt|completion|eos|
|
2023-09-19 06:20:26 +00:00
|
|
|
labels[i][pad_len : pad_len + source_len] = IGNORE_INDEX
|
2023-08-02 02:17:36 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError()
|
|
|
|
|
|
|
|
return sequences_token["input_ids"], labels, sequences_token["attention_mask"]
|
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
def _preprocess_chatglm(
|
|
|
|
sources: Sequence[str],
|
|
|
|
targets: Sequence[str],
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
|
|
|
max_length: int,
|
|
|
|
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
2023-08-29 09:58:51 +00:00
|
|
|
"""
|
|
|
|
Preprocess the data by tokenizing.
|
|
|
|
None for attention mask, ChatGLM will calculate attention mask according to input ids
|
|
|
|
"""
|
2023-09-19 06:20:26 +00:00
|
|
|
|
2023-08-29 09:58:51 +00:00
|
|
|
labels = []
|
|
|
|
input_ids = []
|
|
|
|
for source, target in zip(sources, targets):
|
|
|
|
source_id = tokenizer.encode(text=source, add_special_tokens=False)
|
|
|
|
target_id = tokenizer.encode(text=target, add_special_tokens=False)
|
|
|
|
input_id = tokenizer.build_inputs_with_special_tokens(source_id, target_id)
|
|
|
|
# truncate
|
|
|
|
sp_token_list = [tokenizer.gmask_token_id, tokenizer.bos_token_id]
|
|
|
|
truncate_length = max(0, len(input_id) - max_length)
|
2023-09-19 06:20:26 +00:00
|
|
|
input_id = input_id[truncate_length:]
|
2023-08-29 09:58:51 +00:00
|
|
|
if truncate_length == len(source_id) + 1:
|
2023-09-19 06:20:26 +00:00
|
|
|
input_id = sp_token_list + input_id[1:]
|
2023-08-29 09:58:51 +00:00
|
|
|
elif truncate_length > len(source_id) + 1:
|
2023-09-19 06:20:26 +00:00
|
|
|
input_id = sp_token_list + input_id[2:]
|
|
|
|
|
2023-08-29 09:58:51 +00:00
|
|
|
context_length = input_id.index(tokenizer.bos_token_id)
|
|
|
|
mask_position = context_length - 1
|
2023-09-19 06:20:26 +00:00
|
|
|
label = [IGNORE_INDEX] * context_length + input_id[mask_position + 1 :]
|
|
|
|
|
2023-08-29 09:58:51 +00:00
|
|
|
pad_len = max_length - len(input_id)
|
|
|
|
input_id = input_id + [tokenizer.pad_token_id] * pad_len
|
|
|
|
input_ids.append(input_id)
|
|
|
|
labels.append(label + [IGNORE_INDEX] * pad_len)
|
|
|
|
return torch.tensor(input_ids), torch.tensor(labels), None
|
|
|
|
|
|
|
|
|
2023-03-28 12:25:36 +00:00
|
|
|
class SFTDataset(Dataset):
|
|
|
|
"""
|
|
|
|
Dataset for sft model
|
|
|
|
|
|
|
|
Args:
|
|
|
|
dataset: dataset for supervised model
|
|
|
|
tokenizer: tokenizer for supervised model
|
|
|
|
max_length: max length of input
|
|
|
|
"""
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
def __init__(self, dataset: Dict, tokenizer: PreTrainedTokenizer, max_length: int = 512) -> None:
|
2023-03-28 12:25:36 +00:00
|
|
|
super().__init__()
|
|
|
|
self.input_ids = []
|
|
|
|
|
2023-08-02 02:17:36 +00:00
|
|
|
sources = [data["prompt"] for data in dataset]
|
2023-09-19 06:20:26 +00:00
|
|
|
targets = [data["completion"] + tokenizer.eos_token for data in tqdm(dataset, disable=not is_rank_0())]
|
2023-08-29 09:58:51 +00:00
|
|
|
if isinstance(tokenizer, ChatGLMTokenizer):
|
2023-09-19 06:20:26 +00:00
|
|
|
self.input_ids, self.labels, self.attention_mask = _preprocess_chatglm(
|
|
|
|
sources, targets, tokenizer, max_length
|
|
|
|
)
|
2023-08-29 09:58:51 +00:00
|
|
|
else:
|
2023-09-19 06:20:26 +00:00
|
|
|
self.input_ids, self.labels, self.attention_mask = _preprocess(sources, targets, tokenizer, max_length)
|
2023-03-28 12:25:36 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
2023-08-02 02:17:36 +00:00
|
|
|
length = self.input_ids.shape[0]
|
2023-03-28 12:25:36 +00:00
|
|
|
return length
|
|
|
|
|
|
|
|
def __getitem__(self, idx):
|
2023-08-29 09:58:51 +00:00
|
|
|
if self.attention_mask is not None:
|
2023-09-19 06:20:26 +00:00
|
|
|
return dict(input_ids=self.input_ids[idx], labels=self.labels[idx], attention_mask=self.attention_mask[idx])
|
2023-08-29 09:58:51 +00:00
|
|
|
else:
|
2023-09-19 06:20:26 +00:00
|
|
|
return dict(input_ids=self.input_ids[idx], labels=self.labels[idx])
|
2023-07-28 03:29:55 +00:00
|
|
|
|
|
|
|
|
2023-03-28 12:25:36 +00:00
|
|
|
class SupervisedDataset(Dataset):
|
|
|
|
"""Dataset for supervised fine-tuning."""
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
def __init__(
|
|
|
|
self, data_path: str, tokenizer: PreTrainedTokenizer, max_datasets_size: int = None, max_length: int = 512
|
|
|
|
):
|
2023-08-02 02:17:36 +00:00
|
|
|
super().__init__()
|
2023-03-28 12:25:36 +00:00
|
|
|
logger.info("Loading data...")
|
|
|
|
list_data_dict = jload(data_path)
|
|
|
|
logger.info(f"Loaded {len(list_data_dict)} examples.")
|
|
|
|
|
|
|
|
if max_datasets_size is not None:
|
|
|
|
logger.info(f"Limiting dataset to {max_datasets_size} examples.")
|
|
|
|
list_data_dict = list_data_dict[:max_datasets_size]
|
|
|
|
|
|
|
|
logger.info("Formatting inputs...")
|
2023-08-02 02:17:36 +00:00
|
|
|
prompt_input, prompt_no_input = PROMPT_DICT["prompt_input"], PROMPT_DICT["prompt_no_input"]
|
|
|
|
sources = [
|
|
|
|
prompt_input.format_map(example) if "input" in example else prompt_no_input.format_map(example)
|
|
|
|
for example in list_data_dict
|
|
|
|
]
|
2023-09-19 06:20:26 +00:00
|
|
|
targets = [example["output"] + tokenizer.eos_token for example in list_data_dict]
|
2023-08-02 02:17:36 +00:00
|
|
|
|
|
|
|
logger.info("Tokenizing inputs... This may take some time...")
|
2023-08-29 09:58:51 +00:00
|
|
|
if isinstance(tokenizer, ChatGLMTokenizer):
|
2023-09-19 06:20:26 +00:00
|
|
|
self.input_ids, self.labels, self.attention_mask = _preprocess_chatglm(
|
|
|
|
sources, targets, tokenizer, max_length
|
|
|
|
)
|
2023-08-29 09:58:51 +00:00
|
|
|
else:
|
2023-09-19 06:20:26 +00:00
|
|
|
self.input_ids, self.labels, self.attention_mask = _preprocess(sources, targets, tokenizer, max_length)
|
2023-03-28 12:25:36 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
2023-08-02 02:17:36 +00:00
|
|
|
length = self.input_ids.shape[0]
|
|
|
|
return length
|
2023-03-28 12:25:36 +00:00
|
|
|
|
2023-08-02 02:17:36 +00:00
|
|
|
def __getitem__(self, idx):
|
2023-08-29 09:58:51 +00:00
|
|
|
if self.attention_mask is not None:
|
2023-09-19 06:20:26 +00:00
|
|
|
return dict(input_ids=self.input_ids[idx], labels=self.labels[idx], attention_mask=self.attention_mask[idx])
|
2023-08-29 09:58:51 +00:00
|
|
|
else:
|
2023-09-19 06:20:26 +00:00
|
|
|
return dict(input_ids=self.input_ids[idx], labels=self.labels[idx])
|