Skip to main content

Classification Usages

还没翻译译自英文(docs.vllm.ai)本页 85 段都还没译

Classification Usages

Classification involves predicting which predefined category, class, or label best corresponds to a given input.

Summary

  • Model Usage: (sequence) classification
  • Pooling Task: classify
  • Offline APIs:
    • LLM.classify(...)
    • LLM.encode(..., pooling_task="classify")
  • Online APIs:

The key distinction between (sequence) classification and token classification lies in their output granularity: (sequence) classification produces a single result for an entire input sequence, whereas token classification yields a result for each individual token within the sequence.

Many classification models support both (sequence) classification and token classification. For further details on token classification, please refer to this page.

Only when a classification model outputs num_labels equal to 1 can it be used as a scoring model and have its scoring API enabled, please refer to this page.

Typical Use Cases

Classification

The most fundamental application of classification models is to categorize input data into predefined classes.

Supported Models

Text-only Models

ArchitectureModelsExample HF ModelsLoRAPP
GPT2ForSequenceClassificationGPT2nie3e/sentiment-polish-gpt2-small
Qwen2ForSequenceClassificationCQwen2-basedjason9693/Qwen2.5-1.5B-apeach
*ModelC, *ForCausalLMC, etc.Generative modelsN/A**

Multimodal Models

Note

For more information about multimodal models inputs, see this page.

ArchitectureModelsInputsExample HF ModelsLoRAPP
Qwen2_5_VLForSequenceClassificationCQwen2_5_VL-basedT + IE+ + VE+muziyongshixin/Qwen2.5-VL-7B-for-VideoCls
*ForConditionalGenerationC, *ForCausalLMC, etc.Generative models*N/A**

C Automatically converted into a classification model via --convert classify. (details)
* Feature support is the same as that of the original model.

If your model is not in the above list, we will try to automatically convert the model using as_seq_cls_model. By default, the class probabilities are extracted from the softmaxed hidden state corresponding to the last token.

Cross-encoder Models

Cross-encoder (aka reranker) models are a subset of classification models that accept two prompts as input and output num_labels equal to 1. Most classification models can also be used as cross-encoder models. For more information on cross-encoder models, please refer to this page.

Text-only Models

ArchitectureModelsExample HF ModelsScore template (see note)LoRAPP
BertForSequenceClassificationBERT-basedcross-encoder/ms-marco-MiniLM-L-6-v2, etc.N/A✅︎
GemmaForSequenceClassificationGemma-basedBAAI/bge-reranker-v2-gemma(see note), etc.bge-reranker-v2-gemma.jinja✅︎✅︎
GteNewForSequenceClassificationmGTE-TRM (see note)Alibaba-NLP/gte-multilingual-reranker-base, etc.N/A
LlamaBidirectionalForSequenceClassificationCLlama-based with bidirectional attentionnvidia/llama-nemotron-rerank-1b-v2, etc.nemotron-rerank.jinja✅︎✅︎
ModernBertForSequenceClassificationModernBERT-basedAlibaba-NLP/gte-reranker-modernbert-base, etc.N/A
Qwen2ForSequenceClassificationCQwen2-basedmixedbread-ai/mxbai-rerank-base-v2(see note), etc.mxbai_rerank_v2.jinja✅︎✅︎
Qwen3ForSequenceClassificationCQwen3-basedtomaarsen/Qwen3-Reranker-0.6B-seq-cls, Qwen/Qwen3-Reranker-0.6B(see note), polaria-tech/zerank-2-reranker-vllm(see note), etc.qwen3_reranker.jinja, zerank2.jinja✅︎✅︎
RobertaForSequenceClassificationRoBERTa-basedcross-encoder/quora-roberta-base, etc.N/A✅︎
XLMRobertaForSequenceClassificationXLM-RoBERTa-basedBAAI/bge-reranker-v2-m3, etc.N/A✅︎
*ModelC, *ForCausalLMC, etc.Generative modelsN/AN/A**

C Automatically converted into a classification model via --convert classify. (details)
* Feature support is the same as that of the original model.

Note

Some models require a specific prompt format to work correctly.

You can find Example HF Models's corresponding score template in examples/pooling/score/template/

Examples : examples/pooling/score/using_template_offline.py examples/pooling/score/using_template_online.py

Note

Load the official original BAAI/bge-reranker-v2-gemma by using the following command.

vllm serve BAAI/bge-reranker-v2-gemma --hf_overrides '{"architectures": ["GemmaForSequenceClassification"],"classifier_from_token": ["Yes"],"method": "no_post_processing"}'

Note

The second-generation GTE model (mGTE-TRM) is named NewForSequenceClassification. The name NewForSequenceClassification is too generic, you should set --hf-overrides '{"architectures": ["GteNewForSequenceClassification"]}' to specify the use of the GteNewForSequenceClassification architecture.

Note

Load the official original mxbai-rerank-v2 by using the following command.

vllm serve mixedbread-ai/mxbai-rerank-base-v2 --hf_overrides '{"architectures": ["Qwen2ForSequenceClassification"],"classifier_from_token": ["0", "1"], "method": "from_2_way_softmax"}'

Note

Load the official original Qwen3 Reranker by using the following command. More information can be found at: examples/pooling/score/qwen3_reranker_offline.py examples/pooling/score/qwen3_reranker_online.py.

vllm serve Qwen/Qwen3-Reranker-0.6B --hf_overrides '{"architectures": ["Qwen3ForSequenceClassification"],"classifier_from_token": ["no", "yes"],"is_original_qwen3_reranker": true}'

Note

