
GPT 류 자기회귀(autoregressive) 모델이 학습 중 "미래 토큰을 보면 안 된다" 는 제약을 걸기 위해 쓰는 (L, L) 삼각 마스크.
점수 행렬 에서 는 position 가 position 를 참조하는 점수. causal 에서는 (미래) 를 가려야 함 → 해당 위치를 마스킹 (softmax-v1 전에 -inf 대입 → 확률 0).
두 함수를 완성하세요.
causal_mask(L) — shape (L, L) bool 배열, 하삼각(대각 포함) True.apply_mask(scores, mask, neg=-1e9) — mask == False 위치를 neg 로 채움.softmax-v1 에 넣기 전에 apply_mask(scores, causal_mask(L)) 을 통과시키면 미래 attention weight 가 0 이 됨.
| # | 이름 | 검증 |
|---|---|---|
| 1 | shape (L, L) + bool | |
| 2 | 하삼각 True, 상삼각 False | |
| 3 | 대각은 True | self-attention 허용 |
| 4 | apply_mask 값 대입 | 미래 위치 = neg |
| 5 | softmax-v1 후 미래 확률 0 | 합 1, 미래 ≈ 0 |
코드를 작성하고 Run 을 눌러보세요.