본문 바로가기
C.W.K.
Stream
Lesson 05 of 08 · published

Accelerate: 멀티 디바이스, Mixed Precision, FSDP

~22 min · ops, accelerate

Level 0스카우트
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Accelerate는 학습 코드와 실행 환경 사이에 있어

accelerate는 장치 배치, 혼합 정밀도, gradient accumulation, DDP·FSDP·DeepSpeed 분산 실행, checkpoint 재개를 맡아. Trainer가 내부에서 사용하지만 맞춤 학습 루프에서도 직접 쓸 수 있어.

  • Trainer를 쓴다면 TrainingArgumentsfsdpdeepspeed를 설정해 연결해.
  • 직접 쓴다면 Accelerator()를 만들고 prepare(model, optimizer, dataloader)로 구성 요소를 감싼 뒤 역전파와 갱신을 실행해.

큰 모델의 출발점은 FSDP야

PyTorch에 내장된 FSDP는 통합과 설정이 단순해 대부분의 미세조정과 중간 규모 사전학습에서 먼저 검토할 선택이야. DeepSpeed는 특정 고급 워크로드에서 강하지만 별도 런타임 의존성을 더하므로 실제 병목이 있을 때 비교해.

Code

accelerate config 생성 + 실행·bash
# Interactive — 답 ~/.cache/huggingface/accelerate/default_config.yaml 에
accelerate config

# config 로 스크립트 launch
accelerate launch train.py
accelerate 와 커스텀 학습 루프·python
from accelerate import Accelerator
from torch.utils.data import DataLoader
from transformers import AutoModelForCausalLM, AutoTokenizer, get_cosine_schedule_with_warmup
import torch

acc = Accelerator(mixed_precision="bf16")  # bf16 / fp16 / no
device = acc.device

model = AutoModelForCausalLM.from_pretrained("...")
tok = AutoTokenizer.from_pretrained("...")
optim = torch.optim.AdamW(model.parameters(), lr=1e-4)

# loader = DataLoader(...)
# scheduler = get_cosine_schedule_with_warmup(optim, num_warmup_steps=100, num_training_steps=10_000)

model, optim, loader, scheduler = acc.prepare(model, optim, loader, scheduler)

for step, batch in enumerate(loader):
    out = model(**batch)
    acc.backward(out.loss)
    optim.step(); scheduler.step(); optim.zero_grad()
    if step % 50 == 0 and acc.is_main_process:
        print(step, out.loss.item())

External links

Exercise

accelerate config interactively 실행. 단일 GPU 에서 IMDB Trainer 셋업을 accelerate launch 로 학습. multi-GPU 접근 있으면 FSDP 로 스위치, 같은 레시피가 더 높은 effective batch size + 비슷한 loss 곡선에 동작 검증.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.