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

【注意力MHA,MQA,GQA,MLA】

注意力机制优化简明图解

1. 多头注意力(MHA)

图示:

Input --> [Attention Head 1]--> [Attention Head 2]--> [Attention Head 3]--> ...--> [Attention Head N]--> [Concatenate] --> Output

公式:

Output = Concat ( head 1 , head 2 , … , head N ) \text{Output} = \text{Concat}(\text{head}_1, \text{head}_2, \ldots, \text{head}_N) Output=Concat(head1,head2,,headN)
head i = Attention ( Q , K , V ) \text{head}_i = \text{Attention}(Q, K, V) headi=Attention(Q,K,V)

2. 多查询注意力(MQA)

图示:

Input --> [Shared Keys & Values]--> [Attention Head 1]--> [Attention Head 2]--> [Attention Head 3]--> ...--> [Concatenate] --> Output

公式:
Output = Concat ( head 1 , head 2 , … , head N ) \text{Output} = \text{Concat}(\text{head}_1, \text{head}_2, \ldots, \text{head}_N) Output=Concat(head1,head2,,headN)
head i = Attention ( Q , K shared , V shared ) \text{head}_i = \text{Attention}(Q, K_{\text{shared}}, V_{\text{shared}}) headi=Attention(Q,Kshared,Vshared)

3. 分组查询注意力(GQA)

图示:

Input --> [Attention Group 1]--> [Attention Group 2]--> ...--> [Concatenate] --> Output

公式:
Output = Concat ( group 1 , group 2 , … , group M ) \text{Output} = \text{Concat}(\text{group}_1, \text{group}_2, \ldots, \text{group}_M) Output=Concat(group1,group2,,groupM)
group j = Attention ( Q group j , K group j , V group j ) \text{group}_j = \text{Attention}(Q_{\text{group}_j}, K_{\text{group}_j}, V_{\text{group}_j}) groupj=Attention(Qgroupj,Kgroupj,Vgroupj)

4. 多头潜在注意力(MLA)

图示:

Input --> [Compressed Keys & Values]--> [Attention Head 1]--> [Attention Head 2]--> [Attention Head 3]--> ...--> [Concatenate] --> Output

公式:
Output = Concat ( head 1 , head 2 , … , head N ) \text{Output} = \text{Concat}(\text{head}_1, \text{head}_2, \ldots, \text{head}_N) Output=Concat(head1,head2,,headN)
head i = Attention ( Q , K compressed , V compressed ) \text{head}_i = \text{Attention}(Q, K_{\text{compressed}}, V_{\text{compressed}}) headi=Attention(Q,Kcompressed,Vcompressed)

低秩键值联合压缩公式:

K compressed = U K ⋅ S K ⋅ V K T K_{\text{compressed}} = U_K \cdot S_K \cdot V_K^T Kcompressed=UKSKVKT
V compressed = U V ⋅ S V ⋅ V V T V_{\text{compressed}} = U_V \cdot S_V \cdot V_V^T Vcompressed=UVSVVVT

图示概述

  1. MHA: 每个头独立操作,最终结果拼接。
  2. MQA: 多个头共享键和值,只计算一次查询,减少计算量。
  3. GQA: 查询分组,每组共享键和值,进一步减少计算量。
  4. MLA: 键和值进行压缩,减少内存和计算需求。

这些方法通过不同的策略优化注意力机制,提高了计算效率,降低了内存消耗,使Transformer模型在实际应用中更加高效。

http://www.lryc.cn/news/411796.html

相关文章:

  • 《从零开始做个摸鱼小网站! · 序》灵感来源
  • 计算机基础(Windows 10+Office 2016)教程 —— 第5章 文档编辑软件Word 2016(上)
  • 短视频矩阵管理系统源码:实现短视频内容全面布局
  • 系统设计中15 个最重要的权衡
  • 12年外贸实战经验,一定对你有帮助!
  • Linux---进程(3)---进程状态
  • Drools规则引擎实现停车计费
  • 【python虚拟环境】安装第三方包失败/failed with error code1
  • DiffusionModel-latent diffusion,VAE,U-Net,Text-encoder
  • C# form的移植工作
  • linux防火墙相关命令
  • 实习中学到的一点计算机知识(MP4在企业微信打不开?)
  • ElasticSearch入门语法基础知识
  • 【C++】C++应用案例-dolphin海豚记账本
  • Matlab数据处理学习笔记
  • 浏览器中的同源策略、CORS 以及相关的 Fetch API 使用
  • 爬虫 APP 逆向 ---> 粉笔考研
  • 2024河南萌新联赛第(三)场 河南大学
  • 回溯法---分割回文串
  • DDR等长,到底长度差多少叫等长?
  • 程序员面试题------N皇后问题算法实现
  • 【C++学习】6、继承
  • 从零开始的MicroPython(三) 按键与外部中断
  • Windows下编译安装Kratos
  • 汽车-腾讯2023笔试(codefun2000)
  • 软测面试二十问(最新面试)
  • 风吸杀虫灯采用新型技术 无公害诱虫捕虫
  • 随手记录第十二话 -- JDK8-21版本的新增特性记录(Lambda,var,switch,instanceof,record,virtual虚拟线程等)
  • SpringCloud网关 SpringBoot服务 HTTP/HTTPS路由/监听双支持
  • JavaScript做网页是否过期的处理