检索增强LLM
文章来源:万字长文: 检索增强 LLM (qq.com)
ChatGPT 的出现,让我们看到了大语言模型 ( Large Language Model, LLM ) 在语言和代码理解、人类指令遵循、基本推理等多方面的能力,但幻觉问题 Hallucinations 仍然是当前大语言模型面临的一个重要挑战。简单来说,幻觉问题是指 LLM 生成不正确、荒谬或者与事实不符的结果。此外,数据新鲜度 ( Data Freshness ) 也是 LLM 在生成结果时出现的另外一个问题,即 LLM 对于一些时效性比较强的问题可能给不出或者给出过时的答案。而通过检索外部相关信息的方式来增强 LLM 的生成结果是当前解决以上问题的一种流行方案,这里把这种方案称为 检索增强 LLM ( Retrieval Augmented LLM ),有时候也被称为 检索增强生成 ( Retrieval Augmented Generation, RAG )。
这篇长文将对检索增强 LLM 的方案进行一个相对全面的介绍。主要内容包括:
检索增强 LLM 的概念介绍、重要性及其解决的问题
检索增强 LLM ...
LLMs公开课 - 6.文本理解和生成大模型
1.简介1.1 NLP的主要应用NLP的主要应用主要分为两类:自然语言理解(NLU)和自然语言生成(NLG)。
信息检索是NLU非常有代表性的应用;
文本生成是NLG一个代表性例子;
机器问答综合了自然语言理解和自然语言生成两个任务。
在这三种任务中,大模型都带来了一定的变革。
1.2 信息检索信息检索是非常古老、非常经典的任务。在这一方面,大模型可以帮助机器来提供更加智能、更加准确的搜索结果。
1.3 机器问答问机器一些问题,希望机器能提供我们想要的答案。传统的机器问答方法是基于模板、或者基于知识库的,这样使得它的问答范围受限。
但现在大模型允许机器回答更加复杂的问题,从下面的例子中,列出一些最先进的大模型可以回答的问题。可以看到,即使它背后没有一个知识库去支撑它搜索相关的知识,大模型里面蕴含的知识也足以帮助机器回答上述问题。
1.4文本生成利用大模型可以帮助机器生成更加流畅、自然的文本。
2.信息检索2.1 背景信息以爆炸的形式增加,用户对信息检索的需求也是在急剧增长。
可以看到全球的信息用户数量也非常庞大。
自动检索:根据用户的查询,从海量 ...
LLMs公开课 - 5.高效训练&模型压缩
1.背景介绍1.1 CPU & GPU预训练语言模型以每年十倍的速度增大,越大的模型往往表现出更好的性能;但为了训练这些模型耗费也越来越昂贵,训练代码变得更复杂。
希望让训练过程变得更加简单,训练变得更高效,并且训练更加廉价。
首先要分析GPU内存;其次理解在多张显卡之间的合作模式是怎样的。
深度学习中最常见的矩阵乘法和向量加法适合于用GPU来计算。
CPU和GPU的合作方法通过CPU发送一些控制信号去控制GPU进行计算。
如果想把模型的向量加法或矩阵乘法放到GPU中计算的话,需要把这些数据从CPU上拷贝到GPU上(.cuda)。
显卡中有哪些显存的组成。
1.2 显存组成(1)参数为了加速模型的前向传播,需要把模型所有的参数都放到显卡中。
(2)梯度在反向传播过程中,计算得到的梯度也保存到显卡中。
(3)中间结果模型的中间计算结果,比如线性层 $y=Wx$,为了计算反向传播,需要在前向传播时在显卡中保存模型的输入(中间结果)。
(4)优化器第四部分,在显存中占大头的一部分,就是优化器,比如Adam,需要保存模型的梯度,和相关的历史信息(m ...
LLMs公开课 - 4.Prompt Tuning & Delta Tuning
1.Background1.1 Fine Tuning : BERTBERT不管输入是单句还是两句,它实际上会对每一个token产生一个表征,这些表征可以进行分类。
如果做的是token级别的分类任务,比如NER,那么这些token的表征会送到一个分类层中,来对每个token进行分类。
如果做的是句子级别的分类任务,那么一般会把这个[CLS] token,代表句子级别语义,送到分类层中。
也就是时候,BERT对于不同的任务,会将不同的表征送到分类层中去,比从零学习的神经网络效果会好很多。
BERT : 关系抽取
用 [CLS] 进行分类
Mention Pooling, 实体之间的所有token,concat到一起,然后再接分类层。
Mention Pooling, 区分不同实体,在embedding加入Token type embedding;最后实体之间的所有token,concat到一起,然后再接分类层。考虑位置信息。
Entity Markers : 词表中增加特殊的字符
可以看到这些做法是非常经验性的,并且没有那么直观,最后我们都需要去训练一个分类器,即需要随机初始 ...
LLMs公开课 - 3.Transformer基础
1.Transformer1.1 注意力机制(1)Seq2Seq注意力传统的Seq2Seq序列模型存在信息瓶颈的问题
源句子编码的单向量需要捕获远举子的所有信息
单向量限制了编码器的表示能力:信息瓶颈
注意力机制:
注意力提供了瓶颈问题的解决方案;
核心思想:在解码器的每一步,专注于源序列的特定部分。
(2)Seq2Seq注意力机制
Encoder隐藏状态:$h_{1}, h_{2} \ldots, h_{N} \in \mathbb{R}^{h}$
Decoder在$t$时刻的隐藏状态:$s_{t} \in \mathbb{R}^{h}$
在$t$时刻,计算注意力分数 $e_t$ : $e^{t}=\left[s_{t}^{T} h_{1}, \ldots, s_{t}^{T} h_{N}\right] \in \mathbb{R}^{N}$
使用softmax得到注意力分布$\alpha_t$ :$\alpha^{t}=\operatorname{softmax}\left(e^{t}\right) \in \mathbb{R}^{N}$
利用注意力分布计算编码器隐藏状态 ...
LLMs公开课 - 2.神经网络基础
1.神经网络组成部分1.1 神经网络(1)神经元人工神经网络:灵感来自于大脑中的生物神经网络
神经元是一个具有输入和一个输出和参数$w$,$b$的计算单元
h_{\boldsymbol{w}, b}(\boldsymbol{x})=f\left(\boldsymbol{w}^{T} \boldsymbol{x}+b\right)
(2)单层神经网络
(3)多层神经网络
\begin{array}{l}\boldsymbol{h}_{1}=f\left(\boldsymbol{W}_{1} \boldsymbol{x}+\boldsymbol{b}_{1}\right) \\ \boldsymbol{h}_{2}=f\left(\boldsymbol{W}_{2} \boldsymbol{h}_{1}+\boldsymbol{b}_{2}\right) \\ \boldsymbol{h}_{3}=f\left(\boldsymbol{W}_{3} \boldsymbol{h}_{2}+\boldsymbol{b}_{3}\right)\end{array}1.2 激活函数如果神经网 ...
滑动窗口(Slide Window)
1.滑动窗口算法1.1 滑动窗口介绍
滑动窗口算法(Sliding Window):在给定数组 / 字符串上维护一个固定长度或不定长度的窗口。可以对窗口进行滑动操作、缩放操作,以及维护最优解操作。
滑动操作:窗口可按照一定方向进行移动。最常见的是向右侧移动。
缩放操作:对于不定长度的窗口,可以从左侧缩小窗口长度,也可以从右侧增大窗口长度。
滑动窗口利用了双指针中的快慢指针技巧,可以将滑动窗口看做是快慢指针两个指针中间的区间,也可以将滑动窗口看做是快慢指针的一种特殊形式。
1.2 适用范围滑动窗口算法一般用来解决一些查找满足一定条件的连续区间的性质(长度等)的问题。该算法可以将一部分问题中的嵌套循环转变为一个单循环,因此它可以减少时间复杂度。
按照窗口长度的固定情况,可以将滑动窗口题目分为以下两种:
固定长度窗口:窗口大小是固定的。
不定长度窗口:窗口大小是不固定的。
求解最大的满足条件的窗口。
求解最小的满足条件的窗口。
2.固定长度的滑动窗口
固定长度滑动窗口算法(Fixed Length Sliding Window):在给定数组 / 字符串上维护一个固定长度的窗口 ...
LLMs公开课 - 1.NLP&大模型基础
1.自然语言处理1.1 基础与应用(1)图灵测试原名:Imitation Game
采用一种行为注意的手段,尝试定义人工智能是不是具备人类智能的水平
1997年,人工智能在象棋方面战胜人类
2011年,IBM Watson DeepQA在问答节目上战胜所有人类。
2016年,Alpha go 在围棋方面战胜人类
(2)NLP任务基础任务:
词性标注:
命名实体识别:
共指消解:用代词代替实体
句法关系:互相依存关系
中文自动分词:
1.2 词表示(1)词表示目的:将单词转换为机器可以理解的符号
目标:
词之间相似度的计算
词之间语义的关系
(2)用一组相关的词表示近义词,上位词;
问题:
词语之间的较小差异无法区分;
词义会发生变化,出现新的词义;
主观性的问题,受限于词典的标注;
数据稀疏问题;
大量的人工去构建、维护词典;
(3)one-hot表示把每个词表示成独立的符号;
和词表一样长的向量去找一维跟这个词相对应,整个向量的维度跟词表的长度是相当的;
用来表示文档时非常有效,能较好地完成两个文档之间的相似度计算;
但是,在表示词的时候会有问题:会假设词根词之间的向 ...
数组双指针(Double Pointer)
1.双指针简介
双指针(Two Pointers):指的是在遍历元素的过程中,不是使用单个指针进行访问,而是使用两个指针进行访问,从而达到相应的目的。如果两个指针方向相反,则称为「对撞指针」。如果两个指针方向相同,则称为「快慢指针」。如果两个指针分别属于不同的数组 / 链表,则称为「分离双指针」。
在数组的区间问题上,暴力算法的时间复杂度往往是 $O(n^2)$。而双指针利用了区间「单调性」的性质,可以将时间复杂度降到 $O(n)$。
2.对撞指针
对撞指针:指的是两个指针 $left$、$right$ 分别指向序列第一个元素和最后一个元素,然后 $left$ 指针不断递增,$right$ 不断递减,直到两个指针的值相撞(即 $left == right$),或者满足其他要求的特殊条件为止。
2.1 对撞指针求解步骤
使用两个指针 $left$,$right$。$left$ 指向序列第一个元素,即:$left = 0$,$right$ 指向序列最后一个元素,即:$right = len(nums) - 1$。
在循环体中将左右指针相向移动,当满足一定条件时,将左指针右移,$lef ...
论文精读 ZeRO
ZeRO: Memory Optimizations Toward Training Trillion Parameter Models
论文链接:ZeRO: Memory Optimizations Toward Training Trillion Parameter Models
代码链接: https://github.com/microsoft/DeepSpeed
李沐讲解:https://www.bilibili.com/video/BV1tY411g7ZT
1.摘要ZeRO 是一种用于大规模深度学习模型的内存优化解决方案,通过消除数据和模型并行训练中的内存冗余,同时保持了低通信量和高计算粒度。
实现了ZeRO,可以训练超过100B参数的模型,在400个GPUs上实现超线性加速。
模型训练的挑战:传统的 DP 方法不能减少每个设备的内存使用量,导致在当时 32G 显存的 GPU 上无法训练超过 14 亿个参数的模型
现有的方法:流水线并行,模型并行, CPU offloading,在功能性,内存使用,计算通信效率之间做出了取舍
作者提到模型并行(张量并行)最有潜力,需要将 ...
LLM 推理常见参数
文章参考:LLM 推理常见参数解析 (qq.com)
1.引言 以下图Huggingface Inference API为例(其他框架类似),这里重点介绍$top_k$,$top_p$,$temperature$,$repetition_penalty$参数,以及$greedy~search$和$beam~search$。
2.背景介绍现在常见的LLM基本都是只包含Transformer Decoder的,每个Token在输入模型的Transformer Decoder之前,都会首先从Token Embedding(有些也叫Word Embedding)中通过查表获取对应的embedding向量,然后将embedding向量输入Transformer Decoder,并且在最后一层输出的也是同维度的embedding。在预测下一个Token时,实际只利用了上一个Token的embedding。
如下图所示,将输入“a robot must obey the orders given it”对应的embedding输入Transformer Decoding后,在最后的 ...
大语言模型方法与实践
崔一鸣(https://ymcui.com)老师分享了一期《大规模预训练语言模型方法与实践》,本文为课程笔记。
1.自然语言表示发展简介
2.生成式预训练语言模型:GPT系列
2.1 GPT
2.2 GPT-2
2.3 GPT-3
2.4 InstructGPT
2.5 ChatGPT(GPT-3.5)2022年11月30日,OpenAI推出了划时代的“ChatGPT”
ChatGPT是InstructGPT的衍生模型,可以理解和执行指令并给出详细的回复
被广泛认为是近期“大模型”兴起的开端
ChatGPT打破了预训练模型的刻板印象,展现出了极强的创造力和可能性
2.6 GPT-42023年3月,OpenAI发布了最新一代大型多模态模型 GPT-4,是迄今为止GPT家族中能力最强的模型
主要提升:生成的文本更具创造性、支持图像输入、支持更⻓的 上下文、显著提升推理能力
其他特点
花费了6个月的时间让GPT-4更安全更符合人类偏好
相比GPT3.5能够在拒答问题上提升82%,事实性提 升40%
目前Web Demo界面仅向Plus用户 ...
二分查找(Binary Search)
1.无处不在的二分查找思想二分查找是一种非常简单易懂的快速查找算法,生活中到处可见。
举一个例子,假设只有10个订单,订单金额分别是: 8, 11, 19, 23, 27, 33, 45, 55, 67, 98。从中查找到金额等于19元的订单。
利用二分思想,每次都与区间的中间数据比对大小,缩小查找区间的范围。
二分查找针对的是一个有序的数据集合,查找思想有点类似分治思想。每次都通过跟区间的中间元素对比,将待查找的区间缩小为之前的一半,直到找到要查找的元素,或者区间被缩小为0。
2.O(logn)惊人的查找速度二分查找是一种非常高效的查找算法。
假设数据大小是n,每次查找后数据都会缩小为原来的一半,也就是会除以2。最坏情况下,直到查找区间被缩小为空,才停止。
可以看出来,这是一个等比数列。其中n/2k=1时, k的值就是总共缩小的次数。而每一次缩小操作只涉及两个数据的大小比较,所以,经过了k次区间缩小操作,时间复杂度就是O(k)。通过n/2k=1,可以求得k=log2n,所以时间复杂度就是O(logn)。
3.二分查找的实现简单的二分查找并不难写,注意这里的“简单”二字
最简单的情 ...
大模型Agent技术
视频链接:https://www.bilibili.com/video/BV1mC4y1g7cT文字版链接:https://mp.weixin.qq.com/s/PL-QjlvVugUfmRD4g0P-qQ
现在全球对Agent的关注也是非常狂热的,几个月前,OpenAI 在内部就开始高度关注智能体(Agent)领域,Deep Mind的联合创始人最近也提到下一代 AI 技术走向并非是生成性 AI,而应该是交互性 AI。这种交互性 AI 在很大程度上类似提到的智能体,用户要求完成各种任务,智能体则可以对软件进行操作或者与人进行协作,完成相关的工作。
主要包含以下内容:
LLM Agents综述:对从大模型到现在的智能体的技术发展做一个串讲
通用智能基本原理:介绍通用智能原理和面向目标架构这个两个根本性问题
面向目标架构:
前瞻性分析:
1. LLM Agents综述如果你一直关注 AI 领域,你应该能看到一个清晰的技术脉络,一开始大家玩** Prompt 工程,接着是Prompt Chain或Flow,再到Agent,多Agent**,很清晰的一个脉络架构,我们也会沿着这个脉络 ...
RAG(检索增强生成)技术
1.基本概念检索增强 LLM ( Retrieval Augmented LLM ),简单来说,就是给 LLM 提供外部数据库,对于用户问题 ( Query ),通过一些信息检索 ( Information Retrieval, IR ) 的技术,先从外部数据库中检索出和用户问题相关的信息,然后让 LLM 结合这些相关信息来生成结果。下图是一个检索增强 LLM 的简单示意图。
传统的信息检索工具,比如 Google/Bing 这样的搜索引擎,只有检索能力 ( Retrieval-only ),现在 LLM 通过预训练过程,将海量数据和知识嵌入到其巨大的模型参数中,具有记忆能力 ( Memory-only )。从这个角度看,检索增强 LLM 处于中间,将 LLM 和传统的信息检索相结合,通过一些信息检索技术将相关信息加载到 LLM 的工作内存 ( Working Memory ) 中,即 LLM 的上下文窗口 ( Context Window ),亦即 LLM 单次生成时能接受的最大文本输入。
2.RAG解决的问题
参考资料:ACL 2023 Tutorial: Retrieval-b ...