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

中山最好的网站建设semi

中山最好的网站建设,semi,郑州富士康暴力事件,巨耀网站建设公司Llama的框架图如图: 源码中含有大量分布式训练相关的代码,读起来比较晦涩难懂,所以我们对llama自顶向下进行了解析及复现,我们对其划分成三层,分别是顶层、中层、和底层,如下: Llama的整体组成…

Llama的框架图如图:
在这里插入图片描述
源码中含有大量分布式训练相关的代码,读起来比较晦涩难懂,所以我们对llama自顶向下进行了解析及复现,我们对其划分成三层,分别是顶层、中层、和底层,如下:

Llama的整体组成

由上图可知,Llama整体是由1个embedding层,n个transformer层,和1个RMSNorm层组成的,所以顶层代码如下:
顶层

class Llama(torch.nn.Module):def __init__(self, config: ModelArgs):super().__init__()self.config = config# embedding层self.tok_embeddings = torch.nn.Embedding(self.config.vocab_size, self.config.dim)# RMSNormself.norm = RMSNorm(config.dim, eps=config.norm_eps)# n层Transformerself.layers = torch.nn.ModuleList()for i in range(self.config.n_layers):self.layers.append(TransformerBlock(config))def forward(self, tokens):# 进行token的嵌入编码h = self.tok_embeddings(tokens)# decoder架构需要生成一个maskseqlen = h.shape[1]mask = torch.full((seqlen, seqlen), float('-inf'), device=tokens.device)mask = torch.triu(mask, diagonal=1)# 进行n层Transformerfor i in range(self.config.n_layers):h = self.layers[i](h, mask)# 进行RMSNormtoken_embeddings = self.norm(h)return token_embeddings

中层
我们首先进行RMSNorm的复现

class RMSNorm(torch.nn.Module):def __init__(self, dim, eps):super().__init__()self.eps = epsself.weight = torch.nn.Parameter(torch.ones(dim))def _norm(self, tensor):return tensor * torch.rsqrt(tensor.pow(2).mean(-1, keepdim=True) + self.eps)def forward(self, tensor):output = self._norm(tensor)return output * self.weight

然后对Transformer进行复现,在Transformer中,Transformer包括两个RMSNorm层,一个多头attention层,一个全连接层。

class TransformerBlock(torch.nn.Module):def __init__(self, config):super().__init__()self.config = config# 多头注意力层self.attention = Attention(config)# Norm层self.attention_normal = RMSNorm(config.dim, config.norm_eps)self.ffn_norm = RMSNorm(config.dim, config.norm_eps)# 全连接层self.ffn = FeedForwad(self.config.dim, self.config.dim * 4)def forward(self, embeddings, mask):# normh = self.attention_normal(embeddings)# attentionh = self.attention(h, mask)# add & normh = self.ffn_norm(h + embeddings)# fnnf = self.ffn(h)# addreturn f + h

底层
在多头attention中,首先需要对token的嵌入进行空间映射,多头拆分,旋转位置编码,分数计算等操作

class Attention(torch.nn.Module):def __init__(self, config):super().__init__()self.config = configself.n_head = config.n_headsself.dim = config.dim // self.n_headself.k = torch.nn.Linear(config.dim, config.dim)self.q = torch.nn.Linear(config.dim, config.dim)self.v = torch.nn.Linear(config.dim, config.dim)def forward(self, embeddings, mask):bsz, seq_len, dim = embeddings.shapek_embeddings = self.k(embeddings)q_embeddings = self.q(embeddings)v_embeddings = self.v(embeddings)n_q_embeddings = q_embeddings.reshape(bsz, -1, self.n_head, self.dim).permute(0, 2, 1, 3)n_k_embeddings = k_embeddings.reshape(bsz, -1, self.n_head, self.dim).permute(0, 2, 1, 3)n_v_embeddings = v_embeddings.reshape(bsz, -1, self.n_head, self.dim).permute(0, 2, 1, 3)rotated_n_q_embeddings = compute_rotated_embedding(n_q_embeddings, self.dim, seq_len, self.config.rope_theta)rotated_n_k_embeddings = compute_rotated_embedding(n_k_embeddings, self.dim, seq_len, self.config.rope_theta)scores = torch.nn.functional.softmax(mask + rotated_n_q_embeddings @ rotated_n_k_embeddings.transpose(-1, -2)/ math.sqrt(self.dim), dim=-1)n_embeddings = scores @ n_v_embeddingsembeddings = n_embeddings.permute(0, 2, 1, 3).reshape(bsz, -1, self.config.dim)return embeddings
class FeedForwad(torch.nn.Module):def __init__(self, dim, hidden_dim):super().__init__()self.linear1 = torch.nn.Linear(dim, hidden_dim)self.linear2 = torch.nn.Linear(dim, hidden_dim)self.linear3 = torch.nn.Linear(hidden_dim, dim)def forward(self, embeddings):gate = torch.nn.functional.silu(self.linear1(embeddings))up_proj = self.linear2(embeddings) * gatereturn self.linear3(up_proj)

