출처: 퍼플렉시티
7B 규모 LLM을 8GB 이하의 GPU 메모리에서 학습하기 위해 네트워크 구조 변경 없이 메모리 효율성을 극대화하는 방법은 다음과 같습니다. 이 접근법은 주로 메모리 사용량 감소와 계산 효율성 향상에 초점을 맞추며, 추론 성능을 유지합니다.
핵심 전략 및 방법론
1. 양자화 (Quantization)
- 8/4비트 학습: 옵티마이저 상태와 모델 가중치를 8비트 또는 4비트로 저장하여 메모리 사용량을 최대 82.5% 감소시킵니다27. bitsandbytes 라이브러리를 활용해 FP16/BF16 혼합 정밀도와 결합하면 성능 저하 없이 적용 가능합니다3.
- GaLore (Gradient Low-Rank Projection): 옵티마이저 상태의 메모리 부담을 줄이기 위해 그래디언트 행렬의 저차원 구조를 활용합니다. Adam 옵티마이저의 메모리 요구량을 80% 이상 절감하며, 단일 GPU(예: RTX 4090 24GB)에서도 7B 모델 학습이 가능합니다2.
2. 메모리 최적화 기법
- Gradient Checkpointing: 순전파 시 중간 활성값을 저장하지 않고 역전파 단계에서 재계산하여 VRAM 사용량을 60~70% 절약합니다36.
- Activation Offloading: 활성화 텐서를 CPU 메모리 또는 NVMe 스토리지로 오프로드합니다. 특히 MEMO 프레임워크는 토큰 단위 활성화 재계산과 CPU-GPU 간 스와핑을 통해 100만 토큰 시퀀스 학습을 8개 GPU에서 가능하게 합니다8.
- FlashAttention: 메모리 효율적인 어텐션 알고리즘으로 VRAM 사용량을 최적화하며, 기존 대비 2.2배 빠른 학습 속도를 달성합니다49.
3. 학습 파라미터 최적화
- Gradient Accumulation: 작은 배치 크기를 사용하고 그래디언트를 누적하여 유효 배치 크기를 유지합니다. 8GB GPU에서는 배치 크기 1~2로 설정 후 8~16단계 누적이 일반적입니다35.
- 8비트 옵티마이저: AdamW 대신 8비트로 압축된 Adafactor 또는 LAMB 옵티마이저를 사용해 메모리 사용량을 추가로 줄입니다37.
실행 단계 및 도구
- 라이브러리 선택: DeepSpeed(ZeRO-2/3), Hugging Face Accelerate, bitsandbytes를 조합해 메모리 최적화를 자동화합니다39.
- 모델 초기화: 4비트 양자화된 Llama-7B를 로드하고, GaLore를 적용해 옵티마이저 상태를 저차원으로 프로젝션합니다27.
- 학습 설정:
-
python# 예시: 8비트 Adam + Gradient Checkpointing model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b", load_in_4bit=True) model.gradient_checkpointing_enable() optimizer = bnb.optim.Adam8bit(model.parameters(), lr=1e-5)
- 실행: FlashAttention-2를 활성화하고, train_batch_size=1, gradient_accumulation_steps=8로 설정해 메모리 한계 내에서 실행합니다45.
성능 및 한계
- MFU (Model FLOPS Utilization): MEMO 프레임워크는 기존 대비 2.4배 높은 52.3% MFU를 달성하며, 8GB GPU에서도 실용적인 학습 속도를 보장합니다8.
- 트레이드오프: 양자화와 재계산으로 인해 20~30%의 추가 학습 시간이 발생하지만, 하드웨어 제약을 극복하는 데 필수적입니다16.
이 방법론들은 네트워크 구조 변경 없이도 VRAM 8GB 환경에서 7B 모델의 전체 파라미터 학습(full-parameter training)을 가능하게 하며, 추론 시 양자화된 가중치를 원본 정밀도로 복원해 성능 저하를 방지합니다24.
Citations:
- https://www.reddit.com/r/LocalLLaMA/comments/19f9z64/running_a_local_model_with_8gb_vram_is_it_even/
- https://www.linkedin.com/posts/anima-anandkumar_paper-page-galore-memory-efficient-llm-activity-7171555545848504320-WQAm
- https://huggingface.co/docs/transformers/en/perf_train_gpu_one
- https://huggingface.co/docs/transformers/main/llm_tutorial_optimization
- https://www.nitorinfotech.com/blog/training-large-language-models-llms-techniques-and-best-practices/
- https://github.com/InternLM/Awesome-LLM-Training-System/blob/main/Memory_Optimizations.md
- https://stackoverflow.com/questions/77531993/run-llm-on-single-gpu-with-8gb-ram
- https://openreview.net/forum?id=NIzbVvB3Ox
- https://deepsense.ai/blog/how-to-train-a-large-language-model-using-limited-hardware/
- https://milvus.io/ai-quick-reference/what-innovations-are-improving-llm-efficiency
- https://www.computer.org/publications/tech-news/trends/training-techniques-large-language-models/
- https://www.kisacoresearch.com/content/memory-optimizations-large-language-models-training-inference
- https://magazine.sebastianraschka.com/p/practical-tips-for-finetuning-llms
- https://www.hyperstack.cloud/technical-resources/tutorials/llm-inference-techniques-to-reduce-latency-and-boost-performance
- https://openreview.net/forum?id=s7DkcgpRxL
- https://www.youtube.com/watch?v=mbmaEDL2wms
- https://openreview.net/forum?id=e0bTcdF29g
- https://milvus.io/ai-quick-reference/how-are-llms-optimized-for-memory-usage
- https://www.linkedin.com/pulse/mastering-llm-inference-cost-efficiency-performance-victor-qfs6e
- https://discuss.huggingface.co/t/llama-7b-gpu-memory-requirement/34323
- https://openreview.net/forum?id=rWzECppYr4
Perplexity로부터의 답변: pplx.ai/share
'Computer Vision' 카테고리의 다른 글
TorchScript, ONNX (0) | 2025.03.12 |
---|---|
라벨 맵을 입력으로 받아 고품질 이미지를 생성하는 모델 (0) | 2025.03.04 |