读见

蜂群、鸟群、帮派:如何构建、训练和评估多智能体系统

原题Swarms, Flocks, Gangs: How to build, train, and evaluate multi-agent systems

约 12 分钟约 2,787 字原文英文已翻译

读前速览

4 个要点

本文探讨构建、训练和评估多智能体系统(蜂群)的挑战与方法,并通过实验表明,与智能体各自独立尝试相比,智能体之间的协同沟通能够显著提升任务表现,同时减少 token 用量。

  1. 与智能体各自独立尝试相比,多智能体沟通能够提升表现并减少 token 用量

    实验表明,经过训练的蜂群成功率达到 82.3%,每个团队的 token 用量比进行 8 次重试的未训练团队少 37.4%,说明协同确实能带来价值,而不仅仅是增加尝试次数。

  2. 通过团队奖励和 GRPO 优化训练由 4 个智能体共享的 Qwen3-0.6B 策略

    训练设置使用一个共享语言模型,并为每个智能体设置独立上下文;团队奖励机制会惩罚 token 用量,同时采用 TRL 的 GRPO 算法,在 75 个训练步骤中,每次更新进行 256 次团队尝试。

  3. 训练过程中沟通模式不断演变,广播变得更加频繁,token 使用效率也有所提升

    随着训练推进,每个团队的广播次数从 2.38 次增加到 5.73 次,而每个团队生成的 token 总数则从 12,827 降至 8,026,表明智能体学会了更有效地沟通。

  4. 文章提供了使用 Hugging Face Jobs 基础设施训练蜂群的实践指南

    读者可以使用 Hugging Face Jobs 上 TRL 的 async_grpo_s3_aggregation 示例复现实验。该实验需要两块 H200 GPU(一块用于服务,另一块用于训练),并按照文中提供的 shell 脚本进行实现。

完整摘要

本文介绍智能体蜂群的概念,并探讨多智能体沟通是否真的能提升表现,还是只会消耗更多算力。文章展示了一项实践实验:4 个智能体共享一个 Qwen3-0.6B 策略,解决信息聚合任务。每个智能体会收到一个私有数字,并需要通过沟通生成一份排序正确的列表。训练采用团队奖励机制,同时激励成功完成任务和提高效率;每道题尝试 8 次,并根据 token 用量进行惩罚。结果显示,经过 75 个训练步骤,成功率从 6.6% 提升至 82.3%,每个团队的 token 用量则减少了 37.4%,这表明智能体学会了更有效地沟通。文章还详细介绍了如何使用 Hugging Face Jobs 上的 TRL 训练自己的蜂群,并强调了基础设施需求和实际实现步骤。

目录 · 13 节

查看原文
Part 1: Understanding swarms and training to communicate

这是一个面向怀抱理想的读者的理想主义博客系列。我们(我们自己、社区,以及你)都想构建自己的智能体群,因为它们令人兴奋、充满挑战,而且能力日益增强。此外,如果我们能训练自己的智能体协同工作,那就酷极了。要是这些智能体群还能使用小型且价格实惠的模型,那就更是酷上加酷。This is an aspirational blog series for aspirational readers. We (us, the community, you) want to build our own agent swarms because they're exciting, challenging, and increasingly capable. Furthermore, it would be extremely cool if we could train our own agents to work in a swarm. And exponentially cool if those swarms used small and affordable models.

这就是我们将在本系列中要做的事!每篇博客文章都会介绍一些构建智能体群的背景知识,并附上可在 Hugging Face Jobs 上运行的可复现实例。本篇文章中的示例在两张 H200 上大约需要五个小时,费用约为 50 美元。That's what we'll do in this series! Each blog post will include some context on building swarms, alongside reproducible examples that you can run on Hugging Face Jobs. This post's example takes about five hours on two H200s, which costs roughly $50.

