File size: 3,425 Bytes
9fc3b3a | 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | """
validate.py
νμ΅ μμ μ νκ²½Β·μ€μ Β·λ°μ΄ν°μ
μ μλμΌλ‘ μ κ²νλ€.
"""
import torch
from pathlib import Path
from datasets import load_from_disk
from config import CPTConfig
from logger import get_logger
log = get_logger(__name__)
def _check(condition: bool, msg: str) -> None:
if not condition:
log.error(f"[VALIDATE FAIL] {msg}")
raise ValueError(msg)
def validate_all(cfg: CPTConfig) -> None:
log.info("ββ Validation start ββββββββββββββββββββββββββββββ")
_check_gpu(cfg)
_check_paths(cfg)
_check_dataset(cfg)
_check_hyperparams(cfg)
log.info("ββ Validation passed βββββββββββββββββββββββββββββ")
def _check_gpu(cfg: CPTConfig) -> None:
has_cuda = torch.cuda.is_available()
_check(has_cuda, "CUDA GPUλ₯Ό μ°Ύμ μ μμ΅λλ€.")
total_vram = 0
for i in range(torch.cuda.device_count()):
props = torch.cuda.get_device_properties(i)
total_vram += props.total_memory / 1024 ** 3
# bf16 λͺ¨λμμ 27B λͺ¨λΈ μ΅μ VRAM κ²½κ³ (μλ¬λ μλ)
if total_vram < 60:
log.warning(
f"μ΄ VRAM {total_vram:.1f}GB β bf16 + LoRA 27B νμ΅μ λΆμ‘±ν μ μμ΅λλ€."
)
log.info(f"[OK] GPU : {torch.cuda.device_count()}κ° | μ΄ VRAM {total_vram:.1f}GB")
def _check_paths(cfg: CPTConfig) -> None:
_check(
Path(cfg.paths.base_model).exists(),
f"base_model κ²½λ‘κ° μμ΅λλ€: {cfg.paths.base_model}"
)
_check(
Path(cfg.paths.dataset_dir).exists(),
f"dataset_dir κ²½λ‘κ° μμ΅λλ€: {cfg.paths.dataset_dir}"
)
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("[OK] Paths: λͺ¨λ κ²½λ‘ νμΈ μλ£")
def _check_dataset(cfg: CPTConfig) -> None:
try:
ds = load_from_disk(cfg.paths.dataset_dir)
except Exception as e:
raise ValueError(f"λ°μ΄ν°μ
λ‘λ© μ€ν¨: {e}")
required_cols = {"input_ids", "sources", "categories", "sections"}
missing = required_cols - set(ds.column_names)
_check(not missing, f"λ°μ΄ν°μ
μ νμ 컬λΌμ΄ μμ΅λλ€: {missing}")
_check(len(ds) > 0, "λ°μ΄ν°μ
μ΄ λΉμ΄ μμ΅λλ€.")
lengths = [len(x) for x in ds["input_ids"]]
max_len = max(lengths)
avg_len = sum(lengths) / len(lengths)
log.info(
f"[OK] Dataset: {len(ds):,} chunks | "
f"avg_len={avg_len:.0f} | max_len={max_len}"
)
def _check_hyperparams(cfg: CPTConfig) -> None:
t = cfg.training
_check(t.learning_rate > 0, "learning_rate λ μμμ¬μΌ ν©λλ€.")
_check(t.per_device_train_batch_size >= 1, "batch_size λ 1 μ΄μμ΄μ΄μΌ ν©λλ€.")
_check(t.gradient_accumulation_steps >= 1, "gradient_accumulation_steps λ 1 μ΄μμ΄μ΄μΌ ν©λλ€.")
_check(0.0 <= t.warmup_ratio < 1.0, "warmup_ratio λ [0, 1) λ²μμ¬μΌ ν©λλ€.")
_check(not (t.bf16 and t.fp16), "bf16 κ³Ό fp16 μ λμμ True λ‘ μ€μ ν μ μμ΅λλ€.")
eff_batch = t.per_device_train_batch_size * t.gradient_accumulation_steps
log.info(
f"[OK] HParams: lr={t.learning_rate} | "
f"effective_batch={eff_batch} | "
f"epochs={t.num_train_epochs}"
)
|