UV: The Definitive Solution for PyTorch, flash-attn, VeRL and OpenRLHF
Background 在配置 flash-attn 环境时,经常会遇到安装报错,由于 flash-attn 需要在本地进行编译构建。此外,根据 vLLM GPU Installation,vLLM 团队也在积极使用 uv 这一包管理工具。本文借助最新版本的 uv,给出 linux 下的 flash-attn, PyTorch 等安装的终极解决方案。本文的测试环境为 Ubuntu 22.04.5 WSL2,cuda 版本如下: $ nvidia-smi+-----------------------------------------------------------------------------------------+| NVIDIA-SMI 572.83 Driver Version: 572.83 CUDA Version: 12.8 ||-----------------------------------------+------------------------+--------------------...
Policy Gradient Algorithms: From REINFORCE to PPO
在阅读本文前,需要了解 Policy Gradient Theorem,主要包括策略梯度的求导,强化学习的目标函数。 Policy Gradient Theorem 策略梯度一般的形式如下: ∇θJ(θ)=Eτ∼P(⋅;θ)[∑t=0T−1Ψt∇θlnπθ(at∣st)]\nabla_{\theta} J(\theta) = \mathbb{E}_{\tau \sim \mathbb{P}(\cdot;\theta)}\Bigg[\sum_{t=0}^{T-1} \Psi_{t} \nabla_{\theta} \ln \pi_{\theta}(a_{t} \mid s_{t})\Bigg] ∇θJ(θ)=Eτ∼P(⋅;θ)[t=0∑T−1Ψt∇θlnπθ(at∣st)] 其中,Ψt\Psi_{t}Ψt 可以有如下取值: G(τ)G(\tau)G(τ): 整个轨迹上的回报 GtG_{t}Gt: 自动作 ata_{t}at 后的回报 Gt−b(st)G_{t}-b(s_{t})Gt−b(st): 自动作 ata_{t}at 后回报的 baseline...
The Evolution of Policy Optimization for Enhancing LLM Reasoning: From PPO to GRPO Variants
Preliminary Basic Concepts 状态(State):在时间步 ttt,状态 sts_{t}st 是 prompt (a.k.a., query, question) qqq 与 generated tokens o<to_{<t}o<t 的拼接。即,st=[q,o<t]s_{t} = [q,o_{<t}]st=[q,o<t]。其中: s1=q∼pQs_{1} = q \sim p_{\mathcal{Q}}s1=q∼pQ: 初态 s1s_{1}s1 是从数据集 pQp_{\mathcal{Q}}pQ 中采样的 question qqq o<t=[o1,o2,…,ot−1]o_{<t}=[o_{1},o_{2},\ldots,o_{t-1}]o<t=[o1,o2,…,ot−1]: 截止到当前时间步 ttt 的 output st+1=[st;ot]=[q;o<t+1]s_{t+1} = [s_{t};o_{t}] = [q;o_{<t+1}]st+1=[st;ot...
An Introduction to PPO in RLHF
Reinforcement Learning in NLP 首先,回顾强化学习(Reinforcement Learning, RL)的基本流程。RL 的两个核心角色为智能体(Agent)与环境(Environment)。Agent 位于环境中,且与环境进行交互。在时刻 ttt,二者交互流程如下: Agent 观测到环境的状态(State)sts_{t}st,接着决定采取的行动(Action)ata_{t}at 环境由于 Agent 的动作而改变(也可能自主改变),同时给予 Agent 即时奖励(Reward)rtr_{t}rt RL 的目标是为了学习一个策略 πθ(a∣s)\pi_{\theta}(a \mid s)πθ(a∣s),以指导 agent 在处于状态 sss 下,采取行动 aaa,从而获得最大的累计奖励(Cumulative Reward)GtG_{t}Gt,也称为回报(Return)。 在 LLMs 的背景下,RL 概念的对应关系如下: Agent: LLM 自身 Environment: RLHF 下的环境是一个较为抽象的概念,是确定性的和非交互...
X-Enhanced Contrastive Decoding Strategies for Large Language Models
Notations 以下是与 LLMs 推理相关的通用符号表示: ttt: 当前解码时间步 V\mathcal{V}V: token 词汇表 x\mathbf{x}x: 输入序列 y∈Vy \in \mathcal{V}y∈V: 候选 next token yt∈Vy_{t} \in \mathcal{V}yt∈V: 当前时间步 ttt 选择的 token y<t=[y1,y2,…,yt−1]\mathbf{y}_{<t} = [y_{1}, y_{2}, \ldots, y_{t-1}]y<t=[y1,y2,…,yt−1]: 当前时间步 ttt 之前已生成的输出序列 l(y∣x,y<t)\mathbf{l}(y \mid \mathbf{x},\mathbf{y}_{<t})l(y∣x,y<t): 给定上文(即:输入序列和已生成的输出序列)的 next token 在 softmax 归一化前的 logits 分数 p(y∣x,y<t)p(y \mid \mathbf{x},\mathbf{y}_{<t})p(y∣x...
An Introduction to Policy Optimization
在阅读本文前,需要了解 强化学习的基本概念。本文按顺序探讨了以下几个部分: 策略方法的目标 策略梯度的计算 策略网络的简易训练步骤 reward-to-go 策略梯度 一般形式的策略梯度 在深度学习的背景下,我们规定策略函数使用神经网络近似,表示为:πθ(a∣s)\pi_{\theta}(a \mid s)πθ(a∣s)。其中,θ\thetaθ 为策略网络的参数。 Related Material 本文主要参考 OpenAI Spinning Up Documentation Part 3: Intro to Policy Optimization 编写,对部分内容的进行了删减,感兴趣的读者可以阅读原文。此外,为了确保数学公式的严谨性,参考了 Policy Gradient Algorithms | Lil’Log,该文章主要围绕数学理论展开。类似的文章还有 Going Deeper Into Reinforcement Learning: Fundamentals of Policy Gradients。 Optimization Objective 回顾即时收益与回报的...
Key Concepts in Reinforcement Learning
Related Material 本文主要摘录自 OpenAI Spinning Up Documentation Part 1: Key Concepts in RL,忽略了对具体实现的讲解,同时对回报,价值函数等概念进行了更规范的形式化表述。此外,对 Bellman Equations 展开了详尽地推导。A (Long) Peek into Reinforcement Learning | Lil’Log 一文对这些概念的讲解更加理论深入。 Overview of Reinforcement Learning 强化学习(Reinforcement Learning, RL)中的主要角色是智能体(Agent) 和 环境(Environment)。agent 位于环境中,且与环境进行交互。在每一步交互中, agent 会观测当前环境状态(State)的(可能不完整的)部分信息,然后决定采取一个动作(Action) agent 的动作会改变环境,但环境也可能自行发生变化。同时,环境会给 agent 一个奖励(Reward),用来告诉它当前状态的好坏。 强化学习的目标在于,学习如...
LoRA: From Principle to Implementation
LoRA (Low-Rank Adaptation) is a parameter-efficient fine-tuning (PEFT) technique designed to reduce the number of trainable parameters in LLMs while maintaining high performance. It achieves this by decomposing weight updates into low-rank matrices, significantly reducing memory and computational costs. Motivation: Intrinsic Dimension Hypothesis The Intrinsic Dimension Hypothesis states that high-dimensional models actually lie on a much lower-dimensional subspace. Similarly, LoRA hypothesize...
Additive PEFT: From Adapter to Proxy Tuning
PEFT Taxonomy Taxonomy Idea Examples Additive PEFT Introduce extra trainable parameters while freezing the original ones. Adapter Tuning, Prefix Tuning, Prompt Tuning, Proxy Tuning Selective PEFT Update a subset of the original parameters while freezing the rest. BitFit, Child Tuning Reparameterized PEFT Transform existing parameters for efficient training, then revert back for inference LoRA Based on where the trainable parameters are introduced, additive PEFT can be further c...
Building GPT-2 from Scratch: A Detailed Implementation Guide
本文主要讲解模型的实现,辅以必要的数学公式。而对数学原理的深入讲解详见 Transformer。请确保阅读此文前,对 Transformer Decoder 的数学原理已有全面的了解。 本文代码的实现主要基于 karpathy/nanoGPT 与 karpathy/build-nanogpt,同时参考了 Hugging Face transformers 库对 GPT-2 的实现。本文的 Reference 一节列出了详细的参考列表。与 nanoGPT 实现的差异主要在于: 模块初始化的参数:nanoGPT 与 HuggingFace 多以 config 字典直接传入,而本文各模块的 __init__ 初始化方法,详细列出了每个参数,可以类比字典解包 超参数的数量与名称:仿照 HuggingFace 的实现,添加了 layer_norm_epsilon,修改 block_size 为 n_positions Transformer Decoder and GPT-2 Transformer decoder 由多个相同的 block 堆叠而成,每个 block 结构相同,均由三...