Skip to content

feat(zmq): teach the direct-backend path to speak TokenSpeed - #2036

Merged
slin1237 merged 1 commit into
mainfrom
feat/zmq-tokenspeed
Aug 3, 2026
Merged

feat(zmq): teach the direct-backend path to speak TokenSpeed#2036
slin1237 merged 1 commit into
mainfrom
feat/zmq-tokenspeed

Conversation

@slin1237

@slin1237 slin1237 commented Aug 3, 2026

Copy link
Copy Markdown
Member

Description

Problem

The ZMQ direct-backend path (#2000, built on #2015) speaks only vLLM's EngineCore protocol. TokenSpeed (and later sglang) engines need the same same-host ipc:// fast path, but the crate's transport, connector, and gateway adapter all hard-assumed the vLLM wire format — and several workflow steps hard-assumed "ZMQ worker ⇒ vLLM".

Solution

Make the ZMQ stack engine-neutral and add TokenSpeed as the second protocol:

  • engine-zmq-client: a new EngineProtocol trait seams the shared transport/connector (handshake, ROUTER/DEALER identity framing, output loop, abort-on-drop) away from the per-engine wire structs. VllmProtocol keeps the existing behavior; TokenSpeedProtocol adds the sglang-family msgpack tuples (WireTokenizedGenerateReq 5-tuple, WireSamplingParams 13-tuple, WireBatchTokenIDOut 8-tuple with sampled-token logprob columns). Handshake structs move to a neutral protocol/handshake.rs (re-exported for compatibility). A neutral EngineLoad replaces the vLLM-specific stats type in the shared seam.
  • Gateway adapter (zmq_client.rs): ZmqEngineClient selects the protocol from the worker's explicit runtime_type (tokenspeed vs vllm; anything else is rejected before the handshake and at registration). Streams map both protocols to the existing vLLM-proto pipeline — chunks carry incremental tokens/logprobs, the terminal Complete carries the cumulative set, and a finish-tick's tokens are emitted as a chunk first so streaming never loses the last token.
  • No silent narrowing: requests the wire cannot honor fail loudly with invalid_argument instead of degrading — structured-output constraints, n>1, top-k/prompt logprobs, stop strings (TokenSpeed), logit_bias (TokenSpeed), nonzero data_parallel_rank. Sampled-token logprobs are wired end-to-end on both ZMQ protocols.
  • Runtime plumbing: detect_backend/discover_metadata no longer force ZMQ workers to vLLM — an explicitly configured runtime_type survives to the built worker (unspecified still defaults to vLLM with a warning). BackendClient::runtime_type() reports the actual ZMQ backend runtime.
  • wfaas fix: the DAG scheduler could fail a workflow with a spurious "Workflow deadlocked" when a step completed between the completion-drain and the tracker read (instant-completing ZMQ detection steps hit this routinely). The deadlock branch now re-drains the completion channel before failing, and the run_if paths send their completion inside the tracker lock scope. Regression tests included.
  • smg serve: per-user ZMQ socket dir (SMG_ZMQ_SOCKET_DIR override), FNV handshake-port derivation pinned by conformance vectors against the Python mirror.

Changes

  • crates/engine_zmq_client: protocol/mod.rs (EngineProtocol/EngineOutput/EngineBatch/EngineLoad), protocol/tokenspeed/{mod,request,sampling,output}.rs, protocol/handshake.rs, generic connector.rs/transport.rs, EngineCoreReadyResponse.max_num_batched_tokens widened to i64 (TokenSpeed sends -1 = disabled).
  • model_gateway: routers/grpc/zmq_client.rs (ZmqBackend enum, per-protocol translate/map with loud rejection boundary, logprob accumulation), routers/grpc/backend_client.rs (runtime passthrough), worker/worker.rs (runtime param on connect), workflow/steps/local/{detect_backend,discover_metadata,create_worker}.rs (runtime preservation + ZMQ runtime validation).
  • crates/workflow: deadlock-detector re-drain + run_if in-lock completion send + regression tests.
  • bindings/python/src/smg/serve.py: per-user socket dir.

Test Plan

  • cargo clippy --workspace --all-targets --all-features -- -D warnings, cargo +nightly fmt --all -- --check clean; engine-zmq-client (incl. mock-engine e2e for both protocols), wfaas, and smg zmq_client suites green.
  • Cross-language contract: Python msgspec-encoded fixtures decode in the Rust codec and vice versa (field order, arity, float64 logprob columns).
  • Live e2e on GB300: real TokenSpeed engine (Qwen3-0.6B, branch lightseekorg/tokenspeed#feat/zmq-msgpack) registered via POST /workers {"url":"ipc://...","connection_mode":"zmq","runtime_type":"tokenspeed"} — chat (content + reasoning_content), completions, sampled-token logprobs (--enable-output-logprobs), top-k/prompt-logprob rejections, and repeated worker registration with zero workflow-deadlock failures. The existing vLLM ZMQ path re-validated unchanged.

Follow-ups: engine-side handshake retry, DP>1 (coordinator/wave, task tracked), legacy /v1/completions logprobs rendering (pre-existing gap for all backends).

Checklist
  • cargo +nightly fmt passes
  • cargo clippy --all-targets --all-features -- -D warnings passes
  • (Optional) Documentation updated
  • (Optional) Please join us on Slack #sig-smg to discuss, review, and merge PRs

Make the ZMQ direct-backend stack engine-neutral and add TokenSpeed as
the second wire protocol, speaking its msgpack-native tagged msgspec
structs directly — a same-host TokenSpeed scheduler is driven over
ipc:// with no Python servicer hop.

engine-zmq-client:
- EngineProtocol trait seams the shared transport/connector (handshake,
  ROUTER/DEALER identity framing, output loop, abort) away from the
  per-engine wire structs; the vLLM protocol keeps its behavior.
- TokenSpeed protocol speaks the engine's native tagged structs: the
  tokenized generate request is emitted as the tagged positional prefix
  through `stream` (nested native SamplingParams, normalized frontend-
  side), and the per-step output decodes the tagged slim batch struct
  (token ids, finish reasons, token counts, sampled-token logprob
  columns). Tag-validated decode, trailing-field tolerance, and pinned
  cross-language byte vectors from the Python encoder.
- Handshake structs move to a neutral protocol/handshake.rs; a neutral
  EngineLoad replaces engine-specific stats in the shared seam;
  max_num_batched_tokens widened to i64 (-1 = disabled).

Gateway:
- ZmqEngineClient selects the protocol from the worker's explicit
  runtime_type; unsupported runtimes are rejected before the handshake
  and at registration. BackendClient reports the actual runtime.
- n>1 is fanned out frontend-side: n single-sample wire requests with
  per-sub rids and deterministic seed derivation, streams interleaved
  with per-choice proto indexes, cumulative usage counted once, drop
  aborts every sub.
- Streams emit a finish-tick's tokens as a chunk before the cumulative
  Complete, so streaming never drops the last token. Sampled-token
  logprobs are wired end-to-end on both ZMQ protocols.
- No silent narrowing: structured-output constraints, top-k/prompt
  logprobs, stop strings, logit_bias, and nonzero data_parallel_rank
  fail loudly with invalid_argument.
- Workers honor an optional WorkerSpec.zmq_handshake_address bind
  override; the FNV port derivation from the ipc path remains the
  no-config default (doc + conformance vectors pinned against the
  Python launcher mirror).
- detect_backend/discover_metadata preserve an explicitly configured
  ZMQ runtime; unspecified still defaults to vLLM with a warning.

smg serve / config:
- `smg serve --backend tokenspeed --connection-mode zmq` launches the
  engine headless (`python -m tokenspeed.cli serve --headless` with the
  derived --data-parallel-rpc-port), mirroring the vLLM zmq launcher;
  dense data parallelism = N independent workers.
- `--backend` pins the startup ZMQ worker runtime through both config
  conversion paths and the Python bindings; per-user ZMQ socket dir
  (SMG_ZMQ_SOCKET_DIR override).

workflow engine:
- Fix a spurious "Workflow deadlocked" failure: completions landing
  between the drain and the tracker read are now re-drained in the
  deadlock branch, and the run_if paths send their completion inside
  the tracker lock scope. Regression tests included.

Validated live on GB300 against a real TokenSpeed engine (Qwen3-0.6B):
chat (content + reasoning_content), completions, streaming, n=2,
sampled-token logprobs, loud rejections, engine-side invalid-request
aborts, ENGINE_CORE_DEAD death detection, and the handshake-address
override with a bare-default engine.

Signed-off-by: Simo Lin <25425177+slin1237@users.noreply.github.com>
@github-actions github-actions Bot added python-bindings Python bindings changes grpc gRPC client and router changes tests Test changes labels Aug 3, 2026
@slin1237
slin1237 merged commit 386be7a into main Aug 3, 2026
23 of 35 checks passed
@github-actions github-actions Bot added protocols Protocols crate changes workflow Workflow crate changes model-gateway Model gateway crate changes labels Aug 3, 2026
@slin1237
slin1237 deleted the feat/zmq-tokenspeed branch August 3, 2026 15:44
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 05451134-7ab7-4562-9893-479480a5d656

📥 Commits

Reviewing files that changed from the base of the PR and between 244fd62 and 4443132.

📒 Files selected for processing (31)
  • bindings/python/src/lib.rs
  • bindings/python/src/smg/router.py
  • bindings/python/src/smg/router_args.py
  • bindings/python/src/smg/serve.py
  • bindings/python/tests/test_serve.py
  • crates/engine_zmq_client/src/connector.rs
  • crates/engine_zmq_client/src/lib.rs
  • crates/engine_zmq_client/src/mock_engine.rs
  • crates/engine_zmq_client/src/protocol/handshake.rs
  • crates/engine_zmq_client/src/protocol/mod.rs
  • crates/engine_zmq_client/src/protocol/tokenspeed/mod.rs
  • crates/engine_zmq_client/src/protocol/tokenspeed/output.rs
  • crates/engine_zmq_client/src/protocol/tokenspeed/request.rs
  • crates/engine_zmq_client/src/protocol/tokenspeed/sampling.rs
  • crates/engine_zmq_client/src/protocol/vllm/mod.rs
  • crates/engine_zmq_client/src/transport.rs
  • crates/protocols/src/worker.rs
  • crates/workflow/src/engine.rs
  • crates/workflow/tests/workflow_test.rs
  • model_gateway/src/config/builder.rs
  • model_gateway/src/config/types.rs
  • model_gateway/src/main.rs
  • model_gateway/src/routers/grpc/backend_client.rs
  • model_gateway/src/routers/grpc/common/stages/encode.rs
  • model_gateway/src/routers/grpc/zmq_client.rs
  • model_gateway/src/worker/builder.rs
  • model_gateway/src/worker/worker.rs
  • model_gateway/src/workflow/job_queue.rs
  • model_gateway/src/workflow/steps/local/create_worker.rs
  • model_gateway/src/workflow/steps/local/detect_backend.rs
  • model_gateway/src/workflow/steps/local/discover_metadata.rs

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added TokenSpeed as a supported serving backend alongside vLLM.
    • Added TokenSpeed ZMQ worker launching, request handling, streaming output, sampling, and logprob support.
    • Added backend-aware runtime selection and optional ZMQ handshake address configuration.
    • Added support for multiple generated choices and backend-specific output handling.
  • Bug Fixes

    • Improved workflow completion reliability for skipped and conditionally executed steps.
  • Tests

    • Added comprehensive coverage for TokenSpeed integration, ZMQ routing, handshake configuration, and workflow regressions.

Walkthrough

Changes

TokenSpeed ZMQ integration

Layer / File(s) Summary
Generic engine protocol and transport
crates/engine_zmq_client/src/protocol/*, crates/engine_zmq_client/src/connector.rs, crates/engine_zmq_client/src/transport.rs
The ZMQ client now supports generic engine protocols, batches, outputs, and load data. The vLLM adapter uses the shared protocol contracts.
TokenSpeed wire protocol
crates/engine_zmq_client/src/protocol/tokenspeed/*
TokenSpeed request, sampling, output, framing, positional MessagePack encoding, decoding, normalization, and compatibility tests were added.
Gateway runtime and worker integration
model_gateway/src/main.rs, model_gateway/src/routers/grpc/*, model_gateway/src/config/*, model_gateway/src/worker/*, model_gateway/src/workflow/*
The gateway selects vLLM or TokenSpeed runtimes, maps streams, propagates handshake settings, and preserves runtime metadata.
Python serving and router wiring
bindings/python/src/*, bindings/python/tests/test_serve.py
Python serving adds the TokenSpeed launcher, CLI parsing, ZMQ validation, backend passthrough, runtime configuration, and tests.

Workflow completion signaling

Layer / File(s) Summary
Completion signaling and regression coverage
crates/workflow/src/engine.rs, crates/workflow/tests/workflow_test.rs
The scheduler drains queued completions before deadlock detection. Skip and failure signals are emitted with tracker updates. Regression tests cover skipped dependency chains.

Estimated code review effort: 5 (Critical) | ~120 minutes

Possibly related issues

Possibly related PRs

  • smg-project/smg#2015 — Extends the earlier ZMQ backend integration with generalized client, worker launcher, runtime, and protocol support for TokenSpeed.
  • smg-project/smg#2019 — Shares the BackendClient and router backend-runtime handling used by this integration.
  • smg-project/smg#2032 — Covers the same TokenSpeed ZMQ support components and functions.

Suggested reviewers: catherinesue, key4ng

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/zmq-tokenspeed

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

grpc gRPC client and router changes model-gateway Model gateway crate changes protocols Protocols crate changes python-bindings Python bindings changes tests Test changes workflow Workflow crate changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant