GPT3の仕組み - ビジュアライゼーションとアニメーション
Jay Alammar の記事は、GPT-3 の内部動作を視覚化とアニメーションを用いて解説し、1750億パラメータの巨大モデルがトレーニングデータからどのように学習し、トークンを生成するかという仕組みを解明する。
キーポイント
GPT-3 のトレーニングプロセスとコスト
膨大なテキストデータ(3000億トークン)を用いてスライドウィンドウで学習例を作成し、次単語予測の誤差を最小化するようパラメータを更新するプロセスが数百万回繰り返される。このトレーニングには約 355 GPU 年と 460 万ドルのコストがかかった。
1750億パラメータによる予測機構
GPT-3 は 1750 億個のパラメータ(数値)を内部の数百の行列に格納しており、入力された単語をベクトル変換した上で、これらの行列演算を通じて次に来るトークンを確率的に生成する。
コンテキストウィンドウと処理フロー
モデルは最大 2048 トークン分の文脈(コンテキストウィンドウ)を保持し、入力された単語(例:robotics)が内部の特定のトラック(例:紫色のトラック)を通じて処理され、出力トークン(例:A)へと変換される一連の流れを可視化している。
Transformer デコーダーアーキテクチャ
GPT-3 の基盤となるのは Transformer デコーダーモデルであり、その技術的革新性は規模の大きさ(スケール)にあり、アーキテクチャ自体は既存の研究論文に基づいている。
GPT3 の内部構造と計算
GPT3 は96層のトランスフォーマーデコーダスタックを持ち、各層には18億のパラメータが割り当てられており、ここで主要な計算が行われます。
入力処理と出力生成の仕組み
すべてのトークンは層全体を流れ、入力完了後に出力への注目が切り替わり、各単語はモデルにフィードバックされて逐語的に生成されます。
ファインチューニングによる適応
ファインチューニングは特定のタスクにおいてモデルの性能を向上させるために重み(weights)を更新するプロセスです。
影響分析・編集コメントを表示
影響分析
本記事は、GPT-3 という「黒箱」と呼ばれる巨大言語モデルの内部動作を直感的な視覚化によって解きほぐすことで、技術者や一般ユーザーに対する神秘性を払拭し、その限界と可能性を客観的に理解する手助けとなる。特にトレーニングコストやパラメータの数値的性質に言及することで、大規模モデル開発の実務的な難易度とリソース要件を浮き彫りにしており、AI 業界におけるスケール効果の重要性を再認識させる内容である。
編集コメント
GPT-3 の複雑な内部構造を、直感的な図解とアニメーションで解説した傑作記事です。数式に頼らずモデルの挙動を理解したい方にとって必読の資料と言えます。
GPT3の仕組み - ビジュアライゼーションとアニメーション
ディスカッション: Hacker News (397ポイント, 97コメント), Reddit r/MachineLearning (247ポイント, 27コメント) 翻訳: ドイツ語、韓国語、中国語(簡体字)、ロシア語、トルコ語
技術界はGPT3の誇大な宣伝で騒がしい。大規模言語モデル(GPT3のような)は、その能力で私たちを驚かせ始めている。ほとんどの企業が顧客の前に置くにはまだ完全に信頼できるものではないが、これらのモデルは確実に自動化の歩みと知的コンピュータシステムの可能性を加速させるであろう、賢さの火花を見せている。GPT3の周囲の神秘的なオーラを取り除き、それがどのように訓練され、どのように機能するのかを学ぼう。
訓練された言語モデルはテキストを生成する。
オプションとして、何らかのテキストを入力として渡すことができ、それは出力に影響を与える。
出力は、モデルが大量のテキストを走査した訓練期間中に「学習した」ことから生成される。
訓練とは、モデルに大量のテキストを曝すプロセスである。そのプロセスは完了している。今あなたが見ているすべての実験は、その一つの訓練済みモデルから来ている。その訓練には355 GPU年、460万ドルのコストがかかったと推定されている。
3000億トークンのテキストデータセットは、モデルの訓練例を生成するために使用される。例えば、これらは上部の一文から生成された3つの訓練例である。
すべてのテキストにわたってウィンドウをスライドさせ、多くの例を作成できる様子がわかるだろう。
モデルには例が提示される。私たちは特徴だけを見せ、次の単語を予測するように要求する。
モデルの予測は間違っているだろう。私たちはその予測の誤差を計算し、次回はより良い予測ができるようにモデルを更新する。
数百万回繰り返す
さて、これらの同じステップを少し詳しく見てみよう。
GPT3は実際、一度に1トークンずつ出力を生成する(今はトークンを単語と仮定しよう)。
ご注意ください:これはGPT-3の仕組みの説明であり、何が新しいのかについての議論ではない(主に途方もない大規模さが新しい点である)。アーキテクチャは、この論文 https://arxiv.org/pdf/1801.10198.pdf に基づくトランスフォーマーデコーダーモデルである。
GPT3は巨大である。それは訓練から学んだことを1750億の数値(パラメータと呼ばれる)にエンコードする。これらの数値は、各実行でどのトークンを生成するかを計算するために使用される。
訓練前のモデルはランダムなパラメータから始まる。訓練は、より良い予測につながる値を見つける。
これらの数値は、モデル内部の数百の行列の一部である。予測は主に大量の行列乗算である。
私のYouTubeでのAI入門では、1つのパラメータを持つ単純なMLモデルを示した。この1750億という怪物を解きほぐすには良い出発点だ。
これらのパラメータがどのように分散され、使用されているかを明らかにするには、モデルを開き、中を見る必要がある。
GPT3は2048トークンの幅を持つ。これがその「コンテキストウィンドウ」である。つまり、トークンが処理される2048本のトラックがあるということだ。
紫色のトラックを追ってみよう。システムはどのようにして「robotics」という単語を処理し、「A」を生成するのか?
高レベルのステップ:
単語を、その単語を表すベクトル(数値のリスト)に変換する
予測を計算する
結果のベクトルを単語に変換する
GPT3の重要な計算は、96層のトランスフォーマーデコーダー層のスタック内部で行われる。
これらすべての層が見えるか?これが「ディープラーニング」における「深さ」である。
これらの各層は、それぞれ独自の18億パラメータを持って計算を行う。そこが「魔法」が起こる場所だ。以下はそのプロセスの高レベルなビューである:
デコーダー内部のすべての詳細な説明は、私のブログ記事『The Illustrated GPT2』で見ることができる。
GPT3との違いは、密な自己注意層と疎な自己注意層が交互に現れることである。
これはGPT3内での入力と応答(「Okay human」)のX線写真のようなものだ。すべてのトークンが層スタック全体を流れていく様子に注目してほしい。最初の単語の出力は気にしない。入力が終わったら、出力を気にし始める。私たちはすべての単語をモデルにフィードバックする。
Reactコード生成の例では、説明が入力プロンプト(緑色)となるだろう、加えて、説明=>コードの2、3の例もおそらく含まれると思う。そしてReactコードは、ここでのピンクのトークンのように、トークンごとに生成されていく。
私の推測では、プロンプトとなる例と説明は、例と結果を区切る特定のトークンと共に入力として追加され、モデルに供給される。
これがこのように機能するのは印象的だ。なぜなら、あなたはただGPT3のファインチューニングが開始されるのを待てばいいからだ。可能性はさらに驚くべきものになるだろう。
ファインチューニングは実際にモデルの重みを更新し、モデルが特定のタスクでより良くなるようにする。












