[Perf] Compute maxsim in worker side, reducing redundant copies, 2.7% E2E throughput improvement (#36159)

Signed-off-by: yewentao256 <zhyanwentao@126.com>
This commit is contained in:
Wentao Ye
2026-03-09 20:55:58 -07:00
committed by GitHub
parent 006aea17d7
commit 7279374f91
11 changed files with 518 additions and 58 deletions
@@ -24,17 +24,23 @@ from vllm import SamplingParams
from vllm.distributed.kv_events import BlockStored, KVEventBatch, ZmqEventPublisher
from vllm.engine.arg_utils import EngineArgs
from vllm.platforms import current_platform
from vllm.pooling_params import LateInteractionParams, PoolingParams
from vllm.usage.usage_lib import UsageContext
from vllm.utils.torch_utils import set_default_torch_num_threads
from vllm.v1.engine import EngineCoreRequest
from vllm.v1.engine.core import EngineCore
from vllm.v1.engine.core_client import (
AsyncMPClient,
DPLBAsyncMPClient,
EngineCoreClient,
SyncMPClient,
)
from vllm.v1.engine.utils import CoreEngineProcManager
from vllm.v1.executor.abstract import Executor
from vllm.v1.pool.late_interaction import (
LATE_INTERACTION_MODE_CACHE_QUERY,
LATE_INTERACTION_MODE_SCORE_DOC,
)
from ...distributed.conftest import MockSubscriber
from ...utils import create_new_process_for_each_test
@@ -164,6 +170,71 @@ def test_mp_client_uses_env_timeout(monkeypatch: pytest.MonkeyPatch):
client.shutdown()
def _make_pooling_request(
request_id: str, *, mode: str | None = None, query_key: str | None = None
) -> EngineCoreRequest:
late_interaction_params = None
if mode is not None and query_key is not None:
late_interaction_params = LateInteractionParams(
mode=mode,
query_key=query_key,
)
return EngineCoreRequest(
request_id=request_id,
prompt_token_ids=[1, 2, 3],
mm_features=None,
sampling_params=None,
pooling_params=PoolingParams(
task="token_embed",
late_interaction_params=late_interaction_params,
),
arrival_time=time.time(),
lora_request=None,
cache_salt=None,
data_parallel_rank=None,
)
def test_dplb_late_interaction_sticky_routing():
client = object.__new__(DPLBAsyncMPClient)
client.client_count = 1
client.reqs_in_flight = {}
client.core_engines = [b"\x00\x00", b"\x01\x00", b"\x02\x00"]
client.lb_engines = [[0, 0], [0, 0], [0, 0]]
client.eng_start_index = 0
query_key = "rerank-abc-query-0"
query_request = _make_pooling_request(
"query-req", mode=LATE_INTERACTION_MODE_CACHE_QUERY, query_key=query_key
)
doc_request = _make_pooling_request(
"doc-req", mode=LATE_INTERACTION_MODE_SCORE_DOC, query_key=query_key
)
query_engine = client.get_core_engine_for_request(query_request)
doc_engine = client.get_core_engine_for_request(doc_request)
assert query_engine == doc_engine
assert client.reqs_in_flight["query-req"] == query_engine
assert client.reqs_in_flight["doc-req"] == doc_engine
def test_dplb_non_late_interaction_still_uses_lb():
client = object.__new__(DPLBAsyncMPClient)
client.client_count = 1
client.reqs_in_flight = {}
client.core_engines = [b"\x00\x00", b"\x01\x00", b"\x02\x00"]
client.lb_engines = [[2, 1], [0, 0], [1, 0]]
client.eng_start_index = 0
request = make_request(SamplingParams(max_tokens=1))
chosen_engine = client.get_core_engine_for_request(request)
assert chosen_engine == client.core_engines[1]
assert client.lb_engines[1][0] == 1
def loop_until_done(client: EngineCoreClient, outputs: dict):
while True:
engine_core_outputs = client.get_output().outputs
@@ -0,0 +1,113 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
import torch
from vllm.pooling_params import LateInteractionParams, PoolingParams
from vllm.v1.pool.late_interaction import (
LATE_INTERACTION_MODE_CACHE_QUERY,
build_late_interaction_doc_params,
build_late_interaction_query_params,
compute_maxsim_score,
)
from vllm.v1.worker.gpu.pool.late_interaction_runner import LateInteractionRunner
def _make_pooling_params(
late_interaction_params: LateInteractionParams,
) -> PoolingParams:
return PoolingParams(
task="token_embed",
late_interaction_params=late_interaction_params,
)
def test_postprocess_scores_and_releases_query_cache():
runner = LateInteractionRunner()
query_key = "query-0"
query_emb = torch.tensor([[1.0, 0.0], [0.0, 1.0]], dtype=torch.float32)
doc_emb = torch.tensor([[1.0, 0.0], [0.5, 0.5], [0.0, 1.0]], dtype=torch.float32)
query_params = _make_pooling_params(
build_late_interaction_query_params(query_key=query_key, query_uses=1)
)
query_output = runner.postprocess_pooler_output(
raw_pooler_output=[query_emb],
pooling_params=[query_params],
req_ids=["query-req"],
finished_mask=[True],
)
assert isinstance(query_output, list)
assert query_output[0] is not None
assert query_output[0].shape == torch.Size([])
doc_params = _make_pooling_params(
build_late_interaction_doc_params(query_key=query_key)
)
doc_output = runner.postprocess_pooler_output(
raw_pooler_output=[doc_emb],
pooling_params=[doc_params],
req_ids=["doc-req"],
finished_mask=[True],
)
assert isinstance(doc_output, list)
assert doc_output[0] is not None
assert torch.allclose(doc_output[0], compute_maxsim_score(query_emb, doc_emb))
with pytest.raises(ValueError, match="query cache miss"):
runner.postprocess_pooler_output(
raw_pooler_output=[doc_emb],
pooling_params=[doc_params],
req_ids=["doc-req-2"],
finished_mask=[True],
)
def test_finished_request_releases_unscored_doc_use():
runner = LateInteractionRunner()
query_key = "query-cancel"
query_emb = torch.tensor([[1.0, 0.0], [0.0, 1.0]], dtype=torch.float32)
doc_emb = torch.tensor([[1.0, 0.0], [0.0, 1.0]], dtype=torch.float32)
query_params = _make_pooling_params(
build_late_interaction_query_params(query_key=query_key, query_uses=1)
)
runner.postprocess_pooler_output(
raw_pooler_output=[query_emb],
pooling_params=[query_params],
req_ids=["query-req"],
finished_mask=[True],
)
doc_params = _make_pooling_params(
build_late_interaction_doc_params(query_key=query_key)
)
runner.register_request("doc-req", doc_params)
runner.on_requests_finished({"doc-req"})
with pytest.raises(ValueError, match="query cache miss"):
runner.postprocess_pooler_output(
raw_pooler_output=[doc_emb],
pooling_params=[doc_params],
req_ids=["doc-req-retry"],
finished_mask=[True],
)
def test_invalid_query_uses_raises():
runner = LateInteractionRunner()
bad_meta = LateInteractionParams(
mode=LATE_INTERACTION_MODE_CACHE_QUERY,
query_key="query-bad",
)
bad_meta.query_uses = "bad-int" # type: ignore[assignment]
bad_query_params = _make_pooling_params(bad_meta)
with pytest.raises(ValueError, match="must be an integer value"):
runner.postprocess_pooler_output(
raw_pooler_output=[torch.ones((2, 2), dtype=torch.float32)],
pooling_params=[bad_query_params],
req_ids=["query-req"],
finished_mask=[True],
)