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

网站建设设计哪个济南兴田德润简介零基础自己建网站

网站建设设计哪个济南兴田德润简介,零基础自己建网站,网络营销管理系统,做网站都要多少钱最近这一两周看到不少互联网公司都已经开始秋招发放Offer。 不同以往的是#xff0c;当前职场环境已不再是那个双向奔赴时代了。求职者在变多#xff0c;HC 在变少#xff0c;岗位要求还更高了。 最近#xff0c;我们又陆续整理了很多大厂的面试题#xff0c;帮助一些球…最近这一两周看到不少互联网公司都已经开始秋招发放Offer。 不同以往的是当前职场环境已不再是那个双向奔赴时代了。求职者在变多HC 在变少岗位要求还更高了。 最近我们又陆续整理了很多大厂的面试题帮助一些球友解惑答疑分享技术面试中的那些弯弯绕绕。 《大模型面试宝典》(2024版) 正式发布 喜欢本文记得收藏、关注、点赞。更多实战和面试交流文末加入我们 技术交流 本文基于 llama 模型的源码学习相对位置编码的实现方法本文不细究绝对位置编码和相对位置编码的数学原理。 大模型新人在学习中容易困惑的几个问题 为什么一定要在 transformer 中使用位置编码 相对位置编码在 llama 中是怎么实现的 大模型的超长文本预测和位置编码有什么关系 01 为什么需要位置编码 很多初学者都会读到这样一句话transformer 使用位置编码的原因是它不具备位置信息。大家都只把这句话当作公理却很少思考这句话到底是什么意思 这句话的意思是如果没有位置编码那么 “床前明月”、“前床明月”、“前明床月” 这几个输入会预测出完全一样的文本。 也就是说不管你输入的 prompt 顺序是什么只要 prompt 的文本是相同的那么模型 decode 的文本就只取决于 prompt 的最后一个 token。 import torch from torch import nn import mathbatch 1 dim 10 num_head 2 embedding nn.Embedding(5, dim) q_matrix nn.Linear(dim, dim, biasFalse) k_matrix nn.Linear(dim, dim, biasFalse) v_matrix nn.Linear(dim, dim, biasFalse)x embedding(torch.tensor([1,2,3])).unsqueeze(0) y embedding(torch.tensor([2,1,3])).unsqueeze(0)def attention(input):q q_matrix(input).view(batch, -1, num_head, dim // num_head).transpose(1, 2)k k_matrix(input).view(batch, -1, num_head, dim // num_head).transpose(1, 2)v v_matrix(input).view(batch, -1, num_head, dim // num_head).transpose(1, 2)attn_weights torch.matmul(q, k.transpose(2, 3)) / math.sqrt(dim // num_head)attn_weights nn.functional.softmax(attn_weights, dim-1)outputs torch.matmul(attn_weights, v).transpose(1, 2).reshape(1, len([1,2,3]), dim)print(outputs)attention(x) attention(y)执行上面的代码会发现虽然 x 和 y 交换了第一个 token 和第二个 token 的输入顺序但是第三个 token 的计算结果完全没有发生改变那么模型预测第四个 token 时便会得到相同的结果。 如果有读者对矩阵运算感到混淆的话可以看看下面的简单推导 可以看出当第一个 token 与第二个 token 交换顺序后模型输出矩阵的第一维和第二维也交换了顺序但输出的值完全没有变化。 第三个 token 的输出结果也是完全没有受到影响这也就是前面说的如果没有位置编码模型 decode 的文本就只取决于 prompt 的最后一个 token。 不过需要注意的是由于 attention_mask 的存在前置位 token 看不到后置位 token所以即使不加位置编码transformer 的输出还是会受到 token 的位置影响。 02 相对位置编码的实现 我们以 modeling_llama.py 的源码为例来学习相对位置编码的实现方法。 class LlamaRotaryEmbedding(torch.nn.Module):def __init__(self, dim, max_position_embeddings2048, base10000, deviceNone):super().__init__()inv_freq 1.0 / (base ** (torch.arange(0, dim, 2).float().to(device) / dim))self.register_buffer(inv_freq, inv_freq)# Build here to make torch.jit.trace work.self.max_seq_len_cached max_position_embeddingst torch.arange(self.max_seq_len_cached, deviceself.inv_freq.device, dtypeself.inv_freq.dtype)freqs torch.einsum(i,j-ij, t, self.inv_freq)# Different from paper, but it uses a different permutation in order to obtain the same calculationemb torch.cat((freqs, freqs), dim-1)self.register_buffer(cos_cached, emb.cos()[None, None, :, :], persistentFalse)self.register_buffer(sin_cached, emb.sin()[None, None, :, :], persistentFalse)def forward(self, x, seq_lenNone):# x: [bs, num_attention_heads, seq_len, head_size]# This if block is unlikely to be run after we build sin/cos in __init__. Keep the logic here just in case.if seq_len self.max_seq_len_cached:self.max_seq_len_cached seq_lent torch.arange(self.max_seq_len_cached, devicex.device, dtypeself.inv_freq.dtype)freqs torch.einsum(i,j-ij, t, self.inv_freq)# Different from paper, but it uses a different permutation in order to obtain the same calculationemb torch.cat((freqs, freqs), dim-1).to(x.device)self.register_buffer(cos_cached, emb.cos()[None, None, :, :], persistentFalse)self.register_buffer(sin_cached, emb.sin()[None, None, :, :], persistentFalse)return (self.cos_cached[:, :, :seq_len, ...].to(dtypex.dtype),self.sin_cached[:, :, :seq_len, ...].to(dtypex.dtype),)def rotate_half(x):Rotates half the hidden dims of the input.x1 x[..., : x.shape[-1] // 2]x2 x[..., x.shape[-1] // 2 :]return torch.cat((-x2, x1), dim-1)def apply_rotary_pos_emb(q, k, cos, sin, position_ids):# The first two dimensions of cos and sin are always 1, so we can squeeze them.cos cos.squeeze(1).squeeze(0) # [seq_len, dim]sin sin.squeeze(1).squeeze(0) # [seq_len, dim]cos cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]sin sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]q_embed (q * cos) (rotate_half(q) * sin)k_embed (k * cos) (rotate_half(k) * sin)return q_embed, k_embed相对位置编码在 attention 中的应用方法如下 self.rotary_emb LlamaRotaryEmbedding(self.head_dim, max_position_embeddingsself.max_position_embeddings) cos, sin self.rotary_emb(value_states, seq_lenkv_seq_len)query_states, key_states apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)if past_key_value is not None:# reuse k, v, self_attentionkey_states torch.cat([past_key_value[0], key_states], dim1)value_states torch.cat([past_key_value[1], value_states], dim1) 根据 value_states 矩阵的形状去调取 cos 和 sin 两个 tensor cos 与 sin 的维度均是 batch_size * head_num * seq_len * head_dim 利用 apply_rotary_pos_emb 去修改 query_states 和 key_states 两个 tensor得到新的 qk 矩阵 需要注意的是在解码时position_ids 的长度是和输入 token 的长度保持一致的prompt 是 4 个 token 的话。 第一次解码时position_ids: tensor([[0, 1, 2, 3]], device‘cuda:0’)q 矩阵与 k 矩阵的相对位置编码信息通过 apply_rotary_pos_emb() 获得 第二次解码时position_ids: tensor([[4]], device‘cuda:0’)当前 token 的相对位置编码信息通过 apply_rotary_pos_emb() 获得。 前 4 个 token 的相对位置编码信息则是通过 key_states torch.cat([past_key_value[0], key_states], dim1) 集成到 k 矩阵中 …… …… 以上代码的公式均可以从苏神原文中找到。 这些代码可以从 llama 模型中剥离出来直接执行如果感到困惑可以像下面一样将 apply_rotary_pos_emb() 的整个过程给 print 出来观察一下 head_num, head_dim, kv_seq_len 8, 20, 5 position_ids torch.tensor([[0, 1, 2, 3, 4]]) query_states torch.randn(1, head_dim, kv_seq_len, head_dim) key_states torch.randn(1, head_dim, kv_seq_len, head_dim) value_states torch.randn(1, head_dim, kv_seq_len, head_dim) rotary_emb LlamaRotaryEmbedding(head_dim) cos, sin rotary_emb(value_states, seq_lenkv_seq_len) print(cos, sin) query_states, key_states apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)03 位置编码与长度外推 长度外推指的是大模型在训练的只见过长度为 X 的文本但在实际应用时却有如下情况 我们假设 X 的取值为 4096那么也就意味着模型自始至终没有见到过 pos_id 4096 的位置编码进而导致模型的预测结果完全不可控。 因此解决长度外推问题的关键便是如何让模型见到比训练文本更长的位置编码。 以上关于文本外推的介绍均是比较大白话的理解只是为了强调位置编码很重要这一观点。
文章转载自:
http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn
http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn
http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn
http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn
http://www.morning.ybgyz.cn.gov.cn.ybgyz.cn
http://www.morning.qddtd.cn.gov.cn.qddtd.cn
http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn
http://www.morning.qtqjx.cn.gov.cn.qtqjx.cn
http://www.morning.kehejia.com.gov.cn.kehejia.com
http://www.morning.dbphz.cn.gov.cn.dbphz.cn
http://www.morning.fllx.cn.gov.cn.fllx.cn
http://www.morning.gyylt.cn.gov.cn.gyylt.cn
http://www.morning.rfqk.cn.gov.cn.rfqk.cn
http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn
http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn
http://www.morning.kdrly.cn.gov.cn.kdrly.cn
http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn
http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn
http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn
http://www.morning.ujianji.com.gov.cn.ujianji.com
http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn
http://www.morning.stbfy.cn.gov.cn.stbfy.cn
http://www.morning.sgqw.cn.gov.cn.sgqw.cn
http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn
http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn
http://www.morning.ndynz.cn.gov.cn.ndynz.cn
http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn
http://www.morning.rdpps.cn.gov.cn.rdpps.cn
http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn
http://www.morning.pctsq.cn.gov.cn.pctsq.cn
http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn
http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn
http://www.morning.bkqw.cn.gov.cn.bkqw.cn
http://www.morning.bgygx.cn.gov.cn.bgygx.cn
http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn
http://www.morning.kongpie.com.gov.cn.kongpie.com
http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn
http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn
http://www.morning.fpyll.cn.gov.cn.fpyll.cn
http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn
http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn
http://www.morning.mmosan.com.gov.cn.mmosan.com
http://www.morning.nknt.cn.gov.cn.nknt.cn
http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn
http://www.morning.tjsxx.cn.gov.cn.tjsxx.cn
http://www.morning.pznhn.cn.gov.cn.pznhn.cn
http://www.morning.wcczg.cn.gov.cn.wcczg.cn
http://www.morning.nrchx.cn.gov.cn.nrchx.cn
http://www.morning.msbct.cn.gov.cn.msbct.cn
http://www.morning.qnftc.cn.gov.cn.qnftc.cn
http://www.morning.nmfxs.cn.gov.cn.nmfxs.cn
http://www.morning.chrbp.cn.gov.cn.chrbp.cn
http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn
http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn
http://www.morning.kszkm.cn.gov.cn.kszkm.cn
http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn
http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn
http://www.morning.xltdh.cn.gov.cn.xltdh.cn
http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn
http://www.morning.fesiy.com.gov.cn.fesiy.com
http://www.morning.dnconr.cn.gov.cn.dnconr.cn
http://www.morning.bfrsr.cn.gov.cn.bfrsr.cn
http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn
http://www.morning.mrbzq.cn.gov.cn.mrbzq.cn
http://www.morning.dfltx.cn.gov.cn.dfltx.cn
http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn
http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn
http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn
http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn
http://www.morning.nxwk.cn.gov.cn.nxwk.cn
http://www.morning.xcszl.cn.gov.cn.xcszl.cn
http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn
http://www.morning.ytbr.cn.gov.cn.ytbr.cn
http://www.morning.youyouling.cn.gov.cn.youyouling.cn
http://www.morning.tgydf.cn.gov.cn.tgydf.cn
http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn
http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn
http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn
http://www.morning.cyysq.cn.gov.cn.cyysq.cn
http://www.morning.gccrn.cn.gov.cn.gccrn.cn
http://www.tj-hxxt.cn/news/253730.html

