[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-10 22:08:15 +09:00
parent 21ac3ed255
commit 504fd5fb42
3011 changed files with 380280 additions and 206977 deletions
+224 -39
View File
@@ -2,62 +2,247 @@
id: wiki-2026-0508-pytorch-lightning
title: PyTorch Lightning
category: 10_Wiki/Topics
status: needs_review
status: verified
canonical_id: self
aliases: [DL-PY-LIGHT-001]
aliases: [Lightning, pl, lightning.pytorch]
duplicate_of: none
source_trust_level: A
confidence_score: 1.0
tags: [ai, Deep-Learning, pytorch, pytorch-lightning, Scalability, boilerplate-reduction, MLOps]
confidence_score: 0.9
verification_status: applied
tags: [pytorch, training, framework, distributed]
raw_sources: []
last_reinforced: 2026-04-26
last_reinforced: 2026-05-10
github_commit: pending
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
tech_stack:
language: python
framework: pytorch-lightning
---
# PyTorch Lightning (PyTorch 라이트닝)
# PyTorch Lightning
## 📌 한 줄 통찰 (The Karpathy Summary)
> "반복되는 엔지니어링의 노이즈를 걷어내고, 오직 지능의 '핵심 로직([[Research|Research]])'에만 집중할 수 있는 표준화된 고속도로를 구축하라" — PyTorch의 유연성을 유지하면서 학습 루프, 하드웨어 설정 등 반복적인 코드를 자동화하여 생산성과 가독성을 극대화하는 경량 래퍼(Wrapper) 프레임워크.
## 한 줄
> **"매 PyTorch boilerplate 의 elimination — research-style structured trainer"**. LightningModule (model + optim + step) + Trainer (loop + distributed + logging) 의 separation. 2026 현재 매 still strong for research / classical DL, 매 LLM-era 의 HuggingFace Trainer / Accelerate / TRL 의 dominate.
## 📖 구조화된 지식 (Synthesized Content)
- **추출된 패턴:** "[[_뇌와 팔다리의 분리_ - 관심사의 분리 (Separation of Concerns)|Separation of Concerns]] and Standardized Training Interface" — 모델의 구조(Model), 데이터 처리(Data), 학습 환경(Trainer)을 명확히 분리하여, 코드 한 줄 변경만으로 CPU에서 멀티 GPU나 TPU로 학습 환경을 즉시 전환할 수 있게 만드는 패턴.
- **핵심 구성 요소:**
- **LightningModule:** 모델 구조, 옵티마이저, 학습/검증 단계를 하나로 캡슐화.
- **Trainer:** 학습 루프 제어, 체크포인트 저장, 로그 관리 자동화.
- **DataModule:** 데이터셋 로드 및 전처리 로직의 재사용성 확보.
- **의의:** 복잡한 딥러닝 실험의 재현성(Reproducibility)을 높이고, 팀 단위 협업 시 코드의 일관성을 유지하며, MLOps로의 전환을 용이하게 함.
## 매 핵심
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
- **과거 데이터와의 충돌:** 프레임워크가 무거워지면 제어권이 사라질 것이라는 우려를 '훅(Hook)' 기반의 유연한 오버라이딩 설계로 극복하며, 이제는 대규모 언어 모델 학습과 엔터프라이즈급 AI 개발의 필수 도구로 자리 잡음.
- **정책 변화:** Antigravity 프로젝트는 대규모 모델의 분산 학습 및 성능 벤치마킹 시, 코드 유지보수 효율을 위해 PyTorch Lightning 기반의 프로젝트 구조를 권장함.
### 매 LightningModule lifecycle
- `__init__`: model + hparams.
- `forward(x)`: inference.
- `training_step(batch, idx) -> loss`: per-batch train.
- `validation_step` / `test_step`: eval.
- `configure_optimizers() -> optim | (optim, sched)`: opt + scheduler.
- `on_*_epoch_end` hooks for aggregation.
## 🔗 지식 연결 (Graph)
- [[PyTorch-Foundations|PyTorch-Foundations]], Deep-Learning-Foundations,[[_system|system]]-Design-for-AI-Scale, GPU-[[Optimization|Optimization]]-Foundations
- **Raw Source:** 10_Wiki/Topics/AI/PyTorch-Lightning.md
### 매 Trainer features
- Multi-GPU (DDP, FSDP), TPU, MPS automatic.
- Mixed precision (`precision="bf16-mixed"`).
- Gradient accumulation, clipping built-in.
- Callbacks (EarlyStopping, ModelCheckpoint, LR monitor).
- Loggers (TensorBoard, WandB, MLflow, CSV).
- `fast_dev_run`, `overfit_batches`, `limit_*_batches` for debug.
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
### 매 vs alternatives (2026)
| Framework | Best for |
|---|---|
| Lightning | research, classical CV/NLP, structured projects |
| HF Trainer | HF-ecosystem (transformers + datasets), LLM SFT |
| HF Accelerate | minimal wrapper, retain raw PyTorch loop |
| TRL | RLHF / DPO / GRPO, LLM post-training |
| MosaicML Composer | streaming, throughput-optimized |
| raw PyTorch | full control, simple scripts |
**언제 이 지식을 쓰는가:**
- *(TODO)*
### 매 응용
1. CV training (image classification, segmentation, detection).
2. Tabular DL (TabNet, FT-Transformer).
3. Audio / speech (W2V2 finetune).
4. Mid-size LLM finetune (when not using HF Trainer).
5. Self-supervised pretraining (SimCLR, MAE).
**언제 쓰면 안 되는가:**
- *(TODO)*
## 💻 패턴
## 🧪 검증 상태 (Validation)
### Minimal LightningModule
```python
import lightning as L
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
- **정보 상태:** needs_review
- **출처 신뢰도:** A
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
class LitClassifier(L.LightningModule):
def __init__(self, lr=1e-3):
super().__init__()
self.save_hyperparameters()
self.net = nn.Sequential(
nn.Flatten(), nn.Linear(28*28, 256), nn.ReLU(), nn.Linear(256, 10),
)
self.loss = nn.CrossEntropyLoss()
## 🧬 중복 검사 (Duplicate Check)
def forward(self, x):
return self.net(x)
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
- **처리 방식:** UPDATE (자동 정규화)
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
def training_step(self, batch, idx):
x, y = batch
logits = self(x)
loss = self.loss(logits, y)
self.log("train_loss", loss, prog_bar=True)
return loss
## 🕓 변경 이력 (Changelog)
def validation_step(self, batch, idx):
x, y = batch
logits = self(x)
acc = (logits.argmax(-1) == y).float().mean()
self.log("val_acc", acc, prog_bar=True)
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|------|-----------|-----------|--------|
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
def configure_optimizers(self):
opt = torch.optim.AdamW(self.parameters(), lr=self.hparams.lr)
sched = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=10)
return [opt], [sched]
```
### Trainer with callbacks
```python
from lightning.pytorch.callbacks import EarlyStopping, ModelCheckpoint, LearningRateMonitor
from lightning.pytorch.loggers import WandbLogger
trainer = L.Trainer(
max_epochs=20,
accelerator="auto", # cuda / mps / cpu
devices="auto",
precision="bf16-mixed",
accumulate_grad_batches=4,
gradient_clip_val=1.0,
callbacks=[
EarlyStopping(monitor="val_acc", mode="max", patience=3),
ModelCheckpoint(monitor="val_acc", mode="max", save_top_k=2),
LearningRateMonitor(),
],
logger=WandbLogger(project="lit-mnist"),
)
trainer.fit(LitClassifier(), train_dl, val_dl)
```
### Multi-GPU DDP
```python
trainer = L.Trainer(
accelerator="gpu",
devices=4,
strategy="ddp", # or "fsdp" for >7B params
precision="bf16-mixed",
sync_batchnorm=True,
)
# 매 launch with `python train.py` — Lightning 의 spawn workers
```
### FSDP for large model
```python
from lightning.pytorch.strategies import FSDPStrategy
from torch.distributed.fsdp.wrap import transformer_auto_wrap_policy
from functools import partial
policy = partial(transformer_auto_wrap_policy, transformer_layer_cls={MyTransformerBlock})
trainer = L.Trainer(
devices=8,
strategy=FSDPStrategy(auto_wrap_policy=policy, cpu_offload=False),
precision="bf16-mixed",
)
```
### LightningDataModule
```python
class MNISTDataModule(L.LightningDataModule):
def __init__(self, batch_size=64):
super().__init__()
self.bs = batch_size
def prepare_data(self):
from torchvision.datasets import MNIST
MNIST(".", train=True, download=True)
def setup(self, stage=None):
from torchvision.datasets import MNIST
from torchvision import transforms
t = transforms.ToTensor()
self.train = MNIST(".", train=True, transform=t)
self.val = MNIST(".", train=False, transform=t)
def train_dataloader(self):
return DataLoader(self.train, batch_size=self.bs, num_workers=4, shuffle=True)
def val_dataloader(self):
return DataLoader(self.val, batch_size=self.bs, num_workers=4)
```
### LightningCLI (config-driven)
```python
# train.py
from lightning.pytorch.cli import LightningCLI
def main():
LightningCLI(LitClassifier, MNISTDataModule)
if __name__ == "__main__":
main()
# python train.py fit --config config.yaml --trainer.max_epochs=30
```
### Resume from checkpoint
```python
trainer.fit(model, datamodule, ckpt_path="lightning_logs/version_3/checkpoints/last.ckpt")
# or load model standalone
model = LitClassifier.load_from_checkpoint("path.ckpt")
```
### Manual optimization (GAN, RL)
```python
class LitGAN(L.LightningModule):
def __init__(self):
super().__init__()
self.automatic_optimization = False
...
def training_step(self, batch, idx):
opt_g, opt_d = self.optimizers()
# discriminator step
opt_d.zero_grad(); d_loss = ...; self.manual_backward(d_loss); opt_d.step()
# generator step
opt_g.zero_grad(); g_loss = ...; self.manual_backward(g_loss); opt_g.step()
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Research, multi-experiment, structured | Lightning |
| HF transformers SFT | HF Trainer (closer to ecosystem) |
| Custom training loop, retain control | Accelerate |
| LLM RLHF / DPO / GRPO | TRL |
| Single-GPU script <100 lines | raw PyTorch |
| Need callbacks + DDP fast | Lightning |
**기본값**: 매 research / non-HF training 의 Lightning + bf16-mixed + DDP. 매 HF transformers job 의 HF Trainer. 매 LLM post-training 의 TRL.
## 🔗 Graph
- 부모: [[PyTorch]] · [[Deep-Learning-Frameworks]]
- 변형: [[Lightning-Fabric]] (lower-level)
- 응용: [[Distributed-Training]] · [[Mixed-Precision]]
- Adjacent: [[HuggingFace-Trainer]] · [[Accelerate]] · [[TRL]]
## 🤖 LLM 활용
**언제**: scaffold LightningModule from arch description, generate callback config, debug DDP issues.
**언제 X**: deep performance tuning (FSDP wrap policy, custom strategy) — 매 verify with profiler, 매 LLM 의 outdated API common.
## ❌ 안티패턴
- **`.cuda()` inside LightningModule**: Lightning manages device — use `self.device` or just rely on Trainer.
- **Manual DDP setup**: Lightning handles, don't double-wrap.
- **Logging in DDP without `sync_dist=True`**: rank-0 only logs, miss aggregation.
- **`automatic_optimization=True` for GAN**: silent wrong loss flow — manual mode.
- **Pinning to old Lightning 1.x**: 매 2.x API change (lightning.pytorch namespace), 매 2026 의 2.x+ standard.
## 🧪 검증 / 중복
- Verified (lightning.ai docs 2026, Lightning 2.x release notes, Falcon 2019 origin paper, Lightning Studios).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — LightningModule + Trainer + DDP/FSDP patterns, 2026 alt comparison |