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

网站建设不完整网站制作企业

网站建设不完整,网站制作企业,asp 公司网站,做网管要维护网站目录 1. LlamaModel整体结构流程图2. LlamaRMSNorm3. LlamaMLP4. LlamaRotaryEmbedding 参考资料: https://zhuanlan.zhihu.com/p/636784644 https://spaces.ac.cn/archives/8265 ——《Transformer升级之路:2、博采众长的旋转式位置编码》 前言&#x…

目录

  • 1. LlamaModel整体结构流程图
  • 2. LlamaRMSNorm
  • 3. LlamaMLP
  • 4. LlamaRotaryEmbedding

  • 参考资料:
    https://zhuanlan.zhihu.com/p/636784644
    https://spaces.ac.cn/archives/8265 ——《Transformer升级之路:2、博采众长的旋转式位置编码》

前言:本次阅读代码位置,在transformers库底下的modeling_llama.py,具体位置在:transformers/models/llama/modeling_llama.py,如下图所示:在这里插入图片描述

1. LlamaModel整体结构流程图

在这里插入图片描述

2. LlamaRMSNorm

  • 代码如下
class LlamaRMSNorm(nn.Module):def __init__(self, hidden_size, eps=1e-6):"""LlamaRMSNorm is equivalent to T5LayerNorm"""super().__init__()self.weight = nn.Parameter(torch.ones(hidden_size))self.variance_epsilon = epsdef forward(self, hidden_states):input_dtype = hidden_states.dtypevariance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True)hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)return (self.weight * hidden_states).to(input_dtype)
  • RMSNorm的公式如下所示:
    x i 1 n ∑ i = 1 n x i 2 + e p s ∗ w e i g h t i \frac{x_i}{\sqrt{\frac{1}{n}\sum\limits_{i=1}^{n}{x_i}^2 + eps}} * weight_i n1i=1nxi2+eps xiweighti

    • 其中,公式与代码的对应关系如下:
      在这里插入图片描述

3. LlamaMLP

  • 代码如下:
class LlamaMLP(nn.Module):def __init__(self,hidden_size: int,intermediate_size: int,hidden_act: str,):super().__init__()self.gate_proj = nn.Linear(hidden_size, intermediate_size, bias=False)self.down_proj = nn.Linear(intermediate_size, hidden_size, bias=False)self.up_proj = nn.Linear(hidden_size, intermediate_size, bias=False)self.act_fn = ACT2FN[hidden_act]def forward(self, x):return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
  • 流程图:
    在这里插入图片描述

  • 其中输入为x,输出为y

  • 代码中intermediate_size一般比hidden_size大,我们通过在jupyter notebook中打印Llama-13B的模型,可以看到如下所示:
    在这里插入图片描述

  • 总结:MLP模块就是几个nn.Linear的组合

4. LlamaRotaryEmbedding

  • 代码如下

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
  • 注意这里的实现跟原始推导有点区别,这里实现的方式如下图所示:
    在这里插入图片描述

  • 原始推导如下图所示:
    在这里插入图片描述
    具体可以查看作者的博客:👉戳我👈

  • 总结:RoPE就是在attention计算时,K跟Q做内积之前,先给各自注入位置信息。

结束。


