← Back to Pull requests

[Feature] Add support for Qwen3 Reranker with Sequence Classifier head #698

Closed
unknown closed this pull request without merging on Sep 24, 2025 19:01 UTC
View on GitHub
unknown unknown commented on Aug 13, 2025 01:17 UTC
Author

I totally missed #695 and ended up coding this in a hurry today.

@OlivierDehaene or @Narsil: Happy to work with @sigridjineth if helpful

Summary


Adds support for Qwen3 reranker models (e.g., tomaarsen/Qwen3-Reranker-0.6B-seq-cls) that have been converted from causal LM to sequence classification architecture. These models require specific chat templates for optimal performance. Functionality was added to support custom instructions.

How Qwen3 Reranking (with Seq Classification conversion) Works


Conversion adds a classification head (score.weight) on top of base model
- This PR enables TEI to recognize these models as classifier/reranker
- Works with /rerank endpoint
- Process:
a. Takes query-document pair formatted as conversation
b. Uses LastToken pooling to extract representations
c. Passes through classification head (single linear layer, no bias)
d. Returns logit score (sigmoid applied for probability)

# Key Changes

1. Template Support
- New templates module implementing Qwen3 chat format:
<|im_start|>system
Judge whether the Document meets the requirements...
<|im_start|>user
<Instruct>: {instruction}
<Query>: {query}
<Document>: {document}<|im_end|>
<|im_start|>assistant
<think>

- Added instruction and use_template fields to RerankRequest (HTTP & gRPC)
- Auto-detects Qwen3 seq-cls models for template application
- Default instruction: "Select only the Documents that are semantically similar to the Query."

2. Model Loading
- Flexible tensor loading for score.weight pattern (no bias tensor)
- Uses LastToken pooling for classification models
- Available via /rerank endpoint for Classifier models

Testing


  • Added test_qwen3_reranker() test in backends/candle/tests/test_qwen3.rs

  • Test verifies model loading and prediction functionality
  • Notes


    The model is highly sensitive to the instruction and template format. Without the template, results can be somewhat random. Tuning the Instruction can result in significantly improved discrimination.

    Diff: 262 lines (+196 / -66) across 3 files Reviewer: realsigridjin Review duration: 61543m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.39) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 262 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.333
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #698"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.3% (MEDIUM) · Higher = larger blast radius
    39.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.0% · Safe to merge
    17.0%