模块定位
tokenspeed-engine/crates/ts-host/ 和 ts-control/ 构成每个 engine rank 的原生控制面。它们解决的不是模型计算,而是更容易被 GPU 延迟破坏的事情:接收请求、让 replica 内各 rank 在同一轮看到相同输入、调用 C++ scheduler、限制 in-flight 深度、提交 plan、收割 forward result、流式返回输出。
该模块独立存在的理由是隔离等待关系。GPU 前向可以持续数毫秒,控制 round 只应持续微秒;如果二者共享一个 Python event loop,GIL、同步操作或 kernel launch backpressure 都可能让其他 rank 卡在 collective 上。TokenSpeed 于是让 ts-host 主线程运行 Rust EventLoop,另起 ts-forward 线程串行执行 Python HostDataPlane 操作。
模块架构
ts-host::serve() 先嵌入 CPython,调用 rust_host.bootstrap() 得到 Python HostBootstrap;再把 nanobind 暴露的 scheduler 提取成 Rust facade,建立 ts-collective::Mesh 和 frontend EngineTransport。运行期,主线程持有 EventLoop、scheduler、request state 与 collective;forward thread 持有 Python HostDataPlane,而后者才持有 DeviceHandle。
这个所有权切分比“约定不要在控制线程碰 CUDA”更强:Rust 控制面没有 torch 对象,DataPlane trait 只暴露 admit、execute、complete、poll_cache_events 等命名操作;DeviceHandle 也不返回内部 executor。控制线程即使想绕开计划直接调用模型,也没有可达路径。
调用链路
EventLoop::round() 先 relay frontend 消息,再处理 admission、grammar、pause/weight gate 和 round-head cache/L3 completion。随后 plan_and_dispatch() 调用 scheduler.next_execution_plan(),把 storage 子集送到 L3、把 device work 交给 forward thread,并在 in-flight 深度允许时继续规划下一轮。旧 forward 到达 commit fence 后,DataPlane::complete() 才等待 D2H event;产生的 ForwardEvent 集中送入 scheduler.advance()。
方法速查表
| 方法 | 一行职责 | 关键设计决策 |
|---|---|---|
serve() in ts-host/src/main.rs |
构造解释器、mesh、transport、data plane 和 event loop | 原生 host 是 rank 进程真正的 main |
EventLoop::round() |
执行一个控制轮次 | 所有反馈先入 scheduler,再规划下一轮 |
EventLoop::plan_and_dispatch() |
取得 plan 并提交 L3/device/transfer work | dispatch 不等待 GPU |
EventLoop::commit_down_to() |
将 in-flight forward 收敛到深度阈值 | overlap 有明确上限和 FIFO commit |
DataPlane::execute() |
异步提交 plan | trait 不暴露任意 Python callback |
DataPlane::complete() |
等待 forward 的 host copies | 只在 commit fence 阻塞 |
HostDataPlane.execute() |
plan → PlannedForward → DeviceHandle |
跨语言边界只传值和 schema |
核心实现
DataPlane 是能力边界,不是普通 FFI
DataPlane trait 刻意枚举控制面允许运行的动作:
pub trait DataPlane {
type Pending;
fn admit(&mut self, requests: &[Admission]) -> Result<(), DataPlaneError>;
fn execute(
&mut self,
plan: ExecutionPlan,
dispatch: &Dispatch<'_>,
) -> Result<Option<Self::Pending>, DataPlaneError>;
fn complete(&mut self, pending: Self::Pending)
-> Result<ForwardResult, DataPlaneError>;
fn poll_cache_events(&mut self) -> Result<Vec<CacheEvent>, DataPlaneError>;
fn release(&mut self, rids: &[String]) -> Result<(), DataPlaneError>;
}
execute() 返回 opaque Pending,而不是 tensor 或 model runner;控制面只能稍后把它交回 complete()。这相当于 capability security:把“可以做什么”限制在接口形状里。新增一个 device interaction 必须显式扩大 trait 和 Python adapter,因此 review 能看见控制面能力面的变化。
一轮只有两个反馈点
round head 接收 cache/L3 acknowledgment,forward tail 接收 token result。两类反馈都由 scheduler 消费,执行面不直接改变请求状态:
if storage.tick.is_some() || !ready.is_empty() {
self.scheduler.advance_head(&storage, &ready)?;
}
self.plan_and_dispatch(...)?;
if !events.is_empty() {
self.scheduler.advance(&events)?;
}
这样设计的原因是确定性。replica 内每个 rank 跑同一 scheduler,只要相同 round 获得相同消息与汇总事实,就会得到相同 plan;如果某个 Python backend 能在中途偷改状态,各 rank 很快会在 collective 或 page ownership 上分叉。
overlap 是受限队列,不是自由并发
EventLoop 用 VecDeque<(ForwardBatchView, InputLogprobPlan, Pending)> 保存已 dispatch 未 commit 的 forward。in_flight_depth 允许控制面在前一轮 GPU 仍运行时规划后续工作,但 scheduler 的 ForwardResources.results_in_flight 会把 cache pages 与请求 slot 固定住,直到 result 落地。由此 overlap 提升了 CPU/GPU 并行度,却没有放松资源复用 fence。
attention-DP rank 必须“空跑”
不同 attention-DP replica 可以有不同 batch,但模型的 cross-rank collective 仍要求大家同一步进入。dp_round() 汇总每个 rank 的 token/request/prefill 数;本 rank 无 batch而其他 rank 有时,DpRound.idle_forward=true,data plane 调用 run_idle_forward()。空跑不是浪费性的 fallback,而是保持 collective 序列一致的协议动作。
设计模式
| 模式 | 位置 | 为什么用 |
|---|---|---|
| Actor | EventLoop / PythonDataPlane |
两个线程各自拥有状态,通过 FIFO task 交接 |
| Facade | DataPlane trait |
控制面只能发起命名操作 |
| Command | ExecutionPlan |
scheduler 生成、data plane 消费,跨语言边界稳定 |
| Reactor | EventLoop::round() |
frontend、cache、storage、forward、control operation 汇聚在 round |
| Leader relay | EventLoop::relay() |
replica 首 rank 连接 frontend,其余 rank 复用同一消息序列 |
模块间交互
控制面向上连接 SMG/Dynamo 的 msgpack wire,向下连接 C++ scheduler 与 Python data plane,横向通过 ts-collective::Mesh 连接 replica、PP、DP 和 EP rank。ts-pd、ts-l3、grammar compiler、metrics exporter 都在外围线程工作,只把完成事实送回 round;它们不能把 I/O 或编译等待塞进主循环。
扩展方式
新增控制操作时,先在 ts-wire 定义 schema,再在 ts-control::control 加入生命周期,在 DataPlane 与 PythonDataPlane 暴露最窄的命名方法,最后在 HostDataPlane / DeviceHandle 实现。不要增加“执行任意 Python 函数”或返回内部对象的通用口子;那会破坏本模块最重要的可达性约束。