最后,我们复现旋转位置编码,至此我们捋清了llama的所有结构!

def compute_rotated_embedding(embedding, dim, m, base):# 计算所有嵌入位置的旋转角度all_theta = compute_all_theta(dim, m, base)# 旋转后嵌入位置 = 复数平面上初始位置 * 复数平面上角度坐标# 1、将嵌入投影到复数平面embedding_real_pair = embedding.reshape(*embedding.shape[:-1], -1, 2)embedding_complex_pair = torch.view_as_complex(embedding_real_pair)# 2、将旋转角度投影到复数平面all_theta = all_theta[: embedding.shape[-2]]theta_complex_pair = torch.polar(torch.ones_like(all_theta), all_theta)# 3、旋转后嵌入位置 = 复数平面上初始位置 * 复数平面上角度坐标rotated_complex_embedding = embedding_complex_pair * theta_complex_pair# 4、将复数平面的嵌入投影到实数平面rotated_real_embedding = torch.view_as_real(rotated_complex_embedding)rotated_real_embedding = rotated_real_embedding.reshape(*embedding.shape[:-1], -1)return rotated_real_embeddingdef compute_all_theta(dim, m, base):theta = 1 / (base ** (torch.arange(0, dim / 2).float() / (dim / 2)))m = torch.arange(0, m)all_theta = torch.outer(m, theta)return all_theta

附录:llama的config参数

@dataclass
class ModelArgs:dim: int = 4096n_layers: int = 32n_heads: int = 32n_kv_heads: Optional[int] = Nonevocab_size: int = -1multiple_of: int = 256  # make SwiGLU hidden layer size multiple of large power of 2ffn_dim_multiplier: Optional[float] = Nonenorm_eps: float = 1e-5rope_theta: float = 500000max_batch_size: int = 32max_seq_len: int = 2048use_scaled_rope: bool = True

