191 lines
4.8 KiB
Python
191 lines
4.8 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
import torch
|
|
from datasets import load_dataset
|
|
from peft import (
|
|
LoraConfig,
|
|
get_peft_model,
|
|
prepare_model_for_kbit_training,
|
|
)
|
|
from transformers import (
|
|
AutoModelForCausalLM,
|
|
AutoTokenizer,
|
|
BitsAndBytesConfig,
|
|
)
|
|
from trl import SFTConfig, SFTTrainer
|
|
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
|
CONFIG_PATH = PROJECT_ROOT / "config" / "pipeline.json"
|
|
CHAT_TEMPLATE_PATH = PROJECT_ROOT / "config" / "chat_template.jinja"
|
|
|
|
|
|
def to_prompt_completion(example: dict) -> dict:
|
|
messages = example["messages"]
|
|
|
|
if len(messages) != 3:
|
|
raise ValueError(
|
|
f"Record {example.get('id')} does not contain three messages."
|
|
)
|
|
|
|
return {
|
|
"prompt": messages[:2],
|
|
"completion": [messages[2]],
|
|
}
|
|
|
|
|
|
def main() -> None:
|
|
config = json.loads(CONFIG_PATH.read_text(encoding="utf-8"))
|
|
model_config = config["model"]
|
|
training_config = config["training"]
|
|
pilot_config = training_config["pilot"]
|
|
lora_settings = training_config["lora"]
|
|
|
|
model_path = model_config["base_model"]
|
|
train_path = PROJECT_ROOT / config["paths"]["train"]
|
|
output_dir = PROJECT_ROOT / pilot_config["output_dir"]
|
|
|
|
if output_dir.exists():
|
|
shutil.rmtree(output_dir)
|
|
|
|
print("Loading pilot dataset...")
|
|
|
|
dataset = load_dataset(
|
|
"json",
|
|
data_files=str(train_path),
|
|
split="train",
|
|
)
|
|
|
|
record_count = min(
|
|
int(pilot_config["record_count"]),
|
|
len(dataset),
|
|
)
|
|
|
|
dataset = dataset.select(range(record_count))
|
|
original_columns = dataset.column_names
|
|
|
|
dataset = dataset.map(
|
|
to_prompt_completion,
|
|
remove_columns=original_columns,
|
|
desc="Preparing prompt/completion records",
|
|
)
|
|
|
|
print(f"Pilot records: {len(dataset)}")
|
|
print("Loading tokenizer...")
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(
|
|
model_path,
|
|
use_fast=True,
|
|
)
|
|
|
|
tokenizer.chat_template = CHAT_TEMPLATE_PATH.read_text(
|
|
encoding="utf-8"
|
|
)
|
|
|
|
if tokenizer.pad_token_id is None:
|
|
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
tokenizer.padding_side = "right"
|
|
|
|
quantization_config = BitsAndBytesConfig(
|
|
load_in_4bit=True,
|
|
bnb_4bit_quant_type="nf4",
|
|
bnb_4bit_use_double_quant=True,
|
|
bnb_4bit_compute_dtype=torch.bfloat16,
|
|
)
|
|
|
|
print("Loading base model in 4-bit NF4...")
|
|
|
|
model = AutoModelForCausalLM.from_pretrained(
|
|
model_path,
|
|
quantization_config=quantization_config,
|
|
dtype=torch.bfloat16,
|
|
device_map={"": 0},
|
|
low_cpu_mem_usage=True,
|
|
)
|
|
|
|
model.config.use_cache = False
|
|
|
|
model = prepare_model_for_kbit_training(
|
|
model,
|
|
use_gradient_checkpointing=True,
|
|
gradient_checkpointing_kwargs={
|
|
"use_reentrant": False,
|
|
},
|
|
)
|
|
|
|
lora_config = LoraConfig(
|
|
task_type="CAUSAL_LM",
|
|
r=int(lora_settings["rank"]),
|
|
lora_alpha=int(lora_settings["alpha"]),
|
|
lora_dropout=float(lora_settings["dropout"]),
|
|
target_modules=lora_settings["target_modules"],
|
|
bias="none",
|
|
)
|
|
|
|
model = get_peft_model(model, lora_config)
|
|
model.print_trainable_parameters()
|
|
|
|
training_args = SFTConfig(
|
|
output_dir=str(output_dir),
|
|
max_length=int(model_config["max_sequence_length"]),
|
|
max_steps=int(pilot_config["max_steps"]),
|
|
per_device_train_batch_size=int(
|
|
pilot_config["batch_size"]
|
|
),
|
|
gradient_accumulation_steps=int(
|
|
pilot_config["gradient_accumulation_steps"]
|
|
),
|
|
learning_rate=float(pilot_config["learning_rate"]),
|
|
warmup_ratio=0.1,
|
|
lr_scheduler_type="cosine",
|
|
logging_steps=1,
|
|
save_strategy="no",
|
|
eval_strategy="no",
|
|
bf16=True,
|
|
tf32=True,
|
|
gradient_checkpointing=True,
|
|
gradient_checkpointing_kwargs={
|
|
"use_reentrant": False,
|
|
},
|
|
optim="adamw_torch_fused",
|
|
completion_only_loss=True,
|
|
packing=False,
|
|
report_to="none",
|
|
seed=int(pilot_config["seed"]),
|
|
data_seed=int(pilot_config["seed"]),
|
|
)
|
|
|
|
trainer = SFTTrainer(
|
|
model=model,
|
|
args=training_args,
|
|
train_dataset=dataset,
|
|
processing_class=tokenizer,
|
|
)
|
|
|
|
torch.cuda.reset_peak_memory_stats()
|
|
|
|
print()
|
|
print("Starting pilot training...")
|
|
result = trainer.train()
|
|
|
|
trainer.save_model(str(output_dir))
|
|
tokenizer.save_pretrained(str(output_dir))
|
|
trainer.save_state()
|
|
|
|
peak_memory = torch.cuda.max_memory_allocated() / 1024**3
|
|
|
|
print()
|
|
print("Pilot training passed.")
|
|
print(f"Output: {output_dir}")
|
|
print(f"Training loss: {result.training_loss:.6f}")
|
|
print(f"Peak GPU memory: {peak_memory:.2f} GiB")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|