File size: 10,217 Bytes
6fcd376 |
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
from dataclasses import dataclass, field
from typing import Dict, Optional, Sequence
import logging
import os, sys
import copy
import torch
import transformers
from transformers import LlamaForCausalLM, LlamaTokenizer
from torch.utils.data import Dataset
from transformers import Trainer
sys.path.append(os.path.dirname(__file__))
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from utils.special_tok_llama2 import (
B_CODE,
E_CODE,
B_RESULT,
E_RESULT,
B_INST,
E_INST,
B_SYS,
E_SYS,
DEFAULT_PAD_TOKEN,
DEFAULT_BOS_TOKEN,
DEFAULT_EOS_TOKEN,
DEFAULT_UNK_TOKEN,
IGNORE_INDEX,
)
from conversation_template import json_to_code_result_tok_temp
@dataclass
class ModelArguments:
model_name_or_path: Optional[str] = field(default="./ckpt/llama-2-13b-chat")
peft: bool = field(default=False)
@dataclass
class DataArguments:
data_path: str = field(
default=None, metadata={"help": "Path to the training data."}
)
@dataclass
class TrainingArguments(transformers.TrainingArguments):
cache_dir: Optional[str] = field(default=None)
optim: str = field(default="adamw_torch")
model_max_length: int = field(
default=4096,
metadata={
"help": "Maximum sequence length. Sequences will be right padded (and possibly truncated)."
},
)
def create_peft_config(model):
from peft import (
get_peft_model,
LoraConfig,
TaskType,
prepare_model_for_int8_training,
)
peft_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
inference_mode=False,
r=8,
lora_alpha=16,
lora_dropout=0.05,
target_modules=["q_proj", "v_proj"],
)
# prepare int-8 model for training
model = prepare_model_for_int8_training(model)
model = get_peft_model(model, peft_config)
model.print_trainable_parameters()
print(f"Using Peft")
return model, peft_config
def _tokenize_fn(
strings: Sequence[str], tokenizer: transformers.PreTrainedTokenizer
) -> Dict:
"""Tokenize a list of strings."""
tokenized_list = [
tokenizer(
text,
return_tensors="pt",
padding="longest",
max_length=tokenizer.model_max_length,
truncation=True,
)
for text in strings
]
input_ids = [tokenized.input_ids[0] for tokenized in tokenized_list]
input_ids_lens = [
tokenized.input_ids.ne(tokenizer.pad_token_id).sum().item()
for tokenized in tokenized_list
]
return dict(
input_ids=input_ids,
input_ids_lens=input_ids_lens,
)
def find_all_sublist_end(main_list, sublist):
"""Find all the ending indices of a sublist in a main list."""
sublist_len = len(sublist)
main_list = main_list.tolist()
indices = []
for index in (i for i, e in enumerate(main_list) if e == sublist[0]):
if main_list[index : index + sublist_len] == sublist:
indices.append(index + sublist_len)
return indices
def find_all_sublist_start(main_list, sublist):
"""Find all the starting indices of a sublist in a main list."""
sublist_len = len(sublist)
main_list = main_list.tolist()
indices = []
for index in (i for i, e in enumerate(main_list) if e == sublist[0]):
if main_list[index : index + sublist_len] == sublist:
indices.append(index)
return indices
def preprocess(
trajs: Sequence[str],
tokenizer: transformers.PreTrainedTokenizer,
) -> Dict:
INST_START_INDEX = tokenizer.encode(f"{B_INST}")[-1]
INST_END_INDEX = tokenizer.encode(f"{E_INST}")[-1]
RESULT_START_INDEX = tokenizer.encode(f"{B_RESULT}")[-1]
RESULT_END_INDEX = tokenizer.encode(f"{E_RESULT}")[-1]
"""Preprocess the data by tokenizing."""
examples_tokenized = _tokenize_fn(trajs, tokenizer)
input_ids_lens = examples_tokenized["input_ids_lens"]
input_ids = examples_tokenized["input_ids"] # [torch.tensor , torch.tensor , ...]
labels = copy.deepcopy(input_ids)
# IGNORE INDEX SET
for i, label in enumerate(labels):
user_start_inds = find_all_sublist_start(label, [INST_START_INDEX])
assistant_start_inds = find_all_sublist_end(label, [INST_END_INDEX])
result_start_inds = find_all_sublist_start(label, [RESULT_START_INDEX])
result_end_inds = find_all_sublist_end(label, [RESULT_END_INDEX])
# for debug
# for len_i, ind in enumerate(label):
# print(f'{len_i}|{ind} -> "{tokenizer.decode(ind)}"')
assert len(user_start_inds) == len(
assistant_start_inds
), f"User and Assistant pair should be equal :: \n\tUser [{user_start_inds}]/\n\tAssistant [{assistant_start_inds}]\n\n Text : \n{trajs[i]}"
assert len(result_start_inds) == len(
result_end_inds
), f"Start and End indices pairs do not match.: : \nText : \n{trajs[i]}"
for user_start_ind, assistant_start_ind in zip(
user_start_inds, assistant_start_inds
):
label[user_start_ind + 1 : assistant_start_ind - 1] = IGNORE_INDEX
for start, end in zip(result_start_inds, result_end_inds):
label[start + 1 : end - 1] = IGNORE_INDEX
# cut max length
input_ids = [i[:1500] for i in input_ids]
labels = [i[:1500] for i in labels]
return dict(input_ids=input_ids, labels=labels)
class SupervisedDataset(Dataset):
"""Dataset for supervised fine-tuning."""
def __init__(self, data_path: str, tokenizer: transformers.PreTrainedTokenizer):
super(SupervisedDataset, self).__init__()
logging.warning(f"Loading data from data path : {data_path}")
all_json = os.listdir(data_path)
trajs = list()
for json_file_name in all_json:
traj = json_to_code_result_tok_temp(json_file_name=json_file_name)
trajs.append(traj)
logging.warning("Tokenizing inputs... This may take some time...")
data_dict = preprocess(trajs, tokenizer)
self.input_ids = data_dict["input_ids"]
self.labels = data_dict["labels"]
def __len__(self):
return len(self.input_ids)
def __getitem__(self, i) -> Dict[str, torch.Tensor]:
return dict(input_ids=self.input_ids[i], labels=self.labels[i])
@dataclass
class DataCollatorForSupervisedDataset(object):
"""Collate examples for supervised fine-tuning."""
tokenizer: transformers.PreTrainedTokenizer
def __call__(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
input_ids, labels = tuple(
[instance[key] for instance in instances] for key in ("input_ids", "labels")
)
input_ids = torch.nn.utils.rnn.pad_sequence(
input_ids, batch_first=True, padding_value=self.tokenizer.pad_token_id
)
labels = torch.nn.utils.rnn.pad_sequence(
labels, batch_first=True, padding_value=IGNORE_INDEX
)
return dict(
input_ids=input_ids,
labels=labels,
attention_mask=input_ids.ne(self.tokenizer.pad_token_id),
)
def make_supervised_data_module(
tokenizer: transformers.PreTrainedTokenizer, data_args
) -> Dict:
"""Make dataset and collator for supervised fine-tuning."""
train_dataset = SupervisedDataset(
tokenizer=tokenizer, data_path=data_args.data_path
)
data_collator = DataCollatorForSupervisedDataset(tokenizer=tokenizer)
return dict(
train_dataset=train_dataset, eval_dataset=None, data_collator=data_collator
)
def build_model_from_hf_path(
hf_model_path: str = "./ckpt/llama-2-13b-chat", peft: bool = False
):
# build tokenizer
tokenizer = LlamaTokenizer.from_pretrained(
hf_model_path,
padding_side="right",
use_fast=False,
)
special_tokens_dict = dict()
if tokenizer.pad_token is None:
special_tokens_dict["pad_token"] = DEFAULT_PAD_TOKEN # 32000
if tokenizer.eos_token is None:
special_tokens_dict["eos_token"] = DEFAULT_EOS_TOKEN # 2
if tokenizer.bos_token is None:
special_tokens_dict["bos_token"] = DEFAULT_BOS_TOKEN # 1
if tokenizer.unk_token is None:
special_tokens_dict["unk_token"] = DEFAULT_UNK_TOKEN
tokenizer.add_special_tokens(special_tokens_dict)
tokenizer.add_tokens(
[
B_CODE, # 32001
E_CODE, # 32002
B_RESULT, # 32003
E_RESULT, # 32004
B_INST,
E_INST,
B_SYS,
E_SYS, # 32008
],
special_tokens=True,
)
# build model
if peft:
model = LlamaForCausalLM.from_pretrained(
hf_model_path,
load_in_8bit=True,
device_map="auto",
ignore_mismatched_sizes=True,
torch_dtype=torch.float16,
)
else:
# for llama
# model = LlamaForCausalLM.from_pretrained(
# hf_model_path, ignore_mismatched_sizes=True
# )
# for codellama
from codellama_wrapper import CodeLlamaForCausalLM
model = CodeLlamaForCausalLM.from_pretrained(hf_model_path)
model.resize_token_embeddings(len(tokenizer))
return {"tokenizer": tokenizer, "model": model}
def train():
parser = transformers.HfArgumentParser(
(ModelArguments, DataArguments, TrainingArguments)
)
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
model_dict = build_model_from_hf_path(
hf_model_path=model_args.model_name_or_path, peft=model_args.peft
)
model, tokenizer = model_dict["model"], model_dict["tokenizer"]
# peft setting
model.train()
if model_args.peft:
model, lora_config = create_peft_config(model)
# make dataset
data_module = make_supervised_data_module(tokenizer=tokenizer, data_args=data_args)
trainer = Trainer(
model=model, tokenizer=tokenizer, args=training_args, **data_module
)
# train
trainer.train()
trainer.save_state()
trainer.save_model(output_dir=training_args.output_dir)
if __name__ == "__main__":
train()
|