大模型中哪些模型用到的pre-norm和post-norm技术的?

我整理好的1000+面试题,请看
大模型面试题总结-CSDN博客

或者

https://gitee.com/lilitom/ai_interview_questions/blob/master/README.md

最好将URL复制到浏览器中打开,不然可能无法直接打开


好了,我们今天针对上面的问题,

大模型中哪些模型用到的pre-norm和post-norm技术的?

下面的代码都来自transformers库,代码截取

  • LLAMA

    代码如下,请看核心注释的地方

    class LlamaDecoderLayer(nn.Module):
    def init(self, config: LlamaConfig, layer_idx: int):
    super().init()
    self.hidden_size = config.hidden_size

    复制代码
          self.self_attn = **
    
          self.mlp = LlamaMLP(config)
          self.input_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
          self.post_attention_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
    
      def forward(
          self,
          hidden_states: torch.Tensor,
          ***
      ) -> **:
          residual = hidden_states
          # 输入先norm
          hidden_states = self.input_layernorm(hidden_states)
          # 计算attention
          hidden_states, self_attn_weights, present_key_value = self.self_attn(...        )
          hidden_states = residual + hidden_states
    
          # 先norm后mlp然后加上residual
          residual = hidden_states
          hidden_states = self.post_attention_layernorm(hidden_states)
          hidden_states = self.mlp(hidden_states)
          hidden_states = residual + hidden_states
    
          outputs = (hidden_states,)
    
          if output_attentions:
              outputs += (self_attn_weights,)
    
          if use_cache:
              outputs += (present_key_value,)
    
          return outputs

pre-norm

  • Qwen

    代码如下,请看核心注释额地方

    class Qwen2DecoderLayer(nn.Module):
    def init(self, config: Qwen2Config, layer_idx: int):
    super().init()
    self.hidden_size = config.hidden_size

    复制代码
          self.self_attn = QWEN2_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx)
    
          self.mlp = Qwen2MLP(config)
          self.input_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
          self.post_attention_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
    
      def forward(
          self,
          hidden_states: torch.Tensor,
          **
      ) -> **:
          """
    
          residual = hidden_states
    
          hidden_states = self.input_layernorm(hidden_states)
    
          # Self Attention
          hidden_states, self_attn_weights, present_key_value = self.self_attn(xx)
          hidden_states = residual + hidden_states
    
          # Fully Connected
          residual = hidden_states
          hidden_states = self.post_attention_layernorm(hidden_states)
          hidden_states = self.mlp(hidden_states)
          hidden_states = residual + hidden_states
    
          outputs = (hidden_states,)
    
          if output_attentions:
              outputs += (self_attn_weights,)
    
          if use_cache:
              outputs += (present_key_value,)
    
          return outputs

和llama一样的,是pre-norm.

  • Bert

    代码如下,请看核心注释额地方

    class BertSelfOutput(nn.Module):
    def init(self, config):
    super().init()
    self.dense = nn.Linear(config.hidden_size, config.hidden_size)
    self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
    self.dropout = nn.Dropout(config.hidden_dropout_prob)

    复制代码
      def forward(self, hidden_states: torch.Tensor, input_tensor: torch.Tensor) -> torch.Tensor:
          hidden_states = self.dense(hidden_states)
          hidden_states = self.dropout(hidden_states)
          hidden_states = self.LayerNorm(hidden_states + input_tensor)
          return hidden_states

    class BertOutput(nn.Module):
    def init(self, config):
    super().init()
    self.dense = nn.Linear(config.intermediate_size, config.hidden_size)
    self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
    self.dropout = nn.Dropout(config.hidden_dropout_prob)

    复制代码
      def forward(self, hidden_states: torch.Tensor, input_tensor: torch.Tensor) -> torch.Tensor:
          hidden_states = self.dense(hidden_states)
          hidden_states = self.dropout(hidden_states)
          hidden_states = self.LayerNorm(hidden_states + input_tensor)
          return hidden_states

上面的output代码用在了计算attention那一块,在attention计算完之后和input加起来,再过LN层。 因此是Post-Norm.

相关推荐
Are_You_Okkk_2 小时前
中小型团队知识库搭建:AI开源实践方案
人工智能·开源
Figo_Cheung2 小时前
Figo 关于OntoGuard-CRE 技术白皮书——已在gitee上开源发布
人工智能·安全·gitee·开源·knowledge graph
墨北小七2 小时前
BERT在小说大模型中的核心定位:理解者、解码者、守护者
人工智能·深度学习·神经网络·transformer
TechMasterPlus2 小时前
Hermes Agent 源码深度解析:自我进化的 AI Agent 框架架构设计
人工智能
萧逸才2 小时前
【learn-claude-code-4j】S14FeiShu - 飞书群聊智能体
java·人工智能·ai·飞书
TImCheng06092 小时前
内容运营岗位适合考哪个AI证书,与算法认证侧重点分析
人工智能·算法·内容运营
Gofarlic_OMS2 小时前
Windchill的license合规使用报告自动化生成与审计追踪系统
大数据·运维·人工智能·云原生·自动化·云计算
爱上珍珠的贝壳2 小时前
ESP32-S3-CAM:豆包语音识别文字后控制小车(规划)
人工智能·音频·语音识别·esp32-s3·小车
甜辣uu2 小时前
基于深度学习的CT图像肺结节分割与检测系统
人工智能·深度学习
赵域Phoenix2 小时前
混沌系统是什么?
人工智能·算法·机器学习