欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 【更新】cyのMemo(20240609~)

【更新】cyのMemo(20240609~)

2024/10/24 12:27:45 来源:https://blog.csdn.net/CY19980216/article/details/139551143  浏览:    关键词:【更新】cyのMemo(20240609~)

序言

节后首日,中期结束,接下来一切即将如期而至,时光像道皮鞭在赶鸭子上架,但似乎还并没有准备好一切。

最近几日,休息得不是很好,状态跌落,但总之手头事情告一段落,可以好好休整几日,韬光养晦。

群里静了许多,大家都很忙碌。

筵席终散,然,命运的交点将于未来邂逅。

有感,且歌一场。

《五月初五登高赋》 囚生
端午登高祈福佑,轻语绕梁疾未休。
花开叶落人渐朽,日没月升活似囚。
丛木泥径盘峰秀,远岫倩影碧空游。
君不见月圆常有,空太息圆月难留。


文章目录

  • 序言
  • 20240611


20240611

第一层和第一个头的值权重矩阵如下所示。

v_layer0_head0 = v_layer0 [0] v_layer0_head0.shape
# torch.Size ([128, 4096])

现在使用值权重来获取每个 token 的注意力值,其大小为 [17x128],其中 17 为提示中的 token 数,128 为每个 token 的值向量维数。

v_per_token = torch.matmul (token_embeddings, v_layer0_head0.T)v_per_token.shape
# torch.Size ([17, 128])

与每个 token 的值相乘后得到的注意力向量的形状为 [17*128]。

qkv_attention = torch.matmul (qk_per_token_after_masking_after_softmax, v_per_token) qkv_attention.shape
# torch.Size ([17, 128])

现在有了第一层和第一个头的注意力值。

接下来运行一个循环并执行与上面单元完全相同的数学运算,不过第一层中的每个头除外。

qkv_attention_store = []
for head in range (n_heads):q_layer0_head = q_layer0 [head]k_layer0_head = k_layer0 [head//4] # key weights are shared across 4 heads
v_layer0_head = v_layer0 [head//4] # value weights are shared across 4 heads
q_per_token = torch.matmul (token_embeddings, q_layer0_head.T)k_per_token = torch.matmul (token_embeddings, k_layer0_head.T)v_per_token = torch.matmul (token_embeddings, v_layer0_head.T)q_per_token_split_into_pairs = q_per_token.float ().view (q_per_token.shape [0], -1, 2)q_per_token_as_complex_numbers = torch.view_as_complex (q_per_token_split_into_pairs)q_per_token_split_into_pairs_rotated = torch.view_as_real (q_per_token_as_complex_numbers * freqs_cis [:len (tokens)])q_per_token_rotated = q_per_token_split_into_pairs_rotated.view (q_per_token.shape)k_per_token_split_into_pairs = k_per_token.float ().view (k_per_token.shape [0], -1, 2)k_per_token_as_complex_numbers = torch.view_as_complex (k_per_token_split_into_pairs)k_per_token_split_into_pairs_rotated = torch.view_as_real (k_per_token_as_complex_numbers * freqs_cis [:len (tokens)])k_per_token_rotated = k_per_token_split_into_pairs_rotated.view (k_per_token.shape)qk_per_token = torch.matmul (q_per_token_rotated, k_per_token_rotated.T)/(128)**0.5
mask = torch.full ((len (tokens), len (tokens)), float ("-inf"), device=tokens.device)mask = torch.triu (mask, diagonal=1)qk_per_token_after_masking = qk_per_token + mask
qk_per_token_after_masking_after_softmax = torch.nn.functional.softmax (qk_per_token_after_masking, dim=1).to (torch.bfloat16)qkv_attention = torch.matmul (qk_per_token_after_masking_after_softmax, v_per_token)qkv_attention = torch.matmul (qk_per_token_after_masking_after_softmax, v_per_token)qkv_attention_store.append (qkv_attention)
len (qkv_attention_store)
# 32

现在第一层上的所有 32 个头都有了 qkv_attention 矩阵,并在快结束的时候将所有注意力分数合并为一个大小为 [17x4096] 的大矩阵。

stacked_qkv_attention = torch.cat (qkv_attention_store, dim=-1) stacked_qkv_attention.shape
# torch.Size ([17, 4096])

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com