---
title: "Discussion on \"LLaMA: Open and Efficient Foundation Language Models 논문 리뷰\" | Hashnode"
description: "Discussion on \"LLaMA: Open and Efficient Foundation Language Models 논문 리뷰\". LLaMA는 현대적인 오픈소스 LLM의 중요한 출발점으로서, 2023년 2월 Meta AI가 출시했습니다. 이 모델은 효율적인 트랜스포머 아키텍처 설계를 확립하여 후속 오픈소스 LLM들이 따르는 기술적 표준을 제시했습니다."
url: "https://hashnode.com/posts/llama-open-and-efficient-foundation-language-models/67d994dd734a9902c7d34dfb"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [LLaMA: Open and Efficient Foundation Language Models 논문 리뷰](https://bits-bytes-nn.hashnode.dev/llama-open-and-efficient-foundation-language-models?utm_source=hashnode&utm_medium=feed)

LLaMA는 현대적인 오픈소스 LLM의 중요한 출발점으로서, 2023년 2월 Meta AI가 출시했습니다. 이 모델은 효율적인 트랜스포머 아키텍처 설계를 확립하여 후속 오픈소스 LLM들이 따르는 기술적 표준을 제시했습니다.

Feb 26, 2023 · 18 min read · bits-bytes-nn.hashnode.dev  
Updated Apr 19, 2025  
Read the article

-   [#sft](https://hashnode.com/tag/sft)
-   [#swiglu](https://hashnode.com/tag/swiglu)
-   [#rotarypositionalembeddings](https://hashnode.com/tag/rotarypositionalembeddings)
-   [#prenormalization](https://hashnode.com/tag/prenormalization)
-   [#llama](https://hashnode.com/tag/llama)
-   [#large-language-models-llms](https://hashnode.com/tag/large-language-models-llms)

## Written by

[Jonas Kim](https://hashnode.com/@bits-bytes-nn)

@bits-bytes-nn · Seoul

  
[View profile](https://hashnode.com/@bits-bytes-nn)

♾️ Life is complex. It has both real and imaginary parts.

Sr. Data Scientist at AWS

## Responses

Comments are turned off for this post.

[Jonas Kim](https://hashnode.com/@bits-bytes-nn)

[Mar 18, 2025](https://hashnode.com/posts/llama-open-and-efficient-foundation-language-models/67d994dd734a9902c7d34dfb/comment/67d99a4ff851bfc98f2e5a7e)

```
# xformers 기반의 메모리 효율적인 인과적 어텐션 구현
def memory_efficient_causal_attention(q, k, v):
    # 필요한 어텐션 점수만 계산 (마스킹된 점수는 계산하지 않음)
    batch_size, seq_len, num_heads, head_dim = q.shape
    # 출력 텐서 초기화
    output = torch.zeros_like(q)
    # 누적 소프트맥스 상태 추적
    softmax_scale = 1.0 / math.sqrt(head_dim)
    m_prev = torch.zeros((batch_size, num_heads, 1), device=q.device)  # 최대값 저장
    p_sum = torch.zeros((batch_size, num_heads, 1), device=q.device)   # 합 저장
    o_acc = torch.zeros((batch_size, num_heads, head_dim), device=q.device)  # 가중합 저장
    # 순차적 처리로 메모리 사용 최소화
    for i in range(seq_len):
        # 현재 위치의 쿼리
        qi = q[:, i:i+1]  # [B, 1, H, D]
        # 현재 위치까지의 키와 값만 처리 (인과성)
        ki = k[:, :i+1]   # [B, i+1, H, D]
        vi = v[:, :i+1]   # [B, i+1, H, D]
        # 어텐션 점수 계산 (스케일링 포함)
        si = torch.matmul(qi, ki.transpose(-1, -2)) * softmax_scale  # [B, 1, H, i+1]
        # Rabe & Staats 방식: 소프트맥스 최적화
        mi = torch.max(si, dim=-1, keepdim=True)[0]
        m_curr = torch.maximum(m_prev, mi)
        # 이전 누적값 스케일 조정
        scale_factor = torch.exp(m_prev - m_curr)
        p_sum = p_sum * scale_factor
        o_acc = o_acc * scale_factor.unsqueeze(-1)
        # 새로운 어텐션 가중치 계산
        pi = torch.exp(si - m_curr)
        p_sum = p_sum + pi.sum(dim=-1, keepdim=True)
        # 출력 누적 (중간 어텐션 행렬 저장 없이)
        o_acc = o_acc + torch.matmul(pi, vi).squeeze(1)
        # 현재 위치 출력 계산
        output[:, i] = o_acc / p_sum
        # 상태 업데이트
        m_prev = m_curr
    return output
```
