
기본 컴포넌트:
Continual Learning 추가 요구사항:
총 예상 메모리: ~4,650 MiB (4.5 GB)
배치 처리용 여유 메모리: ~7,832 MiB (7.6 GB)
# 메모리 효율적 설정
batch_size: 8
gradient_accumulation_steps: 4 # 효과적 배치크기: 32
max_length: 2048
use_amp: true
use_gradient_checkpointing: true
target_gpu_util: 0.8
checkpoint_freq: 20
# 학습 설정
max_epochs: 3
learning_rate: 2e-4
weight_decay: 0.01
# Continual Learning 설정
ewc:
lambda: 5000.0
normalize: true
mer:
buffer_size: 5120
beta: 0.1
gamma: 0.1
nvidia-smi로 주기적으로 VRAM 사용량 확인| 구분 | 방법·라이브러리 | 핵심 기능 | 사용 예시 (CUDA 환경) |
|---|---|---|---|
| 1 | torch.cuda.set_per_process_memory_fraction (PyTorch ≥ 1.9) | GPU 당 ― 프로세스별 메모리 상한선 설정 (잔여 VRAM 예약) | python\nimport torch\n# GPU0 메모리의 70%만 사용\ntorch.cuda.set_per_process_memory_fraction(0.7, 0)\n |
| 2 | PYTORCH_CUDA_ALLOC_CONF 환경변수 | Caching Allocator 세부 조정·파편화 완화 → OOM 감소 | bash\nexport PYTORCH_CUDA_ALLOC_CONF=\"max_split_size_mb:128,garbage_collection_threshold:0.6\"\npython train.py\n |
| 3 | CUDAPluggableAllocator (PyTorch 2.1+) | 외부·사용자 정의 메모리 풀 교체 → 예: UVM / 실시간 스왑 allocator | python\nfrom torch.cuda.memory import CUDAPluggableAllocator, change_current_allocator\nalloc = CUDAPluggableAllocator(\"./myalloc.so\",\"my_malloc\",\"my_free\")\nchange_current_allocator(alloc)\n |
| 4 | Accelerate dispatch_model / max_memory ( Accelerate) | 모델 로드·추론 시 GPU 별 최대 VRAM (MiB) 한도 지정 | python\nfrom accelerate import init_empty_weights, load_checkpoint_and_dispatch\nmax_mem = {0: \"19000MiB\"}\nmodel = load_checkpoint_and_dispatch(cfg,max_memory=max_mem)\n |
| 5 | DeepSpeed ZeRO-Offload | 옵티마이저·그라디언트 CPU offload → VRAM 대폭 절감 | deepspeed --zero_stage 3 --offload_param --offload_optimizer ... |
| 6 | xFormers / Flash-Attention | 메모리 절약형 attention 커널 → sequence 모델 VRAM 40–60%↓ | from transformers import AutoModel; model = AutoModel.from_pretrained(..., attn_implementation=\"flash_attention_2\") |
| 7 | Gradient Checkpointing (HF Transformers gradient_checkpointing_enable) | 중간 activation 재계산 → VRAM ≈½ | python\nmodel.gradient_checkpointing_enable()\n |
| 8 | GPUtil (+ pynvml) 모니터링 콜백 | 실시간 VRAM 모니터링 → 임계치 도달 시 torch.cuda.empty_cache() · batch 축소 | python\nimport GPUtil, torch\nif GPUtil.getGPUs()[0].memoryUtil > .85:\n torch.cuda.empty_cache()\n |
set_per_process_memory_fraction 로 최소 15 ~ 20% 여유 VRAM 확보. PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:128 ㆍ 학습 초기에 설정. torch.cuda.empty_cache() 로 즉시 캐시 반환. max_memory 로 큰 체크포인트도 로드 가능. 위 기능들은 모두 CUDA 환경 전용이므로 별도 CPU fallback 없는 순수 GPU 메모리 통제에 적합하다. 필요시 1 → 4단계를 함께 적용하면 DynamicBatchSizeManager 없이도 OOM 발생 빈도를 크게 줄일 수 있다.