polaria-tech/zerank-2-reranker-vllm is zeroentropy/zerank-2-reranker converted to Qwen3ForSequenceClassification; it returns zerank-2's calibrated score sigmoid(logit / 5). Serve it with zerank2.jinja, which also supports the instruction request field.

vllm serve polaria-tech/zerank-2-reranker-vllm --chat-template examples/pooling/score/template/zerank2.jinja

Multimodal Models

Note

For more information about multimodal models inputs, see this page.

ArchitectureModelsInputsExample HF ModelsLoRAPP
JinaVLForSequenceClassificationJinaVL-basedT + IE+jinaai/jina-reranker-m0, etc.✅︎✅︎
LlamaNemotronVLForSequenceClassificationLlama Nemotron Reranker + SigLIPT + IE+nvidia/llama-nemotron-rerank-vl-1b-v2
Qwen3VLForSequenceClassificationQwen3-VL-RerankerT + IE+ + VE+Qwen/Qwen3-VL-Reranker-2B(see note), etc.✅︎✅︎

C Automatically converted into a classification model via --convert classify. (details)
* Feature support is the same as that of the original model.

Note

Similar to Qwen3-Reranker, you need to use the following --hf_overrides to load the official original Qwen3-VL-Reranker. Qwen3-VL officially uses qwen_vl_utils for image preprocessing, while vLLM uses transformers' video_processing_qwen3_vl, which leads to slightly different results compared to the official Hugging Face repository examples.

vllm serve Qwen/Qwen3-VL-Reranker-2B --hf_overrides '{"architectures": ["Qwen3VLForSequenceClassification"],"classifier_from_token": ["no", "yes"],"is_original_qwen3_reranker": true}'

Reward Models

Using (sequence) classification models as reward models. For more information, see Reward Models.

ArchitectureModelsExample HF ModelsLoRAPP
JambaForSequenceClassificationJambaai21labs/Jamba-tiny-reward-dev, etc.✅︎✅︎
Qwen3ForSequenceClassificationCQwen3-basedSkywork/Skywork-Reward-V2-Qwen3-0.6B, etc.✅︎✅︎
LlamaForSequenceClassificationCLlama-basedSkywork/Skywork-Reward-V2-Llama-3.2-1B, etc.✅︎✅︎
*ModelC, *ForCausalLMC, etc.Generative modelsN/A**

C Automatically converted into a classification model via --convert classify. (details)

If your model is not in the above list, we will try to automatically convert the model using as_seq_cls_model. By default, the class probabilities are extracted from the softmaxed hidden state corresponding to the last token.

Offline Inference

Pooling Parameters

The following pooling parameters are supported.

    use_activation: bool | None = None

LLM.classify

The classify method outputs a probability vector for each prompt.

from vllm import LLM

llm = LLM(model="jason9693/Qwen2.5-1.5B-apeach", runner="pooling")
(output,) = llm.classify("Hello, my name is")

probs = output.outputs.probs
print(f"Class Probabilities: {probs!r} (size={len(probs)})")

A code example can be found here: examples/basic/offline_inference/classify.py

LLM.encode

The encode method is available to all pooling models in vLLM.

Set pooling_task="classify" when using LLM.encode for classification Models:

from vllm import LLM

llm = LLM(model="jason9693/Qwen2.5-1.5B-apeach", runner="pooling")
(output,) = llm.encode("Hello, my name is", pooling_task="classify")

data = output.outputs.data
print(f"Data: {data!r}")

Online Serving

Classification API

Online /classify API is similar to LLM.classify.

Completion Parameters

The following Classification API parameters are supported:

The following extra parameters are supported:

Chat Parameters

For chat-like input (i.e. if messages is passed), the following parameters are supported:

these extra parameters are supported instead:

Example Requests

Code example: examples/pooling/classify/classification_online.py

You can classify multiple texts by passing an array of strings:

curl -v "http://127.0.0.1:8000/classify" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "jason9693/Qwen2.5-1.5B-apeach",
    "input": [
      "Loved the new café—coffee was great.",
      "This update broke everything. Frustrating."
    ]
  }'

You can also pass a string directly to the input field:

curl -v "http://127.0.0.1:8000/classify" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "jason9693/Qwen2.5-1.5B-apeach",
    "input": "Loved the new café—coffee was great."
  }'

More examples

More examples can be found here: examples/pooling/classify

Supported Features

Enable/disable activation

You can enable or disable activation via use_activation.

Problem type (e.g. multi_label_classification)

You can modify the problem_type via problem_type in the Hugging Face config. The supported problem types are: single_label_classification, multi_label_classification, and regression.

Implement alignment with transformers ForSequenceClassificationLoss.

Affine Score Calibration

Affine Score Calibration, also known as Platt Scaling (Platt, 1999), is the most widely used method for calibrating classifier outputs into well-calibrated probabilities.

The calibration follows the transformation:

activation((logit - logit_mean) / logit_sigma)

ParameterDefaultDescription
logit_meanNoneMean subtracted from logits (centers scores)
logit_sigmaNoneStandard deviation used to scale logits after mean subtraction

The computation order is as follows:

logits -= logit_mean   # subtract mean (center scores)
logits /= logit_sigma  # divide by sigma (scale)
logits = activation(logits)  # e.g. sigmoid

Example configuration:

--pooler-config '{"use_activation": true, "logit_mean": 4.5, "logit_sigma": 1.0}'

Removed Features

Remove softmax from PoolingParams

We have already removed softmax and activation from PoolingParams. Instead, use use_activation, since we allow classify and token_classify to use any activation function.