2024-06-14|閱讀時間 ‧ 約 24 分鐘

AI說書 - 從0開始 - 15

我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。


回顧我們在AI說書 - 從0開始 - 6中說當Context長度是n,且每個字用d維度的向量表示時有以下結論:

  • Attention Layer的複雜度是O(n^2 * d)
  • Recurrent Layer的複雜度是O(d^2 * n)

這些資料出自於ChatGPT的關鍵技術 - Transformer的原始Google論文:Attention is All You Need, Vaswani et al. (2017)


我們已經用以下各種運算資源,得出Attention Layer比Recurrent Layer運算還快的結論:

  1. CPU執行Attention Layer: AI說書 - 從0開始 - 8
  2. CPU執行Recurrent Layer: AI說書 - 從0開始 - 8
  3. GPU執行Attention Layer: AI說書 - 從0開始 - 10
  4. GPU執行Recurrent Layer: AI說書 - 從0開始 - 10
  5. TPU執行Attention Layer: AI說書 - 從0開始 - 12
  6. TPU執行Recurrent Layer: AI說書 - 從0開始 - 13


綜上,有以下結論:

  • 透過避免Recurrence,Attention Layer比Recurrent Layer運算還快
  • Attention Layer的「One-to-One Word Analysis」使其能「偵測長句子的相依性」
  • Attention Layer的「Matrix Multiplication」充分利用了GPU與TPU的運算優勢
  • Attention Layer的「Matrix Multiplication」釋放了GPU與TPU能力,使其能執行更多運算、學習更多資訊


那明明自然語言處理之前是RNN的天下,怎麼後來變成是Transformer的天下了呢?關鍵分析如下:

  • RNN的「Recurrent Functionality」使其面臨句子很長時會忘記前面的字
  • Transformer的核心關鍵是「Mixing Tokens」,完整原文詮釋是「Transformers do not analyze tokens in sequences but relate every token to the other tokens in a sequence」,示意圖如下:

圖片出自書籍:Transformers for Natural Language Processing and Computer Vision, Denis Rothman, 2024.

分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.