我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。
回顧我們在AI說書 - 從0開始 - 7,已經完成Colab Python環境配置。
針對Attention Layer的程式配置為:
start_time = time.time()
for i in range(n):
for j in range(n):
_ = np.dot(input_seq[i], input_seq[j])
atention_time = time.time() - start_time
print(f"Self-attention computation time: {time.time() - start_time} seconds")
對此,執行結果為:
而針對Recurrent Layer的程式配置為:
start_time = time.time()
hidden_state = np.zeros(d)
for i in range(n):
for j in range(d):
for k in range(d):
hidden_state[j] += input_seq[i, j] * hidden_state[k]
recurrent_time = time.time() - start_time
print(f"Recurrent layer computation time: {time.time() - start_time} seconds")
對此,執行結果為:
最後我們做百分比比較:
total = atention_time + recurrent_time
parcentage_attention_time = round((atention_time / total) * 100, 2)
print(f"The percentage of 'computational time for attention' in the sum of 'attention' and 'recurrent' is {parcentage_attention_time}%")
對此,執行結果為:
綜上知:針對CPU這種運算資源,Attention Layer比Recurrent Layer更有優勢。