C.W.K.
Stream
Lesson 07 of 12 · published

파라미터 수 계산기 — 모델 카드 읽기

~10 min · params, model-card

Level 0Token
0 XP0/94 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

모던 Transformer의 파라미터 수를 (vocab, d_model, n_layers, d_ff, n_q_heads, n_kv_heads) 튜플에서 머릿속으로, 최악으로 냅킨 위에서 추정할 수 있어야 해. 공식은 단순:

N ≈ vocab · d_model + n_layers · ((n_q + 2 n_kv) · d_model · d_head + d_model² + 3 · d_model · d_ff + 2 · d_model)

block당 분해:

  • Q projection: n_q_heads · d_model · d_head — n_q_heads = d_model / d_head이면 d_model²과 동일.
  • K, V projection: 각각 n_kv_heads · d_model · d_head. GQA면 Q보다 훨씬 작음.
  • Output projection: d_model² (head 섞음).
  • FFN (SwiGLU): 3 · d_model · d_ff.
  • RMSNorm × 2: 2 · d_model (무시 가능).

지배적 항은 FFN(block당 파라미터의 약 70%)과 embedding(큰 모델에선 비율적으로 작아지는 일회성 비용).

Code

Parameter calculator·python
def count_params(vocab, d_model, n_layers, d_ff,
                 n_q_heads, n_kv_heads, weight_tied=True):
    d_head = d_model // n_q_heads
    embed = vocab * d_model
    per_layer = (
        # Q, K, V, O projections (GQA-aware)
        (n_q_heads + 2 * n_kv_heads) * d_model * d_head
        + d_model * d_model
        # SwiGLU FFN
        + 3 * d_model * d_ff
        # 2 RMSNorms
        + 2 * d_model
    )
    final_norm = d_model
    lm_head = 0 if weight_tied else vocab * d_model
    return embed + n_layers * per_layer + final_norm + lm_head

# Llama 3 8B: 128K vocab, 4096, 32 layers, 14336 ffn, 32 Q / 8 KV heads
print(count_params(128_000, 4096, 32, 14336, 32, 8) / 1e9, "B")
# ~8.0 B

External links

Exercise

{GPT-2 small, Llama 3 8B, Mistral 7B, Mixtral 8x22B, Qwen 2.5 32B} 각 모델에 대해 config 가져와서 공식으로 파라미터 수 예측, 발표된 숫자와 비교. 어디서 잘못 추정하나? (흔한 실수: K랑 V 둘 다 포함 잊거나, GQA 없는데 있다고 가정.)

Progress

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

댓글 0

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

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