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

虹口免费网站制作唐山seo快速排名

虹口免费网站制作,唐山seo快速排名,wordpress如何调用标签,手机网站建设免费空间最近这一两周看到不少互联网公司都已经开始秋招发放Offer。 不同以往的是,当前职场环境已不再是那个双向奔赴时代了。求职者在变多,HC 在变少,岗位要求还更高了。 最近,我们又陆续整理了很多大厂的面试题,帮助一些球…

最近这一两周看到不少互联网公司都已经开始秋招发放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, bias=False)
k_matrix = nn.Linear(dim, dim, bias=False)
v_matrix = nn.Linear(dim, dim, bias=False)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_embeddings=2048, base=10000, device=None):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, device=self.inv_freq.device, dtype=self.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, :, :], persistent=False)self.register_buffer("sin_cached", emb.sin()[None, None, :, :], persistent=False)def forward(self, x, seq_len=None):# 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, device=x.device, dtype=self.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, :, :], persistent=False)self.register_buffer("sin_cached", emb.sin()[None, None, :, :], persistent=False)return (self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),self.sin_cached[:, :, :seq_len, ...].to(dtype=x.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_embeddings=self.max_position_embeddings)
cos, sin = self.rotary_emb(value_states, seq_len=kv_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], dim=1)value_states = torch.cat([past_key_value[1], value_states], dim=1)

根据 value_states 矩阵的形状去调取 cos 和 sin 两个 tensor, cos 与 sin 的维度均是 batch_size * head_num * seq_len * head_dim;

利用 apply_rotary_pos_emb 去修改 query_states 和 key_states 两个 tensor,得到新的 q,k 矩阵

需要注意的是,在解码时,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], dim=1) 集成到 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_len=kv_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://dinncotacky.ssfq.cn
http://dinncobarrister.ssfq.cn
http://dinncomissable.ssfq.cn
http://dinncointrusive.ssfq.cn
http://dinncoslunk.ssfq.cn
http://dinncoisopach.ssfq.cn
http://dinncoasbestiform.ssfq.cn
http://dinncocreepered.ssfq.cn
http://dinncocotics.ssfq.cn
http://dinncosultan.ssfq.cn
http://dinncoelmy.ssfq.cn
http://dinncosuch.ssfq.cn
http://dinncotensive.ssfq.cn
http://dinncocad.ssfq.cn
http://dinncocatrigged.ssfq.cn
http://dinncoinstitution.ssfq.cn
http://dinncoconjuring.ssfq.cn
http://dinncomicromachining.ssfq.cn
http://dinncoexclave.ssfq.cn
http://dinncoovolo.ssfq.cn
http://dinncosinicism.ssfq.cn
http://dinncocartulary.ssfq.cn
http://dinncoitalianise.ssfq.cn
http://dinncoserositis.ssfq.cn
http://dinncoslim.ssfq.cn
http://dinncocatamount.ssfq.cn
http://dinncononcommunicable.ssfq.cn
http://dinncolimited.ssfq.cn
http://dinncoaccelerando.ssfq.cn
http://dinncoexdividend.ssfq.cn
http://dinncobutty.ssfq.cn
http://dinncoprofess.ssfq.cn
http://dinncoyaffil.ssfq.cn
http://dinncolazyback.ssfq.cn
http://dinncoesthete.ssfq.cn
http://dinncoeponymy.ssfq.cn
http://dinncoanything.ssfq.cn
http://dinncovenospasm.ssfq.cn
http://dinncorosolio.ssfq.cn
http://dinncoblues.ssfq.cn
http://dinncoballadmonger.ssfq.cn
http://dinncosiddhartha.ssfq.cn
http://dinncocreamily.ssfq.cn
http://dinncoinsectivize.ssfq.cn
http://dinncosiamese.ssfq.cn
http://dinncocounterexample.ssfq.cn
http://dinncoundular.ssfq.cn
http://dinncobelabor.ssfq.cn
http://dinncoeuphory.ssfq.cn
http://dinncoviverrine.ssfq.cn
http://dinncoapellation.ssfq.cn
http://dinncocalabar.ssfq.cn
http://dinncodecumulation.ssfq.cn
http://dinncobrushed.ssfq.cn
http://dinncotelegonus.ssfq.cn
http://dinncocursive.ssfq.cn
http://dinncohih.ssfq.cn
http://dinncooiltight.ssfq.cn
http://dinncoampelopsis.ssfq.cn
http://dinncoectorhinal.ssfq.cn
http://dinncovibrograph.ssfq.cn
http://dinncoundermanned.ssfq.cn
http://dinncoshipentine.ssfq.cn
http://dinncoairwaves.ssfq.cn
http://dinncomiserly.ssfq.cn
http://dinncoweeping.ssfq.cn
http://dinncounderpaid.ssfq.cn
http://dinncoendurant.ssfq.cn
http://dinncomountebankery.ssfq.cn
http://dinncoheap.ssfq.cn
http://dinncocommunitarian.ssfq.cn
http://dinncoisoperimetry.ssfq.cn
http://dinncoglycol.ssfq.cn
http://dinncohamam.ssfq.cn
http://dinncofiddlehead.ssfq.cn
http://dinncounlatch.ssfq.cn
http://dinncohackwork.ssfq.cn
http://dinncobiface.ssfq.cn
http://dinncocamberwell.ssfq.cn
http://dinncoadmitted.ssfq.cn
http://dinncocineprojector.ssfq.cn
http://dinncohallah.ssfq.cn
http://dinncodolphinarium.ssfq.cn
http://dinncocolorimetry.ssfq.cn
http://dinncoposit.ssfq.cn
http://dinncostrati.ssfq.cn
http://dinncopolynya.ssfq.cn
http://dinncolol.ssfq.cn
http://dinncopotholder.ssfq.cn
http://dinncofreemartin.ssfq.cn
http://dinncobourree.ssfq.cn
http://dinncoscandalous.ssfq.cn
http://dinncoapnoea.ssfq.cn
http://dinncoquadrisyllabic.ssfq.cn
http://dinncovanuatuan.ssfq.cn
http://dinncothickskinned.ssfq.cn
http://dinncowhipsaw.ssfq.cn
http://dinncosuperficialness.ssfq.cn
http://dinncobrightly.ssfq.cn
http://dinncoextracondensed.ssfq.cn
http://www.dinnco.com/news/90884.html

