LLaMA系列模型架构
1.LLama1.1 简介Open and Efficient Foundation Language Models (Open但没完全Open的LLaMA)
2023年2月,Meta(原Facebook)推出了LLaMA大模型,使用了1.4T token进行训练,虽然最大模型只有65B,但在相关评测任务上的效果可以媲美甚至超过千亿级大模型,被认为是近期开源大模型百花⻬放的开端之一,“羊驼”系列模型及其生态快速发展。
LLaMA 所采用的 Transformer 结构和细节,与标准的 Transformer 架构不同的地方包括采用了前置层归一化(Pre-normalization)并使用 RMSNorm 归一化函数 (Normalizing Function)、激活函数更换为 SwiGLU,并使用了旋转位置嵌入(RoP),整体 Transformer 架构与 GPT-2 类似。
1.2 RMSNorm归一化函数为了使得模型训练过程更加稳定,GPT-2 相较于 GPT 就引入了前置层归一化方法,将第一个层归一化移动到多头自注意力层之前,第二个层归一化也移动到了全连接层之前, ...
ChatGLM系列模型架构
1.ChatGLM1.1 背景主流的预训练框架主要有三种:
autoregressive自回归模型(AR模型):代表作GPT。本质上是一个left-to-right的语言模型。通常用于生成式任务,在长文本生成方面取得了巨大的成功,比如自然语言生成(NLG)领域的任务:摘要、翻译或抽象问答。当扩展到十亿级别参数时,表现出了少样本学习能力。缺点是单向注意力机制,在NLU任务中,无法完全捕捉上下文的依赖关系。
autoencoding自编码模型(AE模型):代表作BERT。是通过某个降噪目标(比如MLM)训练的双向文本编码器。编码器会产出适用于NLU任务的上下文表示,但无法直接用于文本生成。
encoder-decoder(Seq2seq模型):代表作T5。采用双向注意力机制,通常用于条件生成任务,比如文本摘要、机器翻译等。
三种预训练框架各有利弊,没有一种框架在以下三种领域的表现最佳:自然语言理解(NLU)、无条件生成以及条件生成。T5曾经尝试使用MTL的方式统一上述框架,然而自编码和自回归目标天然存在差异,简单的融合自然无法继承各个框架的优点。
在这个天下三分的僵持局面下,GLM诞生 ...
LLMs 推理优化技术
原文链接:Mastering LLM Techniques: Inference Optimization | NVIDIA Technical Blog
堆叠Transformer层以创建大型模型可以获得更好的准确性、few-shot学习能力,甚至在各种语言任务中具有接近人类的涌现能力。这些基础模型的训练成本很高,而且在推理过程中可能需要大量的内存和计算(经常性成本)。当今最流行的大型语言模型(LLM)的大小可以达到数百亿到数千亿个参数,并且根据用例的不同,可能需要摄入长输入(或上下文),这也会增加开销。
这篇文章讨论了LLM推理中最紧迫的挑战,以及一些实用的解决方案。读者应该对transformer架构和注意力机制有一个基本的了解。
1.理解LLM推理大多数流行的only-decode LLM(例如 GPT-3)都是针对因果建模目标进行预训练的,本质上是作为下一个词预测器。这些 LLM 将一系列tokens作为输入,并自回归生成后续tokens,直到满足停止条件(例如,生成tokens数量的限制或遇到停止词)或直到生成特殊的 <end> 标记生成结束的tokens ...
主流大语言模型的技术原理细节
原文链接:主流大语言模型的技术原理细节 (qq.com)作者:spring
1.比较 LLaMA、ChatGLM、Falcon 等大语言模型的细节:tokenizer、位置编码、Layer Normalization、激活函数等。2. 大语言模型的分布式训练技术:数据并行、张量模型并行、流水线并行、3D 并行、零冗余优化器 ZeRO、CPU 卸载技术 ZeRo-offload、混合精度训练、激活重计算技术、Flash Attention、Paged Attention。3. 大语言模型的参数高效微调技术:prompt tuning、prefix tuning、adapter、LLaMA-adapter、 LoRA。
0. 大纲
1. 大语言模型的细节1.0 transformer 与 LLM
1.1 模型结构
1.2 训练目标
1.3 tokenizer
1.4 位置编码
1.5 层归一化
1.6 激活函数
1.7 Multi-query Attention 与 Grouped-query Attention
1.8 并行 transformer blo ...
论文精读 MoE经典论文简牍
参考资料:
MoE (Mixture-of-Experts) 经典文章简读
1.开创工作1.1 MoE
论文名称:Adaptive mixtures of local experts, Neural Computation’1991
期刊/会议:Neural Computation (1991)
论文链接:https://readpaper.com/paper/2150884987
代表性作者:Michael Jordan, Geoffrey Hinton
这是大多数MoE论文都引用的最早的一篇文章,发表于1991年,作者中有两个大家熟知的大佬:Michael Jordan 和 Geoffrey Hinton。
提出了一种新的监督学习过程,一个系统中包含多个分开的网络,每个网络去处理全部训练样本的一个子集。这种方式可以看做是把多层网络进行了模块化的转换。
假设我们已经知道数据集中存在一些天然的子集(比如来自不同的domain,不同的topic),那么用单个模型去学习,就会受到很多干扰(interference),导致学习很慢、泛化困难。这时,我们可以使用多个模型(即专家,exp ...
论文精读 MoE
参考文章:
Mixture of Experts-Introduction
Understanding the Mixture-of-Experts Model in Deep Learning
论文相关:
论文名称:Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer
论文地址:Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer
混合专家(Mixture of Experts,MoE)就像是神经网络世界中的一种团队合作技术。想象一下,把一项大任务分解成更小的部分,让不同的专家来处理每个部分。然后,有一个聪明的法官,他根据情况决定遵循哪位专家的建议,所有这些建议都融合在一起。
尽管它最初是用神经网络来解释的,但你可以将这个想法用于任何类型的专家或模型。这有点像你把不同的味道结合在一起做一道美味的菜,这属于一组很酷的综合学习方法,称为元学习。
因此,在本文中,您将了解专 ...
大模型时代下做科研的四个思路
https://www.bilibili.com/video/BV1oX4y1d7X6/?vd_source=6bc8f793c75740c7bcfb8e281f986a8e
1.总览
Efficient(PEFT):提升训练效率,这里以PEFT(parameter efficient fine tuning)为例
Existing stuff(pretrained model)、New directions:使用别人的预训练模型,新的研究方向
plug-and-play:做一些即插即用的模块,例如模型的模块、目标函数、新损失函数、数据增强方法等等。
Dataset,evaluation and survey:构建数据集、发表分析为主的文章或者综述论文
2.Efficient(PEFT)通过论文AIM为例讲述如何进行PEFT,即在硬件资源有限时对大模型进行高效微调
论文地址:https://arxiv.org/abs/2302.03024
论文标题:AIM: Adapting Image Models for Efficient Video Action Recognition
标 ...
论文精读 ELMo
论文名称:Embeddings from Language Models
论文地址:1802.05365.pdf (arxiv.org)
1.ELMo简介ELMo是2018年3月由华盛顿大学提出的一种预训练模型。
ELMo模型提出的动机源于研究人员认为一个好的预训练语言模型应该能够包含丰富的句法和语义信息, 并且能够对多义词进行建模。 而传统的词向量(2013年的word2vec, 2014年的GloVe)都是上下文无关的,也就是固定的词向量。最典型的例子就是”apple”在不同的语境下, 应该可以表示水果或公司,但是固定的词向量显然无法做到这一点。 因为研究团队利用新的语言模型训练一个上下文相关的预训练模型, 成为ELMo, 并在6个NLP任务上获得提升。
2.ELMo架构2.1 总体架构
从上面的架构图中可以看到, 宏观上ELMo分三个主要模块。
最底层黄色标记的Embedding模块。
中间层蓝色标记的两部分双层LSTM模块。
最上层绿色标记的词向量表征模块。
2.2 Embedding模块ELMo最底层的词嵌入采用CNN对字符级进行编码,本质就是获得一个静态的词嵌入向量 ...
20 字符串算法
20.字符串算法1.字符串基础知识1.1 字符串定义Are your strings immutable? – Daniel Lemire’s blog
Reading 9: Mutability & Immutability (mit.edu)
Python和Java的String是immutable的;immutable指不可变的,即定义了String后,就是不可变的,当增加字符和删减字母时,是新生成了一个字符串。
C++的String是可变的。
immutable优点:线程安全的,可以在多线程环境中使用
Python
12x = 'abbc'x = "abbc"
Java
1String x = "abbc";
C++
1std::string x("abbc");
1.2 遍历字符串python
123for ch in "abbc": print(ch)
Java
12345678String x = "abbc";for (int i = 0; ...
数据结构与算法 (Data Structures and Algorithms)
建立时间复杂度、空间复杂度意识,写出高质量的代码,能够设计基础架构,提升编程技能,训练逻辑思维。
1.易混淆专题
二分查找
2.LeetCode刷题专栏
数据结构与算法总览
复杂度分析
数组、链表、跳表
栈、队列
哈希表、映射、集合
树
递归
分治、回溯
深度优先、广度优先
贪心算法
二分查找
动态规划
字典树和并查集
高级搜索
红黑树和AVL树
位运算
布隆过滤器和LRU缓存
排序算法
高级动态规划
字符串算法
3.LeetCode专题系列文章
编程语言 (Programming Language)
建立时间复杂度、空间复杂度意识,写出高质量的代码,能够设计基础架构,提升编程技能,训练逻辑思维。
1.C++2.Cython
Cython概述
Cython编译运行
Cython语法介绍
Cython中扩展类
Cython模块导入
Cython使用C/C++外部库
01 数据结构与算法总览
1.数据结构百度脑图-便捷的思维工具 (baidu.com)
一维
基础:数组 array (string),链表 linked list
高级:栈 stack, 队列 queue,双端队列 deque,集合 set,映射 map (hash or map),etc
二维
基础:树tree,图 graph
高级:二叉搜索树 binary search tree (red-black tree, AVL),堆 heap,并查集 disjoint set,字典树 Trie
特殊
位运算 Bitwise,布隆过滤器 BloomFilter
LRU Cache
2.算法百度脑图-便捷的思维工具 (baidu.com)
if-else, switch → branch
for, while loop → iteration
递归 Recursion(Divide & Conquer, Backtrace)
搜索 Search:DFS, BFS, A*
动态规划 Dynamic Programming
二分查找 Binary Search
贪心 Greedy
数学 Math ...
02 复杂度分析
1.常用工具配置1.1 电脑设置
Google
Mac:iTerm2 + zsh (or my zsh)
Windows :
VsCode :
VS Code Themes
炫酷的VS Code毛玻璃效果 - 掘金 (juejin.cn)
LeetCode plugin (VsCode)
1.2 Code Style1.3 LeetCode
国内版:刷题联系
国际版:最高票的题解,
1.4 指法操作
ctrl+left/right:选择单词
IDE自动补全
Top tips for \
1.5自定向下的编程方式Clean Code: Book Review | Mark Needham (markhneedham.com)
The best idea in this book for me was the newspaper metaphor that is mentioned with regards to formatting your code. This describes the idea of making code read like a ...
03 数组、链表、跳表
1.基本实现和特性1.1 数组
Java 源码分析(ArrayList)
内存中,一段连续的地址,可以通过内存管理器直接访问,时间复杂度为$O(1)$,访问时间比较快;
增加删除元素比较麻烦,时间复杂度为 $O(n)$
1.2 链表
Linked List 的标准实现代码
Linked List 示例代码
Java 源码分析(LinkedList)
LRU Cache - Linked list: LRU 缓存机制
增加删除结点时间复杂度:$O(1)$
访问结点时间复杂度:$O(n)$
1.3 跳表 SkipList
Redis - Skip List:跳跃表
为啥 Redis 使用跳表(Skip List)而不是使用 Red-Black?
主要在Redis中使用
链表的缺陷:访问时间复杂度比较高 $O(n)$
给链表进行加速中心思想:升维(空间换时间)
跳表:索引,增加索引,链表next速度为1,一级索引速度为2,二级索引速度为4,
实际使用中,可以增加多级索引,实际增加 $log~2n$级索引
跳表查询的时间复杂度 $O(logn)$n/2, n/4, n/8, 第 ...
04 栈、队列
1.栈和队列的基本实现和特性1.1 基本性质Stack:先入后出;添加、删除为$O(1)$,查询为$O(n)$
Queue :先入先出;添加、查询为$O(1)$
栈
队列
1.2 双端队列 插入和删除是 $O(1)$,查询为 $O(n)$
1.3 工程实现
Java 的 Stack 源码
Java 的 Queue 源码
Python 的 heapq
高性能的 container 库
1.4 优先队列Java 的 PriorityQueue 文档
插入操作: $O(1)$
取出操作: $O(log~n)$,按照元素的优先级取出
底层具体实现的数据结构较为多样和复杂:heap、bst、treap
1.5 复杂度分析Big-O Algorithm Complexity Cheat Sheet (Know Thy Complexities!) @ericdrowell (bigocheatsheet.com)
2.题目2.1 有效括号20. 有效的括号 - 力扣(LeetCode)
123456给定一个只包括 '(',')', ...