cpt_aqt / validate.py
starcrow's picture
Upload folder using huggingface_hub
9fc3b3a verified
Raw History Blame Contribute Delete
3.43 kB
"""
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}"
)