-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsingle-head-attention.typ
174 lines (152 loc) · 4.22 KB
/
single-head-attention.typ
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#import "@preview/cetz:0.3.4": canvas, draw
#import draw: rect, content, line
#set page(width: auto, height: auto, margin: 8pt)
#canvas({
// Define spacing variables
let h-sep = 1.2 // horizontal separation between elements
let v-sep = 0.8 // vertical separation between elements
// Helper function for drawing a matrix with colored dimension indicators
let matrix(
pos, // (x, y) position of top-left corner
size, // (width, height) of matrix
label, // matrix label (e.g. Q, K, V)
top-color: none, // color for top dimension line
left-color: none, // color for left dimension line
style: (stroke: rgb(50%, 50%, 50%), fill: white, thickness: 1.5pt), // matrix style
) = {
let (x, y) = pos
let (w, h) = size
let offset = 0.1 // offset for dimension lines to avoid overlap
// Draw matrix rectangle
rect(pos, (x + w, y - h), ..style, name: label)
content(label, $#label$)
// Draw dimension indicators if colors specified
if top-color != none {
line(
(x - 0.02, y + offset),
(x + w + 0.02, y + offset),
stroke: (paint: top-color, thickness: 2pt),
)
}
if left-color != none {
line(
(x - offset, y + 0.02),
(x - offset, y - h - 0.02),
stroke: (paint: left-color, thickness: 2pt),
)
}
}
// Define styles
let value-style = (
stroke: rgb(50%, 50%, 50%),
fill: white,
thickness: 1.5pt,
)
let operation-style = (
stroke: rgb(50%, 50%, 50%),
fill: rgb(30%, 80%, 80%, 30%),
thickness: 1.5pt,
)
let edge-style = (
mark: (start: "|", offset: 0.075, scale: 1.3),
stroke: rgb(50%, 50%, 50%),
thickness: 1.5pt,
)
let arrow-style = (
mark: (
start: (symbol: "|", offset: 0.075, scale: 1.3),
end: (symbol: "stealth", offset: 0.15, scale: 0.45),
fill: rgb(50%, 50%, 50%),
),
stroke: rgb(50%, 50%, 50%),
thickness: 1.5pt,
)
// Title and equation
content((4, 2.5), text(weight: "bold", size: 1.2em)[Single-head attention], name: "title")
content(
(4, -2.75),
$"Attention"(Q, K, V) = "softmax"_"row" ( (Q K^top) / sqrt(d)) V$,
name: "equation",
)
// Main nodes using helper function
matrix(
(0, 2.7),
(0.7, 1.8),
"Q",
top-color: rgb("#FFFF00"),
left-color: rgb("#00FFFF"),
style: value-style,
)
matrix(
(0, 0.4),
(0.7, 0.8),
"K",
top-color: rgb("#FFFF00"),
left-color: rgb("#FF0000"),
style: value-style,
)
matrix(
(0, -1),
(1.0, 1.2),
"V",
top-color: rgb("#FFA500"),
left-color: rgb("#FF0000"),
style: value-style,
)
// Operation nodes with consistent spacing
content(
(h-sep + 0.4, 0),
$dot.op^top$,
frame: "rect",
stroke: rgb(50%, 50%, 50%) + .75pt,
fill: rgb(30%, 80%, 80%, 30%),
padding: (5pt, 3pt, 1pt),
name: "att",
)
content(
(2 * h-sep + 0.6, 0),
[softmax],
frame: "rect",
stroke: rgb(50%, 50%, 50%) + .75pt,
fill: rgb(30%, 80%, 80%, 30%),
padding: (2pt, 3pt, 3pt),
name: "softmax",
)
matrix(
(3 * h-sep + 0.7, 0.9),
(0.8, 1.8),
"A",
top-color: rgb("#FF0000"),
left-color: rgb("#00FFFF"),
style: value-style,
)
content(
(4 * h-sep + 1, 0),
$dot.op$,
frame: "rect",
stroke: rgb(50%, 50%, 50%),
fill: rgb(30%, 80%, 80%, 30%),
padding: (1pt, 4pt, 2pt),
name: "prod",
)
matrix(
(5 * h-sep + 0.7, 0.9),
(1.0, 1.8),
"Y",
top-color: rgb("#FFA500"),
left-color: rgb("#00FFFF"),
style: value-style,
)
// Arrows with proper right angles using perpendicular coordinates
// K to att (straight)
line("K.east", "att.west", ..edge-style, name: "k-to-att")
// Q to att (right angle)
line("Q.east", ("Q.east", "-|", "att.north"), "att.north", ..edge-style, name: "q-to-att")
// V to prod (right angle)
line("V.east", ("V.east", "-|", "prod.south"), "prod.south", ..edge-style, name: "v-to-prod")
// Other straight connections
line("att.east", "softmax.west", stroke: rgb(50%, 50%, 50%), name: "att-to-sm")
line("softmax.east", "A.west", ..arrow-style, name: "sm-to-a")
line("A.east", "prod.west", stroke: rgb(50%, 50%, 50%), name: "a-to-prod")
line("prod.east", "Y.west", ..arrow-style, name: "prod-to-y")
})