多智能体系统很难顺利运行。增加智能体会改变智能体的上下文和预算,进而增加整个系统的复杂性。通过通信将多个智能体的上下文混合起来,并不会天然带来相较于单个智能体的性能提升。模型需要通过训练学会如何交流。我们该如何定义模型应该学习什么?在处理多个智能体时,又该更新哪些模型权重?Multi-Agent Systems are hard to get working. Adding agents changes the agent's context, the budget, and in turn the complexity of the whole system. Mixing agents' context together through communication is not an inherent performance boost compared to a single agent. The model needs to learn to communicate through training. How do we define what models should learn? And which model weights do we update when we're dealing with many agents?

在本系列中,我们将通过教学概览和实践示例来逐步解决这些问题。每篇文章都会提出一个问题,构建一个小型系统,并探究会发生什么,以及你如何复现它。第一个示例从四个智能体、一个共享语言模型和一项简单且可验证的任务开始。它非常适合亲自动手实现、尝试调整并从中学习。In this series, we'll work through those problems with an educational overview and practical examples. Each post will introduce a question, build a small system, and examine what happens and how you can reproduce it. This first example starts with four agents, a shared language model, and a simple verifiable task. It's ideal to implement yourself, to tinker with, and to learn from.

首先,我们先介绍一些关于集群的背景知识。First, we'll start off with some context on swarms.

蜂群的崛起 The rise of the swarms

过去几个月里,我们听到了许多关于智能体群的消息,有好有坏。今年 9 月,OpenAI 宣布,一支约有 1 万个智能体的群体在三维纳维–斯托克斯方程中发现了一个奇点;该方程是千禧年大奖难题之一。此前,一支规模较小、近百个智能体组成的群体,已经推翻了无外力三维欧拉方程的正则性猜想;这是一个与之密切相关的未解问题。We've heard a lot about swarms over the past few months, both good and bad. In September this year, OpenAI announced that a swarm of roughly 10,000 agents had found a singularity in the 3D Navier-Stokes equations, one of the Millennium Prize problems. A smaller group of nearly 100 agents had already disproved regularity for the unforced 3D Euler equations, a closely related open problem.

但智能体群体并不总是会处理我们希望它们处理的问题。METR 对 Hugging Face 事件的调查发现,约 1,200 个本应彼此隔离的 OpenAI 智能体,设法交换了超过 70,000 条消息和文件。约 700 个智能体在寻找蒙骗其基准测试评分器的方法时,加入了对 Hugging Face 的攻击,而它们并未收到这样做的指令。But swarms don't always work on the problems we want them to. METR's investigation of the Hugging Face incident found that around 1,200 OpenAI agents, which were supposed to be isolated, had found a way to exchange over 70,000 messages and files. Around 700 joined an attack on Hugging Face while looking for ways to fool their benchmark's scorer, without instruction to do so.

大规模智能体群体本身并不能证明协作发挥了作用。Noam Brown 估计,Navier–Stokes 结果中不到 10% 来自多智能体协作。在 Anthropic 的漏洞发现实验中,协同工作的智能体比独立工作的智能体发现了更多漏洞,但也搜索了更多代码、使用了更多 token。将比较范围限制在相同目录后,每百万 token 的结果大致相同。A big swarm isn't, by itself, evidence that cooperation helped. Noam Brown estimates that less than 10% of the Navier-Stokes result came from multi-agent cooperation. In Anthropic's vulnerability-finding experiment, coordinated agents found more bugs than independent agents, but also searched more code and used more tokens. When the comparison was restricted to the same directories, the results per million tokens were about the same.

合作带来了帮助,还是我们只是花了更多钱? Did cooperation help, or did we just spend more?

从最简单的情况开始。如果四个智能体各自独立地尝试一项任务,那么只要我们能判断哪个答案是正确的,至少有一个智能体答对的概率就会高于只尝试一次。这为多智能体系统提供了一个有用的基准,但并不能说明智能体彼此提供了帮助。它们可能只是尝试的次数更多而已。Start with the simplest case. If four agents each try a task independently, the chance that at least one gets it right is higher than with a single attempt, as long as we can tell which answer is correct. That makes a useful baseline for a multi-agent system, but it doesn't show that the agents helped each other. They may simply have had more tries.

