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)