File size: 4,472 Bytes
ee6e328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!--Copyright 2023 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->

# μ–΄ν…μ…˜ λ©”μ»€λ‹ˆμ¦˜[[attention_mechanisms]]

λŒ€λΆ€λΆ„μ˜ 트랜슀포머 λͺ¨λΈμ€ 정방행렬인 전체 μ–΄ν…μ…˜μ„ μ‚¬μš©ν•©λ‹ˆλ‹€. 
ν•˜μ§€λ§Œ μ΄λŠ” κΈ΄ ν…μŠ€νŠΈλ₯Ό λ‹€λ£° λ•ŒλŠ” 큰 계산 병λͺ© ν˜„μƒμ„ μœ λ°œν•  수 μžˆμŠ΅λ‹ˆλ‹€. 
`Longformer`와 `Reformer`λŠ” ν›ˆλ ¨ 속도λ₯Ό 높이기 μœ„ν•΄ μ–΄ν…μ…˜ ν–‰λ ¬μ˜ ν¬μ†Œ 버전을 μ‚¬μš©ν•˜μ—¬ νš¨μœ¨μ„ λ†’μ΄λ €λŠ” λͺ¨λΈμž…λ‹ˆλ‹€.

## LSH μ–΄ν…μ…˜[[lsh_attention]]