四次尝试的成本也会是四倍。因此,更公平的比较方式是在预算相同的情况下:如果四个智能体各自只能使用总 token 数的四分之一,它们还能胜过一个用完全部 token 的智能体吗?只有通过测试才能知道,因为答案取决于任务、模型以及工作如何划分。Four attempts also cost four times as much. So the fairer comparison is at an equal budget: if each of four agents gets a quarter of the total tokens, can they still beat one agent that spends all of them? We can only find out by testing, because the answer depends on the task, the model and how the work is divided.

这个系列一直在追问同一个问题:智能体之间的通信究竟有没有帮助,还是只是在消耗更多算力?

This series keeps asking one question: when agents communicate, does it actually help, or are we just spending more compute?

我们通过这个简单的预期来理解这个问题。We understand that question through this simple expectation.

Sketch of expected performance against agent count at matched agents and tokens: communicating agents sit above non-communicating agents
Figure 1. Sketch of the expectation we want to test: compare communicating and independent agents at matched budgets.

我们会将智能体数量视为一种扩展维度,与 token 数量和模型规模并列。有时,增加智能体会有所帮助;有时,协调工作会耗尽我们原本希望节省下来的预算。关键是同时衡量这两方面。我们不会一开始就使用 10,000 个智能体,而是先组建一支小团队,处理一项我们能轻松检查的任务。We'll treat agent count as a scaling dimension, alongside tokens and model size. Sometimes adding agents will help. Sometimes coordination will consume the budget we hoped to save. The point is to measure both. We won't start with 10,000 agents. We'll start with a small team on a task that we can check easily.

多智能体系统与强化学习 Multi-agent Systems and Reinforcement Learning

早在语言模型被称为群体之前,多智能体强化学习就已经开始研究合作与竞争。Multi-agent reinforcement learning has studied cooperation and competition long before language models were called swarms.

单个智能体的任务是一个马尔可夫决策过程:观察、行动、获得奖励。在团队中,所有成员共享奖励,但每个智能体只能看到世界的一部分。这就是去中心化部分可观测马尔可夫决策过程:成员彼此协作,但没有人能看到全貌。A single agent's task is a Markov decision process: observe, act, get a reward. In a team, everyone shares the reward, but each agent sees only part of the world. This is a decentralized partially observable Markov decision process: teammates, but no one sees the whole picture.

这会带来三个实际问题。首先,每个智能体在学习的同时,队友也在不断变化。其次是信用分配问题:团队成功时,哪些行动应当得到认可?获胜的团队可能也包含较弱的行动,而我们最终可能会奖励这些行动。第三,消息会改变其他智能体所观察到的信息,因此,通信也是我们正在训练的策略的一部分。This creates three practical problems. First, teammates keep changing while each agent learns. Second, credit assignment: when the team succeeds, which actions deserve the credit? A winning team can include weak actions, and we may end up rewarding them. Third, a message changes what other agents observe, so communication is part of the policy we're training.

一种常见的解决方案是集中式训练与分散式执行:训练时使用整个团队的信息,但每个智能体只根据自身的观测采取行动。我们的第一个示例采用了这种模式,并使用共享的团队奖励。A common solution is centralized training with decentralized execution: training uses information from the whole team, but each agent acts only on its own observations. Our first example follows this pattern with a shared team reward.

代理也可以采用不同的组织方式。以下是两种实用的起步方式:Agents can also be organized in different ways. Two useful starting points are:

  • 层级结构。一个主代理将工作分配给各个工作代理,并收集它们的结果。Anthropic 的 Research 系统采用这种方式,每个工作代理都有独立的上下文。Kimi K2.5 的 PARL 只训练编排器,并冻结子代理,因此训练多智能体系统并不意味着要更新每个组件。参见 Anthropic 的系统说明和 Kimi 的技术博客。

    A hierarchy. A lead agent assigns work to workers and collects their results. Anthropic's Research system works this way, with a separate context for each worker. Kimi K2.5's PARL trains only the orchestrator and keeps the subagents frozen, so training a multi-agent system doesn't mean updating every component. See Anthropic's system description and Kimi's technical blog.

  • 一个分布式团队。成员彼此发送消息,并决定何时查看收件箱。没有主导智能体分配行动。这里我们将采用这种设定,遵循 s³: Simple Scaling of Swarms via Cooperative Multi-agent RL 中的信息聚合任务。

    A distributed team. Peers send each other messages and decide when to read their inboxes. No lead agent assigns the actions. This is the setup we'll use here, following the information-aggregation task in s³: Simple Scaling of Swarms via Cooperative Multi-agent RL.

