模块定位
TokenSpeed 的分布式层不只是 torch.distributed 封装。runtime/distributed/ 描述模型内 attention/dense/MoE 的 rank mapping 与通信;runtime/pd/ 和 Rust ts-pd 把 Prefill/Decode 变成两个独立 engine;runtime/cache/、ts-l3 与 scheduler storage manager 又把 KV 扩展到 Host L2 和 Mooncake L3。
这些能力共同面对一个问题:远端 transfer 和存储 I/O 的完成时间不受 GPU forward 控制。TokenSpeed 因此不让 Python await 它们,而是把 remote stream、storage op 和 completion fact 都纳入 scheduler plan/round,继续复用同一 FSM 和 admission 流程。
模块架构
SMG gateway 可驱动 Plain、PD 或 EPD 部署。每个 role 都是独立 headless engine,内部仍按 attention DP/TP、PP、MoE EP 等 topology 启动多个 ts-host。PD 的 Python build_host_peer() 根据 cache transfer contract 生成 region/field/stage/step facts;Rust ts-pd 持有 Mooncake transfer engine 与 worker。L3 则由 l3_settings() 生成只含 Host 地址、schema、deployment identity 和 worker limit 的冻结配置,Rust ts-l3 完成真正 I/O。
Host L2、retraction snapshot pool 与 L3 共用 HostCacheStorage 的 pinned allocation;L3 没有单独 Device buffer。Device H2D/D2H 仍由 HostCacheExecutor 在 DeviceHandle 后面排序。
调用链路
PD 路径中,Prefill scheduler 生成 remote_decode,forward thread 在 KV write 已入队后通过 outbox 通知 ts-pd;Decode scheduler 先 admission 目标 pages,再用 remote_prefill 拉取,landing 完成事件推进请求进入 decode。L3 路径中,请求进入 Prefetching,scheduler 生成 StorageQueryOp/StorageGetOp;ts-l3 完成后把 per-key success 与 retirement fact 送回 round head,scheduler 重新匹配 Host cache,最终走普通 H2D + forward。
方法速查表
| 方法 | 一行职责 | 关键设计决策 |
|---|---|---|
PDParallelTopology.from_mapping() |
提取 PD 所需 rank topology | transfer contract 不读取模型对象 |
build_host_peer() |
构造 Prefill/Decode peer settings | cache regions 与 field layout 启动时冻结 |
_HostPeer.execute() |
通知 Rust host remote stream 可执行 | Python 不执行 Mooncake transfer |
HostPrefillPeer.register_layerwise_step_counter() |
注册 layerwise KV readiness | backend 只 tick counter,不操作 transport |
l3_settings() |
Host facts → L3Settings |
deployment identity 包含 checkpoint/layout/numerics |
EventLoop::storage_round() |
汇总 L3 report 并产生 scheduler tick | I/O completion 在 round head 反馈 |
StorageTransferManager |
管理 query/get/put ticket 与 lease | cancellation 与 native I/O retirement 分离 |
核心实现
并行 topology 是组合,不是单个 world size
一个 rank 同时可能属于 replica、pipeline、attention-DP 与 expert group。Rust ts-host 为这些 group 建不同 collective id;Python mapping 还细分 attention head TP、KV parallel、query context parallel、dense TP 与 MoE TP/EP。每种通信只在自己的 group 上发生,避免用一个全局 rank 算术隐式推导所有关系。
PD remote stream 是 plan 的一等成员
DeviceHandle.execute() 无论本轮是否有 model batch,都检查 remote_decode 与 remote_prefill。Prefill 发送必须排在写 KV 的 forward/drafter 之后;Decode 接收必须排在目标 page zero 之后。把 remote stream 放进 plan,才能让 scheduler 声明“哪些 page 已授权给谁”,再由 forward thread用同一 stream fence 落实。
class _HostPeer:
def execute(self, op) -> None:
del op # Rust host reads the staged stream from the plan.
self._outbox.remote_stream_ready()
Python 只发布设备 readiness,Rust peer 读取已经 staged 的 plan 并执行传输。这个极窄接口防止 Python transport 和 C++ scheduler 各自维护一份 request/page 状态。
layerwise streaming 把 readiness 变成计数器协议
Prefill 可不等完整模型 forward 结束才传所有层 KV。启动时 contract 声明 fields_by_step,Attention backend 每完成一个 producer step 只更新 StepCounter;Rust worker 按 interval 读取 pinned host word 并流式发送。一次 layer 内部为了 kernel size 拆成多个 chunk,也只有最后一个 chunk发布 readiness,防止 consumer 读到半层数据。
L3 是异步扩展,不是第二种 scheduler
L3 请求在 FSM 中进入 Prefetching,它没有 Device page 或 request slot,因此不会占 forward candidate。soft deadline 到期只让请求回到本地 admission,不取消底层 native I/O;完成结果只有在所有 owner 证明 retirement、长度完整且 request 仍 attached 时才能发布 Host entry。最终 H2D 继续使用 ordinary cache ops。
deployment identity 防止错误复用
l3_settings() 生成的 identity 不只含 model name,还含 checkpoint revision、cache layout signature、pipeline rank、group sharding、draft revision、quantization、Attention backend、numerics 和 runtime compatibility epoch。同一 token prefix 只有在“会产生相同 KV 字节”的部署之间才共享对象;调度队列参数不影响 KV 字节,因此不进入 identity。
write path 不让慢 PUT 阻塞读
Device→Host L2 publication完成后立即释放 Device source,不等 Mooncake PUT。scheduler 把仍在 Host resident 的 prefix 作为 bounded backup candidate;真正 dispatch PUT 时再 acquire source ref。write credits 最多占 operation/Host credits 的一半,读可使用全部共享预算,避免慢备份耗尽新请求的 prefetch 能力。
设计模式
| 模式 | 位置 | 为什么用 |
|---|---|---|
| Topology Object | PDParallelTopology / mapping |
显式表达重叠 rank group |
| Producer-Consumer | layerwise StepCounter |
KV 按 producer step 流式传输 |
| Lease | StorageLease |
请求取消与 native I/O retirement 解耦 |
| Write-back Cache | L2 → L3 PUT | Host publication不等待远端持久化 |
| Identity Hash | storage_deployment_id() |
只在字节兼容部署间复用 KV |
模块间交互
Scheduler 是所有 page ownership 与 storage ticket 的 owner;Rust control plane 提交 remote/storage work并汇总完成;Python data plane只负责 Device fence 与 region address;Mooncake workers不调用 scheduler、Python GIL 或 GPU collective。这个边界让网络抖动只改变请求何时收到完成事件,不改变谁拥有资源。
扩展方式
新增 transfer backend 时应实现与现有 peer 相同的 staged-plan/readiness/completion contract,而不是从 Attention backend 直接发网络。新增 L3 backend 时保持 ts-l3 Host-only executor 接口,明确 query/get/put/cancel 的 bounded queue、retirement 与 health fact;不要让 worker 持有 mutable scheduler namespace 或 DeviceHandle。