Download validate.py from starcrow/cpt_aqt: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/starcrow/cpt_aqt/resolve/main/validate.py
- Command line
-
hf download hf://starcrow/cpt_aqt/validate.py
-
curl -L -o validate.py https://huggingface.co/starcrow/cpt_aqt/resolve/main/validate.py
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}" | |
| ) | |