我们的第一个实验:四个智能体给数字排序 Our first experiment: four agents ordering numbers

我们将复现《Simple Scaling of Swarms》中的信息共享任务,因为它易于理解,也很容易实现。四个智能体共享一个小型语言模型,并学习汇集各自掌握的信息。We'll reproduce the information-sharing task from Simple Scaling of Swarms because it's interpretable and straightforward to implement. Four agents share a small language model and learn to pool what each of them knows.

每个智能体都会收到一个 1 到 99 之间的整数。它知道自己的分配数字以及队友的名字,但起初不知道其他智能体的数字。例如,四个私有分配数字可能是“五”、“二十六”、“五”和“七十八”。重复数字会保留在列表中,因此正确的最终答案是:Each agent receives one integer from 1 to 99. It knows its own assignment and its teammates' names. It does not initially know the other agents' numbers. For example, four private assignments might be "five", "twenty-six", "five" and "seventy-eight". Duplicates stay in the list, so a correct final answer is:

#### [5, 5, 26, 78]

只要至少有一名代理提交完整且排序正确的列表,团队就算成功。一个正确答案就足够了;成功并不要求四名代理都答对。没有队友提供的信息,代理无法可靠地推断其他人各自的私密分配。The team succeeds when at least one agent submits the complete, correctly sorted list. One correct answer is enough; success does not require all four agents to answer correctly. An agent cannot reliably infer the other private assignments without information from its teammates.

这意味着该任务尚未直接测试图 1。每个智能体都掌握着其他智能体所需的信息,因此,单个智能体或无法相互通信的团队,无论获得多少次重试机会都无法成功。相反,该任务测试的是图 1 背后的前提:一个小型共享策略能否学会通信?这需要消耗多少个 token?之后的一篇文章将转向一个单个智能体可以独立解决的任务,在那里,预算相匹配的比较才有意义。This means the task does not yet test Figure 1 directly. Each agent holds information the others need, so a single agent, or a team without messaging, cannot succeed however many retries it gets. Instead, the task tests the precondition behind Figure 1: can a small shared policy learn to communicate at all, and how many tokens does that cost? A later post moves to a task that a single agent can solve alone, where the matched-budget comparison is meaningful.

主要的通信工具包括 message(to, text)、read_messages() 和 check_notifications()。通知会告知代理存在未读消息;读取收件箱后即可查看消息内容。代理可以发送私信,也可以向所有人广播。The main communication tools are message(to, text), read_messages(), and check_notifications(). A notification tells an agent that unread messages exist; reading its inbox makes their contents available. Agents can send direct messages or broadcast to everyone.

我们定义了一个由四个智能体组成的系统,它们共用一个 Qwen3-0.6B 策略,但各自拥有独立的上下文和私有输入。我们并没有训练四个不同的模型。提示词解释了任务和可用工具,也说明了需要共享任务分配信息。We defined a four-agent system using one shared Qwen3-0.6B policy, with separate contexts and private inputs. We are not training four different models. The prompt explains the task and the tools, including the need to share assignments.

Four agents share one policy with separate contexts and private numbers, exchange messages, submit a sorted list, and train from a common team reward
Figure 2. The task and training loop. The private numbers illustrate a possible successful episode; this graphic is not a recorded conversation or a claim about the learned protocol at update 75.

团队奖励,每道题有八次尝试机会 A team reward and eight attempts at each problem

在本文中,一个团队指的是一次实验:四个智能体尝试解决一个问题。成功的团队会获得奖励,奖励会根据其使用的令牌数扣减。失败的团队则得零分:In this post, a team means one episode: four agents attempting one problem once. A successful team receives a reward reduced by the tokens it uses. A failed team receives zero:

