2024-06-12|閱讀時間 ‧ 約 23 分鐘

AI說書 - 從0開始 - 12

我想要一天分享一點「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)


我們已經用GPU當作運算資源,得出Attention Layer比Recurrent Layer更有優勢,結論彙整於AI說書 - 從0開始 - 10


現在目標是想要用Python來做模擬,且使用TPU當作運算資源。TPU是Google設計專門用來執行矩陣乘法、神經網路運算的設備,因此對Recurrent Layer的序列式運算較不擅長。


延續使用Meta釋出的模型,實作Chat GPT - Part 0的Google Colab設定,我們開始做環境設定:

針對Attention Layer的程式配置為:

import tensorflow as tf
import numpy as np
import time

n = 512
d = 512

input_seq = tf.random.normal((n, d), dtype = tf.float32)

start_time = time.time()
_ = tf.matmul(input_seq, input_seq, transpose_b = True)
attention_time = time.time() - start_time
print(f"Self-attention computation time: {attention_time} seconds")

對此,執行結果為:


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