TL;DR
- I read this because.. : TextSpan(https://github.com/long8v/PTIR/issues/172 ) ์์ ์ด ๋ ผ๋ฌธ์์ ์ด์ฉํ OV circuit์ ์ผ๋ค๊ณ ํ๊ณ mean ablation์์ ์ฌ์ฉ๋ ๊ฒ ๊ฐ์๋ฐ ๋ด์ฉ์ด ์ดํด๊ฐ ์๋ผ์ ์ฝ์.
- problem : Transformer์ ๋์ ๋ฐฉ์์ circuit์ ๋๋ ์ ์๊ฐํด๋ณด์.
Details
Related Work
“circuit"์ด๋ ๋จ์ด๊ฐ ๋ญ๊ฐ ํ๊ณ ๋ดค๋๋ฐ ๋น์ทํ ์ ์๋ค์ด ๋ธ https://distill.pub/2020/circuits/zoom-in/ ์ด ๋ ผ๋ฌธ์ด ์์์ด์์. ๋ด๋ด๋คํธ์ํฌ ๋ด๋ถ์์ feature๋ค์ด ์ด๋ป๊ฒ ์ฐ๊ฒฐ๋์ด ์๋์ง sub-graph๋ฅผ ๋ถ์ํ๋๊ฑฐ๋ผ๊ณ ํจ. ์.. ์์ธํ ์ฝ์ด๋ด์ผ ์๊ฒ ์ง๋ง ๋ถ๋ฆฌํ ์ ์๋๊ฑด ๋ถ๋ฆฌํ๋ ๋ฐฉ์์ธ ๋ฏ ํ๋ค. ์ฌ๊ธฐ์ ์๊ฐํ๋ ์ด๋ป๊ฒ ํ๋๊ฑด์ง ๊ถ๊ธํ๋๋ฐ ํ์ฑํ๋ layer์ ๋ํด์ https://en.wikipedia.org/wiki/DeepDream (code )์ด๋ ๋ฐฉ๋ฒ๋ก ์ ์ฌ์ฉํ๋ค๊ณ ํจ. ์๋ ๋ถํฐ ์ LSD ์ค๋ฌ์ด ๊ทธ๋ฆผ ์ด๋ป๊ฒ ๊ทธ๋ฆฌ๋๊ฐ ๊ถ๊ธํ๋๋ฐ ์ด๋ ๊ฒ ์ค๋๋ ๋ ผ๋ฌธ์ด์๋ค๋..
High-Level Architecture
transformer๋ ๋์ถฉ ๋ณด๋ฉด ์ด๋ ๊ฒ ์๊ฒผ๋ค
- token embedding
- residual stream์ ๊ฐ head ์ฐ์ฐ $h(x_i)$๋ฅผ ๋ํด์ฃผ๋ ๋ถ๋ถ
- residual stream์ mlp๋ฅผ ์ทจํ๊ณ ์ด๋ฅผ ๋ค์ residual stream์ ๋ํด์ฃผ๋ ๋ถ๋ถ
- word unembedding (=> logit ์์ธก)
์ฌ๊ธฐ์ “residual stream"์ ๋ถ์ํ๊ธฐ๋ฅผ channel ๊ฐ ์ปค๋ฎค๋์ผ์ด์
์ ํ๋ ๊ณณ์ด๋ผ๊ณ ๋ถ์ํ๋ค.
residual๋ก ์ฐ๊ฒฐ๋๋ ๋ถ๋ถ์ด ์์ผ๋๊น ๊ฐ ๋ ์ด์ด์ hidden ๋ผ๋ฆฌ๋ ์๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค
Attention Heads are independent and additive
์๊ฑด ๊ทธ๋ฅ ํ๋ ฌ ์ฐ์ฐ์ธ๋ฐ ๊ฐ head ๋ณ๋ก concatํ๊ณ $W_o$๋ฅผ ํ๋ ์์ผ๋ก ๋์ด์์ง๋ง ์ค์ ๋ก ์ด๊ฑด ๊ฐ head๋ณ๋ก $W_o^{h_i}$๋ฅผ ๊ณฑํ๋ค์ summation ํ๋ ๊ฒ๊ณผ ๋์น์ด๋ค. ์ฆ ๊ฐ head ๋ณ๋ก residual stream์ ์ ๋ณด๋ฅผ ๋ฃ์๋ค ๋บ๋ค ํ๋ค๊ณ ๋ณผ ์ ์๋ค.
Attention Heads as Information Movement
์ด๋ residual stream์์ ์ ๋ณด๋ฅผ ์ฝ๋ ๊ฒ๊ณผ ์ฐ๋ ๊ฒ์ด ์์ ๋ถ๋ฆฌ๋ ์ ์๋ค. ์ด๋ฅผ ๋ณด๊ธฐ ์ํด attention ์ฐ์ฐ์ ์กฐ๊ธ ๋ค๋ฅด๊ฒ ์จ๋ณด์.
- ๊ฐ ํ ํฐ๋ค์ด residual stream์ผ๋ก ๋ถํฐ ๋ด ํ์ ธ value vector๋ฅผ ๊ณ์ฐํ๋ค $v_i=W_Vx_i$
- attention score $A_i$๋ฅผ ๋ฐ๊ณ linear combination ํ์ฌ result vector๋ฅผ ๊ตฌํ๋ค $r_i=\sum_j A_{i,j} v_j$
- ๊ฐ head๋ณ๋ก output vector๋ฅผ ๊ตฌํ๋ค $h(x)_i=W_Or_i
๊ฐ step์ matrix multiply๋ก ์ ์ ์ ์๋๋ฐ, ์ ํ๋์ matrix๋ก ํฉ์น์ง ์๋๋ฉด, $x$๋ (seq_len, head_dim)์ 2์ฐจ์ ํ
์์ธ๋ฐ, $W_v$, $W_o$๋ฅผ ๊ณฑํ๋๊ฑด head_dim ์ฐจ์์์ ์ผ์ด๋๊ณ $A$๋ฅผ ๊ณฑํ๋๊ฑด seq_len ์์ ์ผ์ด๋๊ธฐ ๋๋ฌธ์ด๋ค.
์์ ์ฐ์ฐ์ Tensor product
๋ก ํํํ๋ฉด ์๋์ ๊ฐ๋ค.
contextualized embedding $x$๋ฅผ V๋ก ๋ง๋ค๊ณ attention score A๋ ๊ณฑํ๊ณ ์ด๋ฅผ outputrhk rhqgksek. ์ด๋ฅผ ์ ๋ฆฌํ๋ฉด ์๋์ ๊ฐ์ด ๋๊ณ $W_oW_V$๋ ํ๋๋ก ํฉ์น ์ ์๋ค.
Observation about attention heads
- attention head๋ residual stream์์ token์ด ๋ค๋ฅธ ํ ํฐ์ผ๋ก ์ฎ๊ฒจ๊ฐ๋ ์ญํ ์ ํ๋ค. residual vector space๋ฅผ “contextualized word embedding"์ด๋ผ๊ณ ๋ณผ ์ ์๋ค.
- ์ด๋ $A$์ $W_OW_V$ ๋๊ฐ์ linear operation์ผ๋ก ๋ณผ ์ ์๋๋ฐ ๋๊ฐ๊ฐ ๋ค๋ฅธ ์ญํ ์ ํ๋ฉฐ ์์ง์ธ๋ค.
- $A$๋ “์ด๋ค token"์ ์ ๋ณด๊ฐ ์ด๋์ ์ด๋๋ก ๊ฐ๋์ง๋ฅผ ๊ด์ฅํ๋ค
- $W_OW_V$๋ source token์์ “์ด๋ค ์ ๋ณด"๊ฐ ์ฝํ๊ณ ์์ฑ๋๋์ง๋ฅผ ์ ํ๋ค.
- ์ด๋ $A$๋ง softmax๊ฐ ์์ด์ nonlinearํ๊ณ $A$๋ฅผ ๊ณ ์ ํ๋ฉด linear์ฐ์ฐ์ผ๋ก ๋ณผ ์์๋ค.
- $W_Q$, $W_K$๋ ํญ์ ๊ฐ์ด ์์ง์ด๊ณ ๊ทธ๋์ ์ฐ๋ฆฌ๋ $W_OW_V$, $W_Q^TW_V$๋ฅผ ํ๋์ low rank matrix์ฒ๋ผ ์๊ฐํ ์ ์๋ค.
Zero-Layer Transformer
mhsa๊ฐ ์๋ ๊ทธ๋ฅ zero-layer transformer๋ ์ผ์ข
์ bigram์ ํ์ตํ๋ ์ญํ ์ ํ๋ค.
One-Layer Attention-Only Transformer
์๋์ ๊ฐ์ด ์ ๋ฆฌ๋ ์ ์๋ค. h๋ ๊ฐ head๋ณ ์ฐ์ฐ์ด๊ณ sum์ผ๋ก ๊ตฌํ ์ ์๋ค (์์ ์น์
์์ ์ ๋ฆฌํ๋ฏ์ด)
์ด๊ฑธ tensor notation์ผ๋ก ๋ฐ๊พธ๋ฉด
์ด๋ ๊ณ ์ด๊ฑธ ๋ค์ ๋ฐ๊พธ๋ฉด
์ด๋ ๊ฒ ๋๊ฐ๋ก ๋ถ๋ฆฌ๋๋ค. ์์ term์ zero-layer transformer์ bigram statistics๋ฅผ ์ ๋ฌํ๋ ์ญํ ๋ค์ ํญ์ attention head
Splitting Attention Head terms into Query-Key and Output-Value Circuits
๋๋ฒ์งธ ํญ์ ๋ ๋ถ๋ฆฌํ ์ ์๋ค.
์์ ์ค๋ช ํ๋ฏ์ด OV cirtcuit์ how to attend ์ด๊ณ QK circuit์ ์ด๋ค token์ attend ํ ๊ฒ์ด๋ ์ด๋ค.
OV AND QK INDEPENDENCE (THE FREEZING ATTENTION PATTERNS TRICK)
์ด๊ฑฐ ๋ณด๋ ค๊ณ ๋ด๊ฐ ์ฝ์.. ๊ฒฐ๋ก ์ ๋๋ฒ forwardํด์ QK circuit์ ์ ์ฅํด ๋๊ณ ์ด๊ฑธ ๊ณ ์ ๋ ๊ฐ์ผ๋ก ๋ณด๊ณ OV circuit์ ๋ถ์ํ๋ฉด linear ํ๋ฏ๋ก ์ฌ๋ฌ ์ฌ๋ฐ๋ ๋ถ์์ ํ ์ ์๋ค๋ ๊ฒ !
Thinking of the OV and QK circuits separately can be very useful, since they’re both individually functions we can understand (linear or bilinear functions operating on matrices we understand). But is it really principled to think about them independently? One thought experiment which might be helpful is to imagine running the model twice. The first time you collect the attention patterns of each head. This only depends on the QK circuit. 14 The second time, you replace the attention patterns with the “frozen” attention patterns you collected the first time. This gives you a function where the logits are a linear function of the tokens! We find this a very powerful way to think about transformers.
์ฌ์ค ์ด ๋ค์ ๋ถํฐ๊ฐ ๋ ์ฌ๋ฐ๋ ๊ฒ ๊ฐ์๋ฐ… ์ง์ณ์ ์ฌ๊ธฐ๊น์ง๋ง ์ฝ๋๋ค.