GPU / CUDA 개발자를 위한 메모리 중심 최적화 가이드
1. 문제의 변화 : FLOPS 시대 - Data Movement 시대
초기 GPU 최적화의 핵심 목표는 단순
maximize FLOPS
maximize occupancy
maximize tensor core utilization
하지만 대형 AI 모델, 특히 LLM 등장 이후 GPU 최적화의 본질은 다음으로 이동
minimize memory traffic
maximize on-chip reuse
increase arithmetic intensity
이 변화의 이유는 단순
GPU 성능은ㅇ 다음 관계로 결정
Performance = min ( Compute Throughput, Memory Bandwidth )
즉
- 연산 성능보다
- 메모리 대역폭이 먼저 포화되면
GPU 는 Memory-bound 상태에 들어간다.
최근 AI workload 는 이 영역으로 이동하고 있다.
2. Arithmetic Intensity : AI 커널 성능의 핵심 지표
정의
Arithmetic Intensity = FLOPs / Memory Bytes ( AI = 연산량 / 메모리 이동량 )
두 가지 상황
Compute-bound : 연산 >> 메모리 이동
GPU 의 TensorCore 가 최대 성능을 낸다.
- GEMM
- Conv2D
Memory-bound : 메모리 이동 >> 연산
GPU 는 데이터가 오기를 기다린다.
- Attention
- KV cache
- Embedding lookup
- LLM decode
3.. GPU 메모리 계층 ( Memory Hierarchy )
GPU 성능을 이해하기 위해 메모리 계층 구조의 이해 필요
Register (가장 빠름)
Shared Memory
L1 Cache
L2 Cache
HBM (가장 느림 / 용량 큼)
Register : 가장 빠른 메모리
Shared Memory : SM 내부 scratchpad
L2 : GPU 전체 cache
HBM : 외부 고대역폭 DRAM
핵심은 HBM 접근은 매우 비싸다
그래서 최적화 목표는 HBM access 최소화이다.
4. HBM (High Bandwidth Memory) 설명
GPU 에서 사용되는 고대역폭 DRAM
GPU 성능을 결정하는 중요한 요소가 바로
HBM bandwidth
HBM capacity
5. 왜 LLM Inference 는 Memory-bound 인가
token 생성 과정
1. weight load
2. matmul
3. KV cache read
4. KV cache write
문제는
weight size >> compute per token
예
70B model
≈ 140GB weights
토큰 하나 생ㅅ엉 시
weights streaming
KV cache read
KV cache append
연산보다
memory traffic
이 훨씬 커짐
그래서 LLM decode 는
memory bandwidth limited
6. KV Cache 란
Transformer 는 attention 계산에서 이전 token 정보를 저장한다
이 저장된 값이
KV cache
구조
K matrix
V matrix
크기
O ( Context Length x Hidden Size )
예
8k context
batch 16
70B model
KV cache 만 수십 GB
문제
매 token 마다
read KV
append KV
이 발생한다.
그래서 memory traffic 이 폭발한다.
7. FlashAttention : IO-aware 알고리즘
AI 커널 최적화의 방향을 바꾼 알고리즘
reduce HBM traffic
maximize SRAM reuse
기존 Attention
HBM → Q,K,V
compute QK
store attention matrix
reload
softmax
reload
multiply V
store
HBM 왕복 많음
FlashAttention
HBM → tile
shared memory reuse
online softmax
write once
HBM 접근 최소화
8. CUDA Kernel 최적화 전략 변화
과거
maximize occupancy
maximize tensorcore
maximize parallelism
현재
minimize memory traffic
maximize data reuse
9. 주요 최적화 기법
1. Tiling
데이터를 tile 단위로 shared memory 에 로드
HBM → shared memory
reuse
compute
write back
2. Kernel Fusion
여러 연산을 하나의 kernel 로 합친다
GEMM
bias
activation
residual
HBM 왕복 감소
3. Persistent Kernel
kernel 을 계속 실행 상태로 유지
kernel stays
weights stay in SM
stream inputs
4. On-chip Residency
데이터를 가능한 오래
register
shared memory
에 유지한다.
10. AI Compiler 의 역할
단순히 커널 수준에서 이 문제를 해결하는 것이 아닌
operator fusion
memory scheduling
buffer reuse
lifetime analysis
kernel specialization
즉
data movement planning
목표는
minimize memory movement
'명징직조' 카테고리의 다른 글
| 표현은 언제 관계 정보를 담는가 - 양자 얽힘을 출발점으로 본 구조적 정보 표현의 조건 (0) | 2026.03.24 |
|---|---|
| Unified Optimization Stack for AI Systems - Semantic -> Memory -> Hardware (0) | 2026.03.16 |
| AI Compiler - A system that generates optimized kernel code for AI model inference (0) | 2026.01.31 |
| 복잡한 문제 - 문제 자체가 하나의 고정된 해답 공간을 가지지 않는 상태 (0) | 2025.12.12 |
| 체인 -> 상태 머신의 전환 - gpt 4.x 에서 5.0 으로 바뀐 주요 내용 중 추론을 상태 머신으로 다룸 (0) | 2025.12.12 |