type
status
date
slug
summary
tags
password
category
Property
Jun 16, 2024 01:28 PM
icon
1. 从Twitter上的一场口水战说起
介绍一下口水战的双方选手:
- Elon Musk:这个不用多说
- Yann LeCun:FaceBook(Meta)首席AI科学家,CNN之父,把CNN和反向传播结合用于手写数字识别就是他的工作
下面就是一些双方论战的截图(幽默向):
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F34829bb0-969d-4363-92a4-aed250ef858a%2FUntitled.png?table=block&id=350f9acd-65a6-4a81-aedf-3e5ddf3a6e2b&t=350f9acd-65a6-4a81-aedf-3e5ddf3a6e2b&width=336&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fd934b924-0b36-4aea-a71c-ba604b61cfb6%2FUntitled.png?table=block&id=135659e9-b7b5-46d9-9980-6b432208f893&t=135659e9-b7b5-46d9-9980-6b432208f893&width=480&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F979937f3-159d-4241-8472-2621ecb2e629%2FUntitled.png?table=block&id=5478761e-35c8-4123-aa59-ec2be514ffc5&t=5478761e-35c8-4123-aa59-ec2be514ffc5&width=580&cache=v2)
在互相拆台之余,LeCun在宣传自家最新论文时不忘手动@一下马斯克,意味深长地嘱咐道:”马斯克,我们这项研究用来改善你家的 Grok 也没问题“。骂战归骂战,CoPE这篇文章还是很值得研究的,是最近的一段时间少有的在大语言模型理论上有突破的一篇文章。
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F0b43bec3-e83f-48fa-a120-d88be9c83ea8%2FUntitled.png?table=block&id=9c1948d7-d9ab-4b19-8432-a610badcef50&t=9c1948d7-d9ab-4b19-8432-a610badcef50&width=588&cache=v2)
2. Why position encoding in Transformer
如今的大模型已经显示出了强大的威力,Transformer架构功不可没,先回顾一下其中核心的注意力机制:
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F2507a8e4-732d-4acb-8f62-3e022978fa5c%2FUntitled.png?table=block&id=9277acc8-c9ef-466d-b82b-08e5a2739a06&t=9277acc8-c9ef-466d-b82b-08e5a2739a06&width=1173&cache=v2)
可以把Transformer表达成如下算子的形式:
也就是注意力算子A和前馈神经网络F,对于F,它是位置无关的。那么,我们应该在A也就是attention上做文章,让其带上位置编码信息。一种简单的想法就是在其输入上加上位置的embedding。
✨self attention
这里就以单头的self attention为例:
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F4a1bfdb3-1702-4587-8f7e-c87e139571e2%2FUntitled.png?table=block&id=9c9d96c2-536a-4bc6-ba18-dd8155d86207&t=9c9d96c2-536a-4bc6-ba18-dd8155d86207&width=707.9861450195312&cache=v2)
- Q(query):模型从token中提取出的,对token的理解信息。用于主动与其它token计算相似程度
a1计算出的Q与其它位置的token的K的转置相乘得到
- K(key):模型从token中提取出的,与其它token的关系信息,被用于与其它token计算相似程度
- V(value):表示当前token的重要程度
这么说可能有点抽象,再以各种购物网站的商品推荐为例解释一下QKV,以键盘为query,推荐系统根据存储的商品关键词Key,计算出两者的相似度,再乘以各种打法机制,这里只以一个评价数为value,可以得到一个总分的排序,得分越高,排序越靠前。
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F2425eda0-93e7-4f58-9974-cf80cba6e0de%2FUntitled.png?table=block&id=fb5a519c-ca55-4169-89be-e87f2ebfe1ea&t=fb5a519c-ca55-4169-89be-e87f2ebfe1ea&width=707.9861450195312&cache=v2)
一段简单的代码:
✨Transformer中的相对位置编码
从以上的自注意力计算过程可以看出,虽然采用并行计算的方式比传统的RNN快了不少,但仅使用x或者q,k(来自于x),计算注意力的权重时,并未包含编码的位置信息。
因为每个token都要与其它的token进行计算,所以输出的每个b的值都是包含了全局的信息的,但这里的全局信息指的是全局的内容信息,并不包含位置信息。
以”狗咬人“与”人咬狗“两句话为例,”狗“在第一个和第三个位置的输出一样,假设后续的网络是将经过注意力的两句话简单加和,得到的两句话的输出是一样的,显然是不符合要求的。
也就是说,词与词之间不存在顺序关系(打乱一句话,这句话里的每个词的词向量依然不会变),即无位置关系(既然没有,我就加一个,通过位置编码的形式加)。
3. Bert,GPT中的位置编码
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fedd2dbfb-0d89-4ad2-ba9e-20970354288e%2FUntitled.png?table=block&id=17a15d88-3240-4087-8643-1dc9b8e0657d&t=17a15d88-3240-4087-8643-1dc9b8e0657d&width=1193&cache=v2)
在Transformer的原始论文attention is all you need中,提出了两种位置编码的方式,第一种就是直接初始化一个可学习的位置编码矩阵加在输入上。
这样做的好处是极其简单,但缺点在于对于超过序列最大长度的位置外推困难,因为这种方式可能会响应超过序列最大长度的一些embedding,对于我们来说,是一个间隔了,但对于它来说还是一个没有学习到的东西,那么这个时候的外推就是失败的。
4. Sinusoidal位置编码
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fef443ba0-16fc-47d4-8516-2a72ff31e1e8%2FUntitled.png?table=block&id=9d976369-d756-4a2e-b0b9-b4432cb2b55d&t=9d976369-d756-4a2e-b0b9-b4432cb2b55d&width=1178&cache=v2)
这是Transformer原始论文提出的第二种位置编码的方式,但原文并没有对其做过多解释,现在也有很多其它人对其做了一些理解。
这是一种自定义的位置编码,可以表达成一个二维函数f(position, dimension)
这个函数有两个要求:
- 随着position,dimmension的增长应该是有界的
- 足够的区分度,对每个position,对应的向量应该是不同的
对于SinCos位置编码(我习惯这么叫),它利用如下公式产生PE:
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F53c62e30-012b-4882-a22c-90a45f7630be%2FUntitled.png?table=block&id=c8aaf215-86cf-47f1-af7f-e09d7f756a66&t=c8aaf215-86cf-47f1-af7f-e09d7f756a66&width=340&cache=v2)
对于图中红色的列(偶数列),它采用sin计算,对于图中绿色的列,它用cos计算,这样就可以使得每行的向量完全不同,这样便满足了带来绝对位置信息的要求。
又因为三角函数有如下公式:
可以把某个pos分解开来,以pos=5分解为pos’=1,k=4为例,将原始的PE公式可以变换为:
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F57a7f94f-f466-412b-890a-72a70fadfede%2FUntitled.png?table=block&id=b5c06991-9a54-4fb0-8c89-8a43537b734b&t=b5c06991-9a54-4fb0-8c89-8a43537b734b&width=576&cache=v2)
即图中蓝色小点的值=绿色小点的值交叉相乘的形式,这样便为每个pos的pe值带来了与其它pos的pe值的线性关系,即带来了相对位置关系的可能。当然,这里只是一些简单的推导,更详细的推导可以参考https://kexue.fm/archives/8231
以及https://kexue.fm/archives/8130这两篇文章。这两篇文章指出了此种位置编码其实是一种”想要成为相对位置编码的绝对位置编码“,是有一些瑕疵的,现在也很少直接使用这种形式的位置编码的工作,原因不详。
5. 旋转位置编码RoPE
✨前置:线性代数知识补充
关于行向量和矩阵
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fd54e745e-97f1-4bfa-9866-b2cf67ebd8ef%2FUntitled.png?table=block&id=afdbb334-40c3-4253-98c5-07c9ab03a537&t=afdbb334-40c3-4253-98c5-07c9ab03a537&width=336&cache=v2)
关于旋转矩阵
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F84d7ac9e-6e9d-4ba5-8792-df972ad05a7a%2FUntitled.png?table=block&id=0c350ce7-5894-4db8-8dc0-3cc94e9c6eb9&t=0c350ce7-5894-4db8-8dc0-3cc94e9c6eb9&width=384&cache=v2)
在高维空间中旋转
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fdca0bc4d-772b-4117-8741-7c17c02d582f%2FUntitled.png?table=block&id=ccfd9403-fc13-4b4f-9bdb-c7005d0ad23b&t=ccfd9403-fc13-4b4f-9bdb-c7005d0ad23b&width=576&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fdd14b13c-8574-42a9-b33d-d23c1c9fd565%2FUntitled.png?table=block&id=af53d1d7-9081-4893-8c90-7630e11b24c5&t=af53d1d7-9081-4893-8c90-7630e11b24c5&width=576&cache=v2)
大的具有一些良好的性质,可以将其拆成在不同的子空间上进行不同的旋转。
✨RoPE的Motivation
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F1859518c-c546-4a51-9a40-b0d230276310%2FUntitled.png?table=block&id=b1cd695c-23ba-4bec-80d8-0334b71ad41f&t=b1cd695c-23ba-4bec-80d8-0334b71ad41f&width=384&cache=v2)
二维空间的一个解
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fa0d4fb93-a9c4-4990-8259-6776531e8600%2FUntitled.png?table=block&id=8770bfac-4160-4d2e-a6bf-a5fef0b8f930&t=8770bfac-4160-4d2e-a6bf-a5fef0b8f930&width=405.0000305175781&cache=v2)
在得到QK矩阵,也就是投影之后做,可以得到一个包含i-j信息的解
推广到高维空间
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F302f4e7a-70c2-41c3-9e22-5e52ec6496ed%2FUntitled.png?table=block&id=d6836315-d489-4327-ba97-1138f21680ff&t=d6836315-d489-4327-ba97-1138f21680ff&width=805&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F1fc8f953-92ad-47bf-8840-af3a742114c7%2FUntitled.png?table=block&id=da267b99-d36a-4825-98c5-3d4393e79f05&t=da267b99-d36a-4825-98c5-3d4393e79f05&width=744&cache=v2)
✨整体视角
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F81f6ea26-ed81-412a-9f81-a4fd053d886a%2FUntitled.png?table=block&id=f3d5c1b4-9b6e-4ced-a9ca-5b2ab8647dd8&t=f3d5c1b4-9b6e-4ced-a9ca-5b2ab8647dd8&width=576&cache=v2)
区分度
这种方式不会产生周期重复
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F7cbdc3a4-37c8-45b5-9470-44c87da66237%2FUntitled.png?table=block&id=c3abde86-7644-4e59-8b86-fcafc2b9bd43&t=c3abde86-7644-4e59-8b86-fcafc2b9bd43&width=733&cache=v2)
可能的另外一个优势
- 在多个block前向传递的过程中,position的位置信息不会丢失
- 每个block都会先做QKV的投影,然后QK投影之后会做位置旋转变换
相对于原来的加性绝对位置编码来说,在前向传递的过程中,位置信息可能会慢慢丢失
✨回看Sin绝对位置编码
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fdee2c436-ec95-40c6-ad92-b905d61d0f37%2FUntitled.png?table=block&id=6d6d2059-64c8-4971-8c00-4498697ec722&t=6d6d2059-64c8-4971-8c00-4498697ec722&width=1552&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Faa6af294-c52b-4253-b71d-71840926d4ce%2FUntitled.png?table=block&id=ab69f0a7-45ef-4e6e-b43e-c9f661589291&t=ab69f0a7-45ef-4e6e-b43e-c9f661589291&width=658.888916015625&cache=v2)
✨RoPE代码实现
在代码实现中,避开了旋转矩阵相乘
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F4726ffbf-5ffc-4f7e-b4c7-159dfc1252a5%2FUntitled.png?table=block&id=81ec6745-489d-496f-80ff-9f14b6a3091f&t=81ec6745-489d-496f-80ff-9f14b6a3091f&width=720&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F2d9633e1-0626-42a8-ab16-492a4a6df98b%2FUntitled.png?table=block&id=2fcc0c74-7f1a-4567-8aa1-e006e610fe4e&t=2fcc0c74-7f1a-4567-8aa1-e006e610fe4e&width=839&cache=v2)
代码实现如下:
带RoPE的self-attention代码实现:
✨RoPE总结
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fe0f5d6eb-cee3-430a-b1c6-687c20ffa8cf%2FUntitled.png?table=block&id=1d2221c0-b1b1-4694-8048-67587ec238f2&t=1d2221c0-b1b1-4694-8048-67587ec238f2&width=685.0000610351562&cache=v2)
6. CoPE
✨Motivation
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fba13ab89-2ce6-49e1-ae97-b2c39d87bf4e%2FUntitled.png?table=block&id=fde5c65d-eb99-48ac-b268-8fd269a52d18&t=fde5c65d-eb99-48ac-b268-8fd269a52d18&width=1185&cache=v2)
若采用相对位置编码方式(这里举的例子是一种更简单的相对位置编码,当然RoPE之类的相对位置编码),对于当前词元的位置往前数,这个Attension Score的值是在逐步递减的,
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fea8f93c7-5947-4ec2-9b7b-b7fd48753583%2FUntitled.png?table=block&id=a36a8f81-5f9b-403d-95ad-b0d65c797673&t=a36a8f81-5f9b-403d-95ad-b0d65c797673&width=1186&cache=v2)
✨Method
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F22c32c5f-6391-46da-8f5b-b2ad94094a0f%2FUntitled.png?table=block&id=c494c3a7-04cd-4e14-9637-0e66fcd3552a&t=c494c3a7-04cd-4e14-9637-0e66fcd3552a&width=1193&cache=v2)
✨PreComputing & implementation
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F2c35e475-4d07-4228-a69d-7efcff8956f6%2FUntitled.png?table=block&id=7a239966-908d-4b45-aaaf-549015910526&t=7a239966-908d-4b45-aaaf-549015910526&width=1184&cache=v2)
✨Experiments
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fd18a41fc-f9de-499e-a9dd-31205291f2a3%2FUntitled.png?table=block&id=0efe1f52-832c-4945-bc20-47214cfde1c9&t=0efe1f52-832c-4945-bc20-47214cfde1c9&width=1181&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2Fa3948fa6-f502-4047-b391-1965a1e90e59%2FUntitled.png?table=block&id=61fcff0c-266c-47e4-a1b1-1e7108d983ab&t=61fcff0c-266c-47e4-a1b1-1e7108d983ab&width=1175&cache=v2)
✨Conclusion
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F13d5f67b-84ef-4697-92f8-fac38e183453%2F2c089b0d-a064-4d28-94e8-5a9d5797fec0%2FUntitled.png?table=block&id=4cd10195-a155-4f96-900c-0767a93153d0&t=4cd10195-a155-4f96-900c-0767a93153d0&width=1182&cache=v2)