[Reformer](#reformer)λŠ” LSH(Locality Sensitive Hashing) μ–΄ν…μ…˜μ„ μ‚¬μš©ν•©λ‹ˆλ‹€. softmax(QK^t)μ—μ„œλŠ” ν–‰λ ¬ QK^t의 (softmax μ°¨μ›μ—μ„œ) κ°€μž₯ 큰 μš”μ†Œλ“€λ§Œ μœ μš©ν•œ κΈ°μ—¬λ₯Ό ν•  κ²ƒμž…λ‹ˆλ‹€. 
λ”°λΌμ„œ 각각의 쿼리 q에 λŒ€ν•΄, q와 κ°€κΉŒμš΄ ν‚€ k만 κ³ λ €ν•  수 μžˆμŠ΅λ‹ˆλ‹€. ν•΄μ‹œ ν•¨μˆ˜λŠ” q와 kκ°€ κ°€κΉŒμš΄μ§€ μ—¬λΆ€λ₯Ό κ²°μ •ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. 
μ–΄ν…μ…˜ λ§ˆμŠ€ν¬λŠ” ν˜„μž¬ 토큰을 λ§ˆμŠ€ν‚Ήν•˜μ—¬ λ³€κ²½λ©λ‹ˆλ‹€. 이 λ•Œ 첫 번째 μœ„μΉ˜μ˜ 토큰은 μ œμ™Έν•©λ‹ˆλ‹€. μ™œλƒν•˜λ©΄ 쿼리와 ν‚€κ°€ λ™μΌν•œ 값을 κ°–κ²Œ 되기 λ•Œλ¬Έμž…λ‹ˆλ‹€(μ„œλ‘œ 맀우 μœ μ‚¬ν•¨). 
ν•΄μ‹œλŠ” μ•½κ°„μ˜ λ¬΄μž‘μœ„μ„±μ„ κ°€μ§ˆ 수 μžˆμœΌλ―€λ‘œ, μ‹€μ œλ‘œλŠ” μ—¬λŸ¬ 개의 ν•΄μ‹œ ν•¨μˆ˜κ°€ μ‚¬μš©λ˜κ³  (`n_rounds` λ§€κ°œλ³€μˆ˜μ— μ˜ν•΄ 결정됨) κ·Έ 후에 평균값을 μ·¨ν•˜κ²Œ λ©λ‹ˆλ‹€.

## 지역 μ–΄ν…μ…˜[[local_attention]]

[Longformer](#longformer)λŠ” 지역 μ–΄ν…μ…˜μ„ μ‚¬μš©ν•©λ‹ˆλ‹€. μ’…μ’… νŠΉμ • 토큰에 λŒ€ν•΄ 지역 μ»¨ν…μŠ€νŠΈ(예: μ™Όμͺ½κ³Ό 였λ₯Έμͺ½μ— μžˆλŠ” 두 개의 토큰은 λ¬΄μ—‡μΈκ°€μš”?)λ§ŒμœΌλ‘œλ„ μž‘μ—…μ„ μˆ˜ν–‰ν•˜λŠ”λ° μΆ©λΆ„ν•©λ‹ˆλ‹€. 
λ˜ν•œ μž‘μ€ μ°½(window)을 가진 μ–΄ν…μ…˜ λ ˆμ΄μ–΄λ₯Ό μŒ“μŒμœΌλ‘œμ¨ λ§ˆμ§€λ§‰ λ ˆμ΄μ–΄λŠ” μ°½ λ‚΄μ˜ ν† ν°λΏλ§Œ μ•„λ‹ˆλΌ 더 λ§Žμ€ 수의 토큰에 λŒ€ν•œ 수용 μ˜μ—­(receptive field)을 κ°–κ²Œ λ˜μ–΄ 전체 λ¬Έμž₯의 ν‘œν˜„μ„ ꡬ좕할 수 μžˆμŠ΅λ‹ˆλ‹€.

사전에 μ„ νƒλœ 일뢀 μž…λ ₯ 토큰듀은 μ „μ—­ μ–΄ν…μ…˜μ„ λ°›μŠ΅λ‹ˆλ‹€. 이 λͺ‡ 개의 토큰에 λŒ€ν•΄μ„œλŠ” μ–΄ν…μ…˜ 행렬이 λͺ¨λ“  토큰에 μ ‘κ·Όν•  수 있으며, 이 과정은 λŒ€μΉ­μ μœΌλ‘œ μ΄λ£¨μ–΄μ§‘λ‹ˆλ‹€. 
λ‹€λ₯Έ λͺ¨λ“  토큰듀은 둜컬 μ°½ λ‚΄μ˜ 토큰듀에 더해 ν•΄λ‹Ή νŠΉμ • 토큰듀에도 μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” λ…Όλ¬Έμ˜ Figure 2dμ—μ„œ λ‚˜νƒ€λ‚˜λ©°, μ•„λž˜μ— μƒ˜ν”Œ μ–΄ν…μ…˜ λ§ˆμŠ€ν¬κ°€ μ œμ‹œλ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€:


<div class="flex justify-center">
    <img scale="50 %" align="center" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/local_attention_mask.png"/>
</div>


적은 νŒŒλΌλ―Έν„°μ˜ μ–΄ν…μ…˜ 행렬을 μ‚¬μš©ν•˜λ©΄ λͺ¨λΈμ΄ 더 큰 μ‹œν€€μŠ€ μž…λ ₯ 길이λ₯Ό κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

## λ‹€λ₯Έ 방법듀[[other_tricks]]

### 좕별 μœ„μΉ˜ 인코딩[[axial_positional_encodings]]

[Reformer](#reformer)λŠ” 좕별 μœ„μΉ˜ 인코딩(axial positional encodings)을 μ‚¬μš©ν•©λ‹ˆλ‹€. 기쑴의 트랜슀포머 λͺ¨λΈμ—μ„œλŠ” μœ„μΉ˜ 인코딩 ν–‰λ ¬ EλŠ” 크기가 \\(l \times d\\)인 행렬이며, 
μ—¬κΈ°μ„œ \\(l\\)은 μ‹œν€€μŠ€ 길이(sequence length)이고 \\(d\\)λŠ” μˆ¨κ²¨μ§„ μƒνƒœ(hidden state)의 μ°¨μ›μž…λ‹ˆλ‹€. 맀우 κΈ΄ ν…μŠ€νŠΈμ˜ 경우, 이 행렬은 맀우 크며 GPU μƒμ—μ„œ 곡간을 많이 차지할 수 μžˆμŠ΅λ‹ˆλ‹€. 
이λ₯Ό μ™„ν™”ν•˜κΈ° μœ„ν•΄, 좕별 μœ„μΉ˜ 인코딩은 큰 ν–‰λ ¬ Eλ₯Ό 두 개의 μž‘μ€ ν–‰λ ¬ E1κ³Ό E2둜 λΆ„ν•΄ν•©λ‹ˆλ‹€. μ΄λ•Œ E1의 ν¬κΈ°λŠ” \\(l_{1} \times d_{1}\\)이고, E2의 ν¬κΈ°λŠ” \\(l_{2} \times d_{2}\\)μž…λ‹ˆλ‹€. 
μ΄λ•Œ \\(l_{1} \times l_{2} = l\\)이고 \\(d_{1} + d_{2} = d\\)(길이에 λŒ€ν•œ κ³±μ…ˆ 연산을 μ‚¬μš©ν•˜λ©΄ 훨씬 μž‘μ•„μ§‘λ‹ˆλ‹€). E의 μ‹œκ°„ 단계 j에 λŒ€ν•œ μž„λ² λ”©μ€ E1μ—μ„œ μ‹œκ°„ 단계 \\(j \% l1\\)의 μž„λ² λ”©κ³Ό E2μ—μ„œ μ‹œκ°„ 단계  \\(j // l1\\)의 μž„λ² λ”©μ„ μ—°κ²°ν•˜μ—¬ μ–»μŠ΅λ‹ˆλ‹€.