"""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)