原文を表示
How GPT3 Works - Visualizations and Animations
Discussions: Hacker News (397 points, 97 comments), Reddit r/MachineLearning (247 points, 27 comments) Translations: German, Korean, Chinese (Simplified), Russian, Turkish
The tech world is abuzz with GPT3 hype. Massive language models (like GPT3) are starting to surprise us with their abilities. While not yet completely reliable for most businesses to put in front of their customers, these models are showing sparks of cleverness that are sure to accelerate the march of automation and the possibilities of intelligent computer systems. Let’s remove the aura of mystery around GPT3 and learn how it’s trained and how it works.
A trained language model generates text.
We can optionally pass it some text as input, which influences its output.
The output is generated from what the model “learned” during its training period where it scanned vast amounts of text.
Training is the process of exposing the model to lots of text. That process has been completed. All the experiments you see now are from that one trained model. It was estimated to cost 355 GPU years and cost $4.6m.
The dataset of 300 billion tokens of text is used to generate training examples for the model. For example, these are three training examples generated from the one sentence at the top.
You can see how you can slide a window across all the text and make lots of examples.
The model is presented with an example. We only show it the features and ask it to predict the next word.
The model’s prediction will be wrong. We calculate the error in its prediction and update the model so next time it makes a better prediction.
Repeat millions of times
Now let’s look at these same steps with a bit more detail.
GPT3 actually generates output one token at a time (let’s assume a token is a word for now).
Please note: This is a description of how GPT-3 works and not a discussion of what is novel about it (which is mainly the ridiculously large scale). The architecture is a transformer decoder model based on this paper https://arxiv.org/pdf/1801.10198.pdf
GPT3 is MASSIVE. It encodes what it learns from training in 175 billion numbers (called parameters). These numbers are used to calculate which token to generate at each run.
The untrained model starts with random parameters. Training finds values that lead to better predictions.
These numbers are part of hundreds of matrices inside the model. Prediction is mostly a lot of matrix multiplication.
In my Intro to AI on YouTube, I showed a simple ML model with one parameter. A good start to unpack this 175B monstrosity.
To shed light on how these parameters are distributed and used, we’ll need to open the model and look inside.
GPT3 is 2048 tokens wide. That is its “context window”. That means it has 2048 tracks along which tokens are processed.
Let’s follow the purple track. How does a system process the word “robotics” and produce “A”?
High-level steps:
Convert the word to a vector (list of numbers) representing the word
Compute prediction
Convert resulting vector to word
The important calculations of the GPT3 occur inside its stack of 96 transformer decoder layers.
See all these layers? This is the “depth” in “deep learning”.
Each of these layers has its own 1.8B parameter to make its calculations. That is where the “magic” happens. This is a high-level view of that process:
You can see a detailed explanation of everything inside the decoder in my blog post The Illustrated GPT2.
The difference with GPT3 is the alternating dense and sparse self-attention layers.
This is an X-ray of an input and response (“Okay human”) within GPT3. Notice how every token flows through the entire layer stack. We don’t care about the output of the first words. When the input is done, we start caring about the output. We feed every word back into the model.
In the React code generation example, the description would be the input prompt (in green), in addition to a couple of examples of description=>code, I believe. And the react code would be generated like the pink tokens here token after token.
My assumption is that the priming examples and the description are appended as input, with specific tokens separating examples and the results. Then fed into the model.
It’s impressive that this works like this. Because you just wait until fine-tuning is rolled out for the GPT3. The possibilities will be even more amazing.
Fine-tuning actually updates the model’s weights to make the model better at a certain task.












関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み