team allowance = 4 × (8,192 work tokens + 1,024 final-answer tokens)
               = 36,864 tokens

reward = success × (1 − 0.25 × min(1, generated team tokens / 36,864))

举例来说,一个回答正确且只使用一半额度的团队会获得 0.875。发送消息不会获得额外奖励。激励机制是:在完成任务的同时,尽量少使用生成的 token。For illustration, a correct team using half its allowance receives 0.875. There is no separate reward for sending messages. The incentive is to solve the task while using fewer generated tokens.

对于每个训练题,我们会收集八次相互独立的团队尝试,并在组内对其奖励进行归一化。表现优于所在组平均水平的尝试会获得正优势,表现较差的则会获得负优势。同一团队中的每个智能体都会获得该团队的优势,包括最终答案错误的智能体。For every training problem, we collect eight independent team attempts. Their rewards are normalized within that group. An attempt that does better than its group average gets a positive advantage; one that does worse gets a negative advantage. Every agent in the same team receives that team's advantage, including an agent whose own final answer was wrong.

在 TRL 中,每个智能体采样得到的 token 构成一条训练行。它收到的消息和工具返回结果提供上下文。损失先在每个团队内部取平均,再在团队之间取平均,因此对话更长的团队不会因此获得更高权重。In TRL, each agent's sampled tokens form a training row. Its incoming messages and tool results provide context. The loss is averaged within each team and then across teams, so longer conversations do not give a team more weight.

我们的结果来自原始实验,该实验每次更新使用 256 次团队尝试(32 个问题,每个问题尝试八次)。本演练使用 TRL 的默认值 --teams-per-update 128,换算下来每次更新约有 150 次团队尝试。

Our results come from the original experiment, which used 256 team attempts per update (32 problems with eight attempts each). The walkthrough uses TRL's default of --teams-per-update 128, which works out to about 150 team attempts per update.

我们的结果 Our Results

经过 75 步训练,成功率从 6.6% 上升至 82.3%,这是对比前五次和最后五次更新得出的结果。在第 75 个检查点,四智能体团队解决了 32 道留出题目中的 26 道(81.25%)。论文报告称,经过 200 次更新后,四智能体的成功率达到 93.2%。Over 75 steps, training success rose from 6.6% to 82.3%, comparing the first and last five updates. At checkpoint 75, the four-agent team solved 26 of 32 held-out problems (81.25%). The paper reports 93.2% four-agent success after 200 updates.

团队每次尝试使用的 token 也减少了 37.4%,从约 1.3 万降至 8000。与此同时,信息共享更加活跃:每个团队的广播次数从 2.38 次增至 5.73 次,尽管 token 总用量有所下降。The team also used 37.4% fewer tokens per attempt, falling from about 13k to 8k. This improvement accompanied more active information sharing: broadcasts increased from 2.38 to 5.73 per team, even as total token use fell.

Figure 3. The blue curve describes training batches of 256 team attempts, collected before each update. Orange points are held-out checks after the indicated update.

团队在训练后期使用的 token 也更少。比较前五次更新与第 71–75 次更新:The team also used fewer tokens later in training. Comparing the first five updates with updates 71–75:

Training metric Updates 1–5, mean Updates 71–75, mean
Success 6.56% 82.34%
Generated tokens per team 12,826.9 8,026.0
Messages per team 3.48 6.28
Broadcasts per team 2.38 5.73
Teams with one broadcast per agent 4.06% 18.20%

这意味着在这两个时间窗口之间,每个训练团队生成的 token 减少了 37.4%。仅看 batch 75,平均每个团队生成了 7,624 个 token。这些是训练测量数据;我们没有保留已训练检查点在留出集上的 token 效率测量数据。That is a 37.4% reduction in generated tokens per training team between those windows. At batch 75 itself, the mean was 7,624 tokens per team. These are training measurements; we did not retain held-out token-efficiency measurements for the trained checkpoint.

