2026-04-18 10:56:22 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
|
|
|
|
2026-04-29 19:38:22 +08:00
|
|
|
from typing import TypeAlias
|
2026-04-18 10:56:22 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2026-04-29 19:38:22 +08:00
|
|
|
|
|
|
|
|
from vllm import ClassificationOutput
|
|
|
|
|
from vllm.config import ModelConfig
|
|
|
|
|
from vllm.entrypoints.chat_utils import ChatTemplateConfig
|
|
|
|
|
from vllm.entrypoints.openai.engine.protocol import UsageInfo
|
|
|
|
|
from vllm.entrypoints.pooling.base.serving import PoolingServeContext, PoolingServing
|
|
|
|
|
from vllm.logger import init_logger
|
|
|
|
|
from vllm.renderers import BaseRenderer
|
|
|
|
|
|
|
|
|
|
from .io_processor import ClassifyIOProcessor
|
|
|
|
|
from .protocol import (
|
2026-04-18 10:56:22 +08:00
|
|
|
ClassificationData,
|
|
|
|
|
ClassificationRequest,
|
|
|
|
|
ClassificationResponse,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logger = init_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2026-04-29 19:38:22 +08:00
|
|
|
ClassificationServeContext: TypeAlias = PoolingServeContext[ClassificationRequest]
|
2026-04-18 10:56:22 +08:00
|
|
|
|
|
|
|
|
|
2026-04-29 19:38:22 +08:00
|
|
|
class ServingClassification(PoolingServing):
|
2026-04-18 10:56:22 +08:00
|
|
|
request_id_prefix = "classify"
|
|
|
|
|
|
2026-04-29 19:38:22 +08:00
|
|
|
def init_io_processor(
|
2026-04-18 10:56:22 +08:00
|
|
|
self,
|
2026-04-29 19:38:22 +08:00
|
|
|
model_config: ModelConfig,
|
|
|
|
|
renderer: BaseRenderer,
|
|
|
|
|
chat_template_config: ChatTemplateConfig,
|
|
|
|
|
) -> ClassifyIOProcessor:
|
|
|
|
|
return ClassifyIOProcessor(
|
|
|
|
|
model_config=model_config,
|
|
|
|
|
renderer=renderer,
|
|
|
|
|
chat_template_config=chat_template_config,
|
2026-04-18 10:56:22 +08:00
|
|
|
)
|
|
|
|
|
|
2026-04-29 19:38:22 +08:00
|
|
|
async def _build_response(
|
2026-04-18 10:56:22 +08:00
|
|
|
self,
|
|
|
|
|
ctx: ClassificationServeContext,
|
2026-04-29 19:38:22 +08:00
|
|
|
) -> ClassificationResponse:
|
|
|
|
|
final_res_batch_checked = await self.io_processor.post_process_async(
|
|
|
|
|
ctx.final_res_batch
|
|
|
|
|
)
|
2026-04-18 10:56:22 +08:00
|
|
|
|
2026-04-29 19:38:22 +08:00
|
|
|
id2label = getattr(self.model_config.hf_config, "id2label", {})
|
2026-04-18 10:56:22 +08:00
|
|
|
num_prompt_tokens = 0
|
2026-04-29 19:38:22 +08:00
|
|
|
items: list[ClassificationData] = []
|
2026-04-18 10:56:22 +08:00
|
|
|
for idx, final_res in enumerate(final_res_batch_checked):
|
|
|
|
|
classify_res = ClassificationOutput.from_base(final_res.outputs)
|
|
|
|
|
|
|
|
|
|
probs = classify_res.probs
|
|
|
|
|
predicted_index = int(np.argmax(probs))
|
|
|
|
|
label = id2label.get(predicted_index)
|
|
|
|
|
|
|
|
|
|
item = ClassificationData(
|
|
|
|
|
index=idx,
|
|
|
|
|
label=label,
|
|
|
|
|
probs=probs,
|
|
|
|
|
num_classes=len(probs),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
items.append(item)
|
|
|
|
|
prompt_token_ids = final_res.prompt_token_ids
|
|
|
|
|
num_prompt_tokens += len(prompt_token_ids)
|
|
|
|
|
|
|
|
|
|
usage = UsageInfo(
|
|
|
|
|
prompt_tokens=num_prompt_tokens,
|
|
|
|
|
total_tokens=num_prompt_tokens,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return ClassificationResponse(
|
|
|
|
|
id=ctx.request_id,
|
|
|
|
|
created=ctx.created_time,
|
|
|
|
|
model=ctx.model_name,
|
|
|
|
|
data=items,
|
|
|
|
|
usage=usage,
|
|
|
|
|
)
|