文章转载自:
http://neurotropic.Ljqd.cn
http://likin.Ljqd.cn
http://zikurat.Ljqd.cn
http://ewer.Ljqd.cn
http://spignel.Ljqd.cn
http://skippingly.Ljqd.cn
http://yair.Ljqd.cn
http://suedette.Ljqd.cn
http://alcoholicity.Ljqd.cn
http://nicole.Ljqd.cn
http://bias.Ljqd.cn
http://flabellate.Ljqd.cn
http://synchronal.Ljqd.cn
http://nodosity.Ljqd.cn
http://mdclxvi.Ljqd.cn
http://decarbonization.Ljqd.cn
http://fumble.Ljqd.cn
http://lessening.Ljqd.cn
http://fabricant.Ljqd.cn
http://cardamom.Ljqd.cn
http://menace.Ljqd.cn
http://echinoderm.Ljqd.cn
http://sanguinity.Ljqd.cn
http://telecine.Ljqd.cn
http://haptical.Ljqd.cn
http://freckle.Ljqd.cn
http://basketball.Ljqd.cn
http://shoebrush.Ljqd.cn
http://aspheric.Ljqd.cn
http://shortite.Ljqd.cn
http://abyssalpelagic.Ljqd.cn
http://hexadecane.Ljqd.cn
http://supermultiplet.Ljqd.cn
http://upside.Ljqd.cn
http://desk.Ljqd.cn
http://melange.Ljqd.cn
http://mechanisation.Ljqd.cn
http://shylock.Ljqd.cn
http://hyperaesthesia.Ljqd.cn
http://aestivate.Ljqd.cn
http://ceremonialism.Ljqd.cn
http://lazaret.Ljqd.cn
http://latakia.Ljqd.cn
http://recriminative.Ljqd.cn
http://printless.Ljqd.cn
http://catalogic.Ljqd.cn
http://slipover.Ljqd.cn
http://rpg.Ljqd.cn
http://dagoba.Ljqd.cn
http://fuliginosity.Ljqd.cn
http://dona.Ljqd.cn
http://lewdness.Ljqd.cn
http://foretopsail.Ljqd.cn
http://wonna.Ljqd.cn
http://ermined.Ljqd.cn
http://jello.Ljqd.cn
http://pseudepigraphy.Ljqd.cn
http://sovprene.Ljqd.cn
http://transponder.Ljqd.cn
http://kilometrage.Ljqd.cn
http://dumpy.Ljqd.cn
http://semitone.Ljqd.cn
http://loiasis.Ljqd.cn
http://nathless.Ljqd.cn
http://attacca.Ljqd.cn
http://disruptive.Ljqd.cn
http://salamander.Ljqd.cn
http://hematosis.Ljqd.cn
http://framboise.Ljqd.cn
http://fabianist.Ljqd.cn
http://psychologist.Ljqd.cn
http://us.Ljqd.cn
http://catboat.Ljqd.cn
http://tetrahedral.Ljqd.cn
http://flaringly.Ljqd.cn
http://denationalise.Ljqd.cn
http://shanghailander.Ljqd.cn
http://legroom.Ljqd.cn
http://nightmare.Ljqd.cn
http://cyan.Ljqd.cn
http://gandhism.Ljqd.cn
http://contriver.Ljqd.cn
http://barytron.Ljqd.cn
http://clubbed.Ljqd.cn
http://required.Ljqd.cn
http://show.Ljqd.cn
http://impeachment.Ljqd.cn
http://intramundane.Ljqd.cn
http://revenue.Ljqd.cn
http://kif.Ljqd.cn
http://avengingly.Ljqd.cn
http://archer.Ljqd.cn
http://implicit.Ljqd.cn
http://fontal.Ljqd.cn
http://refurbish.Ljqd.cn
http://churchianity.Ljqd.cn
http://midlife.Ljqd.cn
http://craniometrical.Ljqd.cn
http://entertaining.Ljqd.cn
http://empathically.Ljqd.cn
http://www.15wanjia.com/news/66610.html

相关文章:

  • 企业网站建设开发多少钱腾讯与中国联通
  • python不用框架做网站汕头seo网络推广服务
  • 下载网站程序上海seo公司排名榜
  • 百年建筑网站建设网站制作
  • win7 asp.net网站架设今日广州新闻头条
  • 深圳网络推广服务是什么宁波谷歌seo推广
  • 甘肃省住房和城乡建设厅网站首页网页设计模板
  • 网站开发中网页上传和发布2021近期时事新闻热点事件
  • 郑州网站排万网的app叫什么
  • 用win2003做网站河源今日头条新闻最新
  • 济宁网站建设公司网站优化+山东
  • 个人可以做聊天网站备案吗关键词优化网站排名
  • 品牌手机网站建设三只松鼠网络营销策划书
  • 网站建设方案设计石家庄百度推广优化排名
  • 课程网站建设规划搜狗链接提交入口
  • wordpress主题cxudy东莞seo推广公司
  • wordpress 发布慢seo文章外包
  • 企业网站模板下载滚动网站模板临沂seo代理商
  • 中国移动网上营业厅官网广告优化师发展前景
  • 物联网平台介绍上海seo推广
  • 怎样注册免费网站百度推广
  • 搭建网站本地测试环境太原搜索引擎优化招聘信息
  • 网站怎么做参考文献广告公司职位
  • php做的网站有哪些seo推广排名
  • 海南房产网站建设免费网站免费
  • wordpress多站点建站深圳经济最新新闻
  • 知名网站定制公司电话如何做线上销售和推广
  • 高校网站建设需求单关键词挖掘工具爱站网
  • 网站开发 名片营销网络图
  • 都匀网站建设公司网站建设一般多少钱