请注意,token 数量减少并不意味着消息数量也减少了。参与的智能体变多了,但广播比直接消息更常见。生成的 token 包括推理内容、工具调用以及消息正文,因此仅凭消息数量无法判断团队生成了多少文本。Notice that fewer tokens did not mean fewer messages. More agents began participating but broadcasting became more common, rather than direct messaging. Generated tokens include reasoning and tool calls as well as message bodies, so message count alone does not tell us how much text the team produced.

Figure 4. Top: training token use and a trailing five-update mean. Bottom: mutually exclusive communication classes, based on sent-message events. A team is classified first by whether some agent never sends, then whether any direct message occurs, then whether every agent sends exactly one broadcast; the remaining teams repeat broadcasts.

更多重试,还是一支训练有素的团队? More retries or a trained team?

没有任何一个智能体能够独自解决这个问题,因此,“重试次数更多的单个智能体”最接近的对应项就是重试次数更多的未训练团队。训练数据中已经包含了这一比较。每次更新都会为每道题收集八次尝试,而且任务答案很容易核验,因此,只要八次尝试中有一次成功,我们就可以将该题计为已解决。在训练初期,这一计数反映了未训练团队的八次尝试择优成功率:No agent can solve the problem alone, so the closest equivalent of "a single agent with more retries" is the untrained team with more retries. The training data already contains this comparison. Each update collects eight attempts per problem, and the task's answers are easy to check, so we can count a problem as solved when any of its eight attempts succeeded. Early in training, that count gives the untrained team's best-of-8 success rate:

Success Generated tokens per problem
Untrained team, best of 8 attempts (updates 1–5) 37.5% about 103,000
Trained team, one attempt (updates 71–75) 82.3% about 8,000

进行八次重试后,未经过训练的团队解决的问题不到一半,消耗的 token 数约为经过训练的团队尝试一次的 13 倍。请将此视为估算值。两行数据都使用了训练问题,而非留出问题;并且从第 2 到第 5 次更新开始,采样时使用的策略已经过少量训练。With eight retries, the untrained team solves fewer than half the problems and spends about 13 times as many tokens as one attempt from the trained team. Read this as an estimate. Both rows use training problems rather than held-out ones, and updates 2–5 already sample from a slightly trained policy.

基础设施 Infrastructure

模型很小,但 rollout 的规模并不小。每次更新都会生成数百万 token 的智能体对话,上下文长度最高可达 40k token。我们使用一块 H200 作为训练器,另外七块 H200 运行 vLLM rollout 引擎,以快速完成这些 rollout,并将每个 worker 的八次尝试放在一起。下面的演示在两块 GPU 上运行同一任务。以下是基础设施详情:The model is small, but the rollouts are not. Each update generates millions of tokens of agent conversation at contexts of up to 40k tokens. We used one H200 for the trainer and seven H200s running vLLM rollout engines to get through those rollouts quickly, and kept each worker's eight attempts together. The walkthrough below runs the same task on two GPUs. Here are the infra details:

Setting Recorded experiment
Shared policy Qwen3-0.6B
Logical agents per team 4
Problems per update 32
Team attempts per problem 8
Work / final tokens per agent 8,192 / 1,024
Learning rate / temperature 5e-7 / 1.0
Training problem pool 1,024 generated problems
Reporting endpoint Preserved checkpoint 75
Execution 1 trainer GPU + 7 rollout GPUs

实践示例:训练你自己的蜂群 Practical example: Train your own swarm

上面的图表描述了我们最初进行的 75 次更新实验。在本教程中,我们将在 Hugging Face Jobs 上运行 TRL 的 swarm 示例。一块 H200 使用 vLLM 为共享的 Qwen3-0.6B 策略提供服务,另一块 H200 则负责训练它。The figures above describe our original 75-update experiment. In this walkthrough we'll run TRL's swarm example on Hugging Face Jobs. One H200 serves the shared Qwen3-0.6B policy with vLLM and a second H200 trains it.

1. 为此次运行创建一个存储桶 1. Create a bucket for the run

使用具备运行 Jobs 和创建存储桶权限的 Hugging Face 账户登录。将 YOUR_USERNAME 替换为你的 Hub 用户名。Sign in with a Hugging Face account that can run Jobs and create buckets. Replace YOUR_USERNAME with your Hub username.

