『비전공자도 이해할 수 있는 챗GPT』,
『비전공자도 이해할 수 있는 AI 지식』
10만 베스트셀러를 기록한
세상에서 가장 이해하기 쉬운 챗GPT 교양서

HOME » WIKI » Flash Attention

Flash Attention

softmax

naive

\(\sigma(z_i) = \frac{e^{z_{i}}}{\sum_{j=1}^K e^{z_{j}}} \ \ \ for\ i=1,2,\dots,K\)

1

safe softmax

It involves subtracting the maximum value from all the output values before applying the softmax equation. This step helps to prevent the exponent from becoming extremely large, which could lead to computational challenges or overflow errors.

>>> np.exp(0.5)
np.float64(1.6487212707001282)
>>> np.exp(1)
np.float64(2.718281828459045)
>>> np.exp(10)
np.float64(22026.465794806718)
np.exp(x - max_x) / sum(np.exp(x - max_x))

2

safe softmax with online normalizer calculation

Essentially, the algorithm keeps the maximum value \(m\) and the normalization term \(d\) as it iterates over elements of the input array. At each iteration it needs to adjust the normalizer \(d\) to the new maximum \(m_j\) and only then add new value to the normalizer.

online softmax

# loop 1: get the maximum value of x and the accumulated exponential values
max_x = -np.inf
accum_exp = 0.
for t in x:
    max_x_new = t if t > max_x else max_x
    accum_exp = np.exp(max_x - max_x_new) * accum_exp + np.exp(t - max_x_new)
    max_x = max_x_new

# loop 2: get the softmax output by dividing the exponential of `x-max(x)` with `accum_exp`
out = [0. for _ in range(len(x))]
for i, t in enumerate(x):
    out[i] = np.exp(t - max_x) / accum_exp

tiling

>>> a = np.array([0.1, 0.5, 0.4, 0.2, 0.3, 0.3])
>>> sum([np.exp(aa - 0.5) for aa in a])
4.9534
>>> l1 = np.exp(0.1 - 0.5) + np.exp(0.5 - 0.5) + np.exp(0.4 - 0.5)
2.5751
>>> l2 = np.exp(0.2 - 0.3) + np.exp(0.3 - 0.3) + np.exp(0.3 - 0.3)
2.9048
>>> np.exp(0.5 - 0.5) * l1 + np.exp(0.3 - 0.5) * l2
4.9534

Flash Attention (Tiling)

C++, Torch Profiling

fanano = load(name='fanano', sources=['fanano.cpp', 'fanano.cu'], verbose=True)

빌드시 undefined symbol 오류는 argument type이 잘못되어 있는 경우이므로 주의

프로파일링 시 레거시인 torch.autograd.profiler.profiletorch.profiler.profile는 결과가 다르므로 주의, 특히 CUDA 실행 시간이 많이 차이가 난다.

Last Modified: 2024/10/29 18:50:38
자바 알고리즘 인터뷰 파이썬 알고리즘 인터뷰

카카오 코딩 테스트 출제위원이 직접 집필한,
리트코드(LeetCode) 문제로 풀어보는,
구글, 마이크로소프트, 네이버, 카카오
코딩 테스트 완벽 가이드
『자바 알고리즘 인터뷰』,
『파이썬 알고리즘 인터뷰』

이 사이트의 운영 비용을 후원할 수 있으며, 후원자에게 혜택을 제공할 예정입니다.

© 2000 - Sang Park Except where otherwise noted, content on this site is licensed under a CC BY-NC 4.0.
This site design was brought from Distill. Logo and wiki background image was brought from Bear.