大模型中哪些模型用到的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.

相关推荐
阿里云大数据AI技术10 小时前
构建高转化海外电商搜索:阿里云OpenSearch行业算法版的全链路智能优化策略实战
人工智能·搜索引擎
Awu122711 小时前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude
字节跳动视频云技术团队11 小时前
让 Agent 成为音视频工作台:AI MediaKit CLI + Skill 发布
人工智能·音视频开发
魏祖潇11 小时前
framework 整合实战——DDD/TDD/SDD 三件套在 framework 仓的真实落地
人工智能·后端
Token炼金师11 小时前
去噪扩散:从随机噪声到高保真图像的数学之路
人工智能·aigc
这个DBA有点耶11 小时前
AI写的SQL跑崩了生产库,这锅谁背?
数据库·人工智能·程序员
阿里云大数据AI技术12 小时前
阿里云 EMR AI 助手正式发布:从问答工具到全栈智能运维助手
运维·人工智能
Larcher13 小时前
从零搭建 MCP 服务——让 AI 拥有无限扩展能力
人工智能·程序员
zzzzzz31013 小时前
你的 AI 写的 React 烂透了?这个 8000+ Star 的开源工具能揪出 90% 的「Agent 屎山」
人工智能
小星AI13 小时前
MCP协议超详细教程,从入门到实战
人工智能