GPT
특징
나는과 점심에
"를 보고나는, 점심에, 밥을
"를 보고나는, 점심에, 밥을, 먹었다
" 4가지의 모든 토큰에 대해 본다.open source
기반 LLM 모델.Multi-Modal
로 공개.GPT-2
까지는 Decoder Architecture만 가져와서 사용했지만, LlaMA는 이를 개선
함.Positional Encoding
을 통해 절대적인 위치에 대한 정보를 제공하지만, 맨 처음 단에 이를 적용하기 때문에 layer가 깊어질 수록 뒤로 전달되는 위치 정보는 얕아지는 문제가 잇음.위의 Transformer의 위치 정보에 대한 문제점을 해결하기 위해 Rotery Positional Embedding(RoPE)
를 사용함.
기하 벡터의 회전 변환
을 사용.
θ
의 누적량이 달라져 절대적 위치와 상대적 위치를 모두 알 수 있다.기존에 Input단계에서 적용을 Query와 Key단계에서 적용하는 것으로 변경.
Attention layer가 계산 될때마다 위치정보가 계속 추가됨.
실제 Llama 3 Code
def apply_rotary_emb(
xq: torch.Tensor,
xk: torch.Tensor,
freqs_cis: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor]:
xq_ = torch.view_as_complex(xq.float().reshape(*xq.shape[:-1], -1, 2))
xk_ = torch.view_as_complex(xk.float().reshape(*xk.shape[:-1], -1, 2))
freqs_cis = reshape_for_broadcast(freqs_cis, xq_)
xq_out = torch.view_as_real(xq_ * freqs_cis).flatten(3)
xk_out = torch.view_as_real(xk_ * freqs_cis).flatten(3)
return xq_out.type_as(xq), xk_out.type_as(xk)
RMS Normalization
class RMSNorm(torch.nn.Module):
def __init__(self, dim: int, eps: float = 1e-6):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def _norm(self, x):
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x):
output = self._norm(x.float()).type_as(x)
return output * self.weight
SwiGLU
Gelu
에서 발전된 방식으로 계산의 효율성을 보다 높여서 진행하는 방법.Grouped Query Attention(GQA)
Grouped Query Attention
Multi layer Perceptron(FFN), MLP:
비선형성
의 추가로 복잡한 패턴이나 데이터의 비선형적 특징을 잘 포착할 수 있어진다.Flash Attention
A100
만 하더라도 20mb로 매우 작은 크기를 가지고 있다.Flash Attention
이다.