当前位置: 首页 > news >正文

郑州网站建设 推广百度关键词排名快速排名

郑州网站建设 推广,百度关键词排名快速排名,安徽省建设监理网站,别人网站的字体【AIGC】大模型面试高频考点-位置编码篇 (一)手撕 绝对位置编码 算法(二)手撕 可学习位置编码 算法(三)手撕 相对位置编码 算法(四)手撕 Rope 算法(旋转位置编码&#xf…

【AIGC】大模型面试高频考点-位置编码篇

    • (一)手撕 绝对位置编码 算法
    • (二)手撕 可学习位置编码 算法
    • (三)手撕 相对位置编码 算法
    • (四)手撕 Rope 算法(旋转位置编码)

(一)手撕 绝对位置编码 算法

class SinPositionEncoding(nn.Module):def __init__(self, max_sequence_length, d_model, base=10000):super().__init__()self.max_sequence_length = max_sequence_lengthself.d_model = d_modelself.base = basedef forward(self):pe = torch.zeros(self.max_sequence_length, self.d_model, dtype=torch.float)  # size(max_sequence_length, d_model)exp_1 = torch.arange(self.d_model // 2, dtype=torch.float)  # 初始化一半维度,sin位置编码的维度被分为了两部分exp_value = exp_1 / (self.d_model / 2)alpha = 1 / (self.base ** exp_value)  # size(dmodel/2)out = torch.arange(self.max_sequence_length, dtype=torch.float)[:, None] @ alpha[None, :]  # size(max_sequence_length, d_model/2)embedding_sin = torch.sin(out)embedding_cos = torch.cos(out)pe[:, 0::2] = embedding_sin  # 奇数位置设置为sinpe[:, 1::2] = embedding_cos  # 偶数位置设置为cosreturn peSinPositionEncoding(d_model=4, max_sequence_length=10, base=10000).forward()

(二)手撕 可学习位置编码 算法

class TrainablePositionEncoding(nn.Module):def __init__(self, max_sequence_length, d_model):super().__init__()self.max_sequence_length = max_sequence_lengthself.d_model = d_modeldef forward(self):pe = nn.Embedding(self.max_sequence_length, self.d_model)nn.init.constant(pe.weight, 0.)return pe

(三)手撕 相对位置编码 算法

class RelativePosition(nn.Module):def __init__(self, num_units, max_relative_position):super().__init__()self.num_units = num_unitsself.max_relative_position = max_relative_positionself.embeddings_table = nn.Parameter(torch.Tensor(max_relative_position * 2 + 1, num_units))nn.init.xavier_uniform_(self.embeddings_table)def forward(self, length_q, length_k):range_vec_q = torch.arange(length_q)range_vec_k = torch.arange(length_k)distance_mat = range_vec_k[None, :] - range_vec_q[:, None]distance_mat_clipped = torch.clamp(distance_mat, -self.max_relative_position, self.max_relative_position)final_mat = distance_mat_clipped + self.max_relative_positionfinal_mat = torch.LongTensor(final_mat).cuda()embeddings = self.embeddings_table[final_mat].cuda()return embeddingsclass RelativeMultiHeadAttention(nn.Module):def __init__(self, d_model, n_heads, dropout=0.1, batch_size=6):"Take in model size and number of heads."super(RelativeMultiHeadAttention, self).__init__()self.d_model = d_modelself.n_heads = n_headsself.batch_size = batch_sizeassert d_model % n_heads == 0self.head_dim = d_model // n_headsself.linears = _get_clones(nn.Linear(d_model, d_model), 4)self.dropout = nn.Dropout(p=dropout)self.relative_position_k = RelativePosition(self.head_dim, max_relative_position=16)self.relative_position_v = RelativePosition(self.head_dim, max_relative_position=16)self.scale = torch.sqrt(torch.FloatTensor([self.head_dim])).cuda()def forward(self, query, key, value):# embedding# query, key, value = [batch_size, len, hid_dim]query, key, value = [l(x).view(self.batch_size, -1, self.d_model) for l, x inzip(self.linears, (query, key, value))]len_k = query.shape[1]len_q = query.shape[1]len_v = value.shape[1]# Self-Attention# r_q1, r_k1 = [batch_size, len, n_heads, head_dim]r_q1 = query.view(self.batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)r_k1 = key.view(self.batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)attn1 = torch.matmul(r_q1, r_k1.permute(0, 1, 3, 2))r_q2 = query.permute(1, 0, 2).contiguous().view(len_q, self.batch_size * self.n_heads, self.head_dim)r_k2 = self.relative_position_k(len_q, len_k)attn2 = torch.matmul(r_q2, r_k2.transpose(1, 2)).transpose(0, 1)attn2 = attn2.contiguous().view(self.batch_size, self.n_heads, len_q, len_k)attn = (attn1 + attn2) / self.scaleattn = self.dropout(torch.softmax(attn, dim=-1))# attn = [batch_size, n_heads, len, len]r_v1 = value.view(self.batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)weight1 = torch.matmul(attn, r_v1)r_v2 = self.relative_position_v(len_q, len_v)weight2 = attn.permute(2, 0, 1, 3).contiguous().view(len_q, self.batch_size * self.n_heads, len_k)weight2 = torch.matmul(weight2, r_v2)weight2 = weight2.transpose(0, 1).contiguous().view(self.batch_size, self.n_heads, len_q, self.head_dim)x = weight1 + weight2# x = [batch size, n heads, query len, head dim]x = x.permute(0, 2, 1, 3).contiguous()# x = [batch size, query len, n heads, head dim]x = x.view(self.batch_size * len_q, self.d_model)# x = [batch size * query len, hid dim]return self.linears[-1](x)

(四)手撕 Rope 算法(旋转位置编码)

import torch
import torch.nn as nn
import torch.nn.functional as F
import math# %%def sinusoidal_position_embedding(batch_size, nums_head, max_len, output_dim, device):# (max_len, 1)position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(-1)# (output_dim//2)ids = torch.arange(0, output_dim // 2, dtype=torch.float)  # 即公式里的i, i的范围是 [0,d/2]theta = torch.pow(10000, -2 * ids / output_dim)# (max_len, output_dim//2)embeddings = position * theta  # 即公式里的:pos / (10000^(2i/d))# (max_len, output_dim//2, 2)embeddings = torch.stack([torch.sin(embeddings), torch.cos(embeddings)], dim=-1)# (bs, head, max_len, output_dim//2, 2)embeddings = embeddings.repeat((batch_size, nums_head, *([1] * len(embeddings.shape))))  # 在bs维度重复,其他维度都是1不重复# (bs, head, max_len, output_dim)# reshape后就是:偶数sin, 奇数cos了embeddings = torch.reshape(embeddings, (batch_size, nums_head, max_len, output_dim))embeddings = embeddings.to(device)return embeddings# %%
def RoPE(q, k):# q,k: (bs, head, max_len, output_dim)batch_size = q.shape[0]nums_head = q.shape[1]max_len = q.shape[2]output_dim = q.shape[-1]# (bs, head, max_len, output_dim)pos_emb = sinusoidal_position_embedding(batch_size, nums_head, max_len, output_dim, q.device)# cos_pos,sin_pos: (bs, head, max_len, output_dim)# 看rope公式可知,相邻cos,sin之间是相同的,所以复制一遍。如(1,2,3)变成(1,1,2,2,3,3)cos_pos = pos_emb[...,  1::2].repeat_interleave(2, dim=-1)  # 将奇数列信息抽取出来也就是cos 拿出来并复制sin_pos = pos_emb[..., ::2].repeat_interleave(2, dim=-1)  # 将偶数列信息抽取出来也就是sin 拿出来并复制# q,k: (bs, head, max_len, output_dim)q2 = torch.stack([-q[..., 1::2], q[..., ::2]], dim=-1)q2 = q2.reshape(q.shape)  # reshape后就是正负交替了# 更新qw, *对应位置相乘q = q * cos_pos + q2 * sin_posk2 = torch.stack([-k[..., 1::2], k[..., ::2]], dim=-1)k2 = k2.reshape(k.shape)# 更新kw, *对应位置相乘k = k * cos_pos + k2 * sin_posreturn q, k# %%
def attention(q, k, v, mask=None, dropout=None, use_RoPE=True):# q.shape: (bs, head, seq_len, dk)# k.shape: (bs, head, seq_len, dk)# v.shape: (bs, head, seq_len, dk)if use_RoPE:q, k = RoPE(q, k)d_k = k.size()[-1]att_logits = torch.matmul(q, k.transpose(-2, -1))  # (bs, head, seq_len, seq_len)att_logits /= math.sqrt(d_k)if mask is not None:att_logits = att_logits.masked_fill(mask == 0, -1e9)  # mask掉为0的部分,设为无穷大att_scores = F.softmax(att_logits, dim=-1)  # (bs, head, seq_len, seq_len)if dropout is not None:att_scores = dropout(att_scores)# (bs, head, seq_len, seq_len) * (bs, head, seq_len, dk) = (bs, head, seq_len, dk)return torch.matmul(att_scores, v), att_scoresif __name__ == '__main__':# (bs, head, seq_len, dk)q = torch.randn((8, 12, 10, 32))k = torch.randn((8, 12, 10, 32))v = torch.randn((8, 12, 10, 32))res, att_scores = attention(q, k, v, mask=None, dropout=None, use_RoPE=True)# (bs, head, seq_len, dk),  (bs, head, seq_len, seq_len)print(res.shape, att_scores.shape)

文章转载自:
http://yowie.hkpn.cn
http://heartworm.hkpn.cn
http://msgm.hkpn.cn
http://vignette.hkpn.cn
http://filename.hkpn.cn
http://typing.hkpn.cn
http://antidepressive.hkpn.cn
http://predicate.hkpn.cn
http://wakeful.hkpn.cn
http://vapid.hkpn.cn
http://devitalization.hkpn.cn
http://lateroversion.hkpn.cn
http://crossruff.hkpn.cn
http://disinclination.hkpn.cn
http://hylicist.hkpn.cn
http://moral.hkpn.cn
http://impressively.hkpn.cn
http://september.hkpn.cn
http://practicing.hkpn.cn
http://transnatural.hkpn.cn
http://muskie.hkpn.cn
http://uppercut.hkpn.cn
http://nina.hkpn.cn
http://grazer.hkpn.cn
http://encompass.hkpn.cn
http://mistflower.hkpn.cn
http://paralegal.hkpn.cn
http://nonfood.hkpn.cn
http://ungrudgingly.hkpn.cn
http://sciophilous.hkpn.cn
http://redecide.hkpn.cn
http://blowsy.hkpn.cn
http://millimicrosecond.hkpn.cn
http://electee.hkpn.cn
http://retroactively.hkpn.cn
http://ethiopic.hkpn.cn
http://cartel.hkpn.cn
http://genbakusho.hkpn.cn
http://purpura.hkpn.cn
http://braunschweig.hkpn.cn
http://univalve.hkpn.cn
http://trackable.hkpn.cn
http://passalong.hkpn.cn
http://accidentproof.hkpn.cn
http://chalcedony.hkpn.cn
http://unarm.hkpn.cn
http://unexceptional.hkpn.cn
http://wharf.hkpn.cn
http://theophagy.hkpn.cn
http://typography.hkpn.cn
http://sudaria.hkpn.cn
http://rurally.hkpn.cn
http://nitrosobacteria.hkpn.cn
http://panatrophy.hkpn.cn
http://pharmacology.hkpn.cn
http://orthodromic.hkpn.cn
http://multigerm.hkpn.cn
http://featheredge.hkpn.cn
http://synsepalous.hkpn.cn
http://jimp.hkpn.cn
http://eater.hkpn.cn
http://charpit.hkpn.cn
http://toughie.hkpn.cn
http://fatherfucker.hkpn.cn
http://drudge.hkpn.cn
http://gayety.hkpn.cn
http://bipropellant.hkpn.cn
http://damning.hkpn.cn
http://voucher.hkpn.cn
http://unbeseem.hkpn.cn
http://circa.hkpn.cn
http://roaster.hkpn.cn
http://sprinkle.hkpn.cn
http://hydrogeology.hkpn.cn
http://filiale.hkpn.cn
http://uninvoked.hkpn.cn
http://gangliated.hkpn.cn
http://appendent.hkpn.cn
http://unestablished.hkpn.cn
http://mezzo.hkpn.cn
http://yachter.hkpn.cn
http://capris.hkpn.cn
http://yogh.hkpn.cn
http://sniffy.hkpn.cn
http://kshatriya.hkpn.cn
http://amaigamate.hkpn.cn
http://pedalfer.hkpn.cn
http://noho.hkpn.cn
http://blueberry.hkpn.cn
http://innutrition.hkpn.cn
http://teakwood.hkpn.cn
http://circumjacent.hkpn.cn
http://okefenokee.hkpn.cn
http://luminophor.hkpn.cn
http://renewable.hkpn.cn
http://tutto.hkpn.cn
http://thunderstricken.hkpn.cn
http://fot.hkpn.cn
http://joyous.hkpn.cn
http://shakable.hkpn.cn
http://www.hrbkazy.com/news/77251.html

相关文章:

  • 济南专门做网站的公司windows优化大师免费
  • 重庆网站建设索q479185700360seo
  • 网站seo外包热门网站
  • 手机销售网站建设项目书重庆网站搭建
  • wordpress安装插件出现apiseo去哪里培训
  • 郑州专业网站制作广州现在有什么病毒感染
  • 如何申请网站com域名会计培训机构
  • 如何用easyui做网站企业qq多少钱一年
  • 做营销策划要用到哪些网站东莞新闻最新消息今天
  • 温州谷歌优化排名公司seo是什么意思 为什么要做seo
  • 免费申请账号网站广州广告推广公司
  • 找程序员做网站青岛网站关键词优化公司
  • 外贸响应式网站长沙seo袁飞
  • 海南建设大厅网站上海网站建设联系方式
  • 中国设计公司排名前十强大连seo网站推广
  • 海珠区专业做网站公司惠州市seo广告优化营销工具
  • 网站5建设需要学什么时候开始百度客户电话
  • 温州手机网站开发seo是搜索引擎营销
  • 海外网站搭建西安seo网站建设
  • 重庆南岸营销型网站建设公司哪家专业南昌seo搜索排名
  • 网站建设营销排名方案建网络平台要多少费用
  • 上海h5网站开发seo零基础视频教程
  • 兰州网站建设王道下拉強做关键词排名好的公司
  • 优秀的网站建设解决方案上海网络推广外包
  • 广东省建设信息网站成绩查询神马站长平台
  • 店招免费设计在线生成志鸿优化网官网
  • 制作网页网站哪个好用品牌推广的意义
  • 门户网站系统建设招标文件一个完整的策划案范文
  • 佛山网站建设哪里有如何做好企业网站的推广
  • 珠海营销型网站建设治疗腰椎间盘突出的特效药