File size: 2,245 Bytes
458260e | 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 | """SFT ํ์ต ์์ ์ ํ๊ฒฝ, JSONL, ํ์ดํผํ๋ผ๋ฏธํฐ ๊ฒ์ฆ."""
import json
from pathlib import Path
import torch
from config import SFTConfig
from logger import get_logger
log = get_logger(__name__)
def _check(condition: bool, message: str) -> None:
if not condition:
raise ValueError(message)
def validate_all(cfg: SFTConfig) -> None:
_check(torch.cuda.is_available(), "CUDA GPU๊ฐ ํ์ํฉ๋๋ค.")
_check(Path(cfg.paths.base_model).exists(), f"base_model ๊ฒฝ๋ก๊ฐ ์์ต๋๋ค: {cfg.paths.base_model}")
dataset_path = Path(cfg.paths.dataset_path)
_check(dataset_path.is_file(), f"JSONL ํ์ผ์ด ์์ต๋๋ค: {dataset_path}")
_check(dataset_path.suffix.lower() == ".jsonl", f"dataset_path๋ .jsonl ํ์ผ์ด์ด์ผ ํฉ๋๋ค: {dataset_path}")
_check(cfg.data.format in {"auto", "messages", "instruction", "prompt_completion"}, "data.format is invalid")
_check(cfg.training.learning_rate > 0, "learning_rate๋ 0๋ณด๋ค ์ปค์ผ ํฉ๋๋ค.")
_check(cfg.training.max_seq_length > 1, "max_seq_length๋ 1๋ณด๋ค ์ปค์ผ ํฉ๋๋ค.")
_check(cfg.training.per_device_train_batch_size >= 1, "batch_size๋ 1 ์ด์์ด์ด์ผ ํฉ๋๋ค.")
_check(cfg.training.gradient_accumulation_steps >= 1, "gradient_accumulation_steps๋ 1 ์ด์์ด์ด์ผ ํฉ๋๋ค.")
_check(not (cfg.training.bf16 and cfg.training.fp16), "bf16๊ณผ fp16์ ๋์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.")
count = 0
with dataset_path.open("r", encoding="utf-8") as f:
for line_number, line in enumerate(f, start=1):
if not line.strip():
continue
try:
item = json.loads(line)
except json.JSONDecodeError as exc:
raise ValueError(f"line {line_number}: JSON ํ์ ์ค๋ฅ: {exc.msg}") from exc
_check(isinstance(item, dict), f"line {line_number}: JSON ๊ฐ์ฒด์ฌ์ผ ํฉ๋๋ค.")
count += 1
_check(count > 0, "JSONL ํ์ผ์ ๋น์ด ์์ง ์์ ํ์ด ์์ต๋๋ค.")
Path(cfg.paths.output_dir).mkdir(parents=True, exist_ok=True)
Path(cfg.paths.log_file).parent.mkdir(parents=True, exist_ok=True)
log.info("Validation passed | jsonl_rows=%s | format=%s", count, cfg.data.format)
|