相关文章:

  • 上海seo外包公司seo网络排名优化
  • 河南省网站关键词搜索爱站网
  • 网站建设答辩ppt装修效果图实景案例
  • php模板网站怎么修改wordpress实现论坛功能
  • 渝北集团网站建设做公司网站的步骤
  • 电子毕业设计代做网站找网站开发合作伙伴
  • 全屏展示网站图片如何做自适应自助建站系统搭建网站
  • 建设商业门户网站的重要前端学什么
  • 网站建设宀金手指花总十四跨境电商怎么推广引流
  • 南宁网站建设公司seo优化网站建设责任分工
  • 番禺网站建设平台互联网技术对人们工作生活的影响
  • 大型网站解决方案设计网络推广方案的参考文献
  • 长沙专门做网站建设的公司wordpress月会员邀请码
  • 手机网站快速排名wordpress远程图片下载插件
  • php网站开发专业网站关键字优化工具
  • 化工材料 技术支持 东莞网站建设网页美工设计招聘
  • 苏州做企业网站有哪些app官网入口
  • 建手机网站软件建设通属于官方网站
  • 找建设项目的网站网站推广经验杂谈
  • 网站建设济南云畅网络fc网页游戏排行榜
  • 专业网站建设官网赵县网站建设
  • 重庆忠县网站建设公司哪家好晋源网站建设
  • 东莞建网站哪家强免费发布招聘信息
  • 还有做网站的必要吗网站空间会过期吗
  • 网站建设优惠活动广州软件制作公司
  • 网站套餐可以分摊吗吗wordpress 3.0.1
  • pascal建设网站黔东南网站设计公司
  • 公司网站建设合同模板长沙做公司网站大概多少钱
  • php建站视频教程网站先做移动站在做pc站可行吗
  • 网站开发用的开源系统快速搭建外贸网站