(EngineCore pid=232) INFO 11-24 11:39:51 [factory.py:62] Creating v1 connector with name: NixlPullConnector and engine_id: b32cb5f2-9e95-4f74-a390-d8112958a3b4
如果请求带了 conversation_id,Proxy 就从 D 的流式响应末尾取出块信息,按会话 ID 暂存。下一轮请求过来,Proxy 再根据 ID 找到之前的信息(也就是上次用的 D 是哪个),把这份元数据附到发给 P 的请求里。这样,P 就知道上一轮的 KV 留在哪个 D 上,以及该读哪些块。
python3 -u - <<'PYCLIENT'
import json
from time import perf_counter
from urllib.request import Request, urlopen
from tokenizers import Tokenizer
# Qwen2.5 的原始提示词。末尾的 worker 用于将首轮输入对齐到 256 token。
FIRST = """<|im_start|>system
You are an operations engineer writing a practical field guide. Use numbered, detailed paragraphs, concrete commands, and what the command output means. Keep developing the checklist with useful examples until the whole procedure is covered.<|im_end|>
<|im_start|>user
Incident {case}. Write a detailed 20-step checklist to investigate this scenario. A Kubernetes worker is NotReady after a maintenance reboot. New pods remain Pending and traffic to one internal service has become intermittent. The control plane is reachable. Container images came from a private registry. The cluster uses containerd and CoreDNS. Start with read-only diagnostics, then explain where to look for kubelet, containerd, CNI, DNS, certificate, firewall, disk pressure, scheduling, and node health problems. Include an example at each step. Then describe how to confirm service recovery.
Available resources: The operator has a terminal, kubectl, journalctl, ssh, a timestamped event list, and access to node logs. The operator has a terminal, kubectl, journalctl, ssh, a timestamped event list, and access to node logs.
Case labels: worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker worker
<|im_end|>
<|im_start|>assistant
"""
FOLLOWUP = """<|im_end|>
<|im_start|>user
Which check should I run first? Answer in one sentence.<|im_end|>
<|im_start|>assistant
"""
tokenizer = Tokenizer.from_file("/models/tokenizer.json")
followup_ids = tokenizer.encode(FOLLOWUP, add_special_tokens=False).ids
def send(ids, session, length):
body = {
"model": "qwen-bidir", "conversation_id": session, "cache_salt": session,
"prompt": ids, "stream": True, "stream_options": {"include_usage": True},
"temperature": 0, "seed": 7, "max_tokens": length, "min_tokens": length,
"ignore_eos": True, "return_token_ids": True, "skip_special_tokens": False,
}
req = Request(
"http://127.0.0.1:29192/v1/completions",
data=json.dumps(body, sort_keys=True).encode(),
headers={"Content-Type": "application/json"},
)
output_ids, ttft_ms = [], None
start = perf_counter()
with urlopen(req, timeout=90) as response:
for line in response:
now = perf_counter()
if not line.startswith(b"data: "):
continue
if line.strip() == b"data: [DONE]":
assert len(output_ids) == length and ttft_ms is not None
return output_ids, ttft_ms
for choice in json.loads(line[6:]).get("choices", []):
output_ids.extend(choice.get("token_ids") or [])
if ttft_ms is None and choice.get("text"):
ttft_ms = (now - start) * 1000
raise RuntimeError("SSE 提前结束")
for sample in range(7):
session = f"long-reply-600-{sample}"
first_ids = tokenizer.encode(
FIRST.format(case=sample + 1), add_special_tokens=False
).ids
assert len(first_ids) == 256 and len(followup_ids) == 22
answer_ids, _ = send(first_ids, session, 600)
_, ttft = send(first_ids + answer_ids + followup_ids, session, 8)
print(f"{session} {'warmup' if sample == 0 else 'measured'} TTFT={ttft:.2f} ms")
PYCLIENT
双向组首轮结束时,Proxy 记下 D 返回的块信息。第二轮 P 命中本地的 256 个 token,再从 D 读回上一轮回答中 599 个 token 的 KV。P 还要算追问的 22 个 token 和上一轮回答的最后一个 token。最后一个 token 在上一轮生成后还没进入下一次前向计算。
跑标准 PD 时,把上面的 bidirectional 改为 false,重启 P、D 和 Proxy,然后用同一份输入复跑客户端。第二轮 P 仍命中首轮输入,上一轮回答和本轮追问都由 P 计算。两轮 D 都从 P 拉取本轮所需的 KV。
[Proxy] conv=long-reply-600-1: cache MISS
[Proxy] conv=long-reply-600-1: cached D blocks (remote_request_id=…, blocks=14)
[Proxy] conv=long-reply-600-1: cache HIT (age=0.1s)
[Proxy] conv=long-reply-600-1: sending D's cached blocks to P (remote_request_id=…)
[Proxy] Prefill done in 103ms
[P] KV Transfer metrics: Num successful transfers=1, Avg xfer time (ms)=13.836, … Avg MB per transfer=7.5, … Num failed transfers=0