|
from datasets import Dataset |
|
import os |
|
import torch |
|
import torch.nn as nn |
|
import datasets |
|
from datasets import Dataset |
|
import bitsandbytes as bb |
|
from transformers import AutoTokenizer, LlamaForCausalLM, TrainingArguments, BitsAndBytesConfig |
|
from trl import SFTTrainer, DataCollatorForCompletionOnlyLM |
|
import json |
|
from peft import LoraConfig, get_peft_model |
|
|
|
os.environ["TOKENIZERS_PARALLELISM"] = "false" |
|
|
|
|
|
dataset = Dataset.from_list(json.load(open('formatted_training_examples.json', 'r'))) |
|
|
|
print(dataset,"\n\n\n") |
|
|
|
|
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("Gryphe/MythoLogic-Mini-7b", max_length=4000, padding_side="right") |
|
|
|
|
|
tokenizer.pad_token_id = tokenizer.eos_token_id |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dataset = dataset.map(lambda example: {"text": example["text"] + tokenizer.eos_token}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
dataset = dataset.map(lambda example: {"text": example["text"].replace("Shirogane", "{user}").replace("Takeru", "{user}").replace("Shirooogaaane","{user}").replace("Sh-Shirogane","{user}")}) |
|
|
|
dataset = dataset.train_test_split(test_size=0.05) |
|
|
|
print(dataset) |
|
|
|
print(dataset["train"][0]["text"]) |
|
|
|
|
|
print("Piggy") |
|
|
|
|
|
response_template = [2277, 29937, 13291, 29901] |
|
|
|
|
|
print("\n\n\n====================\n\n\n") |
|
print(type(response_template), response_template) |
|
print("\n\n\n====================\n\n\n") |
|
|
|
|
|
collator = DataCollatorForCompletionOnlyLM( |
|
|
|
response_template=response_template, |
|
tokenizer=tokenizer, |
|
mlm=False |
|
) |
|
|
|
quantization_config = BitsAndBytesConfig( |
|
load_in_4bit=True, |
|
bnb_4bit_use_double_quant=True, |
|
llm_int8_enable_fp32_cpu_offloat=True, |
|
bnb_4bit_compute_dtype=torch.float16, |
|
) |
|
|
|
base_model = LlamaForCausalLM.from_pretrained( |
|
"Gryphe/MythoLogic-Mini-7b", |
|
quantization_config=quantization_config, |
|
device_map="auto", |
|
trust_remote_code=True, |
|
) |
|
|
|
lora_config = LoraConfig( |
|
r=16, |
|
lora_alpha=32, |
|
target_modules=["q_proj","k_proj","v_proj","o_proj", |
|
|
|
], |
|
lora_dropout=0.05, |
|
bias="none", |
|
task_type="CAUSAL_LM", |
|
) |
|
|
|
model = get_peft_model(base_model, lora_config) |
|
model.print_trainable_parameters() |
|
|
|
model.enable_input_require_grads() |
|
|
|
training_args = TrainingArguments( |
|
per_device_eval_batch_size=1, |
|
gradient_accumulation_steps=16, |
|
gradient_checkpointing=True, |
|
learning_rate=1e-4, |
|
num_train_epochs=3, |
|
|
|
fp16=True, |
|
output_dir="outputs", |
|
) |
|
|
|
import transformers |
|
trainer = SFTTrainer( |
|
model=model, |
|
args=training_args, |
|
train_dataset=dataset["train"], |
|
eval_dataset=dataset["test"], |
|
tokenizer=tokenizer, |
|
|
|
data_collator=collator, |
|
max_seq_length=4000, |
|
dataset_text_field="text", |
|
) |
|
|
|
trainer.train() |
|
trainer.save_model("MythoChizuru-7b") |