Download utils.py from starcrow/sft_qat: direct link, hf CLI and curl.
- Browser
- Download file 3.93 kB
-
https://huggingface.co/starcrow/sft_qat/resolve/main/utils.py
- Command line
-
hf download hf://starcrow/sft_qat/utils.py
-
curl -L -o utils.py https://huggingface.co/starcrow/sft_qat/resolve/main/utils.py
3.93 kB
| """ | |
| utils.py | |
| νλΌλ―Έν° μ μΆλ ₯, GPU λ©λͺ¨λ¦¬ μΆλ ₯ λ± κ³΅ν΅ μ νΈλ¦¬ν° λͺ¨μ. | |
| """ | |
| import os | |
| import torch | |
| from logger import get_logger | |
| log = get_logger(__name__) | |
| # ββββββββββββββββββββββββββββββββββββββββββββββ | |
| # λͺ¨λΈ νλΌλ―Έν° ν΅κ³ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββ | |
| def print_param_stats(model) -> None: | |
| """νμ΅ κ°λ₯ / μ 체 νλΌλ―Έν° μμ λΉμ¨μ μΆλ ₯νλ€.""" | |
| total = sum(p.numel() for p in model.parameters()) | |
| trainable = sum(p.numel() for p in model.parameters() if p.requires_grad) | |
| ratio = 100 * trainable / total if total > 0 else 0.0 | |
| log.info("=" * 55) | |
| log.info(f" Trainable parameters : {trainable:>15,}") | |
| log.info(f" Total parameters : {total:>15,}") | |
| log.info(f" Trainable ratio : {ratio:.4f} %") | |
| log.info("=" * 55) | |
| # ββββββββββββββββββββββββββββββββββββββββββββββ | |
| # GPU λ©λͺ¨λ¦¬ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββ | |
| def print_gpu_memory(tag: str = "") -> None: | |
| """νμ¬ νλ‘μΈμ€κ° μ μ μ€μΈ GPU VRAM μ¬μ©λμ μΆλ ₯νλ€.""" | |
| if not torch.cuda.is_available(): | |
| log.info("CUDA not available β skipping memory report.") | |
| return | |
| for i in range(torch.cuda.device_count()): | |
| allocated = torch.cuda.memory_allocated(i) / 1024 ** 3 | |
| reserved = torch.cuda.memory_reserved(i) / 1024 ** 3 | |
| total_mem = torch.cuda.get_device_properties(i).total_memory / 1024 ** 3 | |
| label = f"[{tag}] " if tag else "" | |
| log.info( | |
| f"{label}GPU {i} ({torch.cuda.get_device_name(i)}) | " | |
| f"Allocated {allocated:.2f} GB / Reserved {reserved:.2f} GB / " | |
| f"Total {total_mem:.2f} GB" | |
| ) | |
| # ββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 체ν¬ν¬μΈνΈ νμ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββ | |
| def find_last_checkpoint(checkpoint_dir: str) -> str | None: | |
| """ | |
| HuggingFace Trainer νμμ checkpoint-XXXX ν΄λ μ€ | |
| μ€ν λ²νΈκ° κ°μ₯ ν° κ²μ λ°ννλ€. μμΌλ©΄ None. | |
| """ | |
| import re | |
| from pathlib import Path | |
| ckpt_root = Path(checkpoint_dir) | |
| if not ckpt_root.exists(): | |
| return None | |
| pattern = re.compile(r"^checkpoint-(\d+)$") | |
| candidates = [] | |
| for d in ckpt_root.iterdir(): | |
| m = pattern.match(d.name) | |
| if m and d.is_dir(): | |
| candidates.append((int(m.group(1)), str(d))) | |
| if not candidates: | |
| return None | |
| candidates.sort(key=lambda x: x[0]) | |
| _, path = candidates[-1] | |
| return path | |
| # ββββββββββββββββββββββββββββββββββββββββββββββ | |
| # νκ²½ μ 보 | |
| # ββββββββββββββββββββββββββββββββββββββββββββββ | |
| def log_environment() -> None: | |
| """Python, torch, CUDA λ²μ κ³Ό GPU μ 보λ₯Ό λ‘κ·Έμ λ¨κΈ΄λ€.""" | |
| import sys | |
| log.info(f"Python : {sys.version}") | |
| log.info(f"PyTorch : {torch.__version__}") | |
| log.info(f"CUDA : {torch.version.cuda}") | |
| if torch.cuda.is_available(): | |
| for i in range(torch.cuda.device_count()): | |
| props = torch.cuda.get_device_properties(i) | |
| log.info(f" GPU {i} : {props.name} | {props.total_memory / 1024**3:.1f} GB") | |
| else: | |
| log.warning("CUDA device not found.") | |