探索和构建 LLaMA 3 架构:深入探讨组件、编码和推理技术(七)前馈神经网络

探索和构建 LLaMA 3 架构:深入探讨组件、编码和推理技术(七)前馈神经网络

在Transformer架构中,前馈层扮演着至关重要的角色,通常位于注意力层和标准化处理之后。前馈层由三个线性变换组成。

python 复制代码
class FeedForward(nn.Module):
    def __init__(self, args: ModelArgs):
        super().__init__()
        # Assuming 'hidden_dim' is calculated as per your specifications
        hidden_dim = 4 * args.dim
        hidden_dim = int(2 * hidden_dim / 3)  # Applying your specific transformation
        if args.ffn_dim_multiplier is not None:
            hidden_dim = int(args.ffn_dim_multiplier * hidden_dim)
        #hidden_dim = int(2 * hidden_dim / 3)  # Applying your specific transformation
        hidden_dim = args.multiple_of * ((hidden_dim + args.multiple_of - 1) // args.multiple_of)

        self.w1 = nn.Linear(args.dim, hidden_dim, bias=False)
        self.w2 = nn.Linear(hidden_dim, args.dim, bias=False)  # This layer seems to be missing in your original setup
        self.w3 = nn.Linear(args.dim, hidden_dim, bias=False)  # Corrected to match checkpoint

    def forward(self, x: torch.Tensor):
        swish = F.silu(self.w1(x))  # Apply first transformation
        x_V = self.w3(x) 
        x = swish * x_V        # Apply contraction to original dimension
        x = self.w2(x)  # Apply optional additional transformation
        return x

在前向传递过程中,输入张量x经历多层线性变换。第一次转换后应用的SwiGLU激活函数增强了模型的表达能力。最终的变换将张量映射回其原始维度。 SwiGLU 激活和多个前馈层的这种独特组合增强了模型的性能。


系列博客

探索和构建 LLaMA 3 架构:深入探讨组件、编码和推理技术(一)Llama3 模型 架构
https://duanzhihua.blog.csdn.net/article/details/138208650

探索和构建 LLaMA 3 架构:深入探讨组件、编码和推理技术(二)RoPE位置编码
https://duanzhihua.blog.csdn.net/article/details/138212328

探索和构建 LLaMA 3 架构:深入探讨组件、编码和推理技术(三)KV缓存
https://duanzhihua.blog.csdn.net/article/details/138213306

探索和构建 LLaMA 3 架构:深入探讨组件、编码和推理技术(四)分组多查询注意力
https://duanzhihua.blog.csdn.net/article/details/138216050

探索和构建 LLaMA 3 架构:深入探讨组件、编码和推理技术(五)RMS 均方根归一化
https://duanzhihua.blog.csdn.net/article/details/138216630

探索和构建 LLaMA 3 架构:深入探讨组件、编码和推理技术(六)SwiGLU 激活函数
https://duanzhihua.blog.csdn.net/article/details/138217261

相关推荐
晨尘光1 天前
在Windows下编译出llama_cpp_python的DLL后,在虚拟环境中使用方法
python·llama
风筝超冷4 天前
LLaMA-Factory - 批量推理(inference)的脚本
llama
bluebonnet275 天前
【agent开发】部署LLM(一)
python·llama
阿牛大牛中6 天前
LLaDa——基于 Diffusion 的大语言模型 打平 LLama 3
人工智能·语言模型·llama
Lilith的AI学习日记6 天前
【AI面试秘籍】| 第25期:RAG的关键痛点及解决方案深度解析
人工智能·深度学习·机器学习·chatgpt·aigc·llama
LChuck8 天前
【大模型微调】魔搭社区GPU进行LLaMA-Factory微调大模型自我认知
人工智能·语言模型·自然语言处理·nlp·llama·魔搭社区·modelscope
燕双嘤8 天前
Fine-tuning:微调技术,训练方式,LLaMA-Factory,ms-swift
llama
装不满的克莱因瓶11 天前
【小白AI教程】大模型知识扫盲通识
人工智能·数学建模·ai·大模型·llm·llama·rag
TGITCIC13 天前
英伟达破局1000 Token/秒!Llama 4以光速重塑AI推理边界
人工智能·大模型·llama·英伟达·大模型速度·ai赛道·大模型基座
天天爱吃肉821814 天前
【 大模型技术驱动智能网联汽车革命:关键技术解析与未来趋势】
语言模型·汽车·llama