C.W.K.
Stream
Lesson 10 of 10 · published

Mixed Precision 과 Reproducibility

~18 min · amp, fp16, bf16

Level 0Curious
0 XP0/73 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Mixed precision 이 뭔가

Modern GPU 가 float16 (FP16) 또는 bfloat16 (BF16) operation 을 float32 (FP32) 보다 2-8x 빠르게 돌려. Mixed precision 은: forward pass 를 FP16/BF16 로 cast, FP32 의 master weight copy 유지, FP16 gradient 의 작은 dynamic range 처리에 GradScaler 사용. 결과 — 더 빠른 training, 더 적은 VRAM, 보통 accuracy hit 없음.

BF16 은 FP32 와 같은 exponent range (그래서 GradScaler 안 필요) 지만 lower precision. FP16 은 더 precise 한데 smaller range (underflow 피하려 GradScaler 필요). H100 / A100 에서는 둘 다 빠름, Apple Silicon 에서는 BF16 이 보통 올바른 pick.

팁: Modern accelerator 에서 train 하는데 mixed precision 안 쓰면, 좋은 이유 없이 시간과 VRAM 2x 내고 있어. torch.autocast + GradScaler 가 한 줄 upgrade.

표준 recipe

Forward 와 loss 를 torch.autocast(device_type="cuda", dtype=torch.bfloat16) 로 wrap. FP16 면 GradScaler 도: scaler.scale(loss).backward(), scaler.unscale_(opt), 다음 clip, 다음 scaler.step(opt), 다음 scaler.update().

Reproducibility 주의

Mixed precision 이 seed set 해도 작은 non-determinism 추가 — FP16/BF16 의 rounding pattern 이 operation order 에 의존. 정확한 final metric ± 0.1% 의 statistical reproducibility 계획, bitwise 아님. 정확한 determinism 필요한 application 은 FP32 에서 돌리고 비용 받아들여.

원칙: Mixed precision 이 modern hardware 에서 본질적으로 무료 accuracy + speed. 건너뛰는 valid 이유는: pre-Volta GPU (Tensor Core 없음), 또는 bitwise determinism 이 hard requirement 인 application.

Code

Mixed-precision training in 2026 PyTorch·python
import torch
from torch.cuda.amp import autocast, GradScaler

scaler = GradScaler()
opt = torch.optim.AdamW(model.parameters(), lr=3e-4)

for xb, yb in train_loader:
    xb, yb = xb.to(device), yb.to(device)
    opt.zero_grad()

    with autocast(dtype=torch.bfloat16):
        logits = model(xb)
        loss = loss_fn(logits, yb)

    # FP16 needs GradScaler; BF16 doesn't (but it's harmless to use)
    scaler.scale(loss).backward()
    scaler.unscale_(opt)
    torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
    scaler.step(opt)
    scaler.update()

External links

Exercise

같은 model 과 dataset 에 한 training epoch 를 FP32 와 BF16 로 timing. Wall-clock time, peak VRAM, final validation accuracy 비교. Speedup 은 1.5-3x 에 accuracy loss 없어야.

Progress

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

댓글 0

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

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