苏州企业如何建站,长治seo顾问,网络游戏行业防沉迷自律公约,自己做网站要不要租服务器从代码角度进行Llama 架构分析 Llama 架构分析前言Llama 架构分析分词网络主干DecoderLayerAttentionMLP 下游任务因果推理文本分类 Llama 架构分析
前言 Meta 开发并公开发布了 Llama系列大型语言模型 (LLM)#xff0c;这是一组经过预训练和微调的生成文本模型#xff0c;参… 从代码角度进行Llama 架构分析 Llama 架构分析前言Llama 架构分析分词网络主干DecoderLayerAttentionMLP 下游任务因果推理文本分类 Llama 架构分析
前言 Meta 开发并公开发布了 Llama系列大型语言模型 (LLM)这是一组经过预训练和微调的生成文本模型参数规模从 70 亿到 700 亿不等。 在大多数任务中LLaMA-13B要比GPT-3(175B)的性能要好LLaMA-65B和组好的模型Chinchilla-70B以及PaLM-540B的实力相当。 Llama 架构分析
分词 分词部分主要做的是利用文本分词器对文本进行分词 tokenizer AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
text Hey, are you conscious? Can you talk to me?
inputs tokenizer(text, return_tensorspt)网络主干 主干网络部分主要是将分词得到的input_ids输入到embedding层中进行文本向量化得到hidden_states中间结果然后输入到layers层中得到hidden_states中间结果用于下游任务。 self.embed_tokens nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)self.layers nn.ModuleList([MixtralDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)])self._use_flash_attention_2 config._attn_implementation flash_attention_2self.norm MixtralRMSNorm(config.hidden_size, epsconfig.rms_norm_eps)DecoderLayer 主干网络的layers层就是由多个DecoderLayer组成的由num_hidden_layers参数决定一般我们说的模型量级就取决于这个数量7b的模型DecoderLayer层的数量是32。 DecoderLayer层中又包含了Attention层和MLP层主要的一个思想是利用了残差结构。 如下图所示分为两个部分
第一部分
首先将hidden_states文本向量化的结构进行复制即残差归一化注意力层残差相加
第二部分
首先将第一部分得到的hidden_states进行复制即残差归一化MLP层残差相加
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
#复制一份
residual hidden_states
#归一化
hidden_states self.input_layernorm(hidden_states)#注意力层
hidden_states, self_attn_weights, present_key_value self.self_attn(hidden_stateshidden_states,attention_maskattention_mask,position_idsposition_ids,past_key_valuepast_key_value,output_attentionsoutput_attentions,use_cacheuse_cache,padding_maskpadding_mask,
)
#加上残差
hidden_states residual hidden_states#复制一份
residual hidden_states
#归一化
hidden_states self.post_attention_layernorm(hidden_states)
#mlp
hidden_states self.mlp(hidden_states)
#加上残差
hidden_states residual hidden_statesoutputs (hidden_states,)if output_attentions:outputs (self_attn_weights,)if use_cache:outputs (present_key_value,)return outputsAttention 进行位置编码让模型更好的捕捉上下文信息 #经过线性层
query_states self.q_proj(hidden_states)
key_states self.k_proj(hidden_states)
value_states self.v_proj(hidden_states)#多头注意力形状变换
query_states query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
key_states key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
value_states value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
kv_seq_len key_states.shape[-2]#计算cos、sin
#计算旋转位置嵌入
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)#计算权重
key_states repeat_kv(key_states, self.num_key_value_groups)
value_states repeat_kv(value_states, self.num_key_value_groups)
attn_weights torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)#加上掩码
attn_weights attn_weights attention_mask
#计算softmax
attn_weights nn.functional.softmax(attn_weights, dim-1, dtypetorch.float32).to(query_states.dtype)
attn_output torch.matmul(attn_weights, value_states)attn_output self.o_proj(attn_output)
MLP mlp层的主要作用是应用非线性激活函数和线性投影。 首先将attention层得到的结果经过两个线性层得到gate_proj和up_projgate_proj经过激活函数再和up_proj相乘最后经过一个线性层得到最后的结果 self.gate_proj nn.Linear(self.hidden_size, self.intermediate_size, biasFalse)
self.up_proj nn.Linear(self.hidden_size, self.intermediate_size, biasFalse)
self.down_proj nn.Linear(self.intermediate_size, self.hidden_size, biasFalse)
self.act_fn ACT2FN[config.hidden_act]
down_proj self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))下游任务
因果推理 所谓因果推理就是回归任务。 self.lm_head nn.Linear(config.hidden_size, config.vocab_size, biasFalse)文本分类 即分类任务 self.score nn.Linear(config.hidden_size, self.num_labels, biasFalse)