LLM の核心をやさしく解説
A gentle guide to the core of LLMs
ChatGPT や LLaMA のような大規模言語モデル(LLM)の中心にある「Attention(注意機構)」を、遠くの単語どうしの関係を見る仕組みとして、数式の前にイメージで理解するページです。
This page builds an intuition for Attention — the core mechanism of LLMs like ChatGPT and LLaMA — as a way of relating distant words, before touching any maths.
畳み込みは「小さい窓の中だけを見る」演算です。画像のように「隣のピクセルとの関係」だけ見ればよい場面では十分ですが、文章では事情が違います。
Convolution looks only within a small local window. For images — where only neighboring pixels matter — that's fine. But language is different.
「太郎はケーキを食べた。そのあと彼は眠った。」
"Taro ate the cake. After that, he fell asleep."
「彼」が誰かを理解するには、ずっと前の「太郎」を参照しなければなりません。小さい窓では届かない「遠距離の関係」です。
To understand who "he" is, you must reach back to "Taro" earlier in the text — a long-range relationship that a small window cannot capture.
文章・コード・音楽など「系列データ」には、こういった遠距離の依存関係がたくさんあります。これを解決するために生まれたのが Attention(注意機構)です。
Sequential data — language, code, music — is full of such long-range dependencies. Attention was designed to solve exactly this problem.
Attention をひとことで言うと、こうです:
In one sentence, Attention is:
「今この単語を処理するとき、文中のすべての単語を参照して、関係が深そうな単語ほど強く注目する仕組み」
"When processing the current word, look at all words in the sequence and pay more attention to the ones that seem most relevant."
畳み込みが「窓の中だけ」を見るのに対し、Attention は距離に関係なく文全体を一度に見ます。「彼」という単語を処理するとき、文の先頭にある「太郎」にも強く注目できます。
Where convolution looks only "inside the window," Attention sees the entire sequence at once, regardless of distance. When processing "he," it can strongly attend to "Taro" at the very beginning of the sentence.
Attention は全単語を「同じ強さで」見るわけではありません。関係が深そうな単語を強く見て、無関係な単語は弱く見ます。この「注目の強さ」を数字にしたものが注目の重み(Attention Weight)です。
Attention doesn't look at all words with equal strength. It looks strongly at relevant words and weakly at irrelevant ones. The number that represents this "strength of focus" is called an Attention Weight.
図書館で調べものをするとき、手元の本(注目したい単語)に関係する棚(他の単語)を見に行きます。「この棚はすごく関係ある!」「この棚はあまり関係ない」と重要度を判断しながら情報を集める感覚です。
Imagine researching in a library: from the book you're reading (the current word), you visit shelves (other words) around the room. You judge "this shelf is very relevant!" vs "this one not so much" — and collect information weighted by relevance.
各単語ペアの「関係の強さ」に点数をつけます。でも、そのままでは点数の大きさがバラバラで使いにくいです。
A "relevance score" is computed for every word pair. But raw scores are awkward to work with as-is — they're just arbitrary numbers of different sizes.
そこで softmax の出番です。softmax は、バラバラの点数を 「どの単語にどれだけ注目するか」の振り分け に変える仕組みです。関係が深い単語には大きく、あまり関係ない単語には少しだけ注目が配られます。
That's where softmax comes in. softmax turns those uneven scores into a distribution of attention — assigning a lot of attention to relevant words and very little to unrelated ones.
「注目ポイント」が 10 枚あるとして、「太郎」「ケーキ」「食べた」に配るとします。点数が高かった「太郎」にはたくさん、低かった「食べた」にはほんの少しだけ配ります。
Imagine 10 "attention tokens" to hand out among "Taro", "cake", and "ate". Most go to "Taro" (highest score), very few to "ate" (lowest score).
softmax は、ちょうどこういう「注目の配り方」を自動で決める仕組みです。
softmax automates exactly this kind of distribution.
「彼」を処理するとき、注目の 87% を「太郎」に向ける、というイメージです。全部合わせると 100% ぶん配りきった状態になります。
When processing "he," 87% of attention goes to "Taro." Together all shares add up to 100% — all the attention has been distributed.
なぜ「全部で 100%(=合計 1)」になるように揃えるかというと、あとで「注目の強さに比例して情報を混ぜる」計算をするためです。全部配りきった状態にしておくと、その計算がきれいにまとまります。
The reason the shares are kept at 100% (sum = 1) is that the next step blends information in proportion to these shares. Having them sum to 1 makes that blending step clean and well-defined.
Attention を計算するとき、各単語は3つの役割を同時に担います。
When computing Attention, each word plays three roles simultaneously.
| 記号 | 名前 | 役割のイメージ | 図書館の比喩 | Symbol | Name | Role | Library analogy |
|---|---|---|---|---|---|---|---|
| Q | Query(質問) | 「自分はどんな情報を欲しいか?」 | 図書館で調べたいテーマ | Query | "What information am I looking for?" | The research topic you bring to the library | |
| K | Key(目印) | 「自分はどんな情報を持っているか?(の要約)」 | 各棚の背表紙ラベル | Key | "What information do I hold?" (a summary) | The label on each shelf's spine | |
| V | Value(内容) | 「実際に渡す情報の中身」 | 棚の中の本の内容 | Value | "The actual content I pass along" | The actual books on the shelf |
「自分が欲しい情報(Q)」と「相手が持っている情報の要約(K)」を内積(ドット積)で比較します。似ているほどスコアが高くなります。
The Query (what you're looking for) is compared with each Key (what others hold) via dot product. The more similar they are, the higher the score.
前のステップのスコアを softmax に通して「どこにどれだけ注目するか」の配分に変えます。関係が深い単語ほど大きな値になります。
The scores from step 1 go through softmax, which converts them into an attention distribution — more relevant words get a larger share.
「関係が深い単語ほど強く参照する」ように、注目割合に応じて各単語の Value を混ぜ合わせます。これが Attention の出力です。
Values are blended proportionally to attention weights — more relevant words contribute more to the output. This mixture is what Attention outputs.
ここまでの3ステップを順番に式にしてみます。いきなり全部わかる必要はありません。「さっきの3ステップがどこに対応しているか」だけ追えれば十分です。
Let's write those three steps as formulas one at a time. You don't need to understand everything at once — just trace where each of the three steps appears.
「いま欲しい情報(Q)」と「各単語の目印(K)」を掛け合わせて、関係の強さスコアを作ります。
Multiply "what I'm looking for (Q)" by "each word's label (K)" to get a relevance score.
スコアを softmax に通して、どこにどれだけ注目するかの配分に変えます。
Pass the scores through softmax to get how much to attend to each word.
配分にしたがって各単語の中身(V)を集めます。関係が深い単語からはたくさん、浅い単語からは少しだけ受け取ります。
Collect each word's content (V) according to the distribution — more from relevant words, less from unrelated ones.
この式は新しいことを何も加えていません。ステップ 1〜3 をそのまま 1 行にまとめただけです。
This adds nothing new — it's just steps 1–3 written on one line.
実際の Transformer では式が少し違い、スコアを $\sqrt{d}$ で割ってから softmax に入れます:
In practice, Transformer divides the scores by $\sqrt{d}$ before softmax:
$Q$ や $K$ のベクトルが長いほど内積の値が大きくなりやすく、softmax が極端な配分を出してしまうことがあります。$\sqrt{d}$($d$ はベクトルの長さ)で割ることでスコアを適度な大きさに抑えます。最初の理解では「スコアが暴れすぎないようにする調整」くらいで十分です。
Longer $Q$ and $K$ vectors tend to produce large dot products, pushing softmax toward extreme distributions. Dividing by $\sqrt{d}$ (where $d$ is the vector length) keeps scores in a reasonable range. For now, think of it as "a tweak to stop scores from getting too large."
ここまでの Attention は「1 種類の見方で文全体を参照する」仕組みでした。でも実際の文には、同時に気にすべき関係が複数あります。
The Attention we've seen so far uses one "view" of the sequence. But real language has multiple kinds of relationships worth tracking at the same time.
1 人の先生だけで文を読むのではなく、
Instead of one teacher reading the sentence, imagine:
が同時に文を読んでいる感じです。それぞれの先生が違う視点で関係を見つけ、最後に結果を合わせます。
All reading simultaneously, each from a different angle, then pooling their findings.
Multi-head Attention は、Attention の計算を h 個の「head(頭)」で並列に行い、それぞれが異なる関係を学習できるようにした仕組みです。結果を最後に結合することで、1 種類の Attention よりも豊かに文の情報を捉えられます。
Multi-head Attention runs $h$ Attention computations ("heads") in parallel, each free to learn a different type of relationship. The results are merged at the end, giving a richer picture of the sequence than a single Attention head could provide.
Transformer は、Multi-head Attention と小さなフィードフォワードネットワーク(FFN)のブロックを何層も積み重ねたアーキテクチャです。CNN が畳み込み層を積み重ねるように、Transformer は Attention ブロックを積み重ねます。
Transformer is an architecture that stacks blocks of Multi-head Attention and a small feed-forward network (FFN). Just as CNN stacks convolution layers, Transformer stacks Attention blocks.
Transformer = [Multi-head Attention → FFN] を N 層積み重ねたもの
Transformer = [Multi-head Attention → FFN] repeated N times
Attention の核心計算 Q·Kᵀ と · V は、どちらも大きな行列積(GEMM)です。中学生向けに言えば、大きな表どうしの掛け算です。ROCm では、この種の計算は主に rocBLAS などの行列計算ライブラリが支えます。
The core computations Q·Kᵀ and · V are both large matrix multiplications (GEMM) — in simple terms, multiplying big tables of numbers. In ROCm, this kind of work is mainly supported by matrix-math libraries such as rocBLAS.
また、文が長くなると「単語どうしの組み合わせ」を全部比べる必要があり、メモリをたくさん使います。Flash Attention は、その計算を小さなかたまりに分けて、途中で使うメモリを減らす考え方です。gfx900 のような旧世代 GPU では GPU 内部の小さくて速い作業スペースが限られるため、恩恵が小さめになる場合があります。
As sequences get longer, Attention must compare many pairs of words, which uses a lot of memory. Flash Attention is the idea of breaking that work into smaller chunks so it needs less temporary memory. On older GPUs like gfx900, the small fast workspace inside the GPU is more limited, so the benefit can be smaller.
GEMM: 大きな表どうしの掛け算。Flash Attention: Attention を小分けにしてメモリ節約する工夫。GPU 内部の小さくて速い作業スペース: その場で計算を回すための小さな作業机、くらいの理解で十分です。
GEMM: multiplying large tables of numbers. Flash Attention: a way to split Attention into smaller chunks to save memory. The GPU's small fast workspace: think of it as a tiny desk inside the GPU used for immediate work.