2024-07-01|閱讀時間 ‧ 約 26 分鐘

AI說書 - 從0開始 - 68

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


仔細看 AI說書 - 從0開始 - 66 中,Decoder 的 Multi-Head Attention 框框,會發現有一條線空接,其實它是有意義的,之所以空接,是因為它與 Encoder 對接,全貌如下:

  • 在 Decoder 的 Attention 機制稱為 Cross Attention,其和 Encoder 的 Self-Attetion 做出區隔
  • 此處的 Corss Attention 的機制,有兩個元素從 Encoder 而來 (藍色圈圈),一個元素從 Decoder 自身而來 (綠色圈圈)
  • 下一張圖講述 Cross Attention 機制的運作方式




Cross Attention 機制的運作方式如下:

  • Begin Token 經過 Mask Attention 得到一向量,再乘上 Wq 變成向量 q
  • a1、a2、a3 各別乘上 Wk 與 Wv 變成 k1、k2、k3 v1、v2、v3 ,注意這裡是 Encoder 地盤
  • q 乘上 k1、k2、k3 再經過正規化得 a'1、a'2、a'3
  • a'1、a'2、a'3 各自乘上 v1、v2、v3 相加後得 v
  • 這是第一個字的作法


那第二個字的作法如下:

  • Begin Token 與 「機」經過 Mask Attention 得到一向量,再乘上 Wq 變成向量 q'
  • a1、a2、a3 各別乘上 Wk 與 Wv 變成 k1、k2、k3 v1、v2、v3 ,注意這裡是 Encoder 地盤
  • q' 乘上 k1、k2、k3 再經過正規化得 a'1、a'2、a'3
  • a'1、a'2、a'3 各自乘上 v1、v2、v3 相加後得 v'
  • 這是第二個字的作法


最後列出幾段課本 (Transformers for Natural Language Processing and Computer Vision, 2024) 很美的話,當作知識點整理:

  • The multi-head attention sublayer 2 also only attends to the positions up to the current position the Transformer is predicting to avoid seeing the sequence it must predict.
  • The multi-head attention sublayer 2 draws information from the encoder by taking encoder (K, V) into account during the dot-product attention operations. This sublayer also draws information from the masked multi-head attention sublayer 1 (masked attention) by also taking sublayer 1 (Q) into account during the dot-product attention operations.
  • The linear layer produces an output sequence with a linear function that varies per model but relies on the standard method: y = wx + b.
  • At the top layer of the decoder, the transformer will reach the output layer, which will map the outputs of the model to the size of the vocabulary to produce the raw logits of the prediction.
  • The raw logits of the output can go through a softmax function, apply the values obtained to the tokens in the vocabulary, and choose the best probable token for the task requested, or apply sampling functions.
  • The Transformer produces an output sequence of only one element at a time.
分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.