Part V · 第 21 章 专题 · 21.1Part V · Ch. 21 Special Topics · 21.1

预训练与微调(LoRA)Pre-training & Fine-tuning (LoRA)

先在大量、广泛的数据上训练一个模型,再用一小份新数据把它改造成你要的样子:这就是今天几乎所有机器人基础模型的用法。本节在浏览器里先用 32 台机器人的数据预训练一个关节力矩模型,再用几十个测量点把它适配到一台新机器人上,现场比较线性探针、全量微调和 LoRA:谁更省数据,谁会忘掉旧本领,为什么一个只有 2 列的低秩更新就够用,以及 4 位量化(QLoRA)怎样让 650 亿参数的模型在一张显卡上微调。 First train a model on a lot of broad data, then reshape it into what you need with a small amount of new data: that is how nearly every robot foundation model is used today. This lesson pre-trains a joint-torque model on data from 32 robots right in your browser, then adapts it to a new robot from a few dozen measurements, comparing linear probing, full fine-tuning and LoRA live: which needs the least data, which forgets its old skills, why a low-rank update with only 2 columns is enough, and how 4-bit quantization (QLoRA) lets a 65-billion-parameter model be fine-tuned on a single GPU.

约 50 分钟~50 min 前置:17.3 Transformer 与 Attention · 18.3 对比学习 · 16.2 泛化与正则化 · 1.13 最小二乘 · 1.12 SVDPrereqs: 17.3 Transformers & Attention · 18.3 Contrastive learning · 16.2 Generalization & regularization · 1.13 Least squares · 1.12 SVD 6 个交互6 interactives

    1为什么机器人需要它Why robots need this

    机器人数据很贵。一条遥操作演示要一个人花上一两分钟,一台机器人一天也就采几百条;而 17.3 节那样的 Transformer 动辄上亿参数,从零开始训练需要的数据量远远超过一个实验室能采到的。另一方面,互联网上有数十亿张图片和句子,开放数据集里有上百万条来自几十种机器人的轨迹。预训练 + 微调把这两件事接起来:先在“广而杂”的大数据上学到通用的表示和技能(预训练),再用目标任务的“少而精”的数据做一次小幅修改(微调)。

    18.3 节已经见过最简单的一种:冻结预训练好的编码器,只在它的特征上训练一个线性分类器(线性探针)。本节把问题问完整:什么时候冻结就够了?什么时候必须改动网络本身?改动之后,模型原来会的东西还在不在?一个 70 亿参数的视觉-语言-动作模型(VLA),要怎样才能在一张显卡上、用一两百条演示完成微调?答案的核心是 LoRA:不改原始权重,只学一个低秩的增量 \(\Delta W=BA\)。

    Robot data is expensive. One teleoperated demonstration costs a person a minute or two, and a robot collects a few hundred in a day; yet a transformer like those of Lesson 17.3 easily has hundreds of millions of parameters, and training one from scratch needs far more data than a lab can collect. Meanwhile the internet holds billions of images and sentences, and open datasets hold millions of trajectories from dozens of robots. Pre-training + fine-tuning connects the two: first learn general representations and skills from broad, messy, big data (pre-training), then make a small modification with the target task's small, clean data (fine-tuning).

    Lesson 18.3 already showed the simplest version: freeze a pre-trained encoder and train only a linear classifier on its features (a linear probe). This lesson asks the full question: when is freezing enough? When must the network itself change? After it changes, does the model still know what it knew before? And how can a 7-billion-parameter vision-language-action model (VLA) be fine-tuned on one GPU from one or two hundred demonstrations? The heart of the answer is LoRA: leave the original weights alone and learn only a low-rank increment \(\Delta W=BA\).

    2直觉:预训练学到的是一组好的“基函数”Intuition: pre-training learns a good set of basis functions

    本节的玩具问题是一个真实的机器人问题:保持力矩。机械臂第 2 个关节要停在角度 \(q\) 不动,电机需要输出多大的力矩 \(\tau(q)\)?它由重力(8.1 节的 \(g(q)\),形如 \(a\cos q+b\sin q\),\(a,b\) 取决于负载和质心)、传感器零偏,以及传动系统里每转重复 4 次的力矩波动组成。同一型号的 32 台机器人负载、零偏、波动的相位都不一样,我们在仿真里为每台采了大量数据。

    预训练的网络是一个“主干 + 多个头”的结构:关节角以 \((\cos q,\sin q)\) 的形式输入(转动关节的常规编码),经过两层 32 个单元的 tanh 隐藏层,再到一个 6 维的表示 \(\hl{phi}{\boldsymbol\phi(q)}\);每台机器人各有一个线性的头,\(\tau_k(q)\approx\mathbf w_k^\top\boldsymbol\phi(q)+b_k\)。为了让 32 个头都满意,主干必须学会这个机器人家族共有的结构。页面打开时这次预训练就在后台跑完了:3000 步,32 台机器人的平均误差降到约 0.014 N·m。

    现在来了第 33 台机器人,我们只测了 12 个角度。最省事的做法是冻结主干,只为它拟合一个新的头 \(\hl{head}{\mathbf w}\):这就是线性探针(linear probe),也就是 1.13 节的最小二乘,基函数换成了 \(\phi_1(q),\dots,\phi_6(q)\)。

    This lesson's toy problem is a real robot problem: the holding torque. For joint 2 of an arm to stay still at angle \(q\), how much torque \(\tau(q)\) must its motor produce? It consists of gravity (the \(g(q)\) of Lesson 8.1, of the form \(a\cos q+b\sin q\), with \(a,b\) set by the payload and the centre of mass), a sensor offset, and a torque ripple from the drive train that repeats 4 times per revolution. The 32 robots of one model all differ in payload, offset and ripple phase, and we have collected plenty of simulated data for each.

    The pre-trained network has a "trunk + many heads" structure: the joint angle enters as \((\cos q,\sin q)\) (the usual encoding of a revolute joint), passes through two tanh hidden layers of 32 units, and arrives at a 6-dimensional representation \(\hl{phi}{\boldsymbol\phi(q)}\); each robot has its own linear head, \(\tau_k(q)\approx\mathbf w_k^\top\boldsymbol\phi(q)+b_k\). To satisfy all 32 heads, the trunk must learn the structure the robot family has in common. This pre-training already ran in the background when the page opened: 3000 steps, after which the mean error over the 32 robots is about 0.014 N·m.

    Now a 33rd robot arrives, and we have measured it at only 12 angles. The cheapest option is to freeze the trunk and fit just a new head \(\hl{head}{\mathbf w}\) for it: a linear probe, which is the least squares of Lesson 1.13 with the basis functions \(\phi_1(q),\dots,\phi_6(q)\).

    $$ \hat\tau(q)=\hl{head}{\mathbf w}^\top\hl{phi}{\boldsymbol\phi(q)}+b,\qquad \mathbf w,b=\arg\min\sum_{i=1}^{N}\big(\mathbf w^\top\boldsymbol\phi(q_i)+b-\tau_i\big)^2+\lambda\|\mathbf w\|^2 $$
    FIG 1线性探针 = 在冻结的特征上做最小二乘A linear probe is least squares on frozen features
    左:新机器人的 12 个测量点(琥珀色,可以上下拖动),虚线是它真实的保持力矩曲线(只供你对照,拟合看不到它)。绿线是用预训练特征做的线性探针;切到“预训练之前”,紫线是用同一个网络训练前(随机初始化)的特征做的探针。打开“显示机器人家族”会画出 8 台预训练机器人的曲线(细线)。右上:6 个特征 \(\phi_j(q)\)(青色),线越粗表示探针给它的权重 \(|w_j|\) 越大。读数里的“真实误差”是拟合曲线与虚线在整个关节范围上的均方根误差。 Left: the new robot's 12 measurements (amber, drag them up or down); the dashed curve is its true holding torque (for your eyes only; the fit never sees it). The green curve is the linear probe on the pre-trained features; switch to "before pre-training" and the purple curve is the probe on the features of the same network before training (random initialization). "Show the fleet" draws 8 of the pre-training robots' curves (thin lines). Top right: the 6 features \(\phi_j(q)\) (cyan); a thicker line means a larger probe weight \(|w_j|\). The readout's "true error" is the RMS difference between the fit and the dashed curve over the whole joint range.
    试一试Try this
    • 切到“预训练之前”的特征:同样 12 个点,探针的真实误差从约 0.04 N·m 涨到 0.2 N·m 以上:随机特征里没有这个家族的形状。Switch to the "before pre-training" features: with the same 12 points, the probe's true error rises from about 0.04 N·m to above 0.2 N·m. Random features do not contain the family's shapes.
    • 用预训练特征,把 \(1<q<2\) rad 之间的某个点往上拖 0.8 N·m 以上(就像撞上了限位弹簧):探针弯不过去,这个点上的残差超过 0.3 N·m。With the pre-trained features, drag one point between \(q=1\) and \(q=2\) rad up by more than 0.8 N·m (as if a hard-stop spring pushed back): the probe cannot bend to it, and the residual at that point exceeds 0.3 N·m.
    核心洞见Key insight

    预训练的价值在于把“这一类问题的答案长什么样”压缩进特征里。新任务如果落在这个家族里,只需要几个数就能确定(这里是 7 个:6 个权重加一个偏置),12 个点绰绰有余,而且误差接近测量噪声。随机特征同样能穿过这些点,却不知道点与点之间该是什么形状。反过来,家族之外的东西(一个预训练时从未出现过的限位弹簧),冻结的特征怎么组合都表达不出来:这时就必须改动主干本身,也就是微调。

    The value of pre-training is that it compresses "what answers to this kind of problem look like" into the features. A new task that lies inside the family is pinned down by a handful of numbers (here 7: six weights and a bias), so 12 points are plenty and the error approaches the measurement noise. Random features can pass through the points too, but they do not know what shape belongs between them. Conversely, something outside the family (a hard-stop spring that never appeared in pre-training) cannot be expressed by any combination of the frozen features: then the trunk itself must change, which is fine-tuning.

    3动手:四种适配方法现场比赛Play: four ways to adapt, raced live

    给新机器人准备两种情况。同一家族:只是换了负载,曲线仍是重力 + 零偏 + 波动。新的效应:同样的机器人,但装了一个限位弹簧,\(q\) 超过 1 rad 后多出一段约 2 N·m/rad 的线性回弹力矩,这是预训练数据里从没有过的形状。每种情况采 \(N\) 个带噪声的测量(噪声标准差 0.05 N·m),四种方法同时训练:

    • 从零训练:同样结构的网络,随机初始化,所有参数都训练。
    • 线性探针:主干冻结,新头用岭回归一次解出(凸问题,不需要迭代,所以它的曲线一开始就是终点)。
    • 全量微调:从预训练权重出发,所有参数都训练;新头先用线性探针的解初始化(这个做法叫 LP-FT,第 4 节解释原因;LoRA 的新头也这样初始化)。
    • LoRA:所有预训练权重冻结;第二个隐藏层的 32×32 权重矩阵加上一个秩为 \(r\) 的增量 \(\frac{\alpha}{r}BA\),只训练 \(B\)(32×\(r\))、\(A\)(\(r\)×32)和新头。\(B\) 初始化为零,所以训练开始时模型和预训练模型一模一样。

    四种方法用同样的 Adam 优化器、同样的批次(每步最多 32 个样本),训练 1000 步后自动停下。学习率按各自的习惯取:从零训练和 LoRA 用 0.003,全量微调用小一些的 0.001。

    The new robot comes in two versions. Same family: only the payload changed, so the curve is still gravity + offset + ripple. New effect: the same robot fitted with a hard-stop spring, which adds a linear restoring torque of about 2 N·m/rad beyond \(q=1\) rad, a shape that never appeared in the pre-training data. For each version we take \(N\) noisy measurements (noise standard deviation 0.05 N·m) and train four methods side by side:

    • From scratch: a network of the same shape, randomly initialized, all parameters trained.
    • Linear probe: trunk frozen, the new head solved in one shot by ridge regression (a convex problem with no iterations, so its curve starts at its final value).
    • Full fine-tuning: start from the pre-trained weights and train all parameters; the new head is first initialized with the linear-probe solution (this recipe is called LP-FT; Section 4 explains why; LoRA's new head is initialized the same way).
    • LoRA: all pre-trained weights frozen; the second hidden layer's 32×32 weight matrix gets a rank-\(r\) increment \(\frac{\alpha}{r}BA\), and only \(B\) (32×\(r\)), \(A\) (\(r\)×32) and the new head are trained. \(B\) starts at zero, so at the start of training the model is exactly the pre-trained one.

    All four use the same Adam optimizer and the same batches (at most 32 samples per step), and stop automatically after 1000 steps. Learning rates follow common practice for each: 0.003 for scratch and LoRA, a smaller 0.001 for full fine-tuning.

    FIG 2从零训练、线性探针、全量微调、LoRA:同一份小数据,四种结果Scratch, linear probe, full fine-tuning, LoRA: one small dataset, four outcomes
    左:新机器人的 \(N\) 个测量点(琥珀色)、真实曲线(虚线),以及四个模型此刻的预测:从零训练(紫)、线性探针(蓝)、全量微调(红)、LoRA(绿)。“新的效应”下,竖直虚线标出限位弹簧开始起作用的 \(q=1\) rad。右上:每个模型在整个关节范围上相对真实曲线的均方根误差(测试误差,对数纵轴)随训练步数的变化,颜色同左。表格给出每种方法需要训练的参数个数和当前测试误差。 Left: the new robot's \(N\) measurements (amber), its true curve (dashed), and the four models' current predictions: scratch (purple), linear probe (blue), full fine-tuning (red), LoRA (green). With "new effect", a vertical dashed line marks \(q=1\) rad, where the hard-stop spring starts to act. Top right: each model's RMS error against the true curve over the whole joint range (test error, log scale) versus training step, same colours. The table gives each method's number of trained parameters and its current test error.
    • 选“同一家族”、\(N=16\),训练到 1000 步:线性探针的误差(约 0.05 N·m)是四者中最低的,其余三者都在 0.2 N·m 以上:只有 16 个点时,训练上千个参数反而把噪声也学了进去。Choose "same family" and \(N=16\) and train to 1000 steps: the linear probe's error (about 0.05 N·m) is the lowest of the four, and the other three all end above 0.2 N·m. With only 16 points, training over a thousand parameters also learns the noise.
    • 选“新的效应”、\(N=128\),训练到 1000 步:全量微调和 LoRA 的误差都不到线性探针的一半(约 0.03 对 0.15 N·m),而 LoRA 训练的参数只有全量微调的十分之一。Choose "new effect" and \(N=128\) and train to 1000 steps: full fine-tuning and LoRA both end below half of the linear probe's error (about 0.03 versus 0.15 N·m), while LoRA trains only a tenth as many parameters as full fine-tuning.

    几个值得注意的地方。(1) 线性探针只有 7 个参数,对家族内的任务它几乎不可能过拟合;但在“新的效应”下,无论给多少数据,它都停在约 0.15 N·m:这是偏差,冻结的特征表达不了弹簧的折角(16.2 节的偏差–方差)。(2) 全量微调和 LoRA 在“同一家族”、\(N=16\) 时比它们出发时的探针更差:看右上的红线和绿线:它们从探针的误差出发,随着训练把噪声也拟合进去,误差一路升高(全量微调在第 200 步约 0.11,到 1000 步约 0.21)。这是微调最常见的失败方式,对策是早停、更小的学习率或者干脆只做探针。(3) 在“新的效应”下,默认的 \(N=64\) 里从零训练(约 0.07 N·m)已经比探针好,但仍不如两种微调(约 0.05 N·m):预训练学到的重力和波动形状,从零训练要用更多数据才能自己找到。

    A few things to notice. (1) The linear probe has only 7 parameters and can hardly overfit an in-family task; but with "new effect" it stalls at about 0.15 N·m however much data it gets. That is bias: the frozen features cannot express the spring's kink (the bias–variance trade-off of Lesson 16.2). (2) With "same family" and \(N=16\), full fine-tuning and LoRA end up worse than the probe they started from: in the top-right chart the red and green lines start at the probe's error and climb as training fits the noise as well (full fine-tuning is at about 0.11 by step 200 and about 0.21 by step 1000). This is the most common way fine-tuning fails, and the remedies are early stopping, a smaller learning rate, or just probing. (3) With "new effect" at the default \(N=64\), scratch (about 0.07 N·m) already beats the probe, but not the two fine-tuned models (about 0.05 N·m): the gravity and ripple shapes that pre-training learned take scratch training more data to discover on its own.

    要多少数据?How much data?

    一次训练的结果受随机性影响,下面把 \(N\) 从 8 扫到 256,每个 \(N\) 用 3 组不同的数据和初始化各训练 1000 步,画出测试误差的几何平均。对数–对数坐标下,曲线之间的水平距离就是“省了多少倍的数据”。

    A single run depends on chance, so below \(N\) is swept from 8 to 256, and for each \(N\) every method is trained for 1000 steps on 3 different datasets and initializations; the plot shows the geometric mean of the test error. On log–log axes the horizontal distance between curves is "how many times less data" one method needs.

    FIG 3数据效率:测试误差随演示数 \(N\) 的变化Data efficiency: test error versus the number of samples \(N\)
    横轴:测量点数 \(N\)(对数);纵轴:测试误差(N·m,对数)。实心点和连线是 3 次训练的几何平均,小点是单次结果;颜色同 FIG 2:从零(紫)、线性探针(蓝)、全量微调(红)、LoRA \(r=2\)(绿)。虚线是测量噪声的标准差 0.05 N·m。每个目标的扫描要在浏览器里训练 54 个模型,大约需要 15 到 25 秒;滚出屏幕时会暂停。 Horizontal axis: number of measurements \(N\) (log); vertical axis: test error (N·m, log). Solid markers and lines are the geometric mean of 3 runs, small dots single runs; colours as in FIG 2: scratch (purple), linear probe (blue), full fine-tuning (red), LoRA \(r=2\) (green). The dashed line is the measurement-noise standard deviation, 0.05 N·m. Each target's sweep trains 54 models in your browser and takes about 15 to 25 seconds; it pauses while scrolled out of view.
    • 分别为“同一家族”和“新的效应”跑完扫描。同一家族:线性探针在 \(N\le128\) 的每个 \(N\) 上都最低,到 \(N=256\) 时才和 LoRA 打平(LoRA 略低一点)。新的效应:\(N\le16\) 时探针最低,\(N\ge64\) 时全量微调和 LoRA 最低,两条曲线在中间交叉。Run the sweep for both "same family" and "new effect". Same family: the linear probe is lowest at every \(N\) up to 128, and only at \(N=256\) does LoRA draw level (marginally lower). New effect: the probe is lowest for \(N\le16\), full fine-tuning and LoRA are lowest for \(N\ge64\), and the curves cross in between.
    核心洞见Key insight

    选哪种适配方法,取决于两件事:新任务离预训练分布有多远,以及你有多少数据。离得近、数据少:冻结主干、只训练头。离得远、数据多:改动主干。LoRA 在两者之间:它能改动主干,可改动被限制在一个低维的子空间里。在我们的玩具里(新的效应),从零训练到 256 个点仍停在约 0.06 N·m,比两种微调(约 0.02 N·m)差三倍;在真实的机器人上,输入是图像和语言,“家族”的结构复杂得多,这个差距要大得多。

    Which adaptation method to pick depends on two things: how far the new task is from the pre-training distribution, and how much data you have. Close and little data: freeze the trunk and train only the head. Far and plenty of data: change the trunk. LoRA sits in between: it does change the trunk, but only within a low-dimensional subspace. In our toy (new effect), scratch training still sits at about 0.06 N·m with 256 points, three times worse than the two fine-tuned models (about 0.02 N·m); on real robots, where the inputs are images and language and the "family" has far richer structure, this gap is much larger.

    4另一个视角:学会新的,会忘掉旧的吗?Another view: does learning the new erase the old?

    一个基础模型的价值在于它会很多东西。在新机器人上微调之后,那 32 台老机器人它还能用吗?我们保留每台老机器人原来的头不动,把它们接在微调后的主干上,再测一次误差(下文称“旧机器人误差”)。预训练模型本身的旧机器人误差约 0.014 N·m。如果微调把它大幅抬高,就叫灾难性遗忘(catastrophic forgetting,McCloskey 与 Cohen 1989):神经网络的知识分布在所有共享的权重里,为新任务改动这些权重,就会顺带改动旧任务的答案。

    三种常见的对策:(1) 回放(rehearsal / replay):微调时混入一部分旧数据,损失变成 \(L+\hl{replay}{\beta}\,L_0\)(\(L\) 是新任务的损失,\(L_0\) 是旧任务的损失);机器人领域常说的“联合微调”(co-fine-tuning,RT-2 用网络数据和机器人数据一起微调)就是这一类。(2) 把参数拉回预训练值:加正则项 \(\hl{l2sp}{\lambda}\|\boldsymbol\theta-\boldsymbol\theta_0\|^2\)(L2-SP,Li 等 2018);EWC(Kirkpatrick 等 2017)是它的加权版本,对旧任务越重要的参数拉得越紧。(3) 不动原始权重:LoRA 和各种适配器把改动放在额外的参数里,随时可以拿掉。

    A foundation model is valuable because it knows many things. After fine-tuning on the new robot, can it still serve the 32 old ones? We keep each old robot's original head unchanged, attach it to the fine-tuned trunk and measure the error again (the "old-robot error" below). The pre-trained model itself has an old-robot error of about 0.014 N·m. When fine-tuning pushes it up a lot, that is catastrophic forgetting (McCloskey and Cohen 1989): a neural network's knowledge is spread over all the shared weights, so changing them for the new task also changes the answers to the old ones.

    Three common remedies: (1) Replay (rehearsal): mix some old data into fine-tuning, so the loss becomes \(L+\hl{replay}{\beta}\,L_0\) (\(L\) the new task's loss, \(L_0\) the old tasks'); what robotics calls co-fine-tuning (RT-2 fine-tunes on web data and robot data together) is of this kind. (2) Pull the parameters back towards their pre-trained values: add a penalty \(\hl{l2sp}{\lambda}\|\boldsymbol\theta-\boldsymbol\theta_0\|^2\) (L2-SP, Li et al. 2018); EWC (Kirkpatrick et al. 2017) is a weighted version that holds parameters tighter the more they matter to the old tasks. (3) Do not touch the original weights: LoRA and the various adapters put the change into extra parameters that can be removed at any time.

    FIG 4灾难性遗忘:新机器人误差与旧机器人误差的取舍Catastrophic forgetting: the trade-off between new-robot and old-robot error
    任务固定为“新的效应”、\(N=64\)。左:一台老机器人的真实曲线(虚线)和四个模型用它原来的头给出的预测:全量微调(红)、全量微调 + L2-SP(琥珀)、全量微调 + 回放(青)、LoRA \(r=2\)(绿);“下一台”换一台老机器人看。右上:取舍平面,横轴是新机器人的测试误差,纵轴是 32 台老机器人的平均误差(都取对数);每条线是一个模型在训练中走过的路径,实心点是它现在的位置。用 LP-FT 时,所有路径都从蓝色空心圈出发:线性探针的解(主干未动,所以旧机器人误差就是预训练值)。 The task is fixed to "new effect", \(N=64\). Left: the true curve of one old robot (dashed) and the predictions of four models through its original head: full fine-tuning (red), full fine-tuning + L2-SP (amber), full fine-tuning + replay (cyan), LoRA \(r=2\) (green); "next robot" shows another old robot. Top right: the trade-off plane, with the new robot's test error horizontally and the mean error over the 32 old robots vertically (both log); each line is the path a model takes during training, and its solid dot is where it is now. With LP-FT, all paths start at the hollow blue ring: the linear-probe solution (trunk untouched, so the old-robot error is the pre-trained value).
    • 按默认设置训练到 1000 步:普通全量微调把旧机器人误差从 0.014 抬到 0.14 N·m 以上(十倍多),LoRA 也差不多:合并进去的低秩改动同样会改变旧机器人的答案。Train to 1000 steps with the defaults: plain full fine-tuning raises the old-robot error from 0.014 to above 0.14 N·m (more than tenfold), and LoRA does about the same: a merged low-rank change alters the old robots' answers too.
    • 调大回放权重 \(\beta\)(每次改动会重新开始训练),找一个值,使训练 1000 步后青色模型的旧机器人误差低于 0.04 N·m,同时新机器人误差低于 0.1 N·m(仍明显好于探针的约 0.16)。Raise the replay weight \(\beta\) (each change restarts training) and find a value for which, after 1000 steps, the cyan model's old-robot error is below 0.04 N·m while its new-robot error is below 0.1 N·m (still clearly better than the probe's 0.16 or so).
    • 把新头的初始化从“线性探针(LP-FT)”切到“随机”,训练到 1000 步:普通全量微调的旧机器人误差超过 0.5 N·m,是 LP-FT 时的四倍左右。Switch the new head's initialization from "linear probe (LP-FT)" to "random" and train to 1000 steps: plain full fine-tuning's old-robot error exceeds 0.5 N·m, about four times its LP-FT value.

    LP-FT 的道理(Kumar 等 2022):随机初始化的头一开始给出的是错得离谱的预测,它产生的大梯度会一路传回主干,在头学好之前就把预训练特征扭曲了;先用线性探针把头放到合理的位置,再一起微调,主干只需要做小的修正。在 FIG 4 里,这一个开关就让遗忘差了约四倍,而新机器人误差几乎不变(约 0.05 N·m)。

    回放在这个玩具里比 L2-SP 划算:L2-SP 把所有参数一视同仁地往回拉,要压住遗忘就得把新任务也一起压住(\(\lambda=1\) 时旧机器人误差约 0.04,新机器人误差却退到约 0.12);回放直接告诉网络“这些答案不许变”,网络会把改动放到旧任务不在乎的方向上。代价是你得一直保留、并且能用到旧数据。最后,把 LoRA 适配器“关掉”试试:旧机器人误差立刻回到 0.014,因为原始权重一个都没动。一个基础模型加上很多个可插拔的小适配器(每台机器人、每个任务一个),是 LoRA 在部署上最实际的好处。

    Why LP-FT works (Kumar et al. 2022): a randomly initialized head starts with wildly wrong predictions, and its large gradients flow all the way back into the trunk, distorting the pre-trained features before the head has learned anything. Putting the head in a sensible place with a linear probe first, then fine-tuning everything, leaves the trunk only small corrections to make. In FIG 4 this one switch changes forgetting by a factor of about four while the new-robot error barely moves (about 0.05 N·m).

    In this toy, replay is a better deal than L2-SP: L2-SP pulls every parameter back alike, so holding down forgetting also holds down the new task (at \(\lambda=1\) the old-robot error is about 0.04, but the new-robot error falls back to about 0.12); replay tells the network directly "these answers must not change", and the network moves in directions the old tasks do not care about. The price is that you must keep, and be allowed to use, the old data. Finally, try switching the LoRA adapter "off": the old-robot error returns at once to 0.014, because not a single original weight was touched. One foundation model plus many small pluggable adapters (one per robot or per task) is LoRA's most practical advantage in deployment.

    5LoRA 近看:微调的改动本来就是低秩的LoRA up close: fine-tuning updates are low rank to begin with

    LoRA(Hu 等 2021)基于一个经验观察:预训练模型在下游任务上需要的改动,其“内在维度”很低(Aghajanyan 等 2021 在语言模型上测过:在一个随机的低维子空间里优化几百到几千个参数,就能达到全量微调的大部分效果)。那就直接把权重矩阵的改动限制成低秩:

    LoRA (Hu et al. 2021) rests on an empirical observation: the change a pre-trained model needs for a downstream task has a low "intrinsic dimension" (Aghajanyan et al. 2021 measured it for language models: optimizing a few hundred to a few thousand parameters in a random low-dimensional subspace recovers most of the effect of full fine-tuning). So restrict the change of each weight matrix to low rank directly:

    $$ \mathbf h'=W_0\mathbf h+\hl{dW}{\Delta W}\,\mathbf h,\qquad \hl{dW}{\Delta W}=\frac{\alpha}{r}\,\hl{B}{B}\,\hl{A}{A},\qquad B\in\mathbb R^{d\times r},\;A\in\mathbb R^{r\times k},\;r\ll\min(d,k) $$

    训练的参数从 \(dk\) 个降到 \(r(d+k)\) 个。下面的图先用全量微调在“新的效应”、\(N=128\) 上训练 1000 步,把第二个隐藏层的改动 \(\Delta W=W-W_0\) 拿出来做 1.12 节的 SVD:如果它的奇异值 \(\hl{sigma}{\sigma_i}\) 很快衰减,那么一个低秩矩阵就能代替它。同时训练一个 \(r=2\) 的 LoRA,看它的 \(\Delta W\) 长什么样。

    The trained parameters drop from \(dk\) to \(r(d+k)\). The figure below first runs full fine-tuning on "new effect" with \(N=128\) for 1000 steps, takes the change of the second hidden layer \(\Delta W=W-W_0\), and applies the SVD of Lesson 1.12: if its singular values \(\hl{sigma}{\sigma_i}\) decay quickly, a low-rank matrix can stand in for it. It also trains a LoRA with \(r=2\), to see what its \(\Delta W\) looks like.

    FIG 5权重改动 \(\Delta W\) 的奇异值:秩为 \(r\) 的截断够不够用The singular values of the update \(\Delta W\): is a rank-\(r\) truncation enough?
    左:三个 32×32 矩阵的热图,红 = 正,蓝 = 负,颜色越浓绝对值越大:预训练权重 \(W_0\)(单独的色标)、所选来源的改动 \(\Delta W\)、以及它的秩 \(r\) 截断 \(\Delta W_r\)(后两者共用色标)。右上:\(\Delta W\) 的奇异值(对数纵轴),前 \(r\) 个(保留的)为绿色,其余为灰色线框。三种来源:全量微调后的 \(\Delta W\);\(r=2\) 的 LoRA 学到的 \(\frac{\alpha}{r}BA\);以及只用 4 个样本算出的一次梯度 \(\partial L/\partial W\)。读数给出前 \(r\) 个奇异值占 \(\|\Delta W\|_F^2\) 的比例,以及把全量微调模型的这一层改动换成 \(\Delta W_r\)(其他层保持微调后的值)后的测试误差。 Left: heat maps of three 32×32 matrices, red = positive, blue = negative, stronger colour = larger magnitude: the pre-trained weight \(W_0\) (its own colour scale), the selected source's update \(\Delta W\), and its rank-\(r\) truncation \(\Delta W_r\) (these two share a scale). Top right: the singular values of \(\Delta W\) (log scale), the first \(r\) (kept) in green, the rest as hollow outlines. Three sources: \(\Delta W\) after full fine-tuning; the \(\frac{\alpha}{r}BA\) learned by a LoRA with \(r=2\); and a single gradient \(\partial L/\partial W\) computed from only 4 samples. The readout gives the fraction of \(\|\Delta W\|_F^2\) in the first \(r\) singular values, and the test error of the fully fine-tuned model when this layer's update is replaced by \(\Delta W_r\) (the other layers keep their fine-tuned values).
    • 点“微调”,来源选“全量微调”,然后找出让前 \(r\) 个奇异值占到 \(\|\Delta W\|_F^2\) 的 90% 以上的最小 \(r\)(32 个奇异值里只要 3 个),再看读数:换成这个 \(\Delta W_r\) 后的测试误差几乎等于完整的微调。Press "fine-tune", choose the source "full fine-tuning", then find the smallest \(r\) whose first \(r\) singular values hold at least 90% of \(\|\Delta W\|_F^2\) (only 3 of the 32), and read the readout: with this \(\Delta W_r\) the test error is almost exactly that of the complete fine-tune.
    • 把来源切到“一次梯度(4 个样本)”:恰好只有 4 个奇异值不为零,其余小到 \(10^{-10}\) 以下。Switch the source to "one gradient (4 samples)": exactly 4 singular values are nonzero, and the rest are below \(10^{-10}\).

    第二个任务背后是一条简单而重要的事实:对一个样本,线性层 \(\mathbf z=W\mathbf h\) 的梯度是外积 \(\partial L/\partial W=\boldsymbol\delta\,\mathbf h^\top\)(16.3 节),秩为 1;一个批次 \(n\) 个样本的梯度是 \(n\) 个外积之和,秩不超过 \(n\)。微调的数据少、步数少、而且每步的梯度方向高度相关,累积起来的改动自然集中在少数几个方向上。LoRA 的 \(B\) 和 \(A\) 就是让优化器直接去找这几个方向。

    LoRA 还有一个好处:训练完可以合并,\(W=W_0+\frac{\alpha}{r}BA\) 只是一个同样大小的矩阵,推理时的计算量和延迟与原模型完全相同(读数里的“合并误差”是合并前后输出的最大差,只剩浮点舍入)。想换任务时减去 \(\frac{\alpha}{r}BA\)、加上另一个即可。

    Behind the second task is a simple but important fact: for one sample, the gradient of a linear layer \(\mathbf z=W\mathbf h\) is the outer product \(\partial L/\partial W=\boldsymbol\delta\,\mathbf h^\top\) (Lesson 16.3), of rank 1; the gradient of a batch of \(n\) samples is a sum of \(n\) outer products, of rank at most \(n\). Fine-tuning has little data and few steps, and the gradient directions of successive steps are highly correlated, so the accumulated change naturally concentrates in a few directions. LoRA's \(B\) and \(A\) let the optimizer look for those directions directly.

    LoRA has one more advantage: after training it can be merged. \(W=W_0+\frac{\alpha}{r}BA\) is just a matrix of the same size, so inference costs exactly the same compute and latency as the original model (the readout's "merge error" is the largest difference in output before and after merging: only floating-point rounding). To switch tasks, subtract \(\frac{\alpha}{r}BA\) and add another one.

    6再省一点:把冻结的底座量化(QLoRA)Saving more: quantize the frozen base (QLoRA)

    LoRA 让需要训练的参数少了几百倍,但冻结的底座仍然要整个放进显存:70 亿参数用 16 位存就是 14 GB。全量微调更贵:混合精度的 Adam 每个参数要存 16 字节(16 位权重和梯度各 2 字节,32 位的权重主副本 4 字节,Adam 的两个矩估计 8 字节,ZeRO 论文的估算),70 亿参数就是 112 GB,还没算激活值。QLoRA(Dettmers 等 2023)把冻结的底座量化成 4 位,LoRA 的参数仍用 16 位训练,梯度穿过反量化后的权重传给 \(B\) 和 \(A\)。

    最简单的量化是分块 absmax:把权重分成小块,每块记下最大绝对值 \(c\),再把 \(w/c\in[-1,1]\) 四舍五入到 \(2^{b}-1\) 个等距的格点之一,只存格点编号(\(b\) 位)和每块一个 \(c\)。下面对我们的预训练网络这样做(每块是权重矩阵的一行,第一层则是一列;大多数块有 32 个数),看量化让老机器人的误差变大了多少,再在量化后的底座上训练 LoRA。

    LoRA cuts the parameters that need training by a factor of hundreds, but the frozen base must still fit in GPU memory in full: 7 billion parameters in 16 bits is 14 GB. Full fine-tuning is far more expensive: mixed-precision Adam stores 16 bytes per parameter (2 bytes each for the 16-bit weight and gradient, 4 for a 32-bit master copy of the weight, 8 for Adam's two moment estimates; the estimate of the ZeRO paper), which for 7 billion parameters is 112 GB before counting activations. QLoRA (Dettmers et al. 2023) quantizes the frozen base to 4 bits while the LoRA parameters are still trained in 16 bits, with gradients flowing through the de-quantized weights into \(B\) and \(A\).

    The simplest quantization is block-wise absmax: split the weights into small blocks, record each block's largest magnitude \(c\), round \(w/c\in[-1,1]\) to one of \(2^{b}-1\) evenly spaced levels, and store only the level index (\(b\) bits) plus one \(c\) per block. Below we do this to our pre-trained network (one block = one row of a weight matrix, or one column in the first layer; most blocks hold 32 numbers), see how much the quantization raises the old robots' error, and then train a LoRA on top of the quantized base.

    $$ c=\max_{j\in\mathcal B}|w_j|,\qquad \hl{quant}{\hat w_j=\frac{c}{m}\,\operatorname{round}\!\Big(\frac{m\,w_j}{c}\Big)},\quad m=2^{b-1}-1,\qquad |\hat w_j-w_j|\le\frac{c}{2m} $$
    FIG 6量化底座 + LoRA,以及微调一个大模型要多少显存A quantized base + LoRA, and how much memory fine-tuning a big model takes
    左上:预训练网络所有权重除以各自块的 absmax 之后的直方图(青色),琥珀色竖线是 \(b\) 位的量化格点,每个权重被移到最近的那条线上。左下:一台老机器人的真实曲线(虚线)、全精度模型(蓝)与量化模型(红)的预测。右上:微调一个大模型所需的显存(只算权重、梯度和优化器状态,不算激活值):全量微调、LoRA(16 位底座)、QLoRA(4 位底座);虚线是 24 GB、48 GB 和 80 GB 三种常见显卡的容量。LoRA 的可训练参数按模型的 0.6% 估算(大约是在 7B 级模型的每个线性层上用秩 16),每个可训练参数按 16 字节计;QLoRA 的底座每参数 0.5 字节,另加约 0.016 字节的分块常数(双重量化之后)。 Top left: histogram of all the pre-trained network's weights after dividing by their block's absmax (cyan); the amber vertical lines are the \(b\)-bit levels, and every weight moves to the nearest one. Bottom left: one old robot's true curve (dashed) and the predictions of the full-precision model (blue) and of the quantized model (red). Top right: memory needed to fine-tune a big model (weights, gradients and optimizer state only, no activations): full fine-tuning, LoRA (16-bit base), QLoRA (4-bit base); dashed lines mark 24 GB, 48 GB and 80 GB, three common GPU sizes. LoRA's trainable parameters are taken as 0.6% of the model (roughly rank 16 on every linear layer of a 7B-class model), at 16 bytes per trainable parameter; QLoRA's base takes 0.5 bytes per parameter plus about 0.016 bytes of block constants (after double quantization).
    • 把底座量化到 2 位(每个权重只剩 −c、0、+c 三个值):老机器人的误差涨到 0.6 N·m 以上,几十倍。然后点“在这个底座上训练 LoRA”:新机器人的误差仍能降到 0.04 N·m 以下,和 16 位底座上的 LoRA(约 0.03)相差不多。Quantize the base to 2 bits (each weight keeps only three values, −c, 0 and +c): the old robots' error rises above 0.6 N·m, dozens of times worse. Then press "train LoRA on this base": the new robot's error still falls below 0.04 N·m, not far from LoRA on the 16-bit base (about 0.03).
    • 把模型大小拖到 65B:全量微调要一千多 GB,16 位底座的 LoRA 也要一百多 GB,只有 QLoRA(约 40 GB)放得进一张 48 GB 的显卡。这正是 QLoRA 论文的标志性结果。Drag the model size to 65B: full fine-tuning needs over a thousand GB and LoRA on a 16-bit base over a hundred, and only QLoRA (about 40 GB) fits on one 48 GB GPU. That is the QLoRA paper's signature result.

    注意两个现象。第一,在 2 位底座上,LoRA 训练开始时的误差(约 0.20 N·m)比 16 位底座(约 0.15)差,但训练会把量化误差和新任务一起“修”掉:低秩增量加一个新头,足够补偿量化带来的那部分偏差。QLoRA 论文报告,用 4 位底座 + LoRA 微调,能达到 16 位全量微调的效果。第二,我们这个只有 1357 个参数的小网络对量化非常敏感(4 位时老机器人误差约 0.1 N·m,是全精度的 7 倍);大模型的权重多、冗余大,对 4 位量化宽容得多。QLoRA 的 4 位格点也不是等距的,而是按正态分布的分位数放置(NF4),因为预训练权重大致服从零均值正态分布,靠近 0 的地方格点更密;它还把每块 64 个权重的常数 \(c\) 再量化一次(双重量化),并用分页优化器应对显存峰值。

    Notice two things. First, on the 2-bit base LoRA starts from a worse error than on the 16-bit base (about 0.20 versus 0.15 N·m), but training "repairs" the quantization error along with the new task: the low-rank increment plus a new head are enough to compensate for the bias the quantization introduced. The QLoRA paper reports that a 4-bit base plus LoRA matches 16-bit full fine-tuning. Second, our tiny 1357-parameter network is very sensitive to quantization (at 4 bits the old-robot error is about 0.1 N·m, 7 times the full-precision value); big models, with many more and much more redundant weights, tolerate 4 bits far better. QLoRA's 4-bit levels are also not evenly spaced but placed at the quantiles of a normal distribution (NF4), because pre-trained weights are roughly zero-mean normal and the levels should be denser near 0; it also quantizes the per-block constants \(c\) (one per 64 weights) a second time (double quantization) and uses paged optimizers to absorb memory spikes.

    7再看数学The math

    四个优化问题Four optimization problems

    记预训练得到的主干参数为 \(\boldsymbol\psi_0\),新任务数据为 \(\{(x_i,y_i)\}_{i=1}^N\),新头为 \(\mathbf w\),模型 \(\hat y=\mathbf w^\top\boldsymbol\phi_{\boldsymbol\psi}(x)\)。四种做法只是“哪些变量可以动、从哪里出发”不同:

    Let \(\boldsymbol\psi_0\) be the pre-trained trunk parameters, \(\{(x_i,y_i)\}_{i=1}^N\) the new task's data, \(\mathbf w\) the new head and \(\hat y=\mathbf w^\top\boldsymbol\phi_{\boldsymbol\psi}(x)\) the model. The four approaches differ only in which variables may move and where they start:

    $$ \hl{lp}{\mathbf w^\star=\arg\min_{\mathbf w}\;L(\mathbf w,\boldsymbol\psi_0)+\lambda\|\mathbf w\|^2=(\Phi^\top\Phi+\lambda I)^{-1}\Phi^\top\mathbf y} $$ $$ \hl{ft}{\min_{\mathbf w,\boldsymbol\psi}\;L(\mathbf w,\boldsymbol\psi)},\qquad (\mathbf w,\boldsymbol\psi)\big|_{t=0}=(\mathbf w^\star,\boldsymbol\psi_0) $$ $$ L(\mathbf w,\boldsymbol\psi)+\hl{l2sp}{\lambda\|\boldsymbol\psi-\boldsymbol\psi_0\|^2},\qquad L(\mathbf w,\boldsymbol\psi)+\tfrac{\lambda}{2}\textstyle\sum_i F_i(\psi_i-\psi_{0,i})^2,\qquad L(\mathbf w,\boldsymbol\psi)+\hl{replay}{\beta\,L_0(\boldsymbol\psi)} $$ $$ \hl{lora}{\min_{\mathbf w,B,A}\;L\big(\mathbf w,\;W_0+\tfrac{\alpha}{r}BA\big)},\qquad B\big|_{t=0}=0,\quad A_{ij}\big|_{t=0}\sim\mathcal N(0,\sigma^2) $$

    四行依次是:线性探针(LP);全量微调,从探针的解出发(LP-FT);加在全量微调上的三种防遗忘项,L2-SP、EWC 和回放;LoRA。线性探针里 \(\Phi\) 的第 \(i\) 行是 \(\boldsymbol\phi_{\boldsymbol\psi_0}(x_i)^\top\)(末尾补一个 1 作偏置),这就是 1.13 节的正规方程加岭项(FIG 1 和 FIG 2 用 \(\lambda=0.01\),偏置不受惩罚)。第三行的中间一项是 EWC:\(F_i\) 是旧任务上 Fisher 信息矩阵的对角元,粗略地说是“损失对第 \(i\) 个参数有多敏感”,所以 EWC 是“按重要性加权的 L2-SP”。回放项 \(L_0\) 在 FIG 4 里是 32 台老机器人通过各自冻结的头的均方误差,每步随机取 16 个角度。悬停这些公式,FIG 2 和 FIG 4 里对应的曲线会加粗。

    The four lines are, in order: the linear probe (LP); full fine-tuning, started from the probe's solution (LP-FT); three anti-forgetting terms added to full fine-tuning, L2-SP, EWC and replay; and LoRA. In the linear probe, row \(i\) of \(\Phi\) is \(\boldsymbol\phi_{\boldsymbol\psi_0}(x_i)^\top\) (with a 1 appended for the bias): the normal equations of Lesson 1.13 plus a ridge term (FIG 1 and FIG 2 use \(\lambda=0.01\), with the bias not penalized). The middle term of the third line is EWC: \(F_i\) is the diagonal of the Fisher information matrix on the old tasks, roughly "how sensitive the old loss is to parameter \(i\)", so EWC is "L2-SP weighted by importance". The replay term \(L_0\) in FIG 4 is the mean squared error of the 32 old robots through their frozen heads, at 16 random angles per step. Hover these formulas and the corresponding curves in FIG 2 and FIG 4 thicken.

    LoRA 的梯度与初始化LoRA's gradients and initialization

    令 \(G=\partial L/\partial W\) 为合并后权重 \(W=W_0+sBA\)(\(s=\alpha/r\))上的普通梯度,也就是全量微调会用到的那个梯度。由链式法则:

    Let \(G=\partial L/\partial W\) be the ordinary gradient on the merged weight \(W=W_0+sBA\) (\(s=\alpha/r\)), the very gradient full fine-tuning would use. By the chain rule:

    $$ \frac{\partial L}{\partial \hl{B}{B}}=s\,G\,A^\top,\qquad \frac{\partial L}{\partial \hl{A}{A}}=s\,B^\top G,\qquad G=\sum_{n\in\mathcal B}\boldsymbol\delta_n\mathbf h_n^\top\;\;\Rightarrow\;\;\operatorname{rank}G\le|\mathcal B| $$

    页面的代码正是这样做的:用 nn.js 在合并后的网络上反传得到 \(G\),再乘上 \(A^\top\) 或 \(B^\top\)。从这两个式子能读出初始化的讲究:如果 \(A\) 和 \(B\) 都从零开始,两个梯度都是零,永远动不了;如果都随机,训练一开始模型就被一个随机的 \(sBA\) 扰乱了。所以 LoRA 让 \(B=0\)、\(A\) 随机:起点就是预训练模型,而 \(\partial L/\partial B=sGA^\top\neq0\),第一步就能动起来。缩放 \(s=\alpha/r\) 让“改变秩”和“改变学习率”大致解耦:\(r\) 变大时每一列的贡献按 \(1/r\) 缩小,LoRA 论文因此把 \(\alpha\) 固定为第一个尝试的 \(r\),之后不再调它(我们取 \(\alpha=2r\))。

    参数个数。一个 \(d\times k\) 的矩阵:全量 \(dk\),LoRA \(r(d+k)\)。\(d=k=4096\)、\(r=8\) 时是 65,536 对 16,777,216,约 0.39%。我们的玩具:全量微调 1357 个参数(\(2\cdot32+32\) + \(32\cdot32+32\) + \(32\cdot6+6\) + 新头 7),LoRA \(r=2\) 是 \(2\cdot2\cdot32=128\) 加新头 7,共 135 个。

    适配器(adapter,Houlsby 等 2019)是更早的做法:在每个 Transformer 子层后面插入一个小瓶颈 \(\mathbf h\leftarrow\mathbf h+W_{\uparrow}\,\sigma(W_{\downarrow}\mathbf h)\),\(W_{\downarrow}\in\mathbb R^{m\times d}\),\(m\ll d\),只训练这些小矩阵。参数同样很少,但中间有非线性 \(\sigma\),没法并进原权重,推理时多出串行的计算。提示微调 / 前缀微调则只学几个附加在输入序列(或每层键值)前面的向量,参数更少,表达力也更弱。

    The page's code does exactly this: it backpropagates through the merged network with nn.js to get \(G\), then multiplies by \(A^\top\) or \(B^\top\). These two formulas also explain the initialization. If \(A\) and \(B\) both start at zero, both gradients are zero and nothing ever moves; if both are random, the model is scrambled by a random \(sBA\) before training begins. So LoRA sets \(B=0\) and \(A\) random: the starting point is the pre-trained model, yet \(\partial L/\partial B=sGA^\top\neq0\), so the first step already moves. The scaling \(s=\alpha/r\) roughly decouples "changing the rank" from "changing the learning rate": as \(r\) grows, each column's contribution shrinks as \(1/r\), which is why the LoRA paper fixes \(\alpha\) to the first \(r\) it tries and does not tune it afterwards (we use \(\alpha=2r\)).

    Parameter counts. For a \(d\times k\) matrix: \(dk\) in full, \(r(d+k)\) with LoRA. With \(d=k=4096\) and \(r=8\) that is 65,536 against 16,777,216, about 0.39%. Our toy: full fine-tuning trains 1357 parameters (\(2\cdot32+32\) + \(32\cdot32+32\) + \(32\cdot6+6\) + the new head's 7), LoRA with \(r=2\) trains \(2\cdot2\cdot32=128\) plus the new head's 7, 135 in all.

    Adapters (Houlsby et al. 2019) came earlier: insert a small bottleneck \(\mathbf h\leftarrow\mathbf h+W_{\uparrow}\,\sigma(W_{\downarrow}\mathbf h)\) after each transformer sub-layer, with \(W_{\downarrow}\in\mathbb R^{m\times d}\) and \(m\ll d\), and train only these small matrices. They also have few parameters, but the nonlinearity \(\sigma\) in the middle means they cannot be folded into the original weights, so inference gains extra sequential computation. Prompt tuning / prefix tuning learn only a few vectors prepended to the input sequence (or to each layer's keys and values): even fewer parameters, and less expressive power.

    低秩截断为什么是最优的Why low-rank truncation is optimal

    FIG 5 的 \(\Delta W_r\) 用的是 SVD 截断 \(\Delta W_r=\sum_{i\le r}\sigma_i\mathbf u_i\mathbf v_i^\top\)。Eckart–Young 定理(1.12 节)说它是所有秩不超过 \(r\) 的矩阵里离 \(\Delta W\) 最近的(Frobenius 范数下),误差恰好是 \(\|\Delta W-\Delta W_r\|_F^2=\sum_{i>r}\sigma_i^2\),读数里的能量比例就是 \(\sum_{i\le r}\sigma_i^2/\sum_i\sigma_i^2\)。LoRA 并不先算全量微调再截断,而是直接在秩 \(r\) 的矩阵里优化,学到的 \(\Delta W\) 也不必等于全量微调的截断:FIG 5 里 LoRA 的 \(BA\) 和全量微调的 \(\Delta W\) 长得并不一样,测试误差却差不多,因为能把新任务做好的改动不止一个。

    The \(\Delta W_r\) of FIG 5 is the SVD truncation \(\Delta W_r=\sum_{i\le r}\sigma_i\mathbf u_i\mathbf v_i^\top\). The Eckart–Young theorem (Lesson 1.12) says it is the closest matrix of rank at most \(r\) to \(\Delta W\) in the Frobenius norm, with error exactly \(\|\Delta W-\Delta W_r\|_F^2=\sum_{i>r}\sigma_i^2\); the readout's energy fraction is \(\sum_{i\le r}\sigma_i^2/\sum_i\sigma_i^2\). LoRA does not compute a full fine-tune and truncate it; it optimizes directly over rank-\(r\) matrices, and the \(\Delta W\) it learns need not equal the truncated full fine-tune: in FIG 5 LoRA's \(BA\) looks different from full fine-tuning's \(\Delta W\), yet the test errors are about the same, because more than one change does the new task well.

    一句话总结In one sentence

    预训练给出一个好的起点和一组好的特征;线性探针只在特征上做最小二乘,便宜、稳,但有偏差;全量微调什么都能改,但费数据、费显存、会遗忘;LoRA 把改动限制在 \(W_0+\frac{\alpha}{r}BA\) 这样的低秩子空间里,用很少的参数拿到全量微调的大部分效果,可合并、可拔插,再配上量化的底座(QLoRA)就能在一张显卡上微调大模型。

    Pre-training provides a good starting point and a good set of features; a linear probe does least squares on the features, cheap and stable but biased; full fine-tuning can change anything but costs data and memory and forgets; LoRA restricts the change to a low-rank subspace \(W_0+\frac{\alpha}{r}BA\), getting most of full fine-tuning's effect from very few parameters, mergeable and pluggable, and with a quantized base (QLoRA) a big model can be fine-tuned on a single GPU.

    8机器人中的它:用几百条演示微调一个基础模型In robotics: fine-tuning a foundation model on a few hundred demonstrations

    机器人学习里的预训练大致经历了三个阶段。冻结的视觉编码器:在 ImageNet、人类视频(R3M)或大规模图像上自监督(MVP、VC-1)预训练一个编码器,冻结后在它的特征上训练策略,就是 18.3 节和 18.4 节讲过的“线性探针”思路的延伸。从视觉-语言模型出发:RT-2(Brohan 等 2023)把动作写成文本 token(17.5 节),在大规模视觉-语言模型上用机器人数据微调;它采用联合微调,把原来的网络视觉问答数据和机器人轨迹混在一起训练,正是 FIG 4 的回放,目的是不忘掉从网络上学到的物体和概念。在跨本体机器人数据上预训练的通用策略:Octo(Octo Model Team 2024)在 Open X-Embodiment 的 80 万条轨迹上预训练,论文报告能在普通的消费级显卡上用几个小时把它微调到新的传感器输入和动作空间;OpenVLA(Kim 等 2024)是一个 70 亿参数的 VLA,在 97 万条真实机器人演示上训练。

    OpenVLA 的论文给出了一组很能说明问题的数字。在每个任务只有 10 到 150 条演示的 Franka 机械臂任务上,全量微调需要 8 张 A100 每个任务训练 5 到 15 小时;LoRA(秩 32)只训练 1.4% 的参数,平均成功率 68.2%,与全量微调的 69.7% 相当,只用一张 A100 训练 10 到 15 小时;作者还发现秩的大小对成功率影响很小。部署时,4 位量化推理的成功率与 16 位推理相当,显存不到一半(8 位量化反而变差,作者归因于推理速度变慢)。π0(Black 等 2024)则沿用了语言模型“预训练 + 后训练”的说法:先在大量多机器人数据上预训练,再用精心筛选的高质量数据把它微调到叠衣服这类复杂的灵巧任务上。

    Pre-training in robot learning has gone through roughly three stages. Frozen visual encoders: pre-train an encoder on ImageNet, on human video (R3M) or self-supervised on large image collections (MVP, VC-1), freeze it and train a policy on its features: an extension of the "linear probe" idea of Lesson 18.3 and Lesson 18.4. Starting from a vision-language model: RT-2 (Brohan et al. 2023) writes actions as text tokens (Lesson 17.5) and fine-tunes large vision-language models on robot data; it co-fine-tunes, mixing the original web visual-question-answering data with robot trajectories, which is exactly the replay of FIG 4, so as not to forget the objects and concepts learned from the web. Generalist policies pre-trained on cross-embodiment robot data: Octo (Octo Model Team 2024) is pre-trained on 800k trajectories from Open X-Embodiment, and its paper reports fine-tuning to new sensor inputs and action spaces within a few hours on standard consumer GPUs; OpenVLA (Kim et al. 2024) is a 7-billion-parameter VLA trained on 970k real robot demonstrations.

    The OpenVLA paper reports a telling set of numbers. On Franka-arm tasks with only 10 to 150 demonstrations each, full fine-tuning took 8 A100 GPUs for 5 to 15 hours per task; LoRA (rank 32) trained only 1.4% of the parameters and reached a 68.2% average success rate, on par with full fine-tuning's 69.7%, using a single A100 for 10 to 15 hours; the authors also found that the rank had little effect on success. For deployment, 4-bit quantized inference matched the success rate of 16-bit inference with less than half the GPU memory (8-bit was worse, which the authors attribute to slower inference). π0 (Black et al. 2024) borrows the language-model terms "pre-training + post-training": pre-train on a large multi-robot mixture, then fine-tune on carefully curated high-quality data for complex dexterous tasks such as folding laundry.

    真实系统里In real systems

    先对齐接口,再谈微调:新机器人的相机视角、动作空间(关节位置还是末端位姿,绝对量还是增量)、控制频率和动作归一化的统计量,都要和预训练时的约定一致,或者明确地为它们换一个新的输入/输出头(Octo 就是这样适配新动作空间的)。对不上的接口比任何微调方法的差别都大。 数据少时先试最便宜的:冻结编码器 + 小策略头,或 LoRA;把一部分演示留作验证集,按验证成功率或损失早停(FIG 2 里过拟合的红线和绿线)。 别只看新任务:如果模型还要做原来的任务,微调后要在原来的任务上重新测;需要保留语言理解或旧技能时,混入原始数据联合微调。 一个底座,多个适配器:每台机器人、每个任务一个 LoRA,按需加载,原始权重不变。

    Align the interfaces before you fine-tune: the new robot's camera viewpoints, action space (joint positions or end-effector poses, absolute or delta), control frequency and action-normalization statistics must match the pre-training conventions, or get an explicitly new input or output head (which is how Octo adapts to a new action space). A mismatched interface matters more than the choice of fine-tuning method. With little data, try the cheapest thing first: a frozen encoder with a small policy head, or LoRA; hold out some demonstrations for validation and stop early on validation success or loss (the overfitting red and green lines in FIG 2). Do not look only at the new task: if the model still has to do its old tasks, re-test them after fine-tuning; when language understanding or old skills must be kept, co-fine-tune with the original data mixed in. One base, many adapters: one LoRA per robot or task, loaded on demand, with the original weights untouched.

    9常见误区Common pitfalls

    误区 1:微调一定比冻结好Pitfall 1: assuming fine-tuning always beats freezing

    数据少、任务又在预训练分布之内时,全量微调和 LoRA 都可能比它们出发时的线性探针还差:FIG 2 的“同一家族”、\(N=16\) 里,探针约 0.05 N·m,两种微调从探针的解出发,1000 步后却都超过 0.2 N·m,训练越久越差。没有验证集、按固定步数训练,是这种退化最常见的原因。

    With little data and a task inside the pre-training distribution, full fine-tuning and LoRA can both end up worse than the linear probe they started from: with "same family" and \(N=16\) in FIG 2, the probe is at about 0.05 N·m while both fine-tunes, which start from the probe's solution, end above 0.2 N·m after 1000 steps, getting worse the longer they train. Training for a fixed number of steps with no validation set is the most common cause.

    误区 2:新头随便初始化,学习率照搬预训练Pitfall 2: a carelessly initialized new head and the pre-training learning rate

    随机的新头在前几百步产生大而无意义的梯度,会把预训练特征冲掉(FIG 4 里随机头让遗忘大了约四倍)。先做线性探针再微调(LP-FT),或者先冻结主干训练几百步头再解冻;微调的学习率通常比预训练小一个数量级左右,有时还对靠近输入的层用更小的学习率(ULMFiT 的分层学习率)。

    A random new head produces large, meaningless gradients during the first few hundred steps that wash out the pre-trained features (in FIG 4 the random head makes forgetting about four times worse). Probe first, then fine-tune (LP-FT), or train the head for a few hundred steps with the trunk frozen before unfreezing it; fine-tuning learning rates are usually around an order of magnitude below pre-training, sometimes with even smaller rates for the layers nearest the input (ULMFiT's discriminative learning rates).

    误区 3:以为 LoRA 天生不会遗忘Pitfall 3: believing LoRA cannot forget

    LoRA 的改动一旦合并、并且模型用于旧任务,它和全量微调一样会改变旧答案:FIG 4 里 LoRA 的旧机器人误差(约 0.17 N·m)和全量微调(约 0.18 N·m)差不多。秩小一些通常改得少一些、忘得也少一些(Biderman 等 2024 的标题就是“LoRA 学得少,忘得也少”),但这不是保证。真正不会遗忘的,是把适配器拿掉、换回原始权重。

    Once LoRA's change is merged and the model is used for old tasks, it changes the old answers just as full fine-tuning does: in FIG 4 LoRA's old-robot error (about 0.17 N·m) is close to full fine-tuning's (about 0.18 N·m). A smaller rank usually changes less and forgets less (Biderman et al. 2024 is titled "LoRA Learns Less and Forgets Less"), but that is no guarantee. What truly cannot forget is removing the adapter and going back to the original weights.

    误区 4:只改秩,不管 \(\alpha\) 和学习率Pitfall 4: changing the rank without minding \(\alpha\) and the learning rate

    增量是 \(\frac{\alpha}{r}BA\),同一个学习率下,改 \(r\) 或改 \(\alpha\) 都会改变每一步的实际步长。比较不同的秩时要说清 \(\alpha\) 怎么取(固定 \(\alpha\) 还是固定 \(\alpha/r\)),否则看到的可能只是学习率的差别。另外,只在部分矩阵上加 LoRA(原论文主要用在注意力的 \(W_Q\)、\(W_V\) 上)和在所有线性层上加,效果和参数量都不同。

    The increment is \(\frac{\alpha}{r}BA\), so at a fixed learning rate changing either \(r\) or \(\alpha\) changes the effective step size. When comparing ranks, state how \(\alpha\) is set (fixed \(\alpha\) or fixed \(\alpha/r\)), or what you see may just be a learning-rate effect. Also, adding LoRA to only some matrices (the original paper used it mainly on attention's \(W_Q\) and \(W_V\)) differs, in both effect and parameter count, from adding it to every linear layer.

    误区 5:把“参数少”当成“显存少”和“算得快”Pitfall 5: equating "few parameters" with "little memory" and "fast"

    LoRA 省的是梯度和优化器状态,冻结的底座仍要整个放进显存(16 位 70 亿参数就是 14 GB),前向和反向的计算量也几乎没少,因为梯度仍要穿过整个网络;激活值的显存随批次和序列长度增长,FIG 6 的估算没算它。要再省就得量化底座(QLoRA)、用梯度检查点、或减小批次。

    LoRA saves gradients and optimizer state, but the frozen base must still sit in GPU memory in full (14 GB for 7 billion parameters in 16 bits), and the forward and backward compute barely shrinks because gradients still pass through the whole network; activation memory grows with batch size and sequence length and is not included in the estimate of FIG 6. Saving more means quantizing the base (QLoRA), gradient checkpointing, or smaller batches.

    10小练习Check yourself

    1. 给一个 4096×4096 的权重矩阵加秩 \(r=8\) 的 LoRA,需要训练多少个参数?1. Adding a rank-\(r=8\) LoRA to a 4096×4096 weight matrix: how many parameters are trained?
    \(B\) 是 4096×8,\(A\) 是 8×4096,合计 \(r(d+k)=8\times8192=65{,}536\),约为整个矩阵(选项 D,\(4096^2\))的 0.39%。选项 A 只算了 \(B\) 或 \(A\) 中的一个。\(B\) is 4096×8 and \(A\) is 8×4096, so \(r(d+k)=8\times8192=65{,}536\), about 0.39% of the whole matrix (option D, \(4096^2\)). Option A counts only one of \(B\) and \(A\).
    2. 如果把 LoRA 的 \(A\) 和 \(B\) 都初始化为零,会怎样?2. What happens if both \(A\) and \(B\) of a LoRA are initialized to zero?
    \(\partial L/\partial B=sGA^\top\)、\(\partial L/\partial A=sB^\top G\):\(A=0\) 使前者为零,\(B=0\) 使后者为零,这是一个梯度恒为零的驻点。所以 LoRA 只让 \(B=0\),\(A\) 随机。选项 D 是两者都随机初始化的后果。\(\partial L/\partial B=sGA^\top\) and \(\partial L/\partial A=sB^\top G\): \(A=0\) makes the first zero and \(B=0\) the second, a stationary point where the gradient stays zero. That is why LoRA sets only \(B=0\) and keeps \(A\) random. Option D is what happens when both are initialized randomly.
    3. 新机器人和预训练用的机器人属于同一家族,你只有 16 个测量点。FIG 2 和 FIG 3 建议用哪种方法?3. The new robot belongs to the same family as the pre-training robots, and you have only 16 measurements. Which method do FIG 2 and FIG 3 recommend?
    家族内的任务能被冻结特征表达,探针只有 7 个参数,16 个点就把它确定得很好(约 0.05 N·m);全量微调和 LoRA 有上百到上千个参数,长时间训练会拟合噪声(1000 步后都超过 0.2 N·m);从零训练在这么少的数据下最差。An in-family task can be expressed by the frozen features, and the probe's 7 parameters are well determined by 16 points (about 0.05 N·m); full fine-tuning and LoRA have hundreds to thousands of parameters and fit the noise if trained long (both above 0.2 N·m after 1000 steps); scratch training is worst with this little data.
    4. LoRA 训练完、把 \(\frac{\alpha}{r}BA\) 合并进 \(W_0\) 之后,推理的计算量和原模型相比如何?4. After training a LoRA and merging \(\frac{\alpha}{r}BA\) into \(W_0\), how does inference compute compare with the original model?
    \(W=W_0+\frac{\alpha}{r}BA\) 与 \(W_0\) 形状相同,前向计算没有任何变化(FIG 5 的合并误差只是浮点舍入)。选项 A 是不合并时的做法(方便切换适配器),选项 B 描述的是 Houlsby 式适配器,它因为中间有非线性而无法合并。\(W=W_0+\frac{\alpha}{r}BA\) has the same shape as \(W_0\), so the forward pass does not change at all (FIG 5's merge error is just floating-point rounding). Option A is what happens without merging (handy for switching adapters); option B describes Houlsby-style adapters, which cannot be merged because of the nonlinearity in the middle.
    5. 一个线性层在一个 4 样本批次上的梯度 \(\partial L/\partial W\)(32×32)的秩最多是多少?为什么?5. What is the largest possible rank of a linear layer's gradient \(\partial L/\partial W\) (32×32) on a batch of 4 samples, and why?
    每个样本贡献一个秩 1 的外积(误差信号乘以输入),4 个秩 1 矩阵之和的秩不超过 4,FIG 5 的“一次梯度”正好有 4 个非零奇异值。选项 C 只对单个样本成立。这条事实是“微调改动低秩”的一个直观来源。Each sample contributes a rank-1 outer product (error signal times input), and a sum of 4 rank-1 matrices has rank at most 4; FIG 5's "one gradient" has exactly 4 nonzero singular values. Option C holds only for a single sample. This fact is one intuitive source of "fine-tuning updates are low rank".

    11延伸阅读Further reading

    • M. McCloskey & N. J. Cohen, Catastrophic Interference in Connectionist Networks: The Sequential Learning Problem, Psychology of Learning and Motivation, 1989 — 灾难性遗忘的经典描述the classic description of catastrophic forgetting
    • J. Yosinski, J. Clune, Y. Bengio & H. Lipson, How Transferable Are Features in Deep Neural Networks?, NeurIPS 2014 — 逐层测量特征的可迁移性measuring the transferability of features layer by layer
    • J. Howard & S. Ruder, Universal Language Model Fine-tuning for Text Classification, ACL 2018 — ULMFiT:分层学习率与逐步解冻ULMFiT: discriminative learning rates and gradual unfreezing
    • X. Li, Y. Grandvalet & F. Davoine, Explicit Inductive Bias for Transfer Learning with Convolutional Networks, ICML 2018 — L2-SP:把参数拉回预训练值L2-SP: pulling parameters back to their pre-trained values
    • J. Kirkpatrick et al., Overcoming Catastrophic Forgetting in Neural Networks, PNAS 2017 — EWC:按 Fisher 信息加权的正则EWC: a regularizer weighted by Fisher information
    • N. Houlsby et al., Parameter-Efficient Transfer Learning for NLP, ICML 2019 — 瓶颈适配器bottleneck adapters
    • A. Aghajanyan, S. Gupta & L. Zettlemoyer, Intrinsic Dimensionality Explains the Effectiveness of Language Model Fine-Tuning, ACL 2021 — 微调的内在维度很低fine-tuning has a low intrinsic dimension
    • E. J. Hu et al., LoRA: Low-Rank Adaptation of Large Language Models, ICLR 2022 — 低秩增量 \(\Delta W=BA\)、零初始化的 \(B\)、合并推理the low-rank increment \(\Delta W=BA\), zero-initialized \(B\), merged inference
    • A. Kumar, A. Raghunathan, R. Jones, T. Ma & P. Liang, Fine-Tuning can Distort Pretrained Features and Underperform Out-of-Distribution, ICLR 2022 — LP-FT:先探针、后微调LP-FT: probe first, then fine-tune
    • T. Dettmers, A. Pagnoni, A. Holtzman & L. Zettlemoyer, QLoRA: Efficient Finetuning of Quantized LLMs, NeurIPS 2023 — NF4、双重量化、分页优化器NF4, double quantization, paged optimizers
    • D. Biderman et al., LoRA Learns Less and Forgets Less, TMLR 2024 — LoRA 与全量微调在学习与遗忘上的系统比较a systematic comparison of LoRA and full fine-tuning in learning and forgetting
    • S. Rajbhandari, J. Rasley, O. Ruwase & Y. He, ZeRO: Memory Optimizations Toward Training Trillion Parameter Models, SC 2020 — 混合精度 Adam 每参数 16 字节的显存分析the 16-bytes-per-parameter memory analysis of mixed-precision Adam
    • A. Brohan et al., RT-2: Vision-Language-Action Models Transfer Web Knowledge to Robotic Control, CoRL 2023 — 在视觉-语言模型上联合微调机器人动作co-fine-tuning a vision-language model on robot actions
    • Octo Model Team, Octo: An Open-Source Generalist Robot Policy, RSS 2024 — 在 Open X-Embodiment 上预训练、可微调到新机器人的通用策略a generalist policy pre-trained on Open X-Embodiment and fine-tunable to new robots
    • M. J. Kim et al., OpenVLA: An Open-Source Vision-Language-Action Model, CoRL 2024 — 7B VLA,LoRA 微调与量化部署a 7B VLA, with LoRA fine-tuning and quantized deployment