Training 1: Supervised Fine-Tuning (SFT)
- 日期
- Thu, Sep 17, 2026
- 讲师
- Yueqi Song
- 课程资料
- 21 项
- 内容产物
- 4 项
课程简介
CMU 11-768 Fall 2026 · Lecture 8 学习笔记:Training 1 — Supervised Fine-Tuning (SFT)
来源:平台字幕与官方 Slides。内容已由用户审核确认。
说明:本笔记依据 Lecture 8 的官方 Slides 文本提取与中文逐字稿整理稿整理。Slides 文本为逐页提取,图示、公式与布局未经视觉核验,凡涉及此类内容均标注“待核”。逐字稿为自动字幕清洗稿的机器翻译,存在错词风险,文中仅作补充性引用,不用于确认具体数值或结论。本笔记内容已由用户审核确认。
一、学习目标
学完本讲后,应当能够:
- 说明“更新模型权重”作为一种智能体能力更新手段,与上下文窗口、外部工件相比的优劣(Slides 第 4 页)。
- 描述一条智能体轨迹如何被 chat template 渲染为一个 token 序列,并解释 loss mask 的作用与构建流程(Slides 第 11–13 页)。
- 说明在智能体 SFT 中“只在 assistant token 上计算 loss”这一通用经验法则,以及围绕推理块、end-of-turn token 的若干可调开关(Slides 第 12、15、16 页)。
- 列举选择 SFT 轨迹时的数据来源分类、过滤策略、教师选择与任务来源混合的经验结论(Slides 第 21–28 页)。
- 说明多数据集异构问题与“统一轨迹表示”的动机(Slides 第 31–34 页)。
- 了解真实 SFT 配置涉及的主要超参数维度、MoE 专家路由问题与整段对话打包(Slides 第 36–38 页)。
- 解释训练与部署之间的不匹配,以及为什么训练 loss 不能替代在目标 harness 中的评估(Slides 第 42–45 页)。
- 说明 SFT 作为 RL 冷启动的角色(Slides 第 7、47 页)。
二、概念主线
本讲的主线可以概括为一句话:把记录下来的轨迹转成权重(Slides 第 1 页标题即“Turning recorded trajectories into weights”)。
主线分五段:
- 为什么更新权重:三种更新智能体的方式中,权重更新带来更快推理与更广的行为改变,但代价是更新慢、不透明、模型特定(Slides 第 4 页)。
- 轨迹是什么:一条轨迹包含 system、user、assistant、observation 等行;在 SWE-Gym 的例子中,智能体收到 issue,在沙箱中拿到 bash 与文件编辑工具,产生思考与命令,读取观察结果,最终由仓库自身的测试判定成功与否(Slides 第 3 页)。
- 轨迹如何变成训练信号:经 chat template 渲染成单一 token 串,只有 assistant 片段带 loss,其余是条件上下文(Slides 第 11–13 页)。
- 选哪些轨迹:来源(强模型 / 模型自身)、是否过滤(全部保留 / 仅保留通过验证的)、教师选择、任务来源混合与数据规模(Slides 第 21–28 页)。
- 训练与验证:真实配置、打包、模板漂移;训练分布与部署分布不匹配,因此必须评估(Slides 第 36–45 页)。最后交棒给 RL(Slides 第 46–47 页)。
三、关键机制
3.1 权重更新在三种更新方式中的位置
Slides 第 4 页给出三行对比表:
| 位置 | 优点 | 缺点 |
|---|---|---|
| 上下文窗口 | 对已发生之事高保真 | 昂贵、有噪声、不决定什么重要 |
| 外部工件 | 可检查、可编辑、可检索、可移植 | 必须被归纳、选择与维护 |
| 模型权重(本讲) | 推理更快、行为改变更广 | 更新更慢、不透明、模型特定 |
关键机制:权重改变一次,之后每次调用都获得新行为,提示中无需额外内容(Slides 第 4 页)。前两行以秒级更新;这一行每次改变都要付出一次训练运行的代价,且无法打开权重查看变化,也无法把变化迁移到别的模型。
3.2 轨迹教了什么
Slides 第 5 页列出四项:如何写出该 harness 真正能运行的工具调用;对话变长时如何继续(因为学到的轨迹本身很长);工具结果长什么样,从而学会等待结果而不是自己编一个;以及数据中其它一切——额外评论、重复命令、多种可行解法中的某一种。
3.3 完整训练流水线中的 SFT
Slides 第 6 页给出按 token 量绘制的流程:预训练(Nemotron 3 Ultra 20T tokens)→ 中期训练(K2 Horizon 1.43T)→ SFT(今天,K2 Horizon 332B)→ RL(rollouts + rewards,无固定语料)。图注指出每一步 token 更少、但更重筛选(Slides 第 6 页)。
第 7 页给出“RL 之前的冷启动”三段式:SFT 初始化基线智能体能力 → RL 发展不同推理努力程度的领域专家 → 在策略蒸馏把专家整合为一个模型。幻灯片引用 Kimi K3 的说法:“SFT 阶段为随后的 RL 阶段建立了高质量的冷启动策略。”
关于“为什么不直接用 RL”:DeepSeek-R1-Zero 直接在基础模型上跑 RL、完全没有 SFT;但其下一个模型把 SFT 放回来,R1 从数千条精挑样例开始,因为 R1-Zero 的答案混用语言、难以阅读(Slides 第 7 页)。
3.4 SFT 与 RL 并排对比
Slides 第 8 页:
- SFT:从记录下来的轨迹学习(不论由谁产生);信号是每个 token 一个目标;需要数据与 GPU;擅长学工具调用、格式、长任务;当演示错误、狭窄或用尽时会卡住。
- RL:从自己的 rollouts 学习,由针对其领域设计的奖励打分;信号是每条 rollout 一个数字;需要实时环境、沙箱、可检查的奖励;擅长锐化模型已经能尝试的事;起步太弱或奖励不可检查时会失败。
3.5 轨迹作为 token 序列与 loss mask
Slides 第 11 页给出渲染示例,含 <|im_start|>、<tool_call>、<|im_end|> 等特殊 token;图注说明“ · 表示空格、↵ 表示换行”,<|…|> 标记是单个特殊 token,其余是智能体写入与看到的内容。
Slides 第 12 页给出 loss 公式(待核,公式排版依赖视觉核验):
符号说明(依 Slide 文字):
3.6 mask 的构建流程
Slides 第 13 页三步:
- Render:模板把对话写成一条字符串,并记录每个 assistant 片段从哪里开始、到哪里结束。
- Tokenize:那些字符位置变成 token 位置。
- Mask:assistant 片段内部的 token 得 1,其余得 0。
关键点:到训练开始时,角色信息已经不存在,trainer 只看到字符串与这些位(Slides 第 13 页)。
3.7 各框架的 masking 控制
Slides 第 14 页:TRL 设 assistant_only_loss=True,由模板在渲染时标记 assistant 片段;Axolotl 设 roles_to_train、train_on_eos,在渲染后的字符串中寻找每一轮的 role header;LLaMA-Factory 设 train_on_prompt、mask_history,在编码时把整轮标为训练或忽略。三者是同一个决定,只是 TRL 把它放进模板里。备份页第 49 页给出更细的 flag 行为描述(如 train_on_eos 使每个被训练轮的 EOS 进入 loss;mask_history: true 只保留最后一轮)。
3.8 end-of-turn token
Slides 第 15 页:role header 在推理时由外部提供,从不是目标;模型产生 stop token,因此它必须在掩码之内。“结束一轮”是三个事件而非一个——结束本消息、交给工具、或完成任务;Kimi K3 的模板用不同 token 把它们拼开。若不在 mask 内训练 stop,幻灯片引述“模型可能学不会停止”。
3.9 掩蔽推理块
Slides 第 16 页:两个开关需分别设置——推理是否留在上下文、推理是否被训练。留在上下文时,推理解释了随后的动作;被训练时,它变成模型自己书写的风格。Nemotron 把预算截断的推理留在上下文中,并把人为截断从 loss 中掩蔽掉。
3.10 助手回合内部的加权
Slides 第 17 页介绍同一篇论文的两个组件:SSB 是一个学到的权重,在 loss 中重新平衡推理 token 与工具调用 token;HDR 对困难样例重采样。动机是长推理主导了 loss,而真正需要正确的短工具调用反被淹没;重新平衡在多轮上比单轮更有帮助。
3.11 样例数与 token 数
Slides 第 18 页:MAI-Thinking-1 按样例计数选混合,但 loss 是按 token 计算的;STEM 与编码轨迹长得多,于是几乎占据了全部 token。结论:无论你按轨迹计数平衡了什么,都要检查它在 token 上变成了什么。
3.12 监督轨迹中的开放问题
Slides 第 19 页明说:找不到一篇改变智能体 SFT 掩码并测量其后果的已发表实验;细致的掩码实验都是单轮指令微调,不是智能体;唯一一个“不掩蔽提示有帮助”的情形是长提示、短答案、数据少,而智能体轨迹在这三点上恰好相反。幻灯片指出该实验在 8B 规模上很便宜,且尚未有人做。
3.13 轨迹从哪来
Slides 第 21 页四象限:
| 谁跑的任务 | 全部保留 | 只保留通过验证的 |
|---|---|---|
| 更强的模型 | 普通蒸馏 | SWE-Gym · Nemotron · OT-Agent |
| 模型自己 | MAI,用自己的推理运行 | 专家迭代 · Sep 22 |
大多数智能体 SFT 数据位于右上格。底行是模型自己制造训练数据,重复这个循环就是专家迭代(后续讲座覆盖)。
3.14 蒸馏的具体例子
Slides 第 22 页:GPT-4o 与 Claude 跑了 SWE-Gym 任务,491 条 run 通过仓库测试;一个 32B Qwen 模型仅在这 491 条上微调,在两个 SWE-Bench 划分上提升超过十个点。
Slides 第 23 页:任务集始终不变,只变采样轨迹数量;采样预算用尽时准确率仍在上升;他们不缺任务,缺的是再采样更多 run 的算力。
3.15 成功 rollout 中的不良行为
Slides 第 24 页:这一页上的每条 run 都通过了测试,但看它们怎么通过的——有的在编辑与重跑测试的循环里耗掉大部分回合,有的在最终补丁里留下 print。loss 会一视同仁地复制每个动作;在这些 run 上训练,模型会把乱糟糟的过程连同修复一起学走(Slides 第 24 页)。
3.16 按形状过滤 rollout
Slides 第 25 页:此处最好的过滤器就是数回合数,丢弃所有短于五轮的 run。回合数说明一次运行花了多少工作量,但不说明工作质量好坏。四轮的干净修复会被丢掉,四十轮的烂摊子会被保留;平均而言它仍然有帮助。
3.17 选择教师
Slides 第 26 页:论文报告 GPT-5.3-Codex 在这五个模型里基准最强,但它的 run 产出的学生最弱。排行榜名次在这里给出了错误信息。试两个教师、留下更好的学生,相对而言很便宜。
3.18 任务来源与数据规模
Slides 第 27 页:任务来源的选择对结果影响最大;不同来源帮助不同基准;在调其它任何东西之前先把力气花在这里;混合最好的四到八个来源,再宽没有帮助。
Slides 第 28 页:两条曲线都从同一个 10K 数据集出发,只差在额外数据来自哪里。粉色曲线加同一任务的更多 run,会趋平;蓝色曲线加新任务,持续攀升。
3.19 课堂讨论题
Slides 第 29 页给出同一任务的三条 run:
- A:四轮内解决 issue 的短 run。
- B:做了一次错误编辑、看到测试失败、然后恢复的长 run。
- C:在十一次什么都没改变的尝试后解决 issue 的 run。
讨论问题:保留哪些?保留的那些里,哪些动作承担 loss?你的答案会让“最小回合数”过滤器错在哪里?
逐字稿中课堂讨论的要点(文字定位,约 34:22 起):有同学倾向 B,理由是训练信号落在动作上、长轨迹中观察测试并恢复的能力更有价值;也有同学指出保留 B 的风险是可能学到错误的编辑而学不到恢复能力。讲者的总结倾向是结合 A 与 B,并增加数据量以覆盖对基准最有用的能力。
3.20 多数据集统一格式
Slides 第 31 页:每个数据集由不同组按自己的格式构建;对网页,有的存 HTML,有的存可访问性树;要同时训练两个就得先写两个定制转换器。
Slides 第 32 页:统一表示的做法是把每个数据集读一次并改写为带类型的动作与观察;刚才的网页例子里,HTML 与可访问性树都保留,而不是二选一。每个数据集转换一次、每个 harness 转换一次,而不是每对一次。
Slides 第 33 页:数据统一之后仍要为训练目标 harness 写回;OpenHands、SWE-Agent、AgentLab 采取不同动作,所以同一轨迹在各自那里看起来不同。这也是设置 system prompt、决定上下文变长时怎么办的地方;然后检查结果——工具调用能否解析、每个调用是否带思考、对话是否按应有的方式结束。
Slides 第 34 页:相同 harness、相同模型、相同评估,只改训练混合;在混合上训练能击败匹配的单领域集合,即使在该领域自己的基准上也是如此。
3.21 真实 SFT 配置
Slides 第 36 页(表格,数值需对照 PDF 核验):
| 模型 | 序列 | 批大小 | 学习率 |
|---|---|---|---|
| Nemotron 3 Ultra | packed 294,912 然后 515,000 | 64 | 1.5e-5 → 1e-6 |
| MAI 自蒸馏 | packed 128k | 2,048 | 1.7e-5 → 5.2e-6,2% warmup |
| K2 Horizon | 512K,三阶段 | — | 在高量子集上衰减 |
3.22 MoE 专家路由
Slides 第 37 页:本讲涉及的前沿模型是混合专家;公开论文微调的模型是稠密的。在狭窄的智能体数据上微调 MoE,少数专家会拿走大部分 token(右图所示);MAI 的修法是监督阶段把路由平衡压得极狠——比 RL 期间强一千倍——并把 dropout 提到 0.15。
3.23 打包整段对话
Slides 第 38 页:训练在定长序列上进行,打包用多段对话填满每条序列,而不是用 padding 浪费大部分空间。每段对话被分配到剩余容量最贴合它的那个 pack;没有对话被切分或截断,这是刻意对抗幻觉的选择;相同提示不放进同一个 pack;pack 之后被打乱。
3.24 模板漂移
Slides 第 39 页:你训练用的模板必须与部署用的模板一致,精确到空白字符。把同一条轨迹渲染两次——一次后面加一条工具消息、一次不加——可能得到两个不同字符串。当追加工具消息时,条件思考块会改变更早的 assistant 回合,前缀不再匹配。
3.25 已发布的训练产物
Slides 第 40 页:K2 Horizon 的 model card 有分阶段训练表(步数、token、序列长度、各阶段用途),第 36 页数字即出自该表;它还链接了 Weights & Biases run,可以看到实际 loss 曲线,并提供每个阶段的中间 checkpoint。Nemotron 一并公开了后训练 checkpoint、训练数据与配方。
3.26 训练与运行的不匹配
Slides 第 42 页:训练一次只给一个动作打分,且总是延续录下来的那条 run;部署时没有录像,模型采取的每个动作都会改变它接下来看到的东西。换一个不同的编辑,它就进入任何训练 run 都没访问过的状态;低 loss 对那里会发生什么毫无保证。
Slides 第 43 页给出三条实操建议:信任任何数字之前,先确认你能过拟合二十个例子(若 loss 不降,bug 在上游);然后在 harness 中运行模型、在结果可检查的任务上评估;读已发表数字要小心,有时那是多个 harness 中最好的;划分要与主张匹配,同一任务的所有 run 保持在同一侧,若想说泛化到新仓库就按仓库划分。
Slides 第 44 页:在一个系统里收集的轨迹会连同任务一起教会那个系统的约定;跨若干 harness 收集是实验室避免训练出只在某一个里可用的模型的方式。
Slides 第 45 页:训练一种能力会移动其它能力,无论你是否测量;混合是这里的控制手段,按样例平衡与按 token 平衡不是一回事;所以也要评估你并不打算改进的东西。
3.27 交棒给 RL
Slides 第 47 页:“当 SFT 模型是带着 RL 的考量来选的时候,RL 提供的收益最大。”完全没有 SFT 的 RL 几乎不能移动基础模型;仅靠 SFT 也远达不到两者结合的效果;获胜配方是从一个刻意没有训练到自身最佳分数的 checkpoint 开始 RL。
四、公式的假设与符号
Slides 第 12 页给出的交叉熵形式(排版经文本提取,待核):
:模型参数。 :位置 的 token。 :位置 之前的全部 token,作为条件上下文。 :模型在给定前文下对 的预测概率。 :逐 token 的掩码位,1 表示该 token 的预测计入 loss,0 表示不计入。 - 求和只遍历
的位置。
假设:观测(observations)属于条件上下文而非预测目标,因此其对应位置
关于 end-of-turn token 是否计入 loss:Slides 第 15 页指出模型产生 stop token,因此它必须在掩码内。这可以视为对上述公式的一个具体化:至少某些结构 token 的
关于推理块的掩码:Slides 第 16 页把它描述为两个独立开关,说明
五、常见误区
以为整条轨迹都参与训练。 实际上通用做法是只在 assistant token 上算 loss(Slides 第 12 页);observations 是条件不是目标。但第 19 页明说这是一个开放问题,且“不掩蔽提示有帮助”的唯一反例恰好与智能体轨迹的特征相反。
以为换更强的教师一定更好。 Slides 第 26 页的反例是 GPT-5.3-Codex 基准最强却教出最弱的学生。
以为扩大采样就能持续提升。 Slides 第 23 页显示同一任务集内增加轨迹数在某个点饱和;第 28 页显示加同任务的更多 run 会趋平,加新任务才持续攀升。
以为通过测试就是好数据。 Slides 第 24 页明确列出通过测试但含不良行为的 run(编辑—测试循环、补丁里留
print),并说 loss 会照单全收。以为按样例数平衡混合就够了。 Slides 第 18 页指出按样例平衡,token 上可能完全失衡;第 45 页再次强调按样例平衡与按 token 平衡不同。
以为低训练 loss 意味着部署可用。 Slides 第 42 页说明训练总在延续录制的 run、部署没有录像,一个不同编辑就进入训练未访问过的状态。
忽视模板漂移。 Slides 第 39 页要求训练模板与部署模板一致到空白字符;同一条轨迹渲染两次可能得到不同字符串。
以为 loss 计算里角色信息会一直存在。 Slides 第 13 页明说训练开始时角色已消失,trainer 只看到字符串与位。
把“按回合数过滤”当作质量度量。 Slides 第 25 页明确指出它衡量工作量而非质量,会丢干净的四轮修复、留下四十轮的烂摊子,只是平均而言有帮助。
以为 MoE 与稠密模型的 SFT 一样。 Slides 第 37 页说明狭窄数据会让少数专家吃掉大部分 token,并给出 MAI 的路由与 dropout 处理。
六、复习问题
三种更新智能体的方式各有什么优缺点?为什么权重更新是“单向且不透明”的?(Slides 第 4 页)
在 SWE-Gym 示例中,一次运行最终由什么判定成功?(Slides 第 3 页)
轨迹在 SFT 中能教哪四类东西?(Slides 第 5 页)
训练流水线的四个阶段按 token 量排序是什么关系?为什么说“每一步 token 更少、但更重筛选”?(Slides 第 6 页)
DeepSeek-R1-Zero 与 R1 在是否使用 SFT 上有什么差别,原因是什么?(Slides 第 7 页)
写出交叉熵 loss 形式,并说明
的含义与 observations 的处理方式。(Slides 第 12 页) 掩码构建的三步是什么?为什么说“模板在做记账”?(Slides 第 13 页)
TRL、Axolotl、LLaMA-Factory 三者在掩码控制上的差别是什么?(Slides 第 14、49 页)
为什么 end-of-turn token 必须落在掩码内?为什么说“结束一轮”是三个事件?(Slides 第 15 页)
掩蔽推理块的两个开关分别是什么?把它们各自打开会有什么效果?(Slides 第 16 页)
BalanceSFT 的两个组件分别解决什么问题?为什么重新平衡在多轮上更有帮助?(Slides 第 17 页)
MAI-Thinking-1 在按样例平衡混合时发生了什么?这一教训的实践含义是什么?(Slides 第 18 页)
智能体 SFT 掩码实验为什么仍是开放问题?(Slides 第 19 页)
轨迹来源的四象限如何划分?大多数智能体 SFT 数据落在哪一格?(Slides 第 21 页)
SWE-Gym 蒸馏例子里,数据规模与提升幅度分别是多少?(Slides 第 22 页)
为什么采样轨迹数会在某个点饱和?作者缺的是任务还是算力?(Slides 第 23 页)
举出 Slides 第 24 页中通过测试但不应直接用作 SFT 数据的行为。
为什么“回合数少于五就丢弃”这个过滤器会犯错,却仍然有用?(Slides 第 25 页)
教师模型选择上,论文报告了什么反直觉结果?其建议的实操做法是什么?(Slides 第 26 页)
任务来源、数据量扩展、混合宽度三者的经验结论分别是什么?(Slides 第 27、28 页)
回到 Slides 第 29 页的三条 run:如果由你来定,保哪些、哪些动作承担 loss、“最小回合数”过滤器错在哪?
统一轨迹表示的动机是什么?“每个数据集转换一次、每个 harness 转换一次”相对“每对写一个转换器”省在哪里?(Slides 第 31–33 页)
在混合数据上训练与在匹配的单领域数据上训练相比,结果如何?这说明了什么?(Slides 第 34 页)
真实 SFT 配置中常见的三个超参数维度是什么?各举一个示例模型的做法。(Slides 第 36 页)
为什么在狭窄智能体数据上微调 MoE 会出现专家路由问题?MAI 的处理方式是什么?(Slides 第 37 页)
打包整段对话的规则有哪些?为什么刻意不切分、不截断?(Slides 第 38 页)
模板漂移为什么在工具循环中尤其致命?(Slides 第 39 页)
K2 Horizon 与 Nemotron 各自公开了哪些训练产物?(Slides 第 40 页)
训练与部署的不匹配体现在哪里?为什么低 loss 不能保证部署行为?(Slides 第 42 页)
Slides 第 43 页给出的评估前三条建议是什么?
为什么要在目标领域之外也做评估?“按样例平衡”和“按 token 平衡”差别在哪?(Slides 第 45 页)
SFT 与 RL 的交接原则是什么?为什么说获胜配方是从一个“刻意没有训练到自身最佳分数”的 checkpoint 开始 RL?(Slides 第 47 页)
七、待核事项
- Slides 第 12 页的 loss 公式排版(下标范围、求和条件)经文本提取无法完全确认,需对照官方 PDF。
- Slides 第 4、6、11、12、15、16、24、37、38、42 页含图示或彩色标注框,本笔记只能依据文字描述,图内细节需视觉核验。
- Slides 第 36 页的配置表格中 K2 Horizon 一列部分单元格为空白或文字描述,需对照 PDF。
- 逐字稿中出现的模型名称(如 “Kim K3”、“Neimotron”、“GPD 5.3 codeex”、“255.3 codeex”)为自动字幕机器翻译的疑似错词,本笔记在引用时统一采用 Slides 文本中的写法(Kimi K3、Nemotron、GPT-5.3-Codex 等),但该对应关系本身待核。
- 逐字稿中关于数据集名称的表述(如 “go browse”、“mine to web”、“Sweet Smith”、“Sweetbench verified”、“terminal bench”)疑为字幕错词,本笔记未在正文中作为确切名称使用。
- Slides 第 22 页“提升超过十个点”与第 23 页“采样预算用尽时准确率仍在上升”的具体数值与曲线形态需对照 PDF 与原始论文。
八、给定资料中的官方来源链接
- 课程主页:https://www.cmu-agents.com/
- 课程日程与阅读:https://www.cmu-agents.com/#schedule
- 作业与项目要求:https://www.cmu-agents.com/#assignments
- Assignment 1 · Harness:https://github.com/cmu-agents/assignment-1
- Assignment 2 · Eval:https://github.com/cmu-agents/assignment-2
- 课程视频(Lecture 8):https://www.youtube.com/watch?v=O3HSU0AoILc&list=PLSN0qpDfUvTM&index=8
- 官方课程日程(重复列出,与“课程日程与阅读”同一链接):https://www.cmu-agents.com/#schedule
- Lecture 8 slides:https://www.cmu-agents.com/slides/lecture-08-sft.pdf
把轨迹变成权重:CMU 11-768 第八讲之智能体监督微调导读
来源:平台字幕与官方 Slides。内容已由用户审核确认。
一、这一讲要解决什么问题
前几讲我们讨论的“更新智能体”手段,都发生在推理时:改上下文窗口,或者改外部工件。第八讲把更新位置从“提示”挪到了模型权重本身。课程 Slides 第 4 页把三种做法并排放在一起:上下文窗口对已发生的事情保真度高,但昂贵、有噪声、且不决定什么重要;外部工件可检查、可编辑、可检索、可移植,但必须被归纳、选择和持续维护;权重更新的好处是推理更快、行为改变更广——“改一次权重,之后每一次调用都带上新行为,prompt 里什么都不用加”,代价是更新更慢、不透明、且模型特定(Slides 第 4 页)。
于是问题变成:既然 SFT 这么“重”,它到底教了什么,值得单独讲一讲?Slides 第 5 页给出四个答案:怎么写这个 harness 真正能跑起来的工具调用;对话变长时怎么继续下去;工具结果是长什么样的,从而让模型学会“等结果”而不是自己编一个结果;以及数据里附带的其他东西——额外评论、重复命令、某一种解题方式。
关键术语:本讲中的“轨迹”(trajectory)指一次智能体运行被记录下来的完整消息序列,包含 system / user / assistant / observation 等行;“harness”指承载智能体循环的那套系统(系统提示、工具定义、消息模板等)。这一区分很重要,因为后面“同一段轨迹在不同 harness 下长得不一样”会反复出现(Slides 第 33、44 页)。
二、轨迹先变成 token 序列
Slides 第 3 页给了一个 SWE-Gym 风格的具体例子:智能体拿到一个 issue 和沙箱里的两个工具(bash 和一个文件编辑工具),产出思考与命令,读命令返回的 observation,最后由仓库自己的单元测试决定这次运行是否成功。整条轨迹里 assistant 消息其实很少——逐字稿里提到只有三条 assistant 消息。
学习者解释:这里的要点是,SFT 学习的不只是最终答案,而是“在什么上下文里发什么动作”。这就是为什么后面的损失掩码设计会那么关键——如果不做掩码,模型会被要求去预测那些本不该由它预测的东西。
Slides 第 11 页展示了模板如何把整条轨迹渲染成一个字符串:每个边界都是一个模板 token,<|...|> 是单个特殊 token,其余部分是智能体自己写下和看到的内容。Slides 第 12 页给出了核心公式与掩码定义:
[ L(\theta) = -\sum_{t:,m_t=1} \log p_\theta(x_t \mid x_{<t}) ]
(公式按 Slides 第 12 页文本提取转写,符号细节待对照官方 PDF 核验。)
关键洞察:掩码是一位一个 token 的比特,说明这个 token 的预测是否计入损失。只在 assistant token 上算损失,observation 是条件上下文而不是预测目标。最小化它,就是提高“每个被记录动作在给定其之前一切时的概率”。
三、掩码是怎么造出来的,以及它的边界在哪
Slides 第 13 页把掩码的产生拆成三步:渲染(模板把对话写成一个字符串,并记下每个 assistant 片段的起止)、分词(字符位置变成 token 位置)、掩码(assistant 片段内为 1,其余为 0)。到训练开始时,角色信息已经没了——trainer 只看到字符串和比特(Slides 第 13 页)。
Slides 第 14 页对比了三个框架的同一决策:TRL 用 assistant_only_loss=True,由模板在渲染时标记 assistant 片段;Axolotl 用 roles_to_train 和 train_on_eos,在渲染后的字符串里找每轮的角色头;LLaMA-Factory 用 train_on_prompt 和 mask_history,在编码时把整轮标成训练或忽略。Slides 第 49 页给出更细的标志行为,例如 LLaMA-Factory 的 mask_history: true 表示只训练最后一轮、截断时保留最后一轮;Axolotl 的 train_on_eos: turn 表示每个被训练轮次的 EOS 进入损失。
学习者解释:这三者做的其实是同一个决策,差别只在“谁来记账”——只有 TRL 把这个决定放进了模板里。当你要跨框架迁移数据时,这个差别会导致静默的错配。
Slides 第 15 页讲 end-of-turn token:角色头在推理时是被提供的,因此从不是预测目标;而模型要自己产生停止 token,所以它必须在掩码内。“结束一轮”其实是三个事件而不是一个——结束这条消息、交接给工具、或完成任务;Kimi K3 的模板用不同 token 把它们分开写(Slides 第 15 页)。
Slides 第 16 页讲推理块的掩码:有两个独立的开关——推理是否留在上下文里、推理是否被训练。在上下文里,推理解释紧随其后的动作;被训练,它就变成模型自己书写的风格。Nemotron 的做法是把预算截断的推理保留在上下文中,并把人为截断从损失中掩掉(Slides 第 16 页)。
Slides 第 17 页引入 BalanceSFT 的两个组件:SSB 是一个学出来的权重,用来在损失中重新平衡推理 token 与工具调用 token;HDR 对困难样本重采样。动机是:长推理会主导损失,而真正需要正确的恰恰是那个短的工具调用;重新平衡在多轮上比单轮帮助更大。
Slides 第 18 页给出一个很容易被忽视的教训:MAI 是按样本数来选混合的,但损失是按 token 算的。STEM 和编码轨迹长得多,于是几乎占掉了全部 token。结论是——无论你按轨迹数怎么平衡,都要检查它在 token 上变成了什么。
Slides 第 19 页把这些写成开放问题:没有找到一篇已发表实验是“改动智能体 SFT 的掩码并测量后果”的;精细的掩码实验都是单轮指令调优而非智能体;唯一一个“不掩码提示反而有帮助”的案例是长提示、短答案、数据少,而智能体轨迹在这三点上全部相反。Slides 第 19 页还指出,这个实验在 8B 规模上很便宜,且“就摆在那里没人跑”。
四、选轨迹:老师、来源、长度、多样性
Slides 第 21 页用两行两列概括了轨迹来源:谁跑的任务(更强的模型 / 模型自己)× 你保留什么(全部 / 只保留通过的)。右上角是绝大多数智能体 SFT 数据所在的位置——更强的模型写得更好,而任务自己的测试让“只留好的”变得便宜。左下是普通蒸馏,右下是专家迭代,将在后续讲座展开。
Slides 第 22 页给了一个具体的蒸馏结果:GPT-4o 和 Claude 跑了 SWE-Gym 的任务,491 次运行通过了仓库测试;一个 32B Qwen 模型仅在这 491 条轨迹上微调,在两个 SWE-Bench 划分上的分数都涨了十点以上。Slides 第 23 页接着说轨迹数量与饱和:任务集不变、只改采样轨迹数,采样预算耗尽时准确率仍在上升,他们有的是任务,缺的是采样算力。
Slides 第 24 页提醒:每一次运行都通过了测试,但有的编辑后反复重跑测试占掉大部分轮次,有的在最终补丁里留下 print。损失会照抄每一个动作,好或不好——在这些运行上训练,模型把“乱”和“修复”一起学走(Slides 第 24 页)。
Slides 第 25 页给出一个简单但有效的过滤:数轮次,丢掉所有短于五轮的运行。轮次数量说的是这次跑花了多少工作量,而不是工作质量好不好;四轮的干净修复会被丢掉,四十轮的乱局会被保留,但平均而言它仍然有帮助。
Slides 第 26 页给出本讲最反直觉的一条:论文报告 GPT-5.3-Codex 是这五个模型中基准最强的,但它产生的学生最弱——排行榜名次在这里给了错误信号;而试两个老师、保留更好的学生,相比之下是便宜的。Slides 第 27 页说任务来源的选择对结果的影响最大,不同来源帮助不同基准,所以要在调其他任何东西之前先把力气花在这里;他们的经验结论是混合最好的四到八个来源,更宽没有帮助。
Slides 第 28 页区分两种“加数据”:两条曲线都从同一个 10K 数据集出发,只差在额外数据来自哪里。粉色曲线加同一任务的更多运行,会趋于平缓;蓝色曲线加新任务,持续攀升。Slides 第 29 页把这个问题变成一个课堂讨论:四轮解决、长轨迹错编辑后恢复、十一次无变化尝试后解决——保留哪些?哪些动作承担损失?哪个答案会被“最小轮次过滤”搞错?
学习者解释:这个讨论的设计意图是让人意识到,“成功”与“值得学”不是一回事。一个通过测试的运行里可能包含不该被强化的行为,而一个失败的运行里可能包含最该学的恢复过程。
五、一种格式服务多个数据集
Slides 第 31 页描述异构性:每个数据集由不同团队以自己的格式构建。同一个网页,有的存 HTML,有的存可访问性树。要一起训练两个数据集,你得先写两个定制转换器。Slides 第 32 页给出对策:读一次每个数据集,改写为带类型的动作与观察;对上面那个网页,同时保留 HTML 和可访问性树,而不是二选一;于是每个数据集转换一次、每个 harness 转换一次,而不是每对组合转换一次。
Slides 第 33 页讲 harness 特定渲染:数据进了统一格式,你仍然要为训练目标 harness 写回去。OpenHands、SWE-Agent 和 AgentLab 采取的动作不同,所以同一段轨迹在各自那里长得不一样。这里也是你设定系统提示、决定上下文变长时怎么办的地方;然后检查结果——工具调用能否解析、每个调用是否带一个思考、对话是否按应有的方式结束。
Slides 第 34 页给出多样性对照实验:相同 harness、相同模型、相同评测,只改训练混合;在混合数据上训练,即便在该领域自己的基准上也超过了匹配的单领域集合。
六、跑训练:配置、路由、打包、模板漂移
Slides 第 36 页列出三份真实配置:Nemotron 3 Ultra 打包 294,912 再到 515,000,批次 64,学习率 1.5e-5 → 1e-6;MAI 自蒸馏打包 128k,批次 2,048,学习率 1.7e-5 → 5.2e-6 并带 2% 预热;K2 Horizon 用 512K、分三个阶段、在一个高质量子集上衰减。
Slides 第 37 页讲专家路由:本讲中的前沿模型是混合专家,而公开论文里微调的模型是稠密的。在狭窄的智能体数据上微调 MoE,少数专家会吃掉大部分 token。MAI 的做法是:在监督阶段把路由平衡推得非常狠,比 RL 期间难一千倍,并把 dropout 提到 0.15。
Slides 第 38 页讲打包:训练跑在固定长度序列上,打包用多段对话填满每一条,而不是把大部分序列用 padding 浪费掉。每段对话被分配到剩余容量最紧匹配的那个包里;没有任何对话被切分或截断,这是对抗幻觉的刻意选择;相同提示被排除在同一个包之外,包随后被打乱。
Slides 第 39 页讲模板漂移:你训练时用的模板必须是你服务时用的模板,精细到空白字符。把同一段轨迹渲染两次,一次后面加工具消息、一次不加,你会得到两个不同的字符串;当工具消息被追加时,条件思考块会改变先前那个 assistant 轮次,前缀不再匹配。
Slides 第 40 页提到已发布的训练产物:K2 Horizon 的模型卡里有逐阶段的训练表(步数、token、序列长度、每个阶段是为了什么),上面第 36 页的数字就来自那张表;它还链接了 Weights & Biases 运行,并附带每个阶段的中间检查点。Nemotron 发布了后训练检查点以及训练数据和配方。
七、怎么知道它真的有效
Slides 第 42 页指出训练与运行之间的错配:训练一次只对一个动作评分,而且总是继续被记录的那条运行;部署时没有录像,模型采取的每个动作都会改变它接下来看到的东西。同前缀之后,记录的编辑 A 让测试通过,而模型可能产出一个合理但不同的编辑 B,测试失败——于是它进入了一个没有任何训练运行访问过的状态,低损失对此什么也没保证。
Slides 第 43 页给出实践建议:在信任任何数字之前,先检查你能不能过拟合二十个例子;如果损失不下降,bug 在上游。然后在 harness 中运行模型,在有可检查结果的任务上评测。读已发表数字时要小心,有时它们是几个 harness 中最好的;把划分与你的说法匹配——同一个任务的所有运行放在同一侧,若想说“泛化到新仓库”,就按仓库划分。
Slides 第 44 页讲跨 harness 的鲁棒性:在一个系统里收集的轨迹,会把该系统的约定连同任务一起教给模型。跨几个 harness 收集,是实验室避免训练出“只在一个 harness 里能用”的模型的常见做法。
Slides 第 45 页讲目标领域之外的回归:训练一种能力,你会移动其他能力,无论你是否测量。这里的控制是混合比,而按样本平衡不等于按 token 平衡;所以也要评测那些你并不打算改进的东西。
八、交给强化学习
Slides 第 47 页给出交接的核心判断:“当 SFT 模型是带着 RL 的考量来选的,RL 提供的收益最大。”完全没有 SFT 时,RL 几乎推不动基础模型;只有 SFT 也远达不到两者结合的水平;获胜配方是从一个“刻意没有训练到它自己最佳分数”的检查点开始 RL。
学习者解释:这句话值得多停一秒。它不是“SFT 不太好”,而是说 SFT 的目标函数不该是“在 SFT 数据上尽可能好”。如果你把模型训到在演示分布上饱和,留给 RL 的提升空间反而可能更少,因为 RL 擅长的是打磨模型已经能尝试的东西(Slides 第 8 页)。
九、把本讲压成一条链
- 轨迹是记录,不是答案;SFT 把“在什么上下文里发什么动作”变成权重(Slides 第 3、5 页)。
- 模板把轨迹变成一条 token 序列,掩码决定哪些 token 承担损失(Slides 第 11、12、13 页)。
- 掩码的细节——停止 token、推理块、推理与工具调用的再平衡——都是会改变学到什么的设计选择(Slides 第 15、16、17 页)。
- 选轨迹比调超参更影响结果:老师可能名不副实,来源的组合比来源的数量重要,多样性比同一分布的堆量重要(Slides 第 26、27、28 页)。
- 统一格式能让你用多个数据集,但写回 harness 时仍要检查工具调用能否解析(Slides 第 31、32、33 页)。
- 训练能跑起来不等于有效:训练继续录像,部署继续它自己,所以必须同时看损失和评测(Slides 第 42、43 页)。
- SFT 的正确目标不是自己刷到最高,而是给 RL 一个好起点(Slides 第 47 页)。
十、留给读者的问题
Slides 第 19 页明确说,改动智能体 SFT 掩码并测量后果的已发表实验“就摆在那里没人跑”。如果你要动手,最想先动哪个开关:推理是否进损失、observation 是否进损失、还是停止 token 的拆分粒度?这个问题没有本讲给出的答案,本讲只给出了为什么它值得被问。
官方来源链接
- 课程主页:https://www.cmu-agents.com/
- 课程日程与阅读:https://www.cmu-agents.com/#schedule
- 作业与项目要求:https://www.cmu-agents.com/#assignments
- Assignment 1 · Harness:https://github.com/cmu-agents/assignment-1
- Assignment 2 · Eval:https://github.com/cmu-agents/assignment-2
- 课程视频(Lecture 8):https://www.youtube.com/watch?v=O3HSU0AoILc&list=PLSN0qpDfUvTM&index=8
- Lecture 8 slides:https://www.cmu-agents.com/slides/lecture-08-sft.pdf
核验状态说明
本文依据 Lecture 8 的 Slides 逐页文本提取与中文逐字稿整理稿撰写。Slides 中的图示、布局与公式(如第 12 页损失函数与第 23、28 页曲线形状)仅凭文本提取无法确认,已在相应位置标注或按提取文本转写,正式引用前请对照官方 PDF 核验。逐字稿为平台英文自动字幕清洗稿的机器翻译,状态:用户审核通过;本文未对其中的模型名称与数字做独立核验,涉及具体数值处一律以 Slides 页码为准。本文内容已由用户审核确认。
CMU 11-768 Fall 2026 · Lecture 8 · Training 1: Supervised Fine-Tuning (SFT)|完整中文逐字稿
- 视频:官方视频
- 来源:平台英文自动字幕清洗稿的机器翻译;保留原段落时间戳。
- 状态:用户审核通过;中英文按完整语义段合并,每段保留首条原字幕时间戳。
- 说明:这是逐字稿,不是摘要;保留讲述顺序、例子与课堂互动。
正文
[00:00.320] 大家好。我非常高兴今天能来讲这堂课。是关于智能体的监督微调。在我开始之前,我想问一下你们中有多少人以前训练过智能体?请举手。好的。那么你们中有多少人做过智能体的 SFT? 好的,这实际上比我预期的要多。好的,那么我今天会讲这些主题,因为这是关于智能体训练的第一堂课。所以我首先会整体介绍一下智能体训练是什么意思,然后讲 SFT 的具体主题,以及过渡到 RL,那将是接下来的三堂课。
[00:49.280] 好的。这是从 sui gym 中采样的一个示例轨迹。我们可以看到轨迹中有不同的行。有 system 行、user 行、assistant 行和 observation。在这些之中,我们可以看到这条轨迹中只有三条 assistant 消息。
[01:17.119] 那么在整体设置中,智能体接收一个由用户给出的 issue,以及沙箱中的一些工具,例如在这个案例中是 bash 和一个文件编辑工具。在这个过程中,智能体产生 ss 并生成命令,然后读取那些命令返回的观察结果,最终仓库自己的任务比如单元测试将决定
[01:51.200] 这次运行是否成功。我们之前已经讨论过几种更新智能体的方法,比如首先是上下文窗口。不同方法有不同的优缺点,比如对于上下文窗口,它对已经发生的事情有高保真度,但它代价高、有噪声,并且不能决定什么重要;而对于外部工件,它是可 编辑的、可检索的和可移植的,所以非常容易被人阅读,但它必须被归纳、选择并在整个智能体轨迹过程中维护。所以今天我们要讲的是更新模型权重。这可以带来更快的推理和更广泛的行为改变,但因为我们 需要训练模型,训练过程会更慢并且 更不透明,因为它像是黑盒的,并且是模型特定的,因为训练后会产生模型特定的行为。所以我们改变权重一次,之后的每次调用都会获得新行为,而提示中没有任何额外内容。我们之前讨论的前两件事,比如只在几秒内更新,以及这些每次改变时训练运行的成本,
[03:10.800] 而且你无法打开权重来看看模型内部发生了什么,或者把改变迁移到不同的模型上。所以这件事是单向的且不透明的,就像训练一个模型我们需要轨迹。那么轨迹教了什么?首先是如何写出 harness 实际能运行的工具调用。 第二,如何在对话变长时继续下去,因为我们从中学习的轨迹应该是很长的,以及工具调用结果、工具结果应该是什么样子。所以它等待一个结果而不是自己写一个,以及数据中 whatever else 的内容,比如我们用于训练的那些,例如额外的评论、重复的
[04:02.480] 命令和一种解决问题的方式。有几项工作可以给出一个更广泛的概述,关于前沿实验室或学术界中训练是什么样子的。完整的,这是完整的训练流程。我们首先从预训练开始,这是 token 数量最大的阶段,但这是语言模型学习基本下一 token 预测的阶段,然后是中期训练,这是第二阶段,但人们对中期训练的定义非常不同,中期训练就像是预训练和后训练之间发生的任何事情,有些人现在甚至把 SFT 也算作中期训练,但通常 SFT 被认为是后训练阶段。所以在中期 训练之后,我们进入 SFT。我们就像,这将在今天涵盖,然后下一阶段将是 RL,主要是 rollouts 和 rewards,将在后面涵盖。但请注意,对于大多数预训练、中期训练和 SFT,我们可以找到固定的语料库来训练模型。但对于 RL,通常没有固定的语料库。它是在
[05:17.919] 模型自己的 rollouts 上学习的。所以我会把监督微调作为强化学习之前的冷启动。冷启动意味着这个 checkpoint,RL 从今天的开始,这代表了当今后训练最常见的流程之一。这像是从 Kim K3 的报告中提取的,模型在预训练和 中期训练之后,先做监督微调来初始化一个基线智能体能力,下一阶段是强化学习,模型在其中发展出不同推理努力程度的领域专家。最后一个阶段是在策略蒸馏,将专家整合到一个模型中。这是最后一个阶段,我们将在后面的讲座中涵盖第二和第三个主题。
[06:13.919] 在 Kim K3 中,模型在 SFT 期间学会调用工具并完成长任务,在任何 RL 之前,但有些人可能会问,为什么我们不直接用 RL 代替 SFT,为什么我们现在还做 SFT。这个问题,deepseek R1 zero 尝试过 seek response,再次像 DeepSc R10 直接在基础模型上运行 RL。在任何 SFT 发生之前 他们学到了教训。所以在他们的下一个模型中,他们把 SFT 放回来了,R1 从几千个创建的 SFT 示例开始,因为 R10 的答案会混合语言,没有 SFT 很难阅读。我认为 SFT 阶段为后续的 RL 阶段建立了一个高质量的协调策略。
[07:17.520] 这是 SFT 和 RL 并排的比较。对于监督微调,它从记录下来的智能体轨迹中学习,无论这些轨迹是谁产生的,可以是由更强的模型甚至它们自己产生的,SFT 学习每个 token 的目标信号,它需要数据和 GPU 来训练模型。它擅长学习工具调用格式和长任务,但被 困在错误、狭窄或用尽的演示中。对于强化学习,它从自己的 rollouts 中学习,这些 rollouts 由为其领域设计的奖励来评分。它学习每个 rollout 一个数字的信号,每个长 rollout。它需要一个实时环境、沙箱和可检查的奖励,这些是一些有趣的事实,对我们的人来说
[08:12.560] 解决。而且呃,它擅长强化模型已经尝试去做的事情。呃,但起步阶段是两周,所以奖励有时可能不会像被检查那样。呃,在我们的讲座中,我们将在接下来的三讲中涵盖强化学习,而且我认为下一个主题我将要讲的是把轨迹作为 token 序列,我们都知道从语言模型那里 可能有 chat template,它对每个不同的语言模型都是不同的。所以对于每个 token 序列,呃,就像上面的每个边界都是一个模板 token,呃,让我们在这里使用硬币例子 queen template 例子,呃,它有一些特殊 token,比如呃 IM start,然后有像 to call 等特殊 token,然后我们把这条轨迹喂给
[09:15.839] 这个模板,你就得到一个字符串,代表整条轨迹的 token 字符串。比如,它可能会经历多次工具调用,比如 10 次调用,并到达一个最终答案,向某个 verifier 提交一条最终轨迹。但所有这些都将被表示为会被送入训练基础设施以训练模型的一个字符串。而 这些特殊 token 标记是单个特殊 token。其余部分是 agent 在其输出的轨迹中实际能够呃写入和看到的内容。呃,与语言模型不同,对于 agentic SFT,我们可能不会训练整条输出、整条轨迹。比如,在这里我表示的轨迹中,灰色框是我们视为 context 的那些。所以这些不会被计入呃 loss 计算。呃,但红色框的那些是我们计算 loss 的地方,比如 cross entropy loss 是我们通常使用的。但呃,我们对 assistant token 应用了一个特殊的 mask,我们只在 assistant token 上训练,而不在被视为 context 的非 assistant token 上训练。
[10:35.920] 所以这个 mask 就像每个 token 一位,说明该 token 的预测是否计入最终训练,我们想要最小化它,因为最小化它会提高每个被记录动作的概率,给定它之前的一切作为 context。这将有助于提升呃 agent 模仿轨迹中已有内容的能力,呃 这里的 observations 是条件 context,但不是预测目标。是的。>> 为什么我们不尝试也用训练那样来训练 observation?它可能会带来结果。>> 是的。问题是,为什么我们不也在 observations 上训练模型。呃,我会说,在 agentic SFT 中,只在 assistant messages 上训练是通用的经验法则,因为 observations 或 user context 可能是 由用户或环境给出的,而不是由 assistant 预测给出的,但呃我同意,呃存在一些论文在 observations 上训练,并发现它对于预测世界呃阶段呃状态是有用的。所以呃,我这里讨论的是呃通用配方,但呃你可能会做消融研究,甚至创新,来看看什么 可能是你场景的最佳选择。>> 关于那点快速追问一下,有一篇这样做的特定论文叫做 computational world model,是 Meta 发表的。所以他们训练模型去呃输出工具调用的结果,也就是执行代码。呃,所以他们发现它在与
[12:23.360] 理解执行语义对齐的任务上相当有帮助,但呃你也可以想象它会占用模型某种一部分容量。所以是的,呃绝对地,如果你的模型太小,它可能会损害它在其他事情上的性能。这有点像是一个开放问题,即它如何影响模型的基础 agentic 能力,但它可能对
[12:45.200] 世界建模或规划非常有用。所以它更具实验性,有点像 >> 是的,呃,为了更多地解释 mask 是如何构建的,呃,首先我们有一个 renderer,模板把对话写成呃一个完整字符串,并通过呃一些特殊 token 知道每个 assistant 片段从哪里开始、到哪里结束,然后呃 tokenize 那些字符位置 变成为呃 token 位置,然后这些呃我们把 assistant 内部的 token mask 为 1,其他所有 token 得到 0,因为它不被训练。模板在这里做记账,因为呃 system text 从哪里开始、到哪里停止,等到训练开始时,呃 system message、user、呃 observation 的行已经不在了。trainer 只看到
[13:41.760] 字符串和 bits,别的什么也没有。这些是一些呃 SFT 框架中的一些 masking 控制。我真的很喜欢一个具体例子,就是 llama factory,在那里你可以指定你是否也想在 prompt 上训练,你是否想在计算 loss 时 mask history。呃,所以我可以展示一个呃 llama factory 实际使用的例子。 呃,这是我在做 agentic SFT 时非常喜欢使用的其中一个包。你可以呃生成 rollouts 或合成轨迹,并把它们适配进呃这个基础设施,并确定你想用哪个模型来训练,然后就开始训练。呃,你可以指定参数,比如呃你是想训练整条轨迹,还是想在某
[14:53.839] 个位置把它截断,或者像我们想也在 context 上训练。所以呃还有另一个特殊 token,比如 end of turn token,它对不同模型表示不同,比如对于 queen 模型,它被表示为呃这里这个红色 token,即 I am end,呃而对于其他模型,呃比如呃可能 Kimi、Llama,它们都 有非常不同的 end of token、end of turn token。所以如果你想训练一个模型,你可能需要看一下它们的 chat template 是什么,这样你就不会在 post training 期间呃让模型困惑。呃,我在设置训练轨迹时观察到的一件事是,人们经常呃搞混什么要包含在呃 loss 计算中,什么只是
[16:08.079] 仅 context。呃,比如,这里 assistant 的行头现在被训练为让 agent 产生、生成,而模型只产生呃这里的红色框,而不是灰色框,最后模型产生 stop token。所以它也必须在 mask 内部被训练,而且 end of turn 呃实际上像是多个 事件的组合,而不只是一个,因为 end of turn 意味着你需要结束这条消息,你需要结束这个工具,而且你还需要完成任务。呃,所以比如在这里下面 Kimi K3 的模板中,呃它实际上用单独的 token 把它们分开,以便模型更好地学习如何停止。
[17:02.800] 训练过程中可能被掩蔽的另一件事是推理块。我们知道推理块是非常长的块,有时人们只在非推理部分上训练。所以只训练实际的工具调用或实际的轨迹。
[17:24.880] 所以通常有几个开关你可以尝试实验。推理是留在上下文中还是留在系统消息中?如果它们留在助手消息中,是否应该在上下文中的推理上训练,解释跟随上下文的动作,如果在其上训练,它就变成模型自身书写的一种风格。 例如,Neimotron 比如 Neimotra 3 ultra 在上下文中保留类似预算截断的推理,并在损失中掩蔽人为截断。这些是模型特定的决策,在不同实验室或不同前沿模型开发之间非常不同。
[18:13.679] 嗯 >> 是的当然 >> 有什么区别 >> 呃你是在问为什么有时推理被掩蔽?是的。呃我实际上正在讲这个,因为推理轨迹与实际的工具调用或实际的任务完成阶段相比可能非常非常长,这可能会让模型困惑,只产生长推理但 像与最终结果混在一起。所以呃要么推理有时推理被掩蔽,有时推理被训练,还有去年这篇关于平衡 SFT 的论文,它旨在平衡推理轨迹与执行或工具调用轨迹。所以呃模型可以学习两者。
[19:06.880] 嗯所以如果我们有非常长的推理轨迹,它可能主导损失,而短的像工具调用或任务完成才是我们实际最需要的,比如正确性。例如对于软件工程任务,我们可能希望模型推理它的下一步是什么,但我们不希望模型不实际执行下一步。所以嗯推理 和动作或执行都很重要。所以在这篇平衡 SFT 的论文中,他们引入了推理和工具调用的重新平衡,这有助于更多多轮智能体执行,呃这是来自 MAI 的表格,所以微软 MAI 模型 MAI 思维模型,嗯他们针对不同的能力有非常不同的样本,他们旨在训练。比如例如,他们
[20:13.120] 将 STEM 能力和编码归类为一个类别,将通用智能体能力归为另一个类别,将通用有用性和安全性归为另一个。呃从他们的样本权重你可以看到呃它们有多不同,考虑到每个智能体能力的重要性。嗯但像结果是 token 权重与样本权重非常不同,因为 不同能力可能产生不同长度的 SFT 轨迹,其中 STEM 和编码轨迹通常非常长,相比呃可能通用有用性和安全性,在 MAI 论文中他们倾向于呃平衡混合或通过计算样本数量来选择混合,但像 STEM 和编码轨迹是如此长。所以他们几乎占据了 他们训练的所有 token,无论你如何通过计算轨迹来平衡,你也应该所以这个答案是,你也应该检查它在 token 上是否也平衡,或者像 token 中的分布是否是你偏好的。嗯这些也是监督轨迹中的一些开放问题,其中第一个是我们无法找到
[21:35.039] 一个已发表的实验,改变智能体 SFT 的掩码并测量发生了什么,但正如呃 Benu 所说,有像最近一篇关于计算世界模型的论文,也像在观察上训练。所以我认为这是一个开放问题,值得呃实验,而且仔细的掩蔽实验大多是单轮的,最近呃在指令调优上 而不是在智能体上。嗯而唯一一个取消掩蔽提示有帮助的案例,像有长提示和短答案以及很少呃数据,像下面我附上的论文中,智能体轨迹在所有三个方面都是相反的。所以我仍然认为这是一个开放问题,值得讨论。
[22:25.600] 嗯选择轨迹对智能体 SFT 也很重要。嗯所以这是一个表格,像智能体 SFT 的典型轨迹来自哪里,左列像谁运行任务,像从哪里收集智能体 SFT 轨迹,要么来自能运行它的强模型,要么来自像你正在训练的模型,像呃例如对于 MAI 他们使用它自己的 推理运行或轨迹用于 SFT,嗯是的,所以像取决于角色,嗯你是否保留从生成轨迹中得到的一切,或者你只保留通过某些阈值或验证器的运行。所以如果你保留从强模型生成的一切,那就像叫做普通蒸馏。如果你保留 通过某种验证器的运行,像大多数 SFT 属于这个类别,例如 Nvidia 的 Swimmatron 和开源智能体都落入这个类别。嗯然后像如果你保留从模型自身生成的一切,那么像一个例子是 MAI,但他们感觉差别很小,嗯使用像 FFTD 和呃如果你保留通过
[23:53.200] 验证器的运行,用模型自身,那么这就像叫做专家迭代,应该像后面会讲到。所以大多数智能体 SFT 数据位于像右上框,像只保留用强模型生成的过滤数据的子集,因为更强的模型像比学生能写出更好的轨迹,而且任务本身像任务可能很好,保持呃 只有好的轨迹,呃而在底行,模型自己制造训练数据。所以重复循环,像你得到 S 用于迭代,这将在后面覆盖。这是一些来自 sweet gym 的结果,它落入右上像你保留通过用更强模型运行它的运行类别。呃它发现像它达到了
[24:50.720] 高得多的解决率,在从像 GT40 和 Cloud 等模型蒸馏后,同时只保留通过呃仓库任务的轨迹。嗯就轨迹数量和饱和而言,扩展训练轨迹的数量可能嗯导致模型性能增加,但像它可能在某个点饱和,而且更容易,呃对于 饱和,如果你使用更小的模型训练。所以在这里的图中,像 SGM 的作者始终使用相同的软件工程任务集,只有样本数据的数量像变化。所以随时间增加,准确率仍在上升,当采样预算用完时,他们有任务剩余,像他们用完的是计算 多采样一些呃更多的运行,这个图中的每个点都使用相同的任务,就像当你添加新任务时会发生什么,呃我稍后会讨论,但就像并非所有的智能体轨迹可能都是有帮助的。即使在成功的 rollout 中也有一些理想的行为。所谓成功,我们指的是最终呃任务被成功完成了。但其中有一些
[26:27.840] 中间的东西我们可能无法衡量,比如如果我们直接把它用作 SFT 数据,这可能在教一些不理想的行为。所以让我们假设每次运行都通过了它的单元测试或者仓库测试。但看看比如说其中一个编辑了,呃那个红框里显示的,其中一个编辑了并在循环中重新运行测试,占据了它大部分轮次,而 另一个在最终批次中留下了 print 语句。所以虽然最终答案是错的,但这一切可能仍然会教给模型一些错误的行为,呃这些是它不应该从 SFT 中学到的,而损失函数会复制每一个动作,不管它好不好。所以在这些运行上训练,模型会连同最终的修复一起学到最好的东西。
[27:26.159] 有一些过滤器用来判断什么算是好的 SFT 数据,一个非常简单的例子就是按长度过滤,呃按轨迹的长度,u 比如说一个轨迹的轮次少于,比如说五轮,那它很可能不是很好的轨迹,因为如果它很短,有各种,嗯你可以说各种不同的东西 关于短轨迹,但要么它很短是因为它一次性就正确解决了任务,那智能体就很难学到错误恢复能力,呃要么可能这个轨迹本身就是不正确的。所以要么非常短,要么智能体在生成轨迹时遇到了一些错误,使得它非常短。所以按 轨迹长度过滤是人们今天做的一种有用的方法之一。嗯,这个呃消融研究是由 open s agent 那篇论文做的。所以呃他们的一个结论是,这里最好的过滤器就是简单地计算智能体的轮次数,并丢弃每一个短于五轮的运行。
[28:47.840] 轮次计数说的是一个运行花了多少工作量,呃但不是说这个工作好不好。然后呃嗯在这种情况下,一个四轮的干净修复可能会被丢弃,呃而一个 14 轮的杂乱轨迹可能仍然被保留,但平均而言它仍然有帮助。SFT 中另一个重要的事情是你是否能选择最好的老师,就像模型本身是 最好的模型并不意味着它可能是最好的老师。嗯对于 open source agent 那篇论文,他们在几个基准上评估了几个智能体,他们发现当时评估的最好的智能体,也就是 GPD 5.3 codeex,实际上是所有这些呃模型中最差的老师。 你可以看到,与其他模型相比,它带来的增益更少,甚至从 255.3 codeex 蒸馏后性能还有所下降。所以如果你想从一个更强的模型蒸馏,或者如果你想从一个老师模型生成轨迹,呃你可能需要明智地选择你的老师模型。
[30:05.279] >> 是的。>> 主要在工具调用上教学,这怎么可能?所以 codeex 产生了正确的工具调用,但它们对训练其他模型来说是最低效的。这背后有什么原因吗?怎么会这样?嗯这背后可能有各种原因,比如说呃 GPT 5.3 codeex 擅长训练,擅长呃解决任务本身,但就像 生成的轨迹可能对学生模型不太适用,因为思考模式可能非常不同,或者解决任务的方法可能非常不同。嗯所以是的,这是其中一种解释。嗯另外排行榜排名只说明呃这些模型在这里运行东西的能力,你应该尝试更多的
[31:04.399] 老师并保留更好的学生,这是便宜的,相比之下也是如此,对于 SFT,明智地选择任务来源很重要。外面有太多的资产数据资产了。嗯但可能并非所有都能适用于你想要训练的任务。嗯这仍然是由 open source agent 做的,但就像他们对多少呃 每个不同的智能体轨迹任务来源能对不同的基准贡献多少做了些消融研究。比如说 Sweet Smith 对 Sweetbench verified 贡献最大,但对 terminal bench 贡献较少。而任务来源的选择可能会最大程度地改变结果,最大程度地改变你模型训练的结果。 另外不同的来源可能有助于不同的基准。所以你应该在调整其他任何东西之前把精力花在这里。所以你应该试试呃哪个任务可能适用于你特定的基准,然后蒸馏,也许在那个特定的数据集上训练,而不是在你能得到的所有数据上训练。
[32:26.320] 他们的一个结论或经验性结论是,你最好混合呃四到八个数据来源的数据集,因为比那更宽可能实际上对你特定的基准没有帮助。嗯我们可能认为仅仅扩大规模就能提高智能体在基准上的表现。但就像嗯经验证据表明了相反的情况。 就像如果你只从相同的分布中采样相同的 rollout,那么随着你增加 rollout 的数量,性能实际上可能会下降或随时间饱和。但另一方面,如果你增加分布的多样性,比如说你通过合成增强添加更多数据,那么嗯准确率可能仍然会随着你添加更多样本而上升。
[33:25.279] 所以你可以从这里的三个嗯图看到,u 蓝色曲线代表数据分布不同时的合成增强,呃而红色曲线代表在同一分布内对 rollout 进行上采样。两条曲线都来自相同的,就像 10k 数据集,区别只在于额外数据来自哪里,比如额外数据的分布是否 与先前的数据相同。嗯所以粉色曲线添加了同一任务的更多运行,它随时间趋于平缓,而蓝色曲线添加了新的任务,它持续攀升。嗯我还想做一个简短的讨论,让我们假设我们有同一个任务的三次运行,你正在决定呃比如用哪些来做训练集。比如说选项 A
[34:22.560] 你有一个短运行,在四轮内解决了问题,选项 B 是一个很长的运行,做了一个错误的编辑,所以测试失败了然后恢复了,选项 C 是一个在 11 次尝试后解决了问题,嗯什么都没改变的运行。你会把哪些保留在你的智能体训练轨迹中? 而对于你保留下来的那些,哪些动作应该承担损失,你的哪个答案会让最小项计算器出错?有什么想法吗?可能是B,因为我们只在两列上训练,这意味着测试的上下文其实并不重要,而且你能够解决这个问题多轮并不像 能够在长轨迹上工作、观察测试并恢复、在更难的配置上获得结果那样有价值,尽管它是 >> 是的,我觉得这说得通,嗯,但我们会认为选择B的潜在缺点可能是什么 >> 更多数据,更多计算机训练,然后模型会得到更小的目标,像是过度做过度目标 就像 >> 是的,呃除此之外,还有一种可能,就是学到错误的编辑,而无法学到恢复能力。所以保留这些数据有两面性,但就像这样,它会提高智能体的错误恢复能力,但它也可能呃给智能体带来错误的行为。 是的。嗯还有别的答案吗? >> 是的,请讲。 >> 呃你的推理是什么?所以像是行为而不是经历那个。 >> 是的。但是呃另一方面,如果比如说是嗯第一次尝试的结果就像一方面像是呃它是从一开始就是正确的行为或正确的结果。但另一方面,如果你遇到像是错误的行为
[37:08.720] 在第一轮时,它可能无法学到如何从中恢复。所以就像嗯我会说最好的答案是结合A和B。嗯然后就像这样,你想要增加或增加数据的数量,使其能够覆盖对你的基准测试最有用的能力。
[37:31.920] 嗯对于下一部分,我将谈论我自己的工作,呃与呃Graham Yubik教授一起。这就像是呃关于为智能体SFT开发一种标准化格式。这篇论文叫做Asian data protocol,它的动机是每个数据集都是构建的,每个呃外面有很多智能体SFT数据,但就像每一个都是由 不同的组、不同的实验室构建的,就像以它自己的格式面向不同的目标受众。嗯比如说对于网页数据集,有些可能用HTML作为呃观察,但其他的可能用可访问性树结构。所以呃它们在格式化和工具调用方面非常不同。 所以比如说,如果我想同时训练呃go browse和mine to web,其中go browse使用可访问性树结构进行网页观察,而mind to web使用HTML来理解网页内容,那么我就很难把两者结合起来、把它们合并在一起,因为嗯它们有不同的格式,要一起训练它们,我们需要写一个 定制化的转换器,从它们各自转换到我指定的智能体格式。所以假设我想训练呃open hands智能体同时使用go browse和mine to web。我需要写一个从go browse到open hands的转换器,然后再写一个从mine to web到open hands的。所以那就像是大量的工程工作。
[39:13.359] 嗯所以在我们的论文中,我们表示像是一个轨迹,我们把一个以非常不同格式存在的轨迹表示成一种标准化且唯一的格式,它能够表示各种不同的智能体任务,我们覆盖了呃像是你读一次每一个,转换成我们的标准化格式,然后把我们的标准化格式转换成你 指定的智能体格式。所以你不需要写定制转换器,或者呃就能够使用多个数据集进行训练。比如说对于我们之前刚谈到的网页数据集。嗯我们在数据结构中同时保留HTML和可访问性树结构,而不是只选一个。但就像你仍然可以选你想要的,并转换成你想要的任何格式,比如 在下游。呃当你执行智能体训练时嗯如果像是使用我们的标准化格式,每个资产只需要把自己转换成我们的标准化格式一次,然后把我们的标准化格式转换成你指定的智能体格式一次。所以这会减少计算开销。
[40:36.240] 而一旦数据处于一种格式中,嗯你仍然需要把它写入你的智能体格式。所以如果我想训练open hands,我需要把智能体呃agent data protocol格式转换成open hands格式,呃那是因为不同的智能体采取不同的动作。所以同一个轨迹对于每个智能体来说看起来会不同。 这也是你设置系统提示并决定当上下文变长时该怎么做的地方。然后你想检查结果,比如那些工具调用是否能解析,每个调用是否都带有一个函数,以及对话是否以它应该的方式结束。
[41:23.680] 嗯这是我们做的一项分析,分析我们是否想要在呃多样化的数据上训练,而不是只在一个特定任务的数据上训练。所以就像之前用开源Asian时,他们检验了嗯在每个任务上训练是否能提高不同基准测试的性能,但就像那只是在一其中一个数据集上训练。所以他们只检验了 在嗯每个任务来源上训练的有效性。嗯但就像这里我们检验了在多个不同来源上训练是否能比在单一来源上训练产生更好的性能。所以我们发现,对于几乎每一个不同的智能体任务、智能体基准测试,在多个不同来源上训练与只在 单一来源上训练相比,会产生更高的性能。所以提高数据的多样性在智能体评估中可能帮助很大。所以我们使用相同的测试框架、相同的模型和相同的评估格式来控制这项研究。只有训练混合通过纳入更多样化的数据集而改变。
[42:38.240] 是的。而在混合数据上训练超过了匹配的单一领域数据集。所以呃即使是在该领域自己的基准测试上也是如此。接下来我将谈论实际运行训练流水线。这些是一些来自实际应用的真实的监督微调配置。比如说对于Neatron 3 ultra,他们把呃294K个呃token打包成序列,批次为呃64,他们的学习率 随时间衰减。对于MEI自蒸馏,他们打包了像128K,并且用非常大的批次和一个学习率嗯也随时间衰减,但就像呃可能不同的是,他们在开始完整SFT之前做了一个预热。而对于最近发布的模型K2 Horizon,他们使用512K嗯序列,以及像三种不同的 面临的问题是,他们还没有发布他们具体的,呃,数字,嗯,关于SFT配置,但就像,嗯,他们说他们的学习率在高量子集上衰减了,>> 这不是参数高效的管道,对吧,这就像在轨迹上进行完整训练,你基本上会怎么做预训练,只不过数据不同,>> 是的,但就像,呃,对于预训练也是如此
[44:07.280] 嗯,我认为人们不会屏蔽掉非智能体部分,在SFT期间也不会屏蔽专家路由。呃,我在这门课中讨论的前沿模型大多是专家混合模型,而公开论文中微调的模型是稠密模型。呃,在狭窄的智能体数据上找到一个专家混合模型,少数专家承担了大部分token,就像这里右侧面板所示的那样。 我们可以看到,嗯,我们在左侧的稠密token最多,随着专家数量的增加,我们使用的token数量就减少了,而MAI在监督阶段就像在平衡路由一样,呃,难度是RL期间的1000倍,他们把drop提高到了15。就像我认为阅读一两篇来自前沿实验室的技术报告或者,嗯,公开论文
[45:21.040] 在你开始做SFT之前是很重要的,因为它们提供了很多有趣的见解,你可以用于SFT,还有,嗯,打包整个对话。所以,嗯,通常我们不想要截断和分割,因为我们想要完整的轨迹,但呃,在现实中,每段对话被分配到那个,就像剩余容量,嗯,最紧密匹配的包中 而且没有对话实际上被分割或截断,这就像是一个刻意对抗幻觉的选择。这个图,就像改编自Neotron的技术报告。相同的问题被排除在同一个包之外,包之后被打乱以获得更好的训练效用。
[46:21.839] 我想在这里涵盖的另一件事是模板漂移,你训练时使用的模板必须与你部署时使用的模板一致,以确保这种对齐,甚至精确到空白字符。所以为了获得你模型的最佳效用,嗯,你把同一条轨迹渲染两次,就像一次在它后面加一条工具消息,一次已解决,这样你可以得到两个不同的字符串。 当这两条消息附加了一个条件思考块时,呃,会改变之前的助手轮次,前缀不再匹配。所以这些就像,呃,为什么它在工具循环中是致命的。嗯,是的,还有一些公开发布的训练产物。例如,K2 Horizon的模型卡片有一个逐阶段的训练表格,就像,呃,对于每个阶段、每个token和序列
[47:24.319] 长度以及每个阶段是为了什么。那个表格就是配置幻灯片上的数字,嗯,来源所在。而且我认为K2 Horizon可能是一个好的,呃,方式,虽然我不认为他们的技术报告已经正式发布了,但就像如果它发布了,我会推荐去看一看,因为就像,嗯,它就像完全开放的,包含了训练所有的,嗯,细节,所有的 训练数据,嗯,甚至包括他们在训练期间产生的损失,通过Weights and Biases,就像Weights and Biases是一个你用来在训练期间监控损失的常见平台。所以你可以看到他们实际的,呃,企业级模型的训练损失曲线,而且它还附带了,就像他们训练每个阶段的中间检查点,你可以
[48:13.920] 继续训练或者,嗯,在他们的预训练检查点上进行后训练。另外Neatron也发布了后训练检查点,就像连同训练数据和他们的训练配方一起。嗯,我会建议至少阅读其中一个,嗯,那可以,就像教给你很多关于企业级训练,呃,SFT的知识,更实际地说,我们想要做的是,我们想要 做的是,就像我们想要知道SFT在我们训练一个模型之后或者在我们训练一个模型之前是否真的有效。所以,呃,在训练和运行你的实验之间可能存在不匹配,呃,在评估基准上,例如,嗯,对于它通过轨迹学到的东西。它可能有相同的前缀,执行一些编辑,然后测试通过了,然后它提交了 任务给一个验证器,但就像在现实中,它可能产生另一个与你训练时不同的编辑,然后测试失败了。嗯,那么现在智能体应该做什么不在SFT数据中。所以这就像非常依赖于智能体的,就像,呃,我们刚讨论过的错误恢复能力。
[49:35.280] 所以训练一次只对一个动作评分。嗯,总是继续录制的运行,而且,嗯,当模型被部署时,没有录制,模型采取的每个动作都会改变它接下来可能看到的东西。这就是为什么训练的损失曲线不是,嗯,就像你可以用它来监控训练是否进展顺利,但你不能用它来替代 实际的评估,因为就像训练继续录制,而部署继续它自己。呃,一,如示例一所示,这里不同的编辑,嗯,而且它处于一个没有任何训练运行曾访问过的状态,在那种情况下,低损失什么也不能保证,但,呃,关于那里会发生什么。所以你需要,嗯,所以你需要同时监控训练损失和评估结果。
[50:39.599] 嗯,说到评估结果,就像在信任任何数字之前,你应该检查你可以,嗯,一个经验法则是,就像你可以检查你可以过拟合20个例子。嗯,如果损失不下降,那么bug可能在上游,然后你可以通过在harness中运行模型并在具有可检查结果的任务上评估,你可以阅读,你应该阅读已发布的 数字时要小心。有时它们是几个harness中最好的,而不是你可以从单个harness上单次运行得到的结果。而且你应该尝试将划分与声明匹配,通过保持一个任务的所有运行在同一个侧,并按仓库划分,如果你想说你泛化到了新的。
[51:32.400] 嗯,是的,跨harness的鲁棒性,就像在一个系统中收集的轨迹会连同任务一起教会系统约定。所以例如,如果我通过一个智能体OpenHands收集轨迹,它可能不会,就像泛化到Claude Code或Codex,而跨几个harness收集是现有的前沿实验室或学术实验室避免训练一个模型 只对,呃,只在一个harness中工作的方式。嗯,如果你想训练,呃,就像在目标领域之外,你应该训练一种能力并迁移其他的,无论你是否,就像你想要测量它。所以如果我想要一个好的软件工程模型,我应该仍然可能训练推理能力,因为这些能力可能会,呃,迁移,而且你的混合是 这里的控制,并用例子来平衡它,嗯,跟用 token 来平衡它是不一样的。这是我们从 MAI 嗯 MAI 技术报告中得到的一个教训。所以也要评估那些你并不打算改进的东西,而不是只在你关心的某一个领域上刷榜。
[52:52.400] 这些是 SFT 的一些要点。现在转向 SFT 能怎样帮助 RL。嗯,是的,所以 RL 提供的——我认为,在我看来,RL 提供最大收益的地方是,嗯,当 SFT 模型是带着 RL 的考量来选择的。嗯,要从整个训练流程来看。我我认为 SFT 是你能够从中获得最多智能体能力、通用智能体能力的地方。但是 对于 RL,你可以通过 RL 获得领域特定的能力或更好的推理。嗯,现有的前沿实验室,比如 Neimotron 或者嗯 K2 Horizon,他们都发现 SFT 是智能体能够获得——模型能够获得最多通用智能体嗯能力的地方。而 RL 是你想要针对特定领域进行训练的地方。 然后嗯,还有嗯,这里表格中的三个关键嗯要点都适用于:完全不用 SFT,几乎完全不用 SFT,几乎不会让基础模型有任何提升。这是我们从 DeepSeek R10 得到的要点,而仅靠 SFT 也远远达不到两者结合所达到的效果。所以嗯,制胜配方是从一个检查点开始 RL,而这个检查点是刻意没有训练到它自身最佳分数的,而是像
[54:23.599] 朝着后面训练,嗯,为了后面更好的 RL。所以我把 SFT 看作是一个先验状态,它可以共同启动智能体能力,这嗯对后续的 RL 很有帮助。好的。嗯,这就是我今天讲座的全部内容,嗯,我欢迎任何问题。[掌声] >> 是的,我们有时间提问。
[54:56.240] >> 是的,>> 我有一个问题,关于你说它来自哪里。如果没有的话会怎样 >> 你能不能简要地把媒体那边的问题重复一下? >> 是的,当然。嗯,问题是,嗯,我们刚才说蒸馏发生在你用一个更好的模型来生成 SFT 轨迹的时候,但如果没有更好的模型会发生什么?嗯,好的。 那么,MAI 在这里选择的是从自身蒸馏。呃,这是解决方案之一,另一个解决方案是,比如,我认为对于前沿实验室,尤其是那些引领嗯训练模型研究的专有实验室,比如 OpenAI、Anthropic,嗯,他们更多地依赖通过 RL 来获得智能体能力,嗯,比如 SFT 主要是在 SFT 主要就像我说的,是一个冷启动,嗯,智能体在其中学习通用能力,但提升嗯没那么大,因为如果你用 SFT,那从根本上就受限于教师模型的能力。所以如果你没有更好的教师模型,那么你可能就需要依赖 RL 来训练。
[56:28.319] 这是我的看法。首先在 >> 嗯他们有大量数据,比如 >> 就用 >> 是的,外面有大量的轨迹 >> 加上强化学习,那会在课程中讲 >> 所以对于 RL,我们不依赖现有的轨迹,而是依赖模型自身的嗯规则 >> 现在还有人用流水线来让智能体适应特定任务吗,比如如果你想 导航某个复杂的服务,你会有一个类似于 SFT 的东西,就是你刚才解释的那种,然后再加上一些小循环来微调它,是那种系统吗 >> 呃,我认为一般来说,尤其是对于专有模型,他们期望你只用一个单一的模型,但可能是经过模型权重合并之后,嗯,但只有单一模型会在推理时被实际推理, 所以在推理时。嗯,我不太确定那些专有模型的情况,但对于嗯最近发布的一个开放模型 K2 Horizon,他们在 SFT 阶段和 RL 阶段之后还做了多轮嗯微调,所以他们做了一些循环的嗯微调,即使在通用训练阶段之后。呃,问题是是否还有低秩适配。嗯,我认为对于
[58:30.000] 那些有充足资源的实验室,嗯,他们更偏好全量微调,但呃,比如我自己之前做过呃低秩适配,为了更好的嗯效率。是的。呃。>> 哦,这里。模型。>> 嗯,问题是我们要不要对错误答案进行监督?呃,我会说也许我们想对错误的中间答案进行监督,但我们仍然想要 最终结果是对的,因为那就像,比如如果你用一个编程智能体来写一些代码,你不希望它是错的。>> 是的。比如,我们是否只监督 >> 哦。哦,好吧,我认为我们仍然需要对错误的编辑进行监督,因为那就像我们如何从过去的错误行为中学习。所以我们既监督 正确的结果,也监督恢复的过程。>> 有没有从错误到正确的?嗯,我认为这通常是通过经验来确定的,比如,正如我提到的,你可能想在一小部分数据上训练,但嗯也许用相同的分布或相同的任务,先在一个较小规模的模型上看看是否有效,嗯,然后再运行任何更大规模的东西或扩大规模。
[01:00:30.160] 嗯,是的,这些大多是通过经验确定的。比如,为什么 MAI 的人选择嗯选择使用 56% 的 STEM 和编程?呃,他们是通过经验确定这可能是嗯他们模型的最佳设置 >> 关于用正确性来确定嗯给一个例子多少权重。 强化学习实际上会在这个层面上做这种事,嗯,强化学习,因为我们会讲到嗯 RL 中梯度是如何设置的,但基本上模型在那个例子上获得的奖励越多,那个例子上梯度的权重就越高,所以如果你把正确性作为一个信号,RL 会 有效地为你重新加权。嗯,它在轨迹层面隐式地做到这一点。所以如果模型嗯犯了一些错误然后从中恢复,它反而会强化再次犯同样的错误。如果你知道它们是错误,并且有一种嗯更聪明的方式来,你知道,监督损失,你也许能做得更好,但那些
[01:01:46.319] 方法设置起来可能有点棘手。所以最常见的做法是嗯就用呃轨迹上的整体奖励,嗯,要么像你说的那样过滤掉轨迹只选择正确的然后做 SFT,要么嗯在强化学习中我们可以嗯依靠嗯算法来有效地做到,我们过几天会看到更多关于这方面的内容, 但是的,对于比如只基于嗯你知道的知识来选择,你知道,这个实例在领域上很适合你的下游测试。那最终会变得更偏向经验性。还有一些人们使用的方法,比如如果你有一个评估集,你可以计算你的模型在那个评估集上的损失,你可以用那个作为某种信号来选择
[01:02:38.880] 你的数据混合。嗯,但这都只是近似的,因为你并不是在那个数据混合上做训练。还有其他问题吗?就跟 >> 呃,你是指训练的目标吗? >> 嗯,这是个很好的问题。你有一个通用的答案吗?
[01:03:27.920] >> 是的。呃,就像你之前说的,你可以把 SFT 看作就像预训练一样,只不过你是在格式化你的数据。 >> 是的。 >> 让它具有某种结构化的标签,把它放进聊天格式里,而且你还在损失上应用了一个掩码。所以你不是在监督模型输出,比如说,工具调用的结果,通常是 >> 但你使用的损失函数是相同的,就像你在预训练中使用的交叉熵损失一样,你只是把它应用到不同的数据上,而且是在 >> 是的,这是你的问题 >> 抱歉,哪种模仿 是的。是的。这就像是带教师强制的模仿学习。所以就像教师强制有 一种固定的上下文,并且在训练时不会从模型中采样。嗯,这就是这些 SFT——大多数这些 SFT 所选择的。是的。是的。是的,我们会,我们会讨论那些,我们会更多地讨论那些让模型生成训练数据的,在接下来几讲中。 >> 是的,好问题。那么,让我们感谢一位
CMU 11-768 Fall 2026 · Lecture 8 · Training 1: Supervised Fine-Tuning (SFT)
- 视频:官方视频
- 字幕:en-orig · auto captions
- 处理:连续字幕合并为自然段;每段保留首条字幕的起始时间戳
- 状态:用户审核通过;中英文按完整语义段合并,每段保留首条原字幕时间戳。
正文
[00:00.320] Hi everyone. I'm really glad to uh give the lecture today. Uh it's on super wise fine tuning for agents. So before I start, I want to ask like how many of you have trained agents before? Like please raise your hand. Okay. Um then how many of you have done SFT on agents? Okay, that's actually more than I expected. Um okay so I will be go over these topics today like uh because this is the first lecture on training of agents. So I will first give a overall introduction of like what is training of agents mean and uh and then go over SFT specific topics and also like transitioning into RL which will be the next three lectures.
[00:49.280] Okay. Um this is a sample trajectory sampled from sui gym. We can see there are different rows in the trajectory. Um so there are a row of system, row of user, row of assistant and observation. Like among these uh we can see there's only three assistant messages in this trajectory.
[01:17.119] Um so in the overall setting the agent receives an issue which is like given by the user and a couple of tools in a sandbox like for example in this case this is bash and a file edit tool. So in the process the agent produces ss and generates commands and uh reads the observations that those commands return at eventually the repository's own tasks like unit test will determine whether
[01:51.200] the run succeeded or not. There are several methods of updating the agent that we already discussed about like first context window. Um there are different pros and cons for different methods like for context window it has high fidelity to what already happened but it is costly noisy and does not decide what matters and for external artifacts it'sable editable retrievable and portable so very easy uh reading by human beings but it must be induced selected and maintained throughout the agent trajectory process. So today we'll be talking about updating model weights. This like could incur faster inferences and broader behavior change but like um because we need to train the model the process of training will be slower and more opaque because it's like blackboxed and model specific because it will incur a model specific behavior after training. So we change the weight like once and every call after that gets a new behavior with nothing extra in the prompt. And the first two things we discussed before like update only in seconds and um these like cost of a training run every time you change it
[03:10.800] and also you cannot open like the weights to see what's going on within a model or move the change to a different model. So this thing like is like onedirectional and opaque like to train a model we need trajectories. Then what does the trajectory teach? First of all how to write a tool call that this harness can actually run. Second like how to keep going when the conversation gets long because the trajectories in learn from like we're supposed to be long and what a tool call result uh what the tool result should look like. So um it waits for one instead of writing one itself and whatever else was in the data that what we use for training like for example extra commentary repeated
[04:02.480] commands and one way of solving a problem. There are several work to give a more broad overview of what training looks like in frontier labs or like maybe in academia as well. The full this is the full training pipeline. Um we first start with pre-training which is the largest amount of tokens but like this is the stage where uh where language models learn basic next token prediction and then mid-training this is like the second stage but like people have very different definitions for what mid training is uh like mid training is like anything that happens between pre-training and post- training some people even counts like SFT as mid-training nowadays and then but typically SFT is considered as like post-training stage. Um so after mid training we went to SFT. Uh we are like uh this will be covered today and then the next stage will be RL where it will be mostly rollouts and rewards which will be covered like later. But notice that like for most of pre-training mini training and SFT we can find fixed corpuses that we can uh train the model on. But for RL there's typically no fixed corpuses. it's like learned on the
[05:17.919] model's own like rollouts. So um I will supervise fine-tuning as a cold start before reinforcement learning. Um like cold start means that this checkpoint RL that begins from today's like uh this represent one of the most common pipeline for post training today. This is like um retrieved from Kim K3's report where the model like after pre-training and mid-training it do supervised finetuning first to initialize a baseline agentic capabilities and the next stage is reinforcement learning where models develop domain experts at varying reasoning efforts. The last stage is on policy distillation which consolidate the expert into like one model. Uh this is the last stage and we will cover the next uh the second and the third topic
[06:13.919] like in later lectures in Kim K3 the model learns to call tools and finish long tasks during SFT u before any RL and uh but some might ask so why don't we just use RL instead of like SFT why do we still do SFT nowadays um So this question deepseek R1 zero has like tried to seek response again like DeepSc R10 run RL directly on the base models. Uh so before any SFT happens and they learned their lesson. So like in their very next model uh they put SFT back in R1 starts from a few thousands created examples of SFT because R10's answers just like mix languages and were hard to read without SFT. Uh I think the SFT stage established a high quality coord policy for the subsequent RL stage.
[07:17.520] This is a comparison of SFT and RL side by side. Um so for supervised fine-tuning it learns from recorded trajectories of agents whoever produced them like it can be produced by stronger models or even themselves and SF learn the signal of a target for every token and it needs data and GPUs to train a model with. It's good at learning tool calls formats and long tasks but is stuck with the demonstrations that are wrong or narrow or used up. For reinforcement learning, it's like learn from its own rollouts scored by a reward at the design for its domain. And uh it learns the signal of one number per roll out per per long roll out. And it needs a live environment, sandboxes and checkable rewards which are some like uh some interesting facts for our people to
[08:12.560] solve. And uh is good at sharpening what the model already attempts to. uh but the start is two weeks so the reward might sometimes not be like checked um in our lectures we'll be covering reinforcement learning in the next three lectures and I think next topic I'll be going through is like the trajectories as token sequences we all know from language models that there might be chat template that is different for each different language models So for each token sequence um like every boundary above is a template token and uh let's use the coin example queen template example here where um it has some special tokens like uh IM start and then like have special tokens like to call etc and we feed this trajectories through
[09:15.839] the template and you get one string string of token that represent the whole trajectory. Like for example, it might be going through multiple tool calls like 10 to calls and reaching a final answer, submitting a final trajectory to like some verifier. But like all of these will be represented as one string that will be fit into the training infrastructure to train a model. And the special tokens markers are single special tokens. The rest is what the agent could actually uh write and see in their trajectory that they output. And um different from a language model for agentic SFT, we might not train all of like the output the all of the trajectory. Like for example, here in the trajectory I represented, the gray boxes are the ones that we consider as context. So these will not be in uh calculated towards loss. Uh but the red boxed ones are where we calculate loss like cross entropy loss is what we usually use. But like we have apply a special mask there to assistant tokens that we only train on assistant tokens but not non-assistant tokens which are considered contexts.
[10:35.920] So the mask is like one bit per token saying whether the token's prediction like counts towards the final training and we want to minimize it because like minimizing it raises the probability of each recorded action like given everything before it has context. This will help increase uh the agent's capability of mimicking what is like already in the trajectory and um observations here are conditioning context but not prediction targets. Yeah. >> Why don't we try to also train with train like observation? It could result. >> Yeah. The question is like why don't we train models also on observations. Uh I would say like training on assistant messages are the general rule of thumb like in agentic SFT because like observations or user context might be given from the user or the environment instead of like given from predicted by the assistant but like I agree that like there are papers um existing that are training on observations and find it to be useful for predicting the world uh stage uh state. So like what I'm discussing here is like uh the general recipe but like you might do ablation studies or even innovations to see what might be the best option for your scenario. >> Just a quick followup about that there one particular paper that did that is called the computational world model that meta published. So they're training the model to like output the results of tool calls which are executing code. Um So they find that it's like pretty helpful on tasks that are aligned with
[12:23.360] like understanding execution semantics but like it also you could imagine it like takes up some kind of some of the capacity of the model. So yeah like definitely if your model is too small it might hurt its performance on other things. It's kind of like an open question of could it how does it impact kind of the base agentive abilities of the model but it could be like super
[12:45.200] useful for like world modeling or planning. So it's a little bit more experimental like >> yeah um to explain more on how the mask is built like first we have a render that the template writes the conversation as like one whole string and knows where each assistant span starts and ends with um some special tokens and then like uh tokenize those character positions become like token positions and then these uh we mask like the tokens inside an assistant as one and everything else get the zero because it's not trained on. And the template does the bookkeeping here because like where a system tax starts where it stops like uh by the time training starts the rows uh of system message of users of uh observations are gone. The trainer sees
[13:41.760] the string and the bits and nothing else. These are some masking controls across some like SFT frameworks. I really like one specific example is like llama factory where you can specify whether you want to train on the prompt as well, whether you want to mask a history uh when calculating loss. Um, so I can show an example of like real usage of llama factory. Um, this is one of the packages I really like to use when doing agentic SFT. You can like generate roll outs or synthetic trajectories and fit into like this infrastructure and identify your model that you want to train with and then just start training. Um you can specify arguments like uh whether you want to train the whole trajectory or you want to cut it off at
[14:53.839] some position or like we want to train on the context as well. So um there's another special token like the end of turn token where it's like represented differently for different models like for example for queen models it's represented as um this the uh the red token here of I am end and um for other models uh like like for example um probably Kimi Llama like they all have very different end of token end of turn tokens. So if you want to train a model you might need to take a look at what their chat template is so you don't like confuse the model during post training. And uh one thing I observed like when setting up training trajectories are people often times mixed up uh what to include in um calculating loss and what are like
[16:08.079] context only. Uh for example, the row header of assistant here is now trained for the agent to produce to generate and the model only produces um the red boxes here instead of the gray box and at last the model produces the stop token. So it has to be also be trained inside within the mask and also end of turn um is like actually a combination of multiple events instead of only one because end of turn means that you need to end this message you need to end this tool and also you need to finish the task. Um so like for example in Kimi K3's template here down here um it actually spells them apart with separate tokens for better for the model to better learn how to stop.
[17:02.800] Another thing that um might be masked out during a training process is reasoning blocks. As we know reasoning blocks are very long blocks and um sometimes people only train on like non-re reasoning parts. So only the like actual tool calls or actual um trajectories.
[17:24.880] So there are typical several switches that you can try to experiment with. Does the reasoning stays in the context or do they stay in the system message? Uh if they say in the assistant message whether should it be trained on um in the context reason explain the actions that follows the context uh follows the context and if it is trained on it becomes a style that the model itself writes. For example, Neimotron like Neimotra 3 ultra keeps like budget truncated reasoning in the context and mask the artificial cut off of the loss. These are like model specific decisions that are very different across different labs or different frontier model development.
[18:13.679] Um >> yeah sure >> what's the difference between >> uh are you asking for why like reasoning our mask sometimes? Yeah. Uh I'm actually going over this right now like because reason introjectories could be very very long compared to the actual tool call or the actual task completion stage which might confuse the model to produce like only long reasoning but like mixed up with the final outcome. So uh either reasoning sometimes reasoning is mascal sometimes reasoning is train and there is this paper last year of balanced SFT which it aims to balance like reasoning trajectories alongside with the um execution or tool call trajectory. So uh models can learn both.
[19:06.880] Um so long if we have very long reasoning trajectories it could dominate the loss and the short like tool call or test completion is what we actually most need for like correctness. For example for a task of software engineering we might want the model to reason on what's its next step is but we don't want the model to not actually perform the next step. So um reasoning and action or execution are both important. So in this paper of balance SFT they introduce like rebalancing of uh reasoning and um tool call which like helps more on multi-turn like execution of agents and uh this is a table from MAI so Microsoft MAI model MAI thinking model um they have very different samples across different capability they're aiming to train. Like for example, they
[20:13.120] categorize STEM capability and coding as like one category and general agency capability as another and general helpfulness and safety as like another. Uh from their sample weight you can see like uh how different they are considering each agent capability uh capabilities importance. Um but like the results are like the token ways are very different from sample ways because different capabilities might incur different lengths of SF trajectories where STEM and coding trajectories are typically very long compared to uh maybe general helpfulness and safety where um in the MAI paper they tend to uh balance the mixture or chose the mixture by counting the number of samples but like stem and coding traces like are so long. So they nearly take all the tokens they're training on and whatever you balance by counting trajectories you should also so this answer is like you should also check whether it's also balanced in tokens or like whether the distribution in tokens is what you preferred. Um also these are some open problems in supervising trajectories where the first one is that we could not find a
[21:35.039] published experiment that changes the mask for agent SFT and measures what happens but as uh Benu said there's like a recent paper on uh computation world model that like also trained on observations as well. So I think this is one open question that's like worth uh experimenting on and also the careful masking experiments are mostly single term recently uh on instruction tuning instead of on agents. Um and the one case where unmasking the prompt helped like had long prompts and short answers and little um data like in the paper below I attached like agent trajectories are the opposite on all three. So I still think this is an open question that's like worth discussing.
[22:25.600] Um also choosing the trajectories are important for agent SFT as well. Um so this is a table of like where typical trajectories come from for agent take SFT on the left column like who run the task like where to collect agent SFT trajectory from either is from a strong model that could run it or from the model that like you are training on like uh for example for MAI they use its own reasoning runs or trajectories for SFT and Um yeah, so like depending on the roles um whether you keep everything you got from the uh the generated trajectory or you keep the runs that only passed some threshold or verifier. So if you keep everything that you generated from a strong model that's like called plain distillation. If you keep the runs that pass some sort of verifiers like most of the SFT falls into this category for example Swimmatron by Nvidia and opensource agent all falls into this category. Um and then like if you keep everything that you generated from the model yourself then like this an example is MAI but they felt like little difference um using like FFTD and uh if you keep the runs that pass the
[23:53.200] verifier with the model of itself then this is like called expert iteration which should be like teased later. So most agent SFD data sits in the like top right box of like keeping only a subset of filter data generated with a strong model and because the stronger model like writes better trajectories than the student could and the tasks own like task might be good as keeping uh only the good trajectories and uh in the bottom row the model makes its own training data. So repeat the loop like you get S for iteration which will be covered later. This is some results from sweet gym which falls into the upper right like you keep the runs that passed with a stronger model that runs it category. uh where it founds that like it achieves
[24:50.720] much higher resolution rate after um distilling from models like GT40 and Cloud while only keeping the trajectories that pass um the repository tasks. And um in terms of trajectory count and saturation, scaling the number of training trajectories might um result in an increase in the model performance, but like it might saturate at some point and it's much easier for as uh for saturation if you are using a smaller model for training. So in the plot here like the authors of SGM uses the same task set throughout of software engineering and only the number of sample data like changes. So increased through time and accuracy was still rising when the sampling budget run out and they had task to spare where like what they run out of was compute to sample more uh more runs and every point in this plot uses the same task like what happens when you add new task uh I will be discussing like later um but like not all agentic trajectory might be like helpful. There are some desirable behaviors even in successful rollouts. Uh for successful we mean that the final eventual uh task was successfully completed. But there are
[26:27.840] something intermediate that we might not be able to measure like this might be teaching undesirable behaviors if we use this directly as SFT data. So let's assume that every run passed its like unit test or like the repositories tests. But look at how like for example one edited and uh the red box there like one edited and rerun test in a loop for most of its turns and another left print statements in the final batch. So although the final answer is wrong but this all might still teach the model some wrong behaviors um that it should not learn from SFT and the loss copies like every action no matter like whether it's good or not. So train on these runs and the model learns the best together with the eventual fix.
[27:26.159] There are like some filters for uh what counts as good SFT data like one very simple example is like filtering real by uh by length like length of the trajectory u for example in a trajectory with uh less than let's say five turns it's probably likely that it's not very good trajectories because like if it's short there are like different Um you you can say different things about like short trajectories but either is like short because it solve the task correctly like at one try and then it's difficult for agent to learn error recovery abilities uh or it might just like the trajectory is incorrect itself. So it's like being very short or the agent encounter like some errors when generating the trajectory which makes us very short. So like filtering by trajectory lens is one of the useful approaches that people do today. Um this is like uh this ablation study is performed by the paper open s agent. So uh one of their conclusion is that the best filter here is just count the number of turns of agents and drops every run that's shorter than five.
[28:47.840] And turn count says like how much work a run took uh but not whether the work was any was good. And uh um in this case a four turn clean fix might got dropped um and a 14 turn like messy trajectory might still get kept but on average it still helped. Another thing that's important for SFT is like whether you can choose the best teacher like the model itself being the best model does not mean that that it could be a best teacher. Um for the open source agent paper they evaluated several agents on like several benchmarks and they found that the best agent evaluated at that time which was GPD 5.3 codeex is actually the worst teacher among all of these uh models. You can see that it resulted in like less gain compared to other models or even like some decrease in performances after distilling from 255.3 codeex. So if you want to distill from a stronger model or if you want to generate trajectories from a teacher model uh you might need to choose your teacher model wisely.
[30:05.279] >> Yeah. >> Teach on calls mainly how Is this possible? So the codeex produces correct tool calls but they are the least efficient for training other models. Is there like a reasoning behind it? How could that be? Um there might be various reasons behind it like for example um GPT 5.3 codeex is good at training good at um solving the task itself but like the generated trajectory might not work for the student model that much like because the thinking mode might be very different or the methods of solving the task might be very different. Um so yeah that's one of the explanations. Um also like leaderboard rank only told like whether um whether the models to run things here and you should like try more
[31:04.399] teachers and keeping the better student that is cheap like by comparison also for SFT ch like wisely choosing the task sources are important. There are just so many asset data assets out there. Um but not probably not all of them could work for the task you are looking to train on. Um still done by open source agent but like they perform some abolition studies on how much like each different task source of agent trajectories could contribute to different benchmarks. Like for example, Sweet Smith contributed the most to Sweetbench verified but it contribute less to terminal bench. And the choice of task source could change the results the most like change the results of training your model the most. Also different sources might help different benchmarks. So like you should spend your effort here before tuning anything else. So you should try uh which task might works out for your for your particular benchmark and then distill like maybe train on that specific data set instead of training on all you can get.
[32:26.320] And one of their conclusion or empirical conclusion is that it's best for you to mix like four to eight sources of data sets and because going wider than that might not actually help you for your particular benchmark. Um we might think that scaling up just simply could improve agentic performances on benchmarks. But like um empirical evidence shows the contrary. Like if you only set up sample the same rowouts from the same distribution then performance at might actually drop or be saturated over time as you increase the number of rowouts. But on the other hand, if like you increase the distribution, for example, you add more data through synthetic augmentation, then like um the accuracy might still go up as you add more examples.
[33:25.279] So as you can see from the three um plots here u both the blue curve represents synthetic augmentation uh with different distribution of the data where the red curve represent up sampling rollouts within the same distribution. Both curves are from the same like 10k data set and differ only in where the extra data come from like whether the extra data distribution is the same with the prior data. Um so the the pink curve adds more runs of the same task and it flattens out through time and the blue curves add new task but it keeps climbing. Um I also want to have a short discussion like let's assume that we have three runs of the same task and you are deciding uh like which ones to use for the training set. Let's say option A
[34:22.560] where you have a short run that solved the issue in four turns and option B a long long run that made a wrong edit and so this test failed and recovered and option C a run that solved the issue after 11 attempts um that changed nothing. Which of these would you keep in your agent training trajectories? And for the ones that you keep, which actions um should carry the loss and which of your answer would the minimum term computer gets wrong? Any any thoughts? probably the B because we're training only on two columns which means that the context of the test doesn't really matter and the fact that you will able to solve the problem for turns is not as valuable as being able to work on long trajectories observing the tests and recovering and getting the results on like a harder configuration even though it's >> yeah I I think that makes sense um but what we would think could be the potential disadvantage of choosing B >> more data, more computer training and then the model gets smaller goals and like excessive doing excessive goals like >> yeah uh other than that there's also a probability of just learning the wrong edit and not being able to learn the recovery ability. So like there's both sides of keeping this data but like so it will improve the agents like error recovery ability but it might also uh incur wrong behaviors for agents. Yeah. Um any any other answers? >> Yeah, go ahead. >> Uh and what's your reasoning? So like behavior rather than going through that. >> Yeah. But uh on the other hand if like for example it's um first try results in like on the one hand like uh it's a right behavior or the right results from the start. But like on the other hand, if you encounter like a wrong behavior
[37:08.720] at the first turn, it might not be able to learn like how to recover from it. So like um I would say the best answer is to incorporate both A and B. Um and like so you want to increase or increase number of data that like could cover the most useful abilities towards your benchmark.
[37:31.920] Um for the next section here I will talk about my own work uh with um with professor Graham Yubik. This is like uh on developing a standardized format for agentic SFT. The paper is called Asian data protocol where like this is motivated by each data set was built each uh there are a lot of agentic SFT data out there but like each of them was built by a different group a different lab like in its own format towards a different target audience. Um like for example for web data sets some might be used HTML for uh observations but my others might be using accessibility tree structures. So uh they are very different in terms of the formatting and tool calls. So for example, if I want to train on both um go browse and mine to web where go browse uses accessibility tree structures for web observations and mind to web uses HTMLs for understanding the web content then it will be difficult for me to incorporate both of them to combine them together because um they have different format and to train on them together we need to write a customized converter from each of them to my specified agent format. So let's say I want to train um open hands agent on both go browse and mine to web. I need to write a converter from go browse to open hands and then mine to web to open hands. So that's like a lot of engineering work.
[39:13.359] Um so in our paper we represent like a trae we represent a trajectory that was like in very different formats into a standardized and a unique format that could represent like a variety of different agentic tasks where we covered uh like like where you read like each once and convert it to our standardized format and then convert our standardized format to your specified agentic format. So you don't need to write custom converters or uh to be able to use multiple data sets for training. Like for example for the web data sets we just talked about before. Um we keep the HTML and accessibility tree structures in our data structures instead of only picking one. But like you can still pick what you want and convert to whatever format you want like downstream. Uh when you perform agentic training um if like using our standardized format each assets only need to convert itself into our standardized format for once and then convert our standardized format once to your specified agentic format once. So this will reduce the computational overhead.
[40:36.240] And once the data is in one format um you still need to write it to your agent format. So if I want to train open hands I need to convert a agent uh agent data protocol format into open hands format and uh that's because like different agent takes different actions. So the same trajectory comes out to look different uh for each agent. This is also where you set the system prompt and decide what to do when the context get long. And then you want to check the result like do those who call pars and does each call come from a come with a s a function s and does the conversation end the way it should be.
[41:23.680] Um this is a analysis that we did analyzing whether we want to train on um diverse data instead of only one task specific data. So like previously with open source Asian that they disc they examined whether um training on each task could improve the performance on different benchmarks but like that's only training one on one of the data sets. So they only examine the effectiveness of training on um each task source. Um but like here we examined whether training on multiple different sources could result in better performance than training on a single source. So we discovered that for almost every different agent task agent benchmark training on multiple different sources will result in higher performance compared to only training on a single source. So improving the diversity of data could help a lot in agent take evaluation. So we control this study using the same harness, the same model and the same evaluation format. Only the training mixture change by incorporating a more diverse set of data.
[42:38.240] Yeah. And training on the mixture beat the matching single domain set. So uh even on the domain's own benchmark and next I'll be talking about actually running the training pipeline. These are some real supervised fine tuning configurations from the wild. Like for example for Neatron 3 ultra they packed uh 294 case u tokens into sequences with uh 64 batch and their learning rate decays like throughout time. for MEI self distillation they pack like 128K and with like a very large batch and a learning rate um that also decays through time but like uh was what might different that is that like they did a warm-up before starting the full SFT and for a recently released model K2 Horizon they were using 512K um sequences with like three different faces is they haven't released their specific uh numbers yet um for SFD configurations but like um they said that like their learning rate decayed on like high quality subsets >> this is not parameter efficient piping right this is just like full training on trajectory how would you do pre-training essentially except the data is different >> yes but like uh also for pre-training
[44:07.280] um I think people don't mask out like non-agentic parts also expert routing during SFT. Uh the frontier models that I discussed in this lectures are mostly mix of experts where the models that the public papers fine-tune are dense. uh find a mixture of expert model on narrow agent data and a few experts take most of the tokens like uh what is shown in the right panel here. We can see that um we have the most tokens dense like at the left and as the number of expert increase the token uh the number of token we use like decreases and MAI is like balancing the routing uh hard during the supervised stage and a thousand times harder than during RL and they raise drop to 15 Like I think reading one or two technical reports from Frontier Labs or um public papers
[45:21.040] are important before you start doing SFD because they provide a lot of interesting insights that you could use for SFT and um packing whole conversations. So um typically we don't want truncations and splitting because we want the full trajectory but uh in reality like each conversation is assigned to the pack whose like remaining capacity uh it most tightly fits and no conversation is actually splitted or truncated which is like a deliberate choice against hallucination. And this this plot is like adapted from Neotron's technical report. And identical problems are kept out of the same pack and packs are shuffled afterwards for better training utility.
[46:21.839] Another thing I want to cover here is template drift where the template you train with has to be the template that you serve with to ensure this alignment even down to the whites space. So to have the best utility of your model and um you you render the same trajectory twice like once with a tool message after it and once resolved so you can get two different strings. When the two messages appended a conditional thinking block uh changes the earlier assistant turn and the prefix no longer matches. So these are uh like why it is fatal inside a tuloop. Um yeah and also there are some publicly released training artifacts. For example, K2 Horizon's model card has a stage bystage training table like uh for each stem, each token and sequence
[47:24.319] length and what each phase like was for. That table is where the number on the configuration slide like came from and um I think K2 Horizon might be a good uh way although I don't think their technical report is officially released yet but like if it's released I would recommend taking a look because like it's um it's like fully open containing all the um details of training all the data of training um even the loss like they incurred during training through weights and biases like ways and biases is a common platform you use to monitor loss during training. So you can look at their actual uh enterprise level models training loss curves and it also ships like intermediate checkpoints at each stage of their training that you can
[48:13.920] continue training or um post train on their pre-training checkpoints. Also Neatron released the post training checkpoints too like together with the training data and their training recipe. Um I would suggest like reading at least one of these um that could like teach you a lot on enterprise level training uh SFT more practically we want to do we want to do is like we want to know whether actually SFT worked after we train a model or before we train a model. So uh there might be mismatches between training and running uh your experiments on on evaluation benchmarks for example um for what it has learned through like trajectories. It might have the same prefix and perform some edits and then the test passed and then it submits the job to a verifier but like in reality it might incur another different edit than the edit that you are trained on and then the test failed. Um and then now what the agent should do is not in SFD data. So this is like very depend on the agents like uh error recovery ability that we just discussed.
[49:35.280] So training grades only one action at a time. Um always continuing the recorded run and also um when models are deployed there is no recording and every action the model takes change what it might see next. This is the reason why the loss curve of training is not um like you can use it to monitor whether training is going well but you cannot use it to substitute actual evaluation because like training continues the recording and deployment continues itself. Uh one so as shown in example one different addit here um and it is in in a state no training run ever have visited in that case the low loss promise nothing but uh about what happens there. So you need to um so you need to monitor both training loss and evaluation results.
[50:39.599] Um speaking of evaluation results like before trusting any number you should check that you can um and a rule of thumb is like you can check that you can over fit 20 examples. Um if the loss will not drop then the bug might be like upstream and then you can evaluate by running the model in a harness and on task with check checkable outcomes and you can read you should read publish numbers carefully. Sometimes they are the best of like several harnesses instead of like the results you can get from a single run on single harness. And you should try to match the split to the claim by keeping all runs of one task on the same site and split by repository if you want to say it generalizes to new ones.
[51:32.400] Um yeah robustness across harnesses like trajectories collected in one system teach systems conventions along with the task. So for example, if I collect trajectories through a agent open hands that it might not like generalize to cloud code or codeex and collecting across like several harnesses is how existing frontier labs or academia labs avoid training a model that only works for uh works in one harness. Um if you want to train uh like outside the target domain, you should train on one capability and move the others whether or not like you want to measure it. So if I want to have a good software engineering model, I should still probably train reasoning capabilities because these capabilities might uh transfer and also your mixture is the control here and balancing it by examples uh is not bal is not the same as balancing it by tokens. This is a lesson we learned from the MAI um MAI technical report. So evaluate the things you were not trying to improve too instead of like benchmaxing on only one domain that you care about.
[52:52.400] These are some takeaways from SFT. And now moving towards like what SFT could do to help RL. Um yeah so RL provide I think in my opinion RL provides the most gains where uh when the SFT model is selected with RL in mind. um to view the whole training pipeline. I I think SFT is where you can gain the most agent cap general agentic capabilities from. But for RL, you can gain domain specific or better reasoning through RL. um existing frontier labs like Neimotron or um K2 Horizon, they they all find that SFT is where agents could gain models could gain the most general agent uh capabilities. Well, RL is um where you want to train for a specific domain. And um like also um the three key uh takeaways from the table here are all applied to no SFT at all barely at all barely moves the base model. This is a takeaway that we get from deepseek R10 and SFT alone stops well short of what the pair reaches. So um the winning recipe starts RL from a checkpoint that was deliberately not trained to its own best score but like
[54:23.599] train towards like later uh for better later RL. So I view SFT as a prior state that could co-start agency capabilities that is like well served for later RL. Okay. Um that's all my lecture today and um I welcome any questions. [applause] >> Yeah, we have time for questions.
[54:56.240] >> Yeah, >> I have a question regarding where you said that it comes from. What if there's no >> Would you be able to just briefly repeat the questions from the media? >> Yeah, sure. Um, the question was, uh, we were saying distillation happens when you use a better model to generate trajectories of SFT, but what will happen if there's no better models? Um, okay. So what so what MAI chose here is to distill from itself. uh this is one of the solutions and the other solution is like for example for I think for frontiers lab especially proprietary ones that like lead the um research of uh training training models like for example open AI anthropic um they rely more on gaining agentic capabilities through RL um like SFD is mostly on SFD is mostly like I say a cold start that like where the agents learn general abilities but not improving um that much because like if you are using SFT then it's fundamentally limited by the teacher model's capability. So if you don't have better teacher model then you might need to rely on RL for training.
[56:28.319] That's that's my take. in the first place before >> um they have a lot of data like >> just use >> yeah there's like a huge amount of trajectories out there >> plus reinforcement learning which will lectures >> so for RL we don't rely on existing trajectories but we rely on the model's own like rules >> does anyone use pipeline anymore to adapt agents to specific tasks like if you want to navigate some complicated service you would have like SFP in a sense you explained now and then some smaller cycle to like fine tune it is that system >> uh I think in general especially for the prop priority model they expect you to use like only like one single model but might be after model XO merging uh but like only one single model will be actually be inferenced at like inference time so I'm not sure about like the proprietary ones but for um a recently released open model K2 horizon they did multiple rounds of um like fine-tuning even after an SFT stage and the RL stage so they did like some recurrent um fine-tuning Even after the general training stage, Uh the question is like whether there is still low rank adaptations. Um I think for
[58:30.000] labs that with sufficient resources um they prefer full fine-tuning but uh like for myself I've done uh low rank adaptation before for better like efficiency. Yeah. Uh. >> Oh, here. model. >> Um is the question do we want to supervise it on wrong answers? Uh I would say maybe we want to supervise it on wrong intermediate answers but we want we still want the eventual results to be right because that's like like for example if you are using a coding agent to write some code you don't want it to be wrong. >> Yeah. For example like do we only like supervise on the >> Oh. Oh well, I think we still need to supervise on the like wrong edit because that's like how we learn from the past wrong behaviors. So um we supervise both the right results and the recovery process. >> Is there a wrong to the right? Um I think it's typically determined empirically like for example as I mentioned you might want to train on a smaller portion of the data but like maybe with the same distribution or the same task and see whether it works on a smaller scale model um before running anything like larger scale or scale up.
[01:00:30.160] Um yeah, most of these are determined empirically. Like for example, why does the MAI people choose to um choose to use 56% of STEM and coding? Uh like they determined empirically this might be the best setting for um their model >> for using like correctness to determine um how much to weight an example. Reinforcement learning actually will do this sort of at the level and um the reinforcement learning because like we'll we'll talk about like the way the gradient is set up in the RL but like basically the more reward the model gets on that example the higher the weight of the gradient on that example will be so if you have like correctness as a signal RL will effectively rewe it from you for you um it does that implicitly at the trajectory level So if the model is like making some mistakes and then recovering from them, it's going to reinforce making those same mistakes again. If you know that they're mistakes and have kind of like a smarter way of, you know, supervising the loss, you might be able to do better, but those sorts of
[01:01:46.319] approaches can be a little bit tricky to set up. So the most common thing is to um just uh use the overall reward on the trajectory um and either like filter the trajectories out to just choose the correct ones and do SFT like you said or um in reinforcement learning we can um rely on um the algorithm to effectively and we'll see more about that in a few days but yeah for like choosing kind of just based on like you know the knowledge of you know, this instance is a good fit domainwise for your downstream test. That ends up being a lot more empirical. And there's methods people use to kind of like if you have an evaluation set and you can like compute the loss of your model on that evaluation set, you can use that as some signal to select
[01:02:38.880] your data mixture. Um, but it's all just sort of approximate because you're not doing training on that data mixture. Any other questions? It's just the same as >> uh do you mean the objective of training? >> Um that's a great question. Do you have a general answer for that?
[01:03:27.920] >> Yeah. Uh like you had said before, you can think of SFT as being just like pre-training except you are you're formatting your data. >> Yeah. >> To you know have kind of like the structured tags that put it into a chat format and you're also applying a mask to the loss. So you're not supervising the model to output like you know the results of tool calls typically >> but the loss function that you're using is the same like cross entropy loss that you're using in pre-training you're just applying it to different data and on um just a subset of the >> yeah that's your question >> sorry what kind of imitation Yeah. Yeah. This is like this is imitation learning with with teacher forcing. So like teacher forcing has kind of a fixed context and doesn't uh sample from the model at training time. Um that's what these SF most of these SF are choosing. Yeah. Yeah. Yeah, we'll we'll talk about ones that s we'll talk more about ones that have the model produce the training data in the next few lectures. >> Yeah, good questions. Well, let's thank you a