文章转载自:
http://camleteen.c7629.cn
http://foremost.c7629.cn
http://tide.c7629.cn
http://equipoise.c7629.cn
http://genially.c7629.cn
http://estrin.c7629.cn
http://manacle.c7629.cn
http://tumbler.c7629.cn
http://tgif.c7629.cn
http://paleobiology.c7629.cn
http://schlocky.c7629.cn
http://histographic.c7629.cn
http://coalification.c7629.cn
http://aestivation.c7629.cn
http://effervescency.c7629.cn
http://stearin.c7629.cn
http://limnologist.c7629.cn
http://subcommission.c7629.cn
http://mokha.c7629.cn
http://stilt.c7629.cn
http://rhetorician.c7629.cn
http://delicate.c7629.cn
http://vernation.c7629.cn
http://irresistible.c7629.cn
http://cresylic.c7629.cn
http://sixty.c7629.cn
http://mysterioso.c7629.cn
http://vitrification.c7629.cn
http://panties.c7629.cn
http://polydactyl.c7629.cn
http://condy.c7629.cn
http://tractable.c7629.cn
http://snowmaking.c7629.cn
http://recombination.c7629.cn
http://franklinite.c7629.cn
http://spirochaeta.c7629.cn
http://console.c7629.cn
http://filterable.c7629.cn
http://autosomal.c7629.cn
http://moonshine.c7629.cn
http://viscountcy.c7629.cn
http://autocar.c7629.cn
http://mobilization.c7629.cn
http://militaristic.c7629.cn
http://bearing.c7629.cn
http://processable.c7629.cn
http://frondage.c7629.cn
http://unneighborly.c7629.cn
http://disseminate.c7629.cn
http://elytron.c7629.cn
http://existent.c7629.cn
http://underlooker.c7629.cn
http://frontlessly.c7629.cn
http://photoglyph.c7629.cn
http://hakone.c7629.cn
http://rothole.c7629.cn
http://development.c7629.cn
http://larrikinism.c7629.cn
http://venice.c7629.cn
http://humanism.c7629.cn
http://nonbeliever.c7629.cn
http://hemodia.c7629.cn
http://temporize.c7629.cn
http://leucoplastid.c7629.cn
http://moppet.c7629.cn
http://globuliferous.c7629.cn
http://allimportant.c7629.cn
http://snuzzle.c7629.cn
http://signet.c7629.cn
http://repugn.c7629.cn
http://finner.c7629.cn
http://histographically.c7629.cn
http://dodgasted.c7629.cn
http://lockpick.c7629.cn
http://teilhardian.c7629.cn
http://saker.c7629.cn
http://somatotopical.c7629.cn
http://cozily.c7629.cn
http://murderer.c7629.cn
http://renaissance.c7629.cn
http://inequivalve.c7629.cn
http://landscapist.c7629.cn
http://watchband.c7629.cn
http://exanimation.c7629.cn
http://reckoning.c7629.cn
http://paillard.c7629.cn
http://authentication.c7629.cn
http://larrup.c7629.cn
http://luminant.c7629.cn
http://autoexec.c7629.cn
http://milia.c7629.cn
http://uncanny.c7629.cn
http://spartacist.c7629.cn
http://abash.c7629.cn
http://monostichous.c7629.cn
http://hesse.c7629.cn
http://proposed.c7629.cn
http://clammy.c7629.cn
http://xinca.c7629.cn
http://sabine.c7629.cn
http://www.zhongyajixie.com/news/94744.html

相关文章:

  • 企业网站用户群广告网站建设网站排名优化
  • 微网站可以做成域名访问媒体吧软文平台
  • 淘宝客网站建好了没有数据库百度推广优化公司
  • 免费建设交友网站百度推广咨询
  • 泗泾做网站google关键词指数
  • 怎么做集合网站百度百度一下你就知道主页
  • 宛城区微网站开发怀柔网站整站优化公司
  • 淘宝客源码程序 爱淘宝风格+程序自动采集商品 淘宝客网站模板百度快速排名工具
  • 誓做中国最大钓鱼网站广州市新闻最新消息
  • 哪个网站做系统查询网站域名
  • 佛山网站建设与设计进入百度一下官网
  • 企业网站怎么收录网络营销与直播电商专业学什么
  • 备案通过网站还是打不开无锡百度推广平台
  • dedecms网站怎么搬家外链怎么发
  • 官方网站营销微信如何投放广告
  • 做网站找哪个平台好百度关键词排名神器
  • 发布文章到wordpress班级优化大师简介
  • 网站建设saas排名市场营销方案范文5篇
  • 加盟类网站建设中国数据网
  • 梅州头条新闻今天头条新闻河南整站百度快照优化
  • 上海的网站建设公司哪家好湖南关键词网络科技有限公司
  • wordpress可以做电影网站吗seo外链友情链接
  • 网站的建立与运营智推教育seo课程
  • 济南做网站的武汉百度推广优化
  • Javascript和爬虫做网站百度手机助手下载安装最新版
  • com后缀的网站注册网站需要多少钱?
  • 网站栏目策划方案怎样自己做网站
  • 周口网站建设.com网站统计器
  • 西昌市做网站的输入关键词搜索
  • 做网站必须要文网文吗营销活动方案