[Bugfix] Honor STEP token pooling in DispatchPooler.for_seq_cls (#55307)

Signed-off-by: weihan <130119942+krsish@users.noreply.github.com>
This commit is contained in:
weihan
2026-09-08 04:13:09 +00:00
committed by GitHub
parent 7fa2c63796
commit 9ea8f3ffc3
3 changed files with 73 additions and 47 deletions
@@ -3,12 +3,15 @@
"""Unit tests for sequence and token pooling methods and their factories."""
from dataclasses import dataclass
from types import SimpleNamespace
from unittest.mock import patch
import numpy as np
import pytest
import torch
from transformers import PretrainedConfig
from vllm.config import PoolerConfig, set_current_vllm_config
from vllm.model_executor.layers.pooler import PoolingParamsUpdate
from vllm.model_executor.layers.pooler.seqwise.methods import (
CLSPool,
@@ -16,6 +19,7 @@ from vllm.model_executor.layers.pooler.seqwise.methods import (
MeanPool,
get_seq_pooling_method,
)
from vllm.model_executor.layers.pooler.special import DispatchPooler
from vllm.model_executor.layers.pooler.tokwise.methods import (
AllPool,
StepPool,
@@ -300,6 +304,50 @@ class _FakeVllmConfig:
scheduler_config: _FakeSchedulerConfig
@pytest.mark.parametrize("tok_pooling_type", ["ALL", "STEP"])
def test_dispatch_seq_cls_honors_token_pooling_type(tok_pooling_type):
"""Filter STEP rows before classification; preserve all rows for resolved ALL."""
pooler_config = PoolerConfig(
seq_pooling_type="LAST", tok_pooling_type=tok_pooling_type
)
config = SimpleNamespace(
scheduler_config=_FakeSchedulerConfig(),
model_config=SimpleNamespace(
pooler_config=pooler_config,
head_dtype=None,
hf_config=PretrainedConfig(num_labels=3),
),
)
classifier = torch.nn.Linear(4, 3)
with set_current_vllm_config(config):
pooler = DispatchPooler.for_seq_cls(pooler_config, classifier=classifier)
token_ids = [[2, 99, 3, 99], [4, 99], [5, 6]]
params = [
PoolingParams(
task="token_classify",
use_activation=False,
step_tag_id=99 if tok_pooling_type == "STEP" else None,
)
for _ in token_ids
]
metadata = _make_metadata([4, 2, 2], token_ids=token_ids, pooling_params=params)
hidden = torch.arange(32, dtype=torch.float32).reshape(8, 4)
outputs = pooler(hidden, metadata)
expected_rows = (
[[1, 3], [5], []]
if tok_pooling_type == "STEP"
else [[0, 1, 2, 3], [4, 5], [6, 7]]
)
assert len(outputs) == len(expected_rows)
for output, rows in zip(outputs, expected_rows):
torch.testing.assert_close(output, classifier(hidden[rows]))
assert pooler.get_pooling_updates("token_classify").requires_token_ids == (
tok_pooling_type == "STEP"
)
class TestAllPool:
@staticmethod
def _make_all_pool(