每次运行都使用一个新的存储桶,以便保留之前的测量数据。

Use a fresh bucket for each run so earlier measurements stay available.

uv tool install 'huggingface_hub==2.4.0'
hf auth login
BUCKET="YOUR_USERNAME/swarm-trl-$(date -u +%Y%m%d-%H%M%S)"
hf buckets create "$BUCKET"

2. 在 Hugging Face Jobs 上运行 TRL 示例 2. Run the TRL example on Hugging Face Jobs

将下面的脚本保存为当前目录中的 train-swarm.sh。Save the script below as train-swarm.sh in your current directory.

train-swarm.sh
set -eu
git clone --depth 1 https://github.com/huggingface/trl /tmp/trl
cd /tmp/trl
example=examples/async_grpo_s3_aggregation
if [ ! -f "$example/async_grpo_s3_aggregation.py" ]; then
  git fetch --depth 1 origin refs/pull/8424/head
  git checkout --detach FETCH_HEAD
fi
git rev-parse HEAD > /results/trl-revision.txt
python -m pip install -e ".[vllm,kernels]" "vllm==0.32.0" datasets httpx trackio

base=Qwen/Qwen3-0.6B
url=http://127.0.0.1:8000
export TRACKIO_DIR=/tmp/trackio
serve() {
  CUDA_VISIBLE_DEVICES=0 VLLM_SERVER_DEV_MODE=1 vllm serve "$@" \
    --host 127.0.0.1 --max-model-len 40960 --max-num-seqs 512 --enable-prefix-caching &
  server=$!
  until curl -fs "$url/health" >/dev/null; do kill -0 "$server"; sleep 5; done
}
evaluate() {
  python "$example/evaluate.py" --url "$url" --model "$1" --name "$2" \
    --episodes 192 --concurrency 64 --out "/results/$2.jsonl"
}

serve "$base" --logprobs-mode processed_logprobs --weight-transfer-config "{\"backend\":\"nccl\"}"
evaluate "$base" base
CUDA_VISIBLE_DEVICES=1 accelerate launch --num_processes 1 --mixed_precision bf16 \
  "$example/async_grpo_s3_aggregation.py" --vllm-url "$url" \
  --updates 50 --teams-per-update 128 --concurrent-teams 128 \
  --report-to trackio --output-dir /tmp/swarm
cp -r "$TRACKIO_DIR" /tmp/swarm/checkpoint-50 /results/
kill "$server"
wait "$server" || true
serve /tmp/swarm/checkpoint-50 --logprobs-mode processed_logprobs
evaluate /tmp/swarm/checkpoint-50 trained

下面的命令会将其中的内容发送到作业中。在作业中,vLLM 会在 GPU 0 上提供共享策略。GPU 1 会运行 TRL 的 async_grpo_s3_aggregation.py,使用 Trackio 执行 50 次更新。TRL 的 evaluate.py 会使用相同的 192 道留出题目,对基础模型和第 50 次检查点进行评分。这个集合比我们结果中使用的 32 题检查集更大,因此误差范围更窄。作业会将两项评分、检查点、Trackio 数据以及确切的 Git 提交版本保存到你的存储桶中。The command below sends its contents to the job. Inside the job, vLLM serves the shared policy on GPU 0. GPU 1 runs TRL's async_grpo_s3_aggregation.py for 50 updates with Trackio. TRL's evaluate.py scores the base model and checkpoint 50 on the same 192 held-out problems. That is a larger set than the 32-problem checks in our results, which gives tighter error bars. The job saves both scores, the checkpoint, Trackio data and the exact Git commit to your bucket.

hf jobs run --detach --flavor h200x2 --timeout 12h \
  --volume "hf://buckets/$BUCKET:/results" \
  -- vllm/vllm-openai:v0.32.0 bash -c "$(cat train-swarm.sh)"