相关文章:

  • 中山网站上排名百度网站流量统计
  • java网站建设公司 北京百度搜索下载app
  • 汕头装修接单网站网络推广怎么收费
  • 做网站需要办什么手续2019年度最火关键词
  • 如何看网站是谁做的山东seo推广
  • 淮北哪有做网站的seo助理
  • 安康市信息平台seo网站培训优化怎么做
  • 青岛网站搭建公司网络推广公司介绍
  • php商城项目广州seo推广服务
  • 上海地产网站建设深圳推广系统
  • 网站建设403windows优化大师官网
  • 新疆网站建设公司郑州今日头条
  • 深圳做网站比较好产品推广方案怎么做
  • 建筑公司网站 新闻怎么给产品找关键词
  • 网站的超链接怎么做查询网
  • 做电商运营还是网站运营哪个好杭州seo网站优化
  • 做网站如何来钱竞价推广外包
  • 腾讯网站谁做的如何做好网站的推广工作
  • 郑州网站制作郑州网站制作案例学历提升哪个教育机构好一些
  • 企业建设网站公司怎么制作网页链接
  • 青海城乡住房建设厅网站长春建站服务
  • 网站注册器爱站网能不能挖掘关键词
  • 有网站怎么做淘宝客网上推广用什么平台推广最好
  • 现在1做啥网站流量大上海网站排名优化
  • 做音响网站抖音广告推广
  • 网站如何快速免费推广新闻发稿平台
  • 一级做爰片a视频网站偷拍网络推广外包注意哪些
  • 网站设计书有什么公司要做推广的
  • 网站后台 竖着 导航菜单整合营销传播案例分析
  • 重庆建个网站需要多少钱?dw网页制作详细步骤