一次 TRL rollout 包含一段完整的团队对话。训练器会比较针对同一问题的八次尝试,并为一次尝试中的全部四个智能体赋予相同的优势。每次更新大约使用 150 次团队尝试。TRL 按 token 数量而非团队来打包微批次,因此 --teams-per-update 128 这一设置只是近似值。--report-to trackio 标志会在每次更新时记录训练指标。One TRL rollout contains a whole team conversation. The trainer compares eight attempts at the same problem and gives all four agents in an attempt the same advantage. Each update uses about 150 team attempts. TRL packs micro-batches by token count rather than by team, so the --teams-per-update 128 setting is approximate. The --report-to trackio flag records training metrics at every update.

这次较短的运行比我们最初的实验成本更低,并且在学习曲线上更早停止。使用 TRL 运行此示例,在两张 H100 上耗时约 4.8 小时,并在第 50 个检查点达到 60.9% 的留出集成功率。This shorter run is cheaper than our original experiment, and it stops earlier on the learning curve. TRL's run of this example took about 4.8 hours on two H100s and reached 60.9% held-out success at checkpoint 50.

若要更接近我们最初的实验,请将训练参数改为 --updates 75 --teams-per-update 256 --concurrent-teams 256,在脚本中将 checkpoint-50 替换为 checkpoint-75,并将作业的 --timeout 调高至 24h。这样 rollout 数量就是原来的三倍。两个代码库在系统提示词等细节上仍有差异,因此预期结果相似即可,不必完全复现。

To run closer to our original experiment, change the training flags to --updates 75 --teams-per-update 256 --concurrent-teams 256, replace checkpoint-50 with checkpoint-75 in the script, and raise the job's --timeout to 24h. That is three times as many rollouts. The two codebases still differ in details such as the system prompt, so expect similar results rather than an exact replication.

3. 阅读结果 3. Read the results

作业完成后,将存储桶同步到本地文件夹。其中包含两个评估文件、Trackio 数据、TRL 修订版本以及 checkpoint-50 权重,后者是最大的下载项。在仪表板中,对比训练过程中的 team/success、team/messages 和 team/tokens。作业日志还提供了基础模型和 checkpoint 50 的留出成功率。base.jsonl 或 trained.jsonl 中的每一行都记录了一次这样的评估尝试。Once the job completes, sync the bucket to a local folder. It holds the two evaluation files, the Trackio data, the TRL revision and the checkpoint-50 weights, which are the largest download. In the dashboard, compare team/success, team/messages and team/tokens over training. The job logs also give the held-out success rate for the base model and checkpoint 50. Each line of base.jsonl or trained.jsonl records one of those evaluation attempts.

hf buckets sync "hf://buckets/$BUCKET" ./swarm-results
TRACKIO_DIR="$PWD/swarm-results/trackio" uvx trackio show

接下来我们将尝试的内容 What we'll try next

第一个示例为我们展示了一个小型环境、一项共享奖励,以及一个逐步提升整合私人信息能力的策略。它也说明了为什么需要结合起来解读成功率、令牌使用量和通信结构。经过 75 次更新后,团队已经能够解决大多数经过检查的问题,但其通信模式尚未收敛到最简洁的形式。This first example gives us a small environment, a shared reward and a policy that improves at pooling private information. It also shows why success, token use and communication structure need to be read together. At 75 updates, the team is solving most checked problems, but it has not converged to the cleanest communication pattern.

在接下来的示例中,我们将暂时把训练放到一边,看看如何评估和分析群体智能系统。In the coming examples, we'll take a step back from training and look at how we evaluate and analyze swarms.

目前,我们有了可以具体着手的基础:四个小型智能体、一个可训练的策略、一项可验证的任务,以及一种可复现的方法,用来检视团队学到了什么。For now, we have something concrete to build on: four small agents, one trainable policy, a checkable task, and a reproducible way to examine what the team learned.

值得记下

  1. 这个系列一直在追问一个问题:智能体沟通时,真的能带来帮助吗?还是我们只是在消耗更多算力?
  2. 经过 75 个步骤,训练后的成功率从 6.6% 上升至 82.3%。
  3. 团队每次尝试使用的 token 也减少了 37.4%,从约 13k 降至 8k。
  4. 即使重试 8 次,未训练的团队解决的问题仍不到一半,而且消耗的 token 约为训练后团队